13
13
import static android .accounts .AccountManager .ERROR_CODE_REMOTE_EXCEPTION ;
14
14
import static android .accounts .AccountManager .KEY_ACCOUNT_NAME ;
15
15
import static android .accounts .AccountManager .KEY_ACCOUNT_TYPE ;
16
+ import static android .accounts .AccountManager .KEY_PASSWORD ;
17
+ import static android .icu .lang .UCharacter .GraphemeClusterBreak .L ;
16
18
import static fr .free .nrw .commons .contributions .ContributionsContentProvider .CONTRIBUTION_AUTHORITY ;
17
19
import static fr .free .nrw .commons .modifications .ModificationsContentProvider .MODIFICATIONS_AUTHORITY ;
18
20
@@ -31,7 +33,7 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
31
33
String username , String password ) {
32
34
33
35
Account account = new Account (username , ACCOUNT_TYPE );
34
- boolean created = accountManager ().addAccountExplicitly (account , password , null );
36
+ boolean created = accountManager (context ).addAccountExplicitly (account , password , null );
35
37
36
38
Timber .d ("account creation " + (created ? "successful" : "failure" ));
37
39
@@ -40,7 +42,7 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
40
42
Bundle bundle = new Bundle ();
41
43
bundle .putString (KEY_ACCOUNT_NAME , username );
42
44
bundle .putString (KEY_ACCOUNT_TYPE , ACCOUNT_TYPE );
43
-
45
+ bundle . putString ( KEY_PASSWORD , password );
44
46
45
47
response .onResult (bundle );
46
48
}
@@ -57,7 +59,35 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
57
59
ContentResolver .setSyncAutomatically (account , MODIFICATIONS_AUTHORITY , true ); // Enable sync by default!
58
60
}
59
61
60
- private AccountManager accountManager () {
62
+ /**
63
+ * @return Account|null
64
+ */
65
+ @ Nullable
66
+ public static Account account (Context context ) {
67
+ try {
68
+ Account [] accounts = accountManager (context ).getAccountsByType (ACCOUNT_TYPE );
69
+ if (accounts .length > 0 ) {
70
+ return accounts [0 ];
71
+ }
72
+ } catch (SecurityException e ) {
73
+ Timber .e (e );
74
+ }
75
+ return null ;
76
+ }
77
+
78
+ @ Nullable
79
+ public static String getUserName (Context context ) {
80
+ Account account = account (context );
81
+ return account == null ? null : account .name ;
82
+ }
83
+
84
+ @ Nullable
85
+ public static String getPassword (Context context ) {
86
+ Account account = account (context );
87
+ return account == null ? null : accountManager (context ).getPassword (account );
88
+ }
89
+
90
+ private static AccountManager accountManager (Context context ) {
61
91
return AccountManager .get (context );
62
92
}
63
93
0 commit comments