Moved other singletons over to being managed by Dagger

This commit is contained in:
Paul Hawke 2017-08-25 22:26:03 -05:00
parent 647cc166ef
commit cfe17cafa6
9 changed files with 69 additions and 74 deletions

View file

@ -9,7 +9,6 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import android.support.v4.util.LruCache;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.stetho.Stetho;
@ -27,7 +26,6 @@ import javax.inject.Inject;
import dagger.android.AndroidInjector;
import dagger.android.DaggerApplication;
import fr.free.nrw.commons.auth.AccountUtil;
import fr.free.nrw.commons.caching.CacheController;
import fr.free.nrw.commons.contributions.Contribution;
import fr.free.nrw.commons.data.Category;
import fr.free.nrw.commons.data.DBOpenHelper;
@ -35,9 +33,7 @@ import fr.free.nrw.commons.di.CommonsApplicationComponent;
import fr.free.nrw.commons.di.CommonsApplicationModule;
import fr.free.nrw.commons.di.DaggerCommonsApplicationComponent;
import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.mwapi.ApacheHttpClientMediaWikiApi;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.nearby.NearbyPlaces;
import fr.free.nrw.commons.utils.FileUtils;
import timber.log.Timber;
@ -54,6 +50,7 @@ public class CommonsApplication extends DaggerApplication {
@Inject MediaWikiApi mediaWikiApi;
@Inject AccountUtil accountUtil;
@Inject DBOpenHelper dbOpenHelper;
private Account currentAccount = null; // Unlike a savings account...
public static final String API_URL = "https://commons.wikimedia.org/w/api.php";
@ -73,37 +70,8 @@ public class CommonsApplication extends DaggerApplication {
public static final String FEEDBACK_EMAIL = "commons-app-android@googlegroups.com";
public static final String FEEDBACK_EMAIL_SUBJECT = "Commons Android App (%s) Feedback";
private LruCache<String, String> thumbnailUrlCache = new LruCache<>(1024);
private CacheController cacheData = null;
private DBOpenHelper dbOpenHelper = null;
private NearbyPlaces nearbyPlaces = null;
private CommonsApplicationComponent component;
public CacheController getCacheData() {
if (cacheData == null) {
cacheData = new CacheController();
}
return cacheData;
}
public LruCache<String, String> getThumbnailUrlCache() {
return thumbnailUrlCache;
}
public synchronized DBOpenHelper getDBOpenHelper() {
if (dbOpenHelper == null) {
dbOpenHelper = new DBOpenHelper(this);
}
return dbOpenHelper;
}
public synchronized NearbyPlaces getNearbyPlaces() {
if (nearbyPlaces == null) {
nearbyPlaces = new NearbyPlaces();
}
return nearbyPlaces;
}
@Override
public void onCreate() {
super.onCreate();
@ -127,9 +95,6 @@ public class CommonsApplication extends DaggerApplication {
System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0");
Fresco.initialize(this);
//For caching area -> categories
cacheData = new CacheController();
}
@Override
@ -150,10 +115,10 @@ public class CommonsApplication extends DaggerApplication {
* @return Account|null
*/
public Account getCurrentAccount() {
if(currentAccount == null) {
if (currentAccount == null) {
AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
if(allAccounts.length != 0) {
if (allAccounts.length != 0) {
currentAccount = allAccounts[0];
}
}
@ -164,7 +129,7 @@ public class CommonsApplication extends DaggerApplication {
AccountManager accountManager = AccountManager.get(this);
Account curAccount = getCurrentAccount();
if(curAccount == null) {
if (curAccount == null) {
return false; // This should never happen
}
@ -218,7 +183,6 @@ public class CommonsApplication extends DaggerApplication {
* Deletes all tables and re-creates them.
*/
public void updateAllDatabases() {
DBOpenHelper dbOpenHelper = getDBOpenHelper();
dbOpenHelper.getReadableDatabase().close();
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

View file

@ -4,6 +4,7 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.util.LruCache;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.Toast;
@ -19,6 +20,8 @@ import timber.log.Timber;
public class MediaWikiImageView extends SimpleDraweeView {
@Inject CommonsApplication application;
@Inject MediaWikiApi mwApi;
@Inject LruCache<String, String> thumbnailUrlCache;
private ThumbnailFetchTask currentThumbnailTask;
public MediaWikiImageView(Context context) {
@ -44,8 +47,8 @@ public class MediaWikiImageView extends SimpleDraweeView {
return;
}
if (application.getThumbnailUrlCache().get(media.getFilename()) != null) {
setImageUrl(application.getThumbnailUrlCache().get(media.getFilename()));
if (thumbnailUrlCache.get(media.getFilename()) != null) {
setImageUrl(thumbnailUrlCache.get(media.getFilename()));
} else {
setImageUrl(null);
currentThumbnailTask = new ThumbnailFetchTask(media, mwApi);
@ -91,8 +94,7 @@ public class MediaWikiImageView extends SimpleDraweeView {
} else {
// only cache meaningful thumbnails received from network.
try {
CommonsApplication app = (CommonsApplication) getContext().getApplicationContext();
app.getThumbnailUrlCache().put(media.getFilename(), result);
thumbnailUrlCache.put(media.getFilename(), result);
} catch (NullPointerException npe) {
Timber.e("error when adding pic to cache " + npe);

View file

@ -40,13 +40,11 @@ public class CategoryContentProvider extends ContentProvider {
}
@Inject CommonsApplication application;
private DBOpenHelper dbOpenHelper;
@Inject DBOpenHelper dbOpenHelper;
@Override
public boolean onCreate() {
AndroidInjection.inject(this);
dbOpenHelper = application.getDBOpenHelper();
return false;
}

View file

@ -14,6 +14,7 @@ import javax.inject.Inject;
import dagger.android.AndroidInjection;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.data.DBOpenHelper;
import timber.log.Timber;
public class ContributionsContentProvider extends ContentProvider{
@ -37,6 +38,7 @@ public class ContributionsContentProvider extends ContentProvider{
}
@Inject CommonsApplication application;
@Inject DBOpenHelper dbOpenHelper;
@Override
public boolean onCreate() {
@ -51,7 +53,7 @@ public class ContributionsContentProvider extends ContentProvider{
int uriType = uriMatcher.match(uri);
SQLiteDatabase db = application.getDBOpenHelper().getReadableDatabase();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor;
switch(uriType) {
@ -85,7 +87,7 @@ public class ContributionsContentProvider extends ContentProvider{
@Override
public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
long id = 0;
switch (uriType) {
case CONTRIBUTIONS:
@ -103,7 +105,7 @@ public class ContributionsContentProvider extends ContentProvider{
int rows = 0;
int uriType = uriMatcher.match(uri);
SQLiteDatabase db = application.getDBOpenHelper().getReadableDatabase();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch(uriType) {
case CONTRIBUTIONS_ID:
@ -124,7 +126,7 @@ public class ContributionsContentProvider extends ContentProvider{
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
Timber.d("Hello, bulk insert! (ContributionsContentProvider)");
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
sqlDB.beginTransaction();
switch (uriType) {
case CONTRIBUTIONS:
@ -152,7 +154,7 @@ public class ContributionsContentProvider extends ContentProvider{
In here, the only concat created argument is for id. It is cast to an int, and will error out otherwise.
*/
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
int rowsUpdated = 0;
switch (uriType) {
case CONTRIBUTIONS:

View file

@ -1,12 +1,17 @@
package fr.free.nrw.commons.di;
import android.support.v4.util.LruCache;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.caching.CacheController;
import fr.free.nrw.commons.data.DBOpenHelper;
import fr.free.nrw.commons.mwapi.ApacheHttpClientMediaWikiApi;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.nearby.NearbyPlaces;
@Module
public class CommonsApplicationModule {
@ -26,4 +31,28 @@ public class CommonsApplicationModule {
public MediaWikiApi provideMediaWikiApi() {
return new ApacheHttpClientMediaWikiApi(CommonsApplication.API_URL);
}
@Provides
@Singleton
public CacheController provideCacheController() {
return new CacheController();
}
@Provides
@Singleton
public DBOpenHelper provideDBOpenHelper(CommonsApplication application) {
return new DBOpenHelper(application);
}
@Provides
@Singleton
public NearbyPlaces provideNearbyPlaces() {
return new NearbyPlaces();
}
@Provides
@Singleton
public LruCache<String, String> provideLruCache() {
return new LruCache<>(1024);
}
}

View file

@ -14,6 +14,7 @@ import javax.inject.Inject;
import dagger.android.AndroidInjection;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.data.DBOpenHelper;
import timber.log.Timber;
public class ModificationsContentProvider extends ContentProvider{
@ -37,6 +38,7 @@ public class ModificationsContentProvider extends ContentProvider{
}
@Inject CommonsApplication application;
@Inject DBOpenHelper dbOpenHelper;
@Override
public boolean onCreate() {
@ -58,7 +60,7 @@ public class ModificationsContentProvider extends ContentProvider{
throw new IllegalArgumentException("Unknown URI" + uri);
}
SQLiteDatabase db = application.getDBOpenHelper().getReadableDatabase();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
@ -74,7 +76,7 @@ public class ModificationsContentProvider extends ContentProvider{
@Override
public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
long id = 0;
switch (uriType) {
case MODIFICATIONS:
@ -90,7 +92,7 @@ public class ModificationsContentProvider extends ContentProvider{
@Override
public int delete(@NonNull Uri uri, String s, String[] strings) {
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
switch (uriType) {
case MODIFICATIONS_ID:
String id = uri.getLastPathSegment();
@ -108,7 +110,7 @@ public class ModificationsContentProvider extends ContentProvider{
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
Timber.d("Hello, bulk insert! (ModificationsContentProvider)");
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
sqlDB.beginTransaction();
switch (uriType) {
case MODIFICATIONS:
@ -136,7 +138,7 @@ public class ModificationsContentProvider extends ContentProvider{
In here, the only concat created argument is for id. It is cast to an int, and will error out otherwise.
*/
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
int rowsUpdated = 0;
switch (uriType) {
case MODIFICATIONS:

View file

@ -31,7 +31,6 @@ import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.location.LocationServiceManager;
@ -43,7 +42,7 @@ public class NearbyActivity extends NavigationBaseActivity {
@BindView(R.id.progressBar)
ProgressBar progressBar;
@Inject CommonsApplication application;
@Inject NearbyPlaces nearbyPlaces;
private boolean isMapViewActive = false;
private static final int LOCATION_REQUEST = 1;
@ -94,7 +93,7 @@ public class NearbyActivity extends NavigationBaseActivity {
locationManager = new LocationServiceManager(this);
locationManager.registerLocationManager();
curLatLang = locationManager.getLatestLocation();
nearbyAsyncTask = new NearbyAsyncTask(this, new NearbyController(application));
nearbyAsyncTask = new NearbyAsyncTask(this, new NearbyController(nearbyPlaces));
nearbyAsyncTask.execute();
}
@ -233,7 +232,7 @@ public class NearbyActivity extends NavigationBaseActivity {
}
private void refreshView() {
nearbyAsyncTask = new NearbyAsyncTask(this, new NearbyController(application));
nearbyAsyncTask = new NearbyAsyncTask(this, new NearbyController(nearbyPlaces));
nearbyAsyncTask.execute();
}
@ -262,7 +261,7 @@ public class NearbyActivity extends NavigationBaseActivity {
@Override
protected List<Place> doInBackground(Void... params) {
return nearbyController.loadAttractionsFromLocation(curLatLang, application);
return nearbyController.loadAttractionsFromLocation(curLatLang, NearbyActivity.this);
}
@Override

View file

@ -15,7 +15,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.utils.UiUtils;
@ -27,11 +26,10 @@ import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
public class NearbyController {
private static final int MAX_RESULTS = 1000;
private final NearbyPlaces nearbyPlaces;
private final CommonsApplication application;
public NearbyController(CommonsApplication application) {
this.application = application;
public NearbyController(NearbyPlaces nearbyPlaces) {
this.nearbyPlaces = nearbyPlaces;
}
/**
@ -45,7 +43,6 @@ public class NearbyController {
if (curLatLng == null) {
return Collections.emptyList();
}
NearbyPlaces nearbyPlaces = application.getNearbyPlaces();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<Place> places = prefs.getBoolean("useWikidata", true)
? nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage())

View file

@ -37,6 +37,7 @@ import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.auth.AuthenticatedActivity;
import fr.free.nrw.commons.caching.CacheController;
import fr.free.nrw.commons.category.CategorizationFragment;
import fr.free.nrw.commons.category.OnCategoriesSaveHandler;
import fr.free.nrw.commons.contributions.Contribution;
@ -68,6 +69,7 @@ public class ShareActivity
@Inject CommonsApplication application;
@Inject MediaWikiApi mwApi;
@Inject CacheController cacheController;
private String source;
private String mimeType;
@ -137,7 +139,7 @@ public class ShareActivity
if (!cacheFound) {
//Has to be called after apiCall.request()
application.getCacheData().cacheCategory();
cacheController.cacheCategory();
Timber.d("Cache the categories found");
}
@ -482,12 +484,12 @@ public class ShareActivity
if (imageObj.imageCoordsExists) {
double decLongitude = imageObj.getDecLongitude();
double decLatitude = imageObj.getDecLatitude();
application.getCacheData().setQtPoint(decLongitude, decLatitude);
cacheController.setQtPoint(decLongitude, decLatitude);
}
MwVolleyApi apiCall = new MwVolleyApi(application);
List<String> displayCatList = application.getCacheData().findCategory();
List<String> displayCatList = cacheController.findCategory();
boolean catListEmpty = displayCatList.isEmpty();
// If no categories found in cache, call MediaWiki API to match image coords with nearby Commons categories