1
1
package fr .free .nrw .commons .auth ;
2
2
3
3
import android .accounts .Account ;
4
+ import android .accounts .AccountAuthenticatorResponse ;
4
5
import android .accounts .AccountManager ;
6
+ import android .content .ContentResolver ;
5
7
import android .content .Context ;
6
8
import android .content .SharedPreferences ;
9
+ import android .os .Bundle ;
7
10
8
11
import javax .annotation .Nullable ;
9
12
13
+ import fr .free .nrw .commons .BuildConfig ;
10
14
import fr .free .nrw .commons .mwapi .MediaWikiApi ;
11
15
import io .reactivex .Completable ;
12
16
import io .reactivex .Observable ;
13
17
import timber .log .Timber ;
14
18
15
- import static fr .free .nrw .commons .auth .AccountUtil .ACCOUNT_TYPE ;
19
+ import static android .accounts .AccountManager .ERROR_CODE_REMOTE_EXCEPTION ;
20
+ import static android .accounts .AccountManager .KEY_ACCOUNT_NAME ;
21
+ import static android .accounts .AccountManager .KEY_ACCOUNT_TYPE ;
16
22
17
23
/**
18
24
* Manage the current logged in user session.
@@ -23,21 +29,65 @@ public class SessionManager {
23
29
private Account currentAccount ; // Unlike a savings account... ;-)
24
30
private SharedPreferences sharedPreferences ;
25
31
26
- public SessionManager (Context context , MediaWikiApi mediaWikiApi , SharedPreferences sharedPreferences ) {
32
+
33
+ public SessionManager (Context context ,
34
+ MediaWikiApi mediaWikiApi ,
35
+ SharedPreferences sharedPreferences ) {
27
36
this .context = context ;
28
37
this .mediaWikiApi = mediaWikiApi ;
29
38
this .currentAccount = null ;
30
39
this .sharedPreferences = sharedPreferences ;
31
40
}
32
41
42
+ /**
43
+ * Creata a new account
44
+ *
45
+ * @param response
46
+ * @param username
47
+ * @param password
48
+ */
49
+ public void createAccount (@ Nullable AccountAuthenticatorResponse response ,
50
+ String username , String password ) {
51
+
52
+ Account account = new Account (username , BuildConfig .ACCOUNT_TYPE );
53
+ boolean created = accountManager ().addAccountExplicitly (account , password , null );
54
+
55
+ Timber .d ("account creation " + (created ? "successful" : "failure" ));
56
+
57
+ if (created ) {
58
+ if (response != null ) {
59
+ Bundle bundle = new Bundle ();
60
+ bundle .putString (KEY_ACCOUNT_NAME , username );
61
+ bundle .putString (KEY_ACCOUNT_TYPE , BuildConfig .ACCOUNT_TYPE );
62
+
63
+
64
+ response .onResult (bundle );
65
+ }
66
+
67
+ } else {
68
+ if (response != null ) {
69
+ response .onError (ERROR_CODE_REMOTE_EXCEPTION , "" );
70
+ }
71
+ Timber .d ("account creation failure" );
72
+ }
73
+
74
+ // FIXME: If the user turns it off, it shouldn't be auto turned back on
75
+ ContentResolver .setSyncAutomatically (account , BuildConfig .CONTRIBUTION_AUTHORITY , true ); // Enable sync by default!
76
+ ContentResolver .setSyncAutomatically (account , BuildConfig .MODIFICATION_AUTHORITY , true ); // Enable sync by default!
77
+ }
78
+
79
+ private AccountManager accountManager () {
80
+ return AccountManager .get (context );
81
+ }
82
+
33
83
/**
34
84
* @return Account|null
35
85
*/
36
86
@ Nullable
37
87
public Account getCurrentAccount () {
38
88
if (currentAccount == null ) {
39
89
AccountManager accountManager = AccountManager .get (context );
40
- Account [] allAccounts = accountManager .getAccountsByType (ACCOUNT_TYPE );
90
+ Account [] allAccounts = accountManager .getAccountsByType (BuildConfig . ACCOUNT_TYPE );
41
91
if (allAccounts .length != 0 ) {
42
92
currentAccount = allAccounts [0 ];
43
93
}
@@ -53,7 +103,7 @@ public Boolean revalidateAuthToken() {
53
103
return false ; // This should never happen
54
104
}
55
105
56
- accountManager .invalidateAuthToken (ACCOUNT_TYPE , mediaWikiApi .getAuthCookie ());
106
+ accountManager .invalidateAuthToken (BuildConfig . ACCOUNT_TYPE , mediaWikiApi .getAuthCookie ());
57
107
String authCookie = getAuthCookie ();
58
108
59
109
if (authCookie == null ) {
@@ -92,7 +142,7 @@ public void forceLogin(Context context) {
92
142
93
143
public Completable clearAllAccounts () {
94
144
AccountManager accountManager = AccountManager .get (context );
95
- Account [] allAccounts = accountManager .getAccountsByType (ACCOUNT_TYPE );
145
+ Account [] allAccounts = accountManager .getAccountsByType (BuildConfig . ACCOUNT_TYPE );
96
146
return Completable .fromObservable (Observable .fromArray (allAccounts )
97
147
.map (a -> accountManager .removeAccount (a , null , null ).getResult ()))
98
148
.doOnComplete (() -> currentAccount = null );
0 commit comments