1111
1212import static android .app .Activity .RESULT_CANCELED ;
1313import static android .app .Activity .RESULT_OK ;
14- import static com .nextcloud .android .sso .Constants .NEXTCLOUD_FILES_ACCOUNT ;
15- import static com .nextcloud .android .sso .Constants .NEXTCLOUD_SSO ;
16- import static com .nextcloud .android .sso .Constants .NEXTCLOUD_SSO_EXCEPTION ;
17- import static com .nextcloud .android .sso .Constants .SSO_SHARED_PREFERENCE ;
14+ import static com .nextcloud .android .sso .Constants .INTERNAL_NEXTCLOUD_FILES_ACCOUNT ;
15+ import static com .nextcloud .android .sso .Constants .INTERNAL_NEXTCLOUD_SSO ;
16+ import static com .nextcloud .android .sso .Constants .INTERNAL_NEXTCLOUD_SSO_EXCEPTION ;
17+ import static com .nextcloud .android .sso .Constants .INTERNAL_SSO_SHARED_PREFERENCE ;
1818
1919import android .Manifest ;
2020import android .accounts .Account ;
5151import java .io .IOException ;
5252import java .util .ArrayList ;
5353import java .util .List ;
54+ import java .util .Optional ;
5455import java .util .function .Consumer ;
5556
5657import io .reactivex .annotations .Nullable ;
@@ -68,7 +69,7 @@ public class AccountImporter {
6869 private static SharedPreferences SHARED_PREFERENCES ;
6970
7071 public static boolean accountsToImportAvailable (Context context ) {
71- return findAccounts (context ).size () > 0 ;
72+ return ! findAccounts (context ).isEmpty () ;
7273 }
7374
7475 /**
@@ -78,53 +79,28 @@ public static boolean accountsToImportAvailable(Context context) {
7879 * In real live applications, you won't import an account on each app start, but remember the imported account via SingleAccountHelper.
7980 *
8081 * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
81- * @deprecated Use {@link ImportSsoAccount} - this method will be private in the future
8282 */
83- @ Deprecated ( forRemoval = true )
83+ @ RestrictTo ( RestrictTo . Scope . LIBRARY )
8484 public static void pickNewAccount (Activity activity ) throws NextcloudFilesAppNotInstalledException ,
8585 AndroidGetAccountsPermissionNotGranted {
8686 checkAndroidAccountPermissions (activity );
8787
8888 if (appInstalledOrNot (activity )) {
89- Intent intent = AccountManager .newChooseAccountIntent (null , null , FilesAppTypeRegistry .getInstance ().getAccountTypes (),
89+ Intent intent = AccountManager .newChooseAccountIntent (null , null , FilesAppTypeRegistry .getInstance ().getAccountTypes (),
9090 true , null , AUTH_TOKEN_SSO , null , null );
9191 activity .startActivityForResult (intent , CHOOSE_ACCOUNT_SSO );
9292 } else {
9393 throw new NextcloudFilesAppNotInstalledException (activity );
9494 }
9595 }
9696
97- /**
98- * Prompt dialog to select existing or create a new account
99- * As soon as an account has been imported, we will continue in #onActivityResult()
100- * <p>
101- * In real live applications, you won't import an account on each app start, but remember the imported account via SingleAccountHelper.
102- *
103- * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
104- * @deprecated Use {@link ImportSsoAccount}
105- */
106- @ Deprecated (forRemoval = true )
107- public static void pickNewAccount (Fragment fragment ) throws NextcloudFilesAppNotInstalledException ,
108- AndroidGetAccountsPermissionNotGranted {
109- checkAndroidAccountPermissions (fragment .getContext ());
110-
111- if (appInstalledOrNot (fragment .requireContext ())) {
112- Intent intent = AccountManager .newChooseAccountIntent (null , null , FilesAppTypeRegistry .getInstance ().getAccountTypes (),
113- true , null , AUTH_TOKEN_SSO , null , null );
114- fragment .startActivityForResult (intent , CHOOSE_ACCOUNT_SSO );
115- } else {
116- throw new NextcloudFilesAppNotInstalledException (fragment .requireContext ());
117- }
118- }
119-
12097 /**
12198 * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
122- * @deprecated Use {@link ImportSsoAccount}
12399 */
124- @ Deprecated ( forRemoval = true )
100+ @ RestrictTo ( RestrictTo . Scope . LIBRARY )
125101 public static void requestAndroidAccountPermissionsAndPickAccount (Activity activity ) {
126102 ActivityCompat .requestPermissions (activity , new String []{Manifest .permission .GET_ACCOUNTS },
127- REQUEST_GET_ACCOUNTS_PERMISSION );
103+ REQUEST_GET_ACCOUNTS_PERMISSION );
128104 }
129105
130106 @ RestrictTo (RestrictTo .Scope .LIBRARY )
@@ -153,7 +129,7 @@ private static boolean appInstalledOrNot(Context context) {
153129 returnValue = true ;
154130 break ;
155131 } catch (PackageManager .NameNotFoundException e ) {
156- Log .v (TAG , e .getMessage ());
132+ Log .v (TAG , Optional . ofNullable ( e .getMessage ()). orElse ( e . toString () ));
157133 }
158134 }
159135 return returnValue ;
@@ -210,21 +186,17 @@ public static SingleSignOnAccount getSingleSignOnAccount(Context context, final
210186 throw new NextcloudFilesAppAccountNotFoundException (context , accountName );
211187 }
212188
213- /**
214- * @deprecated Visibility will be private in the future
215- */
216- @ Deprecated (forRemoval = true )
217- public static SingleSignOnAccount extractSingleSignOnAccountFromResponse (Intent intent , Context context ) {
218- Bundle future = intent .getBundleExtra (NEXTCLOUD_SSO );
189+ private static SingleSignOnAccount extractSingleSignOnAccountFromResponse (Intent intent , Context context ) {
190+ Bundle future = intent .getBundleExtra (INTERNAL_NEXTCLOUD_SSO );
219191
220192 String accountName = future .getString (AccountManager .KEY_ACCOUNT_NAME );
221- String userId = future .getString (Constants .SSO_USER_ID );
193+ String userId = future .getString (Constants .INTERNAL_SSO_USER_ID );
222194 if (userId == null ) {
223195 // backwards compatibility
224196 userId = future .getString ("username" );
225197 }
226- String token = future .getString (Constants .SSO_TOKEN );
227- String serverUrl = future .getString (Constants .SSO_SERVER_URL );
198+ String token = future .getString (Constants .INTERNAL_SSO_TOKEN );
199+ String serverUrl = future .getString (Constants .INTERNAL_SSO_SERVER_URL );
228200 String type = future .getString ("accountType" );
229201
230202 SharedPreferences mPrefs = getSharedPreferences (context );
@@ -238,69 +210,35 @@ public static SingleSignOnAccount extractSingleSignOnAccountFromResponse(Intent
238210 return ssoAccount ;
239211 }
240212
241- /**
242- * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
243- * @deprecated Use {@link ImportSsoAccount} - Visibility will be private in the future.
244- */
245- @ Deprecated
213+ @ RestrictTo (RestrictTo .Scope .LIBRARY )
246214 public interface IAccountAccessGranted {
247215 void accountAccessGranted (SingleSignOnAccount singleSignOnAccount );
248216 }
249217
250- /**
251- * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
252- * @deprecated Use {@link ImportSsoAccount}
253- */
254- @ Deprecated
255- public static void onActivityResult (int requestCode , int resultCode , Intent data , Activity activity ,
256- IAccountAccessGranted callback ) throws AccountImportCancelledException {
257- onActivityResult (requestCode , resultCode , data , activity , null , callback , t -> {});
258- }
259-
260- /**
261- * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
262- * @deprecated Use {@link ImportSsoAccount}
263- */
264- @ Deprecated
265- public static void onActivityResult (int requestCode , int resultCode , Intent data , Fragment fragment ,
266- IAccountAccessGranted callback ) throws AccountImportCancelledException {
267- onActivityResult (requestCode , resultCode , data , null , fragment , callback , t -> {});
268- }
269-
270218 @ RestrictTo (RestrictTo .Scope .LIBRARY )
271- public static void onActivityResult (int requestCode , int resultCode , Intent data , Activity activity ,
272- Fragment fragment , IAccountAccessGranted successCallback , Consumer <Throwable > failureCallback )
219+ public static void onActivityResult (int requestCode , int resultCode , Intent data , Activity activity , IAccountAccessGranted successCallback , Consumer <Throwable > failureCallback )
273220 throws AccountImportCancelledException {
274- Context context = (activity != null ) ? activity : fragment .getContext ();
275221
276222 if (resultCode == RESULT_OK ) {
277223 switch (requestCode ) {
278224 case CHOOSE_ACCOUNT_SSO :
279225 try {
280- if (activity != null ) {
281- requestAuthToken (activity , data );
282- } else {
283- requestAuthToken (fragment , data );
284- }
226+ requestAuthToken (activity , data );
285227 } catch (NextcloudFilesAppNotSupportedException |
286228 NextcloudFilesAppAccountPermissionNotGrantedException e ) {
287- UiExceptionManager .showDialogForException (context , e , failureCallback ::accept );
229+ UiExceptionManager .showDialogForException (activity , e , failureCallback ::accept );
288230 }
289231 break ;
290232 case REQUEST_AUTH_TOKEN_SSO :
291- SingleSignOnAccount singleSignOnAccount = extractSingleSignOnAccountFromResponse (data , context );
233+ SingleSignOnAccount singleSignOnAccount = extractSingleSignOnAccountFromResponse (data , activity );
292234 successCallback .accountAccessGranted (singleSignOnAccount );
293235 break ;
294236 case REQUEST_GET_ACCOUNTS_PERMISSION :
295237 try {
296- if (activity != null ) {
297- pickNewAccount (activity );
298- } else {
299- pickNewAccount (fragment );
300- }
238+ pickNewAccount (activity );
301239 } catch (NextcloudFilesAppNotInstalledException |
302240 AndroidGetAccountsPermissionNotGranted e ) {
303- UiExceptionManager .showDialogForException (context , e , failureCallback ::accept );
241+ UiExceptionManager .showDialogForException (activity , e , failureCallback ::accept );
304242 }
305243 break ;
306244 default :
@@ -310,67 +248,43 @@ public static void onActivityResult(int requestCode, int resultCode, Intent data
310248 switch (requestCode ) {
311249 case CHOOSE_ACCOUNT_SSO :
312250 // nothing to do here
313- throw new AccountImportCancelledException (context );
251+ throw new AccountImportCancelledException (activity );
314252 case REQUEST_AUTH_TOKEN_SSO :
315253 try {
316- handleFailedAuthRequest (context , data );
254+ handleFailedAuthRequest (activity , data );
317255 } catch (SSOException e ) {
318- UiExceptionManager .showDialogForException (context , e , failureCallback ::accept );
256+ UiExceptionManager .showDialogForException (activity , e , failureCallback ::accept );
319257 } catch (Exception e ) {
320- Toast .makeText (context , e .getMessage (), Toast .LENGTH_LONG ).show ();
321- //e.printStackTrace();
322- Log .e (TAG , e .getMessage ());
258+ Toast .makeText (activity , e .getMessage (), Toast .LENGTH_LONG ).show ();
259+ Log .e (TAG , e .getMessage (), e );
323260 failureCallback .accept (e );
324261 }
325262 break ;
326263 case REQUEST_GET_ACCOUNTS_PERMISSION :
327- UiExceptionManager .showDialogForException (context , new AndroidGetAccountsPermissionNotGranted (context ), failureCallback ::accept );
264+ UiExceptionManager .showDialogForException (activity , new AndroidGetAccountsPermissionNotGranted (activity ), failureCallback ::accept );
328265 break ;
329266 default :
330267 break ;
331268 }
332269 }
333270 }
334271
335- /**
336- * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
337- * @deprecated Use {@link ImportSsoAccount}
338- */
339- @ Deprecated
340- public static void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults , Activity activity ) {
341- onRequestPermissionsResult (requestCode , permissions , grantResults , activity , null , t -> {});
342- }
343-
344- /**
345- * @see <a href="https://developer.android.com/training/basics/intents/result"><code>ActivityResultContract</code></a>
346- * @deprecated Use {@link ImportSsoAccount}
347- */
348- @ Deprecated
349- public static void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults , Fragment fragment ) {
350- onRequestPermissionsResult (requestCode , permissions , grantResults , null , fragment , t -> {});
351- }
352-
353272 @ RestrictTo (RestrictTo .Scope .LIBRARY )
354273 public static void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults , Activity activity , Fragment fragment , Consumer <Throwable > failureCallback ) {
355- Context context = (activity != null ) ? activity : fragment .getContext ();
356-
274+ //noinspection SwitchStatementWithTooFewBranches
357275 switch (requestCode ) {
358276 case REQUEST_GET_ACCOUNTS_PERMISSION :
359277 if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
360- // Permissions have been granted.. start pick account dialog
278+ // Permissions have been granted… start pick account dialog
361279 try {
362- if (activity != null ) {
363- pickNewAccount (activity );
364- } else {
365- pickNewAccount (fragment );
366- }
280+ pickNewAccount (activity );
367281 } catch (NextcloudFilesAppNotInstalledException |
368282 AndroidGetAccountsPermissionNotGranted e ) {
369- UiExceptionManager .showDialogForException (context , e , failureCallback ::accept );
283+ UiExceptionManager .showDialogForException (activity , e , failureCallback ::accept );
370284 }
371285 } else {
372- // user declined the permission request..
373- UiExceptionManager .showDialogForException (context , new AndroidGetAccountsPermissionNotGranted (context ), failureCallback ::accept );
286+ // user declined the permission request…
287+ UiExceptionManager .showDialogForException (activity , new AndroidGetAccountsPermissionNotGranted (activity ), failureCallback ::accept );
374288 }
375289 break ;
376290 default :
@@ -379,13 +293,9 @@ public static void onRequestPermissionsResult(int requestCode, @NonNull String[]
379293
380294 }
381295
382- /**
383- * @deprecated Will be private in the future
384- */
385- @ Deprecated
386- public static void handleFailedAuthRequest (@ NonNull Context context , @ Nullable Intent data ) throws SSOException {
296+ private static void handleFailedAuthRequest (@ NonNull Context context , @ Nullable Intent data ) throws SSOException {
387297 if (data != null ) {
388- String exception = data .getStringExtra (NEXTCLOUD_SSO_EXCEPTION );
298+ String exception = data .getStringExtra (INTERNAL_NEXTCLOUD_SSO_EXCEPTION );
389299 throw SSOException .parseNextcloudCustomException (context , new Exception (exception ));
390300 } else {
391301 Log .e (TAG , "handleFailedAuthRequest failed - data is null" );
@@ -435,15 +345,15 @@ private static Intent buildRequestAuthTokenIntent(Context context, Intent intent
435345 Intent authIntent = new Intent ();
436346 authIntent .setComponent (new ComponentName (componentName ,
437347 "com.owncloud.android.ui.activity.SsoGrantPermissionActivity" ));
438- authIntent .putExtra (NEXTCLOUD_FILES_ACCOUNT , account );
348+ authIntent .putExtra (INTERNAL_NEXTCLOUD_FILES_ACCOUNT , account );
439349 return authIntent ;
440350 }
441351
442352 public static SharedPreferences getSharedPreferences (Context context ) {
443353 if (SHARED_PREFERENCES != null ) {
444354 return SHARED_PREFERENCES ;
445355 } else {
446- return context .getSharedPreferences (SSO_SHARED_PREFERENCE , Context .MODE_PRIVATE );
356+ return context .getSharedPreferences (INTERNAL_SSO_SHARED_PREFERENCE , Context .MODE_PRIVATE );
447357 }
448358 }
449359
0 commit comments