34
34
import android .util .Log ;
35
35
import android .widget .Toast ;
36
36
37
+ import androidx .core .app .ActivityCompat ;
38
+ import androidx .core .content .ContextCompat ;
39
+ import androidx .fragment .app .Fragment ;
40
+
37
41
import com .nextcloud .android .sso .exceptions .AndroidGetAccountsPermissionNotGranted ;
38
42
import com .nextcloud .android .sso .exceptions .NextcloudFilesAppAccountNotFoundException ;
39
43
import com .nextcloud .android .sso .exceptions .NextcloudFilesAppAccountPermissionNotGrantedException ;
45
49
46
50
import java .io .IOException ;
47
51
import java .util .ArrayList ;
52
+ import java .util .Arrays ;
48
53
import java .util .List ;
49
54
50
- import androidx .core .app .ActivityCompat ;
51
- import androidx .core .content .ContextCompat ;
52
- import androidx .fragment .app .Fragment ;
53
-
54
55
import io .reactivex .annotations .NonNull ;
55
56
56
57
import static android .app .Activity .RESULT_CANCELED ;
@@ -71,42 +72,49 @@ public class AccountImporter {
71
72
72
73
private static SharedPreferences SHARED_PREFERENCES ;
73
74
75
+ private static final List <String > APPS = Arrays .asList (Constants .PACKAGE_NAME_PROD , Constants .PACKAGE_NAME_DEV );
76
+ private static final String [] ACCOUNT_TYPES = {Constants .ACCOUNT_TYPE_PROD , Constants .ACCOUNT_TYPE_DEV };
77
+
74
78
public static boolean accountsToImportAvailable (Context context ) {
75
79
return findAccounts (context ).size () > 0 ;
76
80
}
77
81
78
- public static void pickNewAccount (Activity activity ) throws NextcloudFilesAppNotInstalledException , AndroidGetAccountsPermissionNotGranted {
82
+ public static void pickNewAccount (Activity activity ) throws NextcloudFilesAppNotInstalledException ,
83
+ AndroidGetAccountsPermissionNotGranted {
79
84
checkAndroidAccountPermissions (activity );
80
-
81
- if (appInstalledOrNot (activity , "com.nextcloud.client" )) {
82
- Intent intent = AccountManager .newChooseAccountIntent (null , null , new String []{ "nextcloud" } ,
83
- true , null , null , null , null );
85
+
86
+ if (appInstalledOrNot (activity )) {
87
+ Intent intent = AccountManager .newChooseAccountIntent (null , null , ACCOUNT_TYPES ,
88
+ true , null , null , null , null );
84
89
activity .startActivityForResult (intent , CHOOSE_ACCOUNT_SSO );
85
90
} else {
86
91
throw new NextcloudFilesAppNotInstalledException ();
87
92
}
88
93
}
89
94
90
- public static void pickNewAccount (Fragment fragment ) throws NextcloudFilesAppNotInstalledException , AndroidGetAccountsPermissionNotGranted {
95
+ public static void pickNewAccount (Fragment fragment ) throws NextcloudFilesAppNotInstalledException ,
96
+ AndroidGetAccountsPermissionNotGranted {
91
97
checkAndroidAccountPermissions (fragment .getContext ());
92
-
93
- if (appInstalledOrNot (fragment .getContext (), "com.nextcloud.client" )) {
94
- Intent intent = AccountManager .newChooseAccountIntent (null , null , new String []{ "nextcloud" } ,
95
- true , null , null , null , null );
98
+
99
+ if (appInstalledOrNot (fragment .getContext ())) {
100
+ Intent intent = AccountManager .newChooseAccountIntent (null , null , ACCOUNT_TYPES ,
101
+ true , null , null , null , null );
96
102
fragment .startActivityForResult (intent , CHOOSE_ACCOUNT_SSO );
97
103
} else {
98
104
throw new NextcloudFilesAppNotInstalledException ();
99
105
}
100
106
}
101
107
102
108
public static void requestAndroidAccountPermissionsAndPickAccount (Activity activity ) {
103
- ActivityCompat .requestPermissions (activity , new String []{Manifest .permission .GET_ACCOUNTS }, REQUEST_GET_ACCOUNTS_PERMISSION );
109
+ ActivityCompat .requestPermissions (activity , new String []{Manifest .permission .GET_ACCOUNTS },
110
+ REQUEST_GET_ACCOUNTS_PERMISSION );
104
111
}
105
112
106
113
private static void checkAndroidAccountPermissions (Context context ) throws AndroidGetAccountsPermissionNotGranted {
107
114
// https://developer.android.com/reference/android/accounts/AccountManager#getAccountsByType(java.lang.String)
108
- // Caller targeting API level below Build.VERSION_CODES.O that have not been granted the Manifest.permission.GET_ACCOUNTS permission,
109
- // will only see those accounts managed by AbstractAccountAuthenticators whose signature matches the client.
115
+ // Caller targeting API level below Build.VERSION_CODES.O that have not been granted the
116
+ // Manifest.permission.GET_ACCOUNTS permission, will only see those accounts managed by
117
+ // AbstractAccountAuthenticators whose signature matches the client.
110
118
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
111
119
// Do something for lollipop and above versions
112
120
if (ContextCompat .checkSelfPermission (context , Manifest .permission .GET_ACCOUNTS ) != PackageManager .PERMISSION_GRANTED ) {
@@ -118,26 +126,32 @@ private static void checkAndroidAccountPermissions(Context context) throws Andro
118
126
}
119
127
}
120
128
121
- private static boolean appInstalledOrNot (Context context , String uri ) {
129
+ private static boolean appInstalledOrNot (Context context ) {
130
+ boolean returnValue = false ;
122
131
PackageManager pm = context .getPackageManager ();
123
- try {
124
- pm .getPackageInfo (uri , PackageManager .GET_ACTIVITIES );
125
- return true ;
126
- } catch (PackageManager .NameNotFoundException e ) {
127
- Log .v (TAG , e .getMessage ());
132
+ for (String app : APPS ) {
133
+ try {
134
+ pm .getPackageInfo (app , PackageManager .GET_ACTIVITIES );
135
+ returnValue = true ;
136
+ break ;
137
+ } catch (PackageManager .NameNotFoundException e ) {
138
+ Log .v (TAG , e .getMessage ());
139
+ }
128
140
}
129
- return false ;
141
+ return returnValue ;
130
142
}
131
143
132
144
// Find all currently installed nextcloud accounts on the phone
133
145
public static List <Account > findAccounts (final Context context ) {
134
146
final AccountManager accMgr = AccountManager .get (context );
135
- final Account [] accounts = accMgr .getAccountsByType ( "nextcloud" );
147
+ final Account [] accounts = accMgr .getAccounts ( );
136
148
137
149
List <Account > accountsAvailable = new ArrayList <>();
138
150
for (final Account account : accounts ) {
139
- if ("nextcloud" .equals (account .type )) {
140
- accountsAvailable .add (account );
151
+ for (String accountType : ACCOUNT_TYPES ) {
152
+ if (accountType .equals (account .type )) {
153
+ accountsAvailable .add (account );
154
+ }
141
155
}
142
156
}
143
157
return accountsAvailable ;
@@ -188,10 +202,11 @@ public static SingleSignOnAccount extractSingleSignOnAccountFromResponse(Intent
188
202
}
189
203
String token = future .getString (Constants .SSO_TOKEN );
190
204
String serverUrl = future .getString (Constants .SSO_SERVER_URL );
205
+ String type = future .getString ("accountType" );
191
206
192
207
SharedPreferences mPrefs = getSharedPreferences (context );
193
208
String prefKey = getPrefKeyForAccount (accountName );
194
- SingleSignOnAccount ssoAccount = new SingleSignOnAccount (accountName , userId , token , serverUrl );
209
+ SingleSignOnAccount ssoAccount = new SingleSignOnAccount (accountName , userId , token , serverUrl , type );
195
210
try {
196
211
mPrefs .edit ().putString (prefKey , SingleSignOnAccount .toString (ssoAccount )).apply ();
197
212
} catch (IOException e ) {
@@ -351,9 +366,17 @@ private static Intent buildRequestAuthTokenIntent(Context context, Intent intent
351
366
if (account == null ) {
352
367
throw new NextcloudFilesAppAccountPermissionNotGrantedException ();
353
368
}
369
+
370
+ String componentName ;
371
+ if (account .type .equalsIgnoreCase (Constants .ACCOUNT_TYPE_DEV )) {
372
+ componentName = Constants .PACKAGE_NAME_DEV ;
373
+ } else {
374
+ componentName = Constants .PACKAGE_NAME_PROD ;
375
+ }
376
+
354
377
Intent authIntent = new Intent ();
355
- authIntent .setComponent (new ComponentName ("com.nextcloud.client" ,
356
- "com.owncloud.android.ui.activity.SsoGrantPermissionActivity" ));
378
+ authIntent .setComponent (new ComponentName (componentName ,
379
+ "com.owncloud.android.ui.activity.SsoGrantPermissionActivity" ));
357
380
authIntent .putExtra (NEXTCLOUD_FILES_ACCOUNT , account );
358
381
return authIntent ;
359
382
}
0 commit comments