Skip to content

Commit a0ed180

Browse files
Update README.md samples
Signed-off-by: Stefan Niedermann <info@niedermann.it>
1 parent 8faf4e5 commit a0ed180

File tree

1 file changed

+25
-118
lines changed

1 file changed

+25
-118
lines changed

README.md

Lines changed: 25 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,17 @@ repositories {
4343
}
4444
4545
dependencies {
46-
// Note: Starting with Version 0.5.7 Android Gradle Plugin (AGP) Version 7.0.0 or higher is
47-
// required.
48-
implementation "com.github.nextcloud:Android-SingleSignOn:0.5.7"
46+
// Note: Android Gradle Plugin (AGP) version ≥ 7.0.0 is required.
47+
implementation "com.github.nextcloud:Android-SingleSignOn:0.6.0"
4948
}
5049
```
5150

52-
For Android 11 and later [you need to add queries](https://github.com/nextcloud/Android-SingleSignOn/issues/277) to your app's manifest file. It will not find the Nextcloud app otherwise.
53-
54-
```xml
55-
<queries>
56-
<package android:name="com.nextcloud.client" />
57-
<package android:name="com.nextcloud.android.qa" />
58-
<package android:name="com.nextcloud.android.beta" />
59-
</queries>
60-
```
61-
6251
### 2) To choose an account, include the following code in your login dialog
6352

64-
From an Activity
65-
6653
```java
6754
private void openAccountChooser() {
6855
try {
69-
AccountImporter.pickNewAccount(this);
70-
} catch (NextcloudFilesAppNotInstalledException | AndroidGetAccountsPermissionNotGranted e) {
71-
UiExceptionManager.showDialogForException(this, e);
72-
}
73-
}
74-
```
75-
76-
From a Fragment
77-
78-
```java
79-
private void openAccountChooser() {
80-
try {
81-
AccountImporter.pickNewAccount(currentFragment);
56+
AccountImporter.pickNewAccount(activityOrFragment);
8257
} catch (NextcloudFilesAppNotInstalledException | AndroidGetAccountsPermissionNotGranted e) {
8358
UiExceptionManager.showDialogForException(this, e);
8459
}
@@ -87,109 +62,53 @@ private void openAccountChooser() {
8762

8863
### 3) To handle the result of the Account Chooser, include the following
8964

90-
From an Activity
91-
9265
```java
9366
@Override
9467
public void onActivityResult(int requestCode, int resultCode, Intent data) {
9568
super.onActivityResult(requestCode, resultCode, data);
9669
AccountImporter.onActivityResult(requestCode, resultCode, data, this, new AccountImporter.IAccountAccessGranted() {
9770

98-
NextcloudAPI.ApiConnectedListener callback = new NextcloudAPI.ApiConnectedListener() {
99-
@Override
100-
public void onConnected() {
101-
// ignore this one… see 5)
102-
}
103-
104-
@Override
105-
public void onError(Exception ex) {
106-
// TODO handle errors
107-
}
108-
};
109-
11071
@Override
11172
public void accountAccessGranted(SingleSignOnAccount account) {
112-
Context l_context = getApplicationContext();
73+
final var context = getApplicationContext();
11374

11475
// As this library supports multiple accounts we created some helper methods if you only want to use one.
11576
// The following line stores the selected account as the "default" account which can be queried by using
11677
// the SingleAccountHelper.getCurrentSingleSignOnAccount(context) method
117-
SingleAccountHelper.setCurrentAccount(l_context, account.name);
78+
SingleAccountHelper.setCurrentAccount(context, account.name);
11879

11980
// Get the "default" account
12081
SingleSignOnAccount ssoAccount = null;
12182
try {
122-
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(l_context);
83+
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
12384
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
124-
UiExceptionManager.showDialogForException(l_context, e);
85+
UiExceptionManager.showDialogForException(context, e);
12586
}
12687

127-
NextcloudAPI nextcloudAPI = new NextcloudAPI(l_context, ssoAccount, new GsonBuilder().create(), callback);
88+
final var nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create());
12889

12990
// TODO … (see code in section 4 and below)
13091
}
13192
});
13293
}
133-
```
134-
135-
From a Fragment
13694

137-
```java
138-
@Override
139-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
140-
super.onActivityResult(requestCode, resultCode, data);
141-
142-
AccountImporter.onActivityResult(requestCode, resultCode, data, LoginDialogFragment.this, new AccountImporter.IAccountAccessGranted() {
143-
144-
NextcloudAPI.ApiConnectedListener callback = new NextcloudAPI.ApiConnectedListener() {
145-
@Override
146-
public void onConnected() {
147-
// ignore this one… see 5)
148-
}
149-
150-
@Override
151-
public void onError(Exception ex) {
152-
// TODO handle errors
153-
}
154-
};
155-
156-
@Override
157-
public void accountAccessGranted(SingleSignOnAccount account) {
158-
// As this library supports multiple accounts we created some helper methods if you only want to use one.
159-
// The following line stores the selected account as the "default" account which can be queried by using
160-
// the SingleAccountHelper.getCurrentSingleSignOnAccount(context) method
161-
SingleAccountHelper.setCurrentAccount(getActivity(), account.name);
162-
163-
// Get the "default" account
164-
SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
165-
NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), callback);
166-
167-
// TODO … (see code in section 4 and below)
168-
}
169-
});
170-
}
171-
```
172-
173-
From both an Activity and Fragment
174-
175-
```java
17695
@Override
17796
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
17897
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
179-
18098
AccountImporter.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
18199
}
182100

183101
// Complete example: https://github.com/nextcloud/news-android/blob/890828441ba0c8a9b90afe56f3e08ed63366ece5/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/LoginDialogActivity.java#L470-L475
102+
184103
```
185104

186105
### 4) How to get account information?
187106

188107
```java
189108
// If you stored the "default" account using setCurrentAccount(…) you can get the account by using the following line:
190-
final SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
109+
final var ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
191110

192-
// Otherwise (for multi-account support): (you'll have to keep track of the account names yourself. Note: this has to be the name of SingleSignOnAccount.name)
111+
// Otherwise (for multi-account support you'll have to keep track of the account names yourself. Note: this has to be the name of SingleSignOnAccount.name)
193112
AccountImporter.getSingleSignOnAccount(context, accountName);
194113

195114
ssoAccount.name; // Name of the account used in the android account manager
@@ -200,13 +119,15 @@ ssoAccount.url;
200119

201120
### 5) How to make a network request?
202121

203-
You'll notice that there is an callback parameter in the constructor of the `NextcloudAPI`.
204-
205122
```java
206-
public NextcloudAPI(Context context, SingleSignOnAccount account, Gson gson, ApiConnectedListener callback) {
123+
public NextcloudAPI(Context context, SingleSignOnAccount account, Gson gson) {
207124
```
208125

209-
You can use this callback to subscribe to errors that might occur during the initialization of the API. You can start making requests to the API as soon as you instantiated the `NextcloudAPI` object. For a minimal example to get started (without retrofit) take a look at section 5.2. The callback method `onConnected` will be called once the connection to the files app is established. You can start making calls to the api before that callback is fired as the library will queue your calls until the connection is established[¹](https://github.com/nextcloud/Android-SingleSignOn/issues/400).
126+
You'll notice that there is an optional `ApiConnectedListener` callback parameter in the constructor of the `NextcloudAPI`.
127+
You can use this callback to subscribe to errors that might occur during the initialization of the API.
128+
You can start making requests to the API as soon as you instantiated the `NextcloudAPI` object.
129+
The callback method `onConnected` will be called once the connection to the files app is established.
130+
You can start making calls to the api before that callback is fired as the library will queue your calls until the connection is established[¹](https://github.com/nextcloud/Android-SingleSignOn/issues/400).
210131
211132
#### 5.1) **Using Retrofit**
212133
@@ -255,8 +176,8 @@ public class ApiProvider {
255176
private final API mApi;
256177
257178
public ApiProvider(@NonNull NextcloudAPI.ApiConnectedListener callback) {
258-
final SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
259-
final NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), callback);
179+
final var ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
180+
final var nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), callback);
260181
mApi = new NextcloudRetrofitApiBuilder(nextcloudAPI, API.mApiEndpoint).create(API.class);
261182
}
262183
}
@@ -271,8 +192,6 @@ Note: If you need a different mapping between your json-structure and your java-
271192
`NextcloudAPI` provides a method called `performNetworkRequest(NextcloudRequest request)` that allows you to handle the server response yourself.
272193

273194
```java
274-
import java.util.LinkedList;
275-
276195
public class MyActivity extends AppCompatActivity {
277196

278197
private NextcloudAPI mNextcloudAPI;
@@ -281,8 +200,8 @@ public class MyActivity extends AppCompatActivity {
281200
protected void onStart() {
282201
super.onStart();
283202
try {
284-
final SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(this);
285-
mNextcloudAPI = new NextcloudAPI(this, ssoAccount, new GsonBuilder().create(), apiCallback);
203+
final var ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(this);
204+
mNextcloudAPI = new NextcloudAPI(this, ssoAccount, new GsonBuilder().create());
286205

287206
// Start download of file in background thread (otherwise you'll get a NetworkOnMainThreadException)
288207
new Thread(this::downloadFile).start();
@@ -299,30 +218,18 @@ public class MyActivity extends AppCompatActivity {
299218
mNextcloudAPI.stop();
300219
}
301220

302-
private NextcloudAPI.ApiConnectedListener apiCallback = new NextcloudAPI.ApiConnectedListener() {
303-
@Override
304-
public void onConnected() {
305-
// ignore this one… see 5)
306-
}
307-
308-
@Override
309-
public void onError(Exception ex) {
310-
// TODO handle error in your app
311-
}
312-
};
313-
314221
private void downloadFile() {
315222
final List<Pair<String, String>> parameters = new ArrayList<>();
316223
parameters.add(new QueryPair("quality", "1024p"));
317224
parameters.add(new Pair<>("someOtherParameter", "parameterValue"));
318225

319-
final NextcloudRequest nextcloudRequest = new NextcloudRequest.Builder()
226+
final var nextcloudRequest = new NextcloudRequest.Builder()
320227
.setMethod("GET")
321228
.setParameter(parameters)
322229
.setUrl(Uri.encode("/remote.php/webdav/sample movie.mp4","/"))
323230
.build();
324231

325-
try (InputStream inputStream = mNextcloudAPI.performNetworkRequest(nextcloudRequest)) {
232+
try (final var inputStream = mNextcloudAPI.performNetworkRequest(nextcloudRequest)) {
326233
while(inputStream.available() > 0) {
327234
inputStream.read();
328235
// TODO do something useful with the data here..
@@ -346,7 +253,7 @@ final List<String> depth = new ArrayList<>();
346253
depth.add("0");
347254
header.put("Depth", depth);
348255

349-
final NextcloudRequest nextcloudRequest = new NextcloudRequest.Builder()
256+
final var nextcloudRequest = new NextcloudRequest.Builder()
350257
.setMethod("PROPFIND")
351258
.setHeader(header)
352259
.setUrl(Uri.encode("/remote.php/webdav/" + remotePath, "/"))
@@ -360,7 +267,7 @@ In case that you require some sso features that were introduced in a specific ne
360267
```java
361268
final int MIN_NEXTCLOUD_FILES_APP_VERSION_CODE = 30030052;
362269

363-
if (VersionCheckHelper.verifyMinVersion(context, MIN_NEXTCLOUD_FILES_APP_VERSION_CODE)) {
270+
if (VersionCheckHelper.verifyMinVersion(context, MIN_NEXTCLOUD_FILES_APP_VERSION_CODE, FilesAppType.PROD)) {
364271
// Version requirement is satisfied!
365272
}
366273
```

0 commit comments

Comments
 (0)