Skip to content

Commit

Permalink
#395 Fix sample project
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Niedermann <[email protected]>
  • Loading branch information
stefan-niedermann committed Nov 4, 2021
1 parent 75f2489 commit 16ed351
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException;
import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

import java.io.IOException;
Expand All @@ -41,6 +40,8 @@ protected void onCreate(Bundle savedInstanceState) {
/*
* Prompt dialog to select existing or create a new account
* As soon as an account has been imported, we will continue in #onActivityResult()
*
* In real live applications, you won't import an account on each app start, but remember the imported account via SingleAccountHelper.
*/
AccountImporter.pickNewAccount(this);
} catch (NextcloudFilesAppNotInstalledException | AndroidGetAccountsPermissionNotGranted e) {
Expand All @@ -55,25 +56,32 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
try {
AccountImporter.onActivityResult(requestCode, resultCode, data, this, ssoAccount -> {
Log.i(TAG, "Imported account: " + ssoAccount.name);
SingleAccountHelper.setCurrentAccount(this, ssoAccount.name);

/* Network requests need to be performed on a background thread */
executor.submit(() -> {
runOnUiThread(() -> ((TextView) findViewById(R.id.result)).setText("Loading…"));

// Create local bridge API to the Nextcloud Files Android app
/* Create local bridge API to the Nextcloud Files Android app */
final var nextcloudAPI = createNextcloudAPI(ssoAccount);

// Create the Ocs API to talk to the server
final var ocsAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, "ocs/v2.php/cloud/").create(OcsAPI.class);
/* Create the Ocs API to talk to the server */
final var ocsAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, "/ocs/v2.php/cloud/").create(OcsAPI.class);

try {
// Perform actual requests
final var userName = ocsAPI.getUser(ssoAccount.userId).execute().body().ocs.data.displayName;
final var instanceName = ocsAPI.getCapabilities().execute().body().ocs.data.theming.name;
/* Perform actual requests */
final var user = ocsAPI.getUser(ssoAccount.userId).execute().body().ocs.data;
final var serverInfo = ocsAPI.getServerInfo().execute().body().ocs.data;

((TextView) findViewById(R.id.result)).setText(userName + " on " + instanceName);
/* Set the result on the UI thread */
runOnUiThread(() -> ((TextView) findViewById(R.id.result)).setText(user.displayName + " on " + serverInfo.capabilities.theming.name + " (" + serverInfo.version.semanticVersion + ")"));
} catch (IOException e) {
e.printStackTrace();
}

/*
* Keep the NextcloudAPI alive as long as possible,
* but don't forget to destroy it when you don't need it any longer
*/
nextcloudAPI.stop();
});
});
Expand Down
43 changes: 21 additions & 22 deletions sample/src/main/java/com/nextcloud/android/sso/sample/OcsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
public interface OcsAPI {

@GET("capabilities?format=json")
Call<OcsResponse<OcsCapabilities>> getCapabilities();
Call<OcsResponse<OcsServerInfo>> getServerInfo();

@GET("users/{search}?format=json")
Call<OcsResponse<OcsUser>> getUser(@Path("search") String userId);

/**
* <p>A generic wrapper for <a href="https://www.open-collaboration-services.org/">OpenCollaborationServices</a> calls.</p>
* <p>This is needed for API endpoints located at <code>/ocs/...</code>. It is usually not used for APIs of 3rd party server apps like <a href="https://deck.readthedocs.io/en/latest/API/">Deck</a> or <a href="https://github.com/nextcloud/notes/blob/master/docs/api/README.md">Notes</a></p>
* <p>This is needed for API endpoints located at <code>/ocs/</code>. It is usually not used for APIs of 3rd party server apps like <a href="https://deck.readthedocs.io/en/latest/API/">Deck</a> or <a href="https://github.com/nextcloud/notes/blob/master/docs/api/README.md">Notes</a></p>
*
* @param <T> defines the payload of this {@link OcsResponse}.
*/
Expand All @@ -40,31 +40,30 @@ public static class OcsMeta {
}
}

/*
* Extend the classes by the attributes you are actually using, for example:</p>
* <ul>
* <li><code>version</code></li>
* <li><code>theming</code></li>
* <li><code>server_status</code></li>
* <li><code>deck</code></li>
* <li>…</li>
* </ul>
*/
class OcsCapabilities {
public Theming theming;
class OcsServerInfo {
/* Extend the classes by the attributes you are actually using */
public OcsVersion version;
public OcsCapabilities capabilities;

static class OcsVersion {
/**
* You can map the node names to other variable names using {@link SerializedName}.
* See <a href="https://github.com/google/gson">Gson-</a> and <a href="https://square.github.io/retrofit/">Retrofit-</a>Documentation for all possibilities.
*/
@SerializedName("string")
public String semanticVersion;
}

static class Theming {
public String name;
static class OcsCapabilities {
public Theming theming;

static class Theming {
public String name;
}
}
}

/**
* You can map the node names to other variable names using {@link SerializedName}.
* See <a href="https://github.com/google/gson">Gson-</a> and <a href="https://square.github.io/retrofit/">Retrofit-</a>Documentation for all possibilities.
*/
class OcsUser {
@SerializedName("id")
public String userId;
@SerializedName("displayname")
public String displayName;
}
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/chooseAccountBtn"
android:layout_marginTop="16dp"
android:text="No account chosen yet."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/chooseAccountBtn"
Expand Down
12 changes: 6 additions & 6 deletions sample/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<!-- Base application theme. -->
<style name="Theme.AndroidSingleSignOn" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<item name="colorPrimary">@color/nextcloud_blue</item>
<item name="colorPrimaryVariant">@color/nextcloud_blue</item>
<item name="colorOnPrimary">@android:color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<item name="colorSecondary">@color/nextcloud_blue</item>
<item name="colorSecondaryVariant">@color/nextcloud_blue</item>
<item name="colorOnSecondary">@android:color/white</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
Expand Down
8 changes: 1 addition & 7 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="nextcloud_blue">#FF0082C9</color>
</resources>
12 changes: 6 additions & 6 deletions sample/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<!-- Base application theme. -->
<style name="Theme.AndroidSingleSignOn" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<item name="colorPrimary">@color/nextcloud_blue</item>
<item name="colorPrimaryVariant">@color/nextcloud_blue</item>
<item name="colorOnPrimary">@android:color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<item name="colorSecondary">@color/nextcloud_blue</item>
<item name="colorSecondaryVariant">@color/nextcloud_blue</item>
<item name="colorOnSecondary">@android:color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
Expand Down

0 comments on commit 16ed351

Please sign in to comment.