1
1
package fr .free .nrw .commons ;
2
2
3
- import android .accounts .Account ;
4
- import android .accounts .AccountManager ;
5
- import android .accounts .AccountManagerCallback ;
6
- import android .accounts .AccountManagerFuture ;
7
- import android .accounts .AuthenticatorException ;
8
- import android .accounts .OperationCanceledException ;
9
3
import android .content .Context ;
10
4
import android .content .SharedPreferences ;
11
5
import android .database .sqlite .SQLiteDatabase ;
20
14
import org .acra .annotation .ReportsCrashes ;
21
15
22
16
import java .io .File ;
23
- import java .io .IOException ;
24
17
25
18
import javax .inject .Inject ;
26
19
27
20
import dagger .android .AndroidInjector ;
28
21
import dagger .android .DaggerApplication ;
29
- import fr .free .nrw .commons .auth .AccountUtil ;
22
+ import fr .free .nrw .commons .auth .SessionManager ;
30
23
import fr .free .nrw .commons .contributions .Contribution ;
31
24
import fr .free .nrw .commons .data .Category ;
32
25
import fr .free .nrw .commons .data .DBOpenHelper ;
37
30
import fr .free .nrw .commons .mwapi .MediaWikiApi ;
38
31
import fr .free .nrw .commons .theme .NavigationBaseActivity ;
39
32
import fr .free .nrw .commons .utils .FileUtils ;
33
+ import io .reactivex .android .schedulers .AndroidSchedulers ;
34
+ import io .reactivex .schedulers .Schedulers ;
40
35
import timber .log .Timber ;
41
36
42
37
// TODO: Use ProGuard to rip out reporting when publishing
51
46
public class CommonsApplication extends DaggerApplication {
52
47
53
48
@ Inject MediaWikiApi mediaWikiApi ;
54
- @ Inject AccountUtil accountUtil ;
49
+ @ Inject SessionManager sessionManager ;
55
50
@ Inject DBOpenHelper dbOpenHelper ;
56
51
57
- private Account currentAccount = null ; // Unlike a savings account...
58
52
public static final String API_URL = "https://commons.wikimedia.org/w/api.php" ;
59
53
public static final String IMAGE_URL_BASE = "https://upload.wikimedia.org/wikipedia/commons" ;
60
54
public static final String HOME_URL = "https://commons.wikimedia.org/wiki/" ;
@@ -113,39 +107,6 @@ public CommonsApplicationComponent injector() {
113
107
return component ;
114
108
}
115
109
116
- /**
117
- * @return Account|null
118
- */
119
- public Account getCurrentAccount () {
120
- if (currentAccount == null ) {
121
- AccountManager accountManager = AccountManager .get (this );
122
- Account [] allAccounts = accountManager .getAccountsByType (accountUtil .accountType ());
123
- if (allAccounts .length != 0 ) {
124
- currentAccount = allAccounts [0 ];
125
- }
126
- }
127
- return currentAccount ;
128
- }
129
-
130
- public Boolean revalidateAuthToken () {
131
- AccountManager accountManager = AccountManager .get (this );
132
- Account curAccount = getCurrentAccount ();
133
-
134
- if (curAccount == null ) {
135
- return false ; // This should never happen
136
- }
137
-
138
- accountManager .invalidateAuthToken (accountUtil .accountType (), mediaWikiApi .getAuthCookie ());
139
- try {
140
- String authCookie = accountManager .blockingGetAuthToken (curAccount , "" , false );
141
- mediaWikiApi .setAuthCookie (authCookie );
142
- return true ;
143
- } catch (OperationCanceledException | NullPointerException | IOException | AuthenticatorException e ) {
144
- e .printStackTrace ();
145
- return false ;
146
- }
147
- }
148
-
149
110
public void clearApplicationData (Context context , NavigationBaseActivity .LogoutListener logoutListener ) {
150
111
File cacheDirectory = context .getCacheDir ();
151
112
File applicationDirectory = new File (cacheDirectory .getParent ());
@@ -158,36 +119,10 @@ public void clearApplicationData(Context context, NavigationBaseActivity.LogoutL
158
119
}
159
120
}
160
121
161
- AccountManager accountManager = AccountManager .get (this );
162
- Account [] allAccounts = accountManager .getAccountsByType (accountUtil .accountType ());
163
-
164
- AccountManagerCallback <Boolean > amCallback = new AccountManagerCallback <Boolean >() {
165
-
166
- private int index = 0 ;
167
-
168
- void setIndex (int index ) {
169
- this .index = index ;
170
- }
171
-
172
- int getIndex () {
173
- return index ;
174
- }
175
-
176
- @ Override
177
- public void run (AccountManagerFuture <Boolean > accountManagerFuture ) {
178
- setIndex (getIndex () + 1 );
179
-
180
- try {
181
- if (accountManagerFuture != null ) {
182
- if (accountManagerFuture .getResult ()) {
183
- Timber .d ("Account removed successfully." );
184
- }
185
- }
186
- } catch (OperationCanceledException | IOException | AuthenticatorException e ) {
187
- e .printStackTrace ();
188
- }
189
-
190
- if (getIndex () == allAccounts .length ) {
122
+ sessionManager .clearAllAccounts ()
123
+ .subscribeOn (Schedulers .io ())
124
+ .observeOn (AndroidSchedulers .mainThread ())
125
+ .subscribe (() -> {
191
126
Timber .d ("All accounts have been removed" );
192
127
//TODO: fix preference manager
193
128
PreferenceManager .getDefaultSharedPreferences (CommonsApplication .this ).edit ().clear ().commit ();
@@ -197,22 +132,15 @@ public void run(AccountManagerFuture<Boolean> accountManagerFuture) {
197
132
context .getSharedPreferences ("prefs" , Context .MODE_PRIVATE ).edit ().clear ().commit ();
198
133
preferences .edit ().putBoolean ("firstrun" , false ).apply ();
199
134
updateAllDatabases ();
200
- currentAccount = null ;
201
135
202
136
logoutListener .onLogoutComplete ();
203
- }
204
- }
205
- };
206
-
207
- for (Account account : allAccounts ) {
208
- accountManager .removeAccount (account , amCallback , null );
209
- }
137
+ });
210
138
}
211
139
212
140
/**
213
141
* Deletes all tables and re-creates them.
214
142
*/
215
- public void updateAllDatabases () {
143
+ private void updateAllDatabases () {
216
144
dbOpenHelper .getReadableDatabase ().close ();
217
145
SQLiteDatabase db = dbOpenHelper .getWritableDatabase ();
218
146
0 commit comments