30
30
import fr .free .nrw .commons .data .DBOpenHelper ;
31
31
import fr .free .nrw .commons .modifications .ModifierSequence ;
32
32
import fr .free .nrw .commons .auth .AccountUtil ;
33
+ import fr .free .nrw .commons .nearby .NearbyPlaces ;
33
34
34
35
import org .acra .ACRA ;
35
36
import org .acra .ReportingInteractionMode ;
62
63
)
63
64
public class CommonsApplication extends Application {
64
65
65
- private MWApi api ;
66
66
private Account currentAccount = null ; // Unlike a savings account...
67
67
public static final String API_URL = "https://commons.wikimedia.org/w/api.php" ;
68
68
public static final String IMAGE_URL_BASE = "https://upload.wikimedia.org/wikipedia/commons" ;
@@ -81,13 +81,37 @@ public class CommonsApplication extends Application {
81
81
public static final String FEEDBACK_EMAIL = "commons-app-android@googlegroups.com" ;
82
82
public static final String FEEDBACK_EMAIL_SUBJECT = "Commons Android App (%s) Feedback" ;
83
83
84
- public RequestQueue volleyQueue ;
84
+ private static CommonsApplication instance = null ;
85
+ private AbstractHttpClient httpClient = null ;
86
+ private MWApi api = null ;
87
+ private CacheController cacheData = null ;
88
+ private RequestQueue volleyQueue = null ;
89
+ private DBOpenHelper dbOpenHelper = null ;
90
+ private NearbyPlaces nearbyPlaces = null ;
85
91
86
- public CacheController cacheData ;
92
+ /**
93
+ * This should not be called by ANY application code (other than the magic Android glue)
94
+ * Use CommonsApplication.getInstance() instead to get the singleton.
95
+ */
96
+ public CommonsApplication () {
97
+ CommonsApplication .instance = this ;
98
+ }
87
99
88
- public static CommonsApplication app ;
100
+ public static CommonsApplication getInstance () {
101
+ if (instance == null ) {
102
+ instance = new CommonsApplication ();
103
+ }
104
+ return instance ;
105
+ }
89
106
90
- public static AbstractHttpClient createHttpClient () {
107
+ public AbstractHttpClient getHttpClient () {
108
+ if (httpClient == null ) {
109
+ httpClient = newHttpClient ();
110
+ }
111
+ return httpClient ;
112
+ }
113
+
114
+ private AbstractHttpClient newHttpClient () {
91
115
BasicHttpParams params = new BasicHttpParams ();
92
116
SchemeRegistry schemeRegistry = new SchemeRegistry ();
93
117
schemeRegistry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
@@ -98,14 +122,50 @@ public static AbstractHttpClient createHttpClient() {
98
122
return new DefaultHttpClient (cm , params );
99
123
}
100
124
101
- public static MWApi createMWApi () {
102
- return new MWApi (API_URL , createHttpClient ());
125
+ public MWApi getMWApi () {
126
+ if (api == null ) {
127
+ api = newMWApi ();
128
+ }
129
+ return api ;
130
+ }
131
+
132
+ private MWApi newMWApi () {
133
+ return new MWApi (API_URL , getHttpClient ());
134
+ }
135
+
136
+ public CacheController getCacheData () {
137
+ if (cacheData == null ) {
138
+ cacheData = new CacheController ();
139
+ }
140
+ return cacheData ;
141
+ }
142
+
143
+ public RequestQueue getVolleyQueue () {
144
+ if (volleyQueue == null ) {
145
+ DiskBasedCache cache = new DiskBasedCache (getCacheDir (), 16 * 1024 * 1024 );
146
+ volleyQueue = new RequestQueue (cache , new BasicNetwork (new HurlStack ()));
147
+ volleyQueue .start ();
148
+ }
149
+ return volleyQueue ;
150
+ }
151
+
152
+ public synchronized DBOpenHelper getDBOpenHelper () {
153
+ if (dbOpenHelper == null ) {
154
+ dbOpenHelper = new DBOpenHelper (this );
155
+ }
156
+ return dbOpenHelper ;
157
+ }
158
+
159
+ public synchronized NearbyPlaces getNearbyPlaces () {
160
+ if (nearbyPlaces == null ) {
161
+ nearbyPlaces = new NearbyPlaces ();
162
+ }
163
+ return nearbyPlaces ;
103
164
}
104
165
105
166
@ Override
106
167
public void onCreate () {
107
168
super .onCreate ();
108
- app = this ;
109
169
110
170
Timber .plant (new Timber .DebugTree ());
111
171
@@ -116,9 +176,8 @@ public void onCreate() {
116
176
}
117
177
// Fire progress callbacks for every 3% of uploaded content
118
178
System .setProperty ("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD" , "3.0" );
119
- api = createMWApi ();
120
179
121
- ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration .Builder (getApplicationContext () )
180
+ ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration .Builder (this )
122
181
.discCache (new TotalSizeLimitedDiscCache (StorageUtils .getCacheDirectory (this ), 128 * 1024 * 1024 ))
123
182
.build ();
124
183
ImageLoader .getInstance ().init (imageLoaderConfiguration );
@@ -147,21 +206,14 @@ protected int sizeOf(String key, Bitmap bitmap) {
147
206
}
148
207
};
149
208
}
150
-
151
- //For caching area -> categories
152
- cacheData = new CacheController ();
153
-
154
- DiskBasedCache cache = new DiskBasedCache (getCacheDir (), 16 * 1024 * 1024 );
155
- volleyQueue = new RequestQueue (cache , new BasicNetwork (new HurlStack ()));
156
- volleyQueue .start ();
157
209
}
158
210
159
211
private com .android .volley .toolbox .ImageLoader imageLoader ;
160
212
private LruCache <String , Bitmap > imageCache ;
161
213
162
214
public com .android .volley .toolbox .ImageLoader getImageLoader () {
163
215
if (imageLoader == null ) {
164
- imageLoader = new com .android .volley .toolbox .ImageLoader (volleyQueue , new com .android .volley .toolbox .ImageLoader .ImageCache () {
216
+ imageLoader = new com .android .volley .toolbox .ImageLoader (getVolleyQueue () , new com .android .volley .toolbox .ImageLoader .ImageCache () {
165
217
@ Override
166
218
public Bitmap getBitmap (String key ) {
167
219
return imageCache .get (key );
@@ -176,13 +228,9 @@ public void putBitmap(String key, Bitmap bitmap) {
176
228
}
177
229
return imageLoader ;
178
230
}
179
-
180
- public MWApi getApi () {
181
- return api ;
182
- }
183
231
184
232
/**
185
- * @return Accout |null
233
+ * @return Account |null
186
234
*/
187
235
public Account getCurrentAccount () {
188
236
if (currentAccount == null ) {
@@ -203,21 +251,12 @@ public Boolean revalidateAuthToken() {
203
251
return false ; // This should never happen
204
252
}
205
253
206
- accountManager .invalidateAuthToken (AccountUtil .accountType (), api .getAuthCookie ());
254
+ accountManager .invalidateAuthToken (AccountUtil .accountType (), getMWApi () .getAuthCookie ());
207
255
try {
208
256
String authCookie = accountManager .blockingGetAuthToken (curAccount , "" , false );
209
- api .setAuthCookie (authCookie );
257
+ getMWApi () .setAuthCookie (authCookie );
210
258
return true ;
211
- } catch (OperationCanceledException e ) {
212
- e .printStackTrace ();
213
- return false ;
214
- } catch (AuthenticatorException e ) {
215
- e .printStackTrace ();
216
- return false ;
217
- } catch (IOException e ) {
218
- e .printStackTrace ();
219
- return false ;
220
- } catch (NullPointerException e ) {
259
+ } catch (OperationCanceledException | NullPointerException | IOException | AuthenticatorException e ) {
221
260
e .printStackTrace ();
222
261
return false ;
223
262
}
@@ -248,7 +287,7 @@ public void clearApplicationData(Context context) {
248
287
}
249
288
250
289
//TODO: fix preference manager
251
- PreferenceManager .getDefaultSharedPreferences (app ).edit ().clear ().commit ();
290
+ PreferenceManager .getDefaultSharedPreferences (getInstance () ).edit ().clear ().commit ();
252
291
SharedPreferences preferences = context
253
292
.getSharedPreferences ("fr.free.nrw.commons" , MODE_PRIVATE );
254
293
preferences .edit ().clear ().commit ();
@@ -263,7 +302,7 @@ public void clearApplicationData(Context context) {
263
302
* @param context context
264
303
*/
265
304
public void updateAllDatabases (Context context ) {
266
- DBOpenHelper dbOpenHelper = DBOpenHelper .getInstance (context );
305
+ DBOpenHelper dbOpenHelper = CommonsApplication .getInstance (). getDBOpenHelper ( );
267
306
dbOpenHelper .getReadableDatabase ().close ();
268
307
SQLiteDatabase db = dbOpenHelper .getWritableDatabase ();
269
308
0 commit comments