mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Injected the ContributionDao where needed.
This commit is contained in:
parent
f2ed57a127
commit
9927879680
9 changed files with 70 additions and 46 deletions
|
|
@ -8,13 +8,13 @@ import android.content.Context;
|
|||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
|
||||
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
|
||||
import timber.log.Timber;
|
||||
|
||||
import static android.accounts.AccountManager.ERROR_CODE_REMOTE_EXCEPTION;
|
||||
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
|
||||
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
|
||||
import static fr.free.nrw.commons.modifications.ModificationsContentProvider.AUTHORITY;
|
||||
|
||||
public class AccountUtil {
|
||||
|
||||
|
|
@ -51,8 +51,8 @@ public class AccountUtil {
|
|||
}
|
||||
|
||||
// FIXME: If the user turns it off, it shouldn't be auto turned back on
|
||||
ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
|
||||
ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
|
||||
ContentResolver.setSyncAutomatically(account, CONTRIBUTION_AUTHORITY, true); // Enable sync by default!
|
||||
ContentResolver.setSyncAutomatically(account, AUTHORITY, true); // Enable sync by default!
|
||||
}
|
||||
|
||||
private AccountManager accountManager() {
|
||||
|
|
|
|||
|
|
@ -11,40 +11,51 @@ import android.text.TextUtils;
|
|||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Provider;
|
||||
|
||||
import fr.free.nrw.commons.settings.Prefs;
|
||||
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI;
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.uriForId;
|
||||
|
||||
public class ContributionDao {
|
||||
private final ContentProviderClient client;
|
||||
private final Provider<ContentProviderClient> clientProvider;
|
||||
|
||||
public ContributionDao(ContentProviderClient client) {
|
||||
this.client = client;
|
||||
@Inject
|
||||
public ContributionDao(@Named("contribution") Provider<ContentProviderClient> clientProvider) {
|
||||
this.clientProvider = clientProvider;
|
||||
}
|
||||
|
||||
public void save(Contribution contribution) {
|
||||
ContentProviderClient db = clientProvider.get();
|
||||
try {
|
||||
if (contribution.getContentUri() == null) {
|
||||
contribution.setContentUri(client.insert(BASE_URI, toContentValues(contribution)));
|
||||
contribution.setContentUri(db.insert(BASE_URI, toContentValues(contribution)));
|
||||
} else {
|
||||
client.update(contribution.getContentUri(), toContentValues(contribution), null, null);
|
||||
db.update(contribution.getContentUri(), toContentValues(contribution), null, null);
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
db.release();
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(Contribution contribution) {
|
||||
ContentProviderClient db = clientProvider.get();
|
||||
try {
|
||||
if (contribution.getContentUri() == null) {
|
||||
// noooo
|
||||
throw new RuntimeException("tried to delete item with no content URI");
|
||||
} else {
|
||||
client.delete(contribution.getContentUri(), null, null);
|
||||
db.delete(contribution.getContentUri(), null, null);
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
db.release();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +85,7 @@ public class ContributionDao {
|
|||
return cv;
|
||||
}
|
||||
|
||||
public static Contribution fromCursor(Cursor cursor) {
|
||||
public Contribution fromCursor(Cursor cursor) {
|
||||
// Hardcoding column positions!
|
||||
//Check that cursor has a value to avoid CursorIndexOutOfBoundsException
|
||||
if (cursor.getCount() > 0) {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ import timber.log.Timber;
|
|||
import static android.content.ContentResolver.requestSync;
|
||||
import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED;
|
||||
import static fr.free.nrw.commons.contributions.ContributionDao.Table.ALL_FIELDS;
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.AUTHORITY;
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI;
|
||||
import static fr.free.nrw.commons.settings.Prefs.UPLOADS_SHOWING;
|
||||
|
||||
|
|
@ -58,6 +57,7 @@ public class ContributionsActivity
|
|||
@Inject MediaWikiApi mediaWikiApi;
|
||||
@Inject SessionManager sessionManager;
|
||||
@Inject @Named("default_preferences") SharedPreferences prefs;
|
||||
@Inject ContributionDao contributionDao;
|
||||
|
||||
private Cursor allContributions;
|
||||
private ContributionsListFragment contributionsList;
|
||||
|
|
@ -121,7 +121,7 @@ public class ContributionsActivity
|
|||
@Override
|
||||
protected void onAuthCookieAcquired(String authCookie) {
|
||||
// Do a sync everytime we get here!
|
||||
requestSync(sessionManager.getCurrentAccount(), ContributionsContentProvider.AUTHORITY, new Bundle());
|
||||
requestSync(sessionManager.getCurrentAccount(), ContributionsContentProvider.CONTRIBUTION_AUTHORITY, new Bundle());
|
||||
Intent uploadServiceIntent = new Intent(this, UploadService.class);
|
||||
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
|
||||
startService(uploadServiceIntent);
|
||||
|
|
@ -186,7 +186,7 @@ public class ContributionsActivity
|
|||
|
||||
public void retryUpload(int i) {
|
||||
allContributions.moveToPosition(i);
|
||||
Contribution c = ContributionDao.fromCursor(allContributions);
|
||||
Contribution c = contributionDao.fromCursor(allContributions);
|
||||
if (c.getState() == STATE_FAILED) {
|
||||
uploadService.queue(UploadService.ACTION_UPLOAD_FILE, c);
|
||||
Timber.d("Restarting for %s", c.toString());
|
||||
|
|
@ -197,10 +197,10 @@ public class ContributionsActivity
|
|||
|
||||
public void deleteUpload(int i) {
|
||||
allContributions.moveToPosition(i);
|
||||
Contribution c = ContributionDao.fromCursor(allContributions);
|
||||
Contribution c = contributionDao.fromCursor(allContributions);
|
||||
if (c.getState() == STATE_FAILED) {
|
||||
Timber.d("Deleting failed contrib %s", c.toString());
|
||||
new ContributionDao(getContentResolver().acquireContentProviderClient(AUTHORITY)).delete(c);
|
||||
contributionDao.delete(c);
|
||||
} else {
|
||||
Timber.d("Skipping deletion for non-failed contrib %s", c.toString());
|
||||
}
|
||||
|
|
@ -248,7 +248,7 @@ public class ContributionsActivity
|
|||
|
||||
if (contributionsList.getAdapter() == null) {
|
||||
contributionsList.setAdapter(new ContributionsListAdapter(getApplicationContext(),
|
||||
cursor, 0));
|
||||
cursor, 0, contributionDao));
|
||||
} else {
|
||||
((CursorAdapter) contributionsList.getAdapter()).swapCursor(cursor);
|
||||
}
|
||||
|
|
@ -269,7 +269,7 @@ public class ContributionsActivity
|
|||
// not yet ready to return data
|
||||
return null;
|
||||
} else {
|
||||
return ContributionDao.fromCursor((Cursor) contributionsList.getAdapter().getItem(i));
|
||||
return contributionDao.fromCursor((Cursor) contributionsList.getAdapter().getItem(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,13 +26,13 @@ public class ContributionsContentProvider extends ContentProvider {
|
|||
private static final int CONTRIBUTIONS_ID = 2;
|
||||
private static final String BASE_PATH = "contributions";
|
||||
private static final UriMatcher uriMatcher = new UriMatcher(NO_MATCH);
|
||||
public static final String AUTHORITY = "fr.free.nrw.commons.contributions.contentprovider";
|
||||
public static final String CONTRIBUTION_AUTHORITY = "fr.free.nrw.commons.contributions.contentprovider";
|
||||
|
||||
public static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
|
||||
public static final Uri BASE_URI = Uri.parse("content://" + CONTRIBUTION_AUTHORITY + "/" + BASE_PATH);
|
||||
|
||||
static {
|
||||
uriMatcher.addURI(AUTHORITY, BASE_PATH, CONTRIBUTIONS);
|
||||
uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CONTRIBUTIONS_ID);
|
||||
uriMatcher.addURI(CONTRIBUTION_AUTHORITY, BASE_PATH, CONTRIBUTIONS);
|
||||
uriMatcher.addURI(CONTRIBUTION_AUTHORITY, BASE_PATH + "/#", CONTRIBUTIONS_ID);
|
||||
}
|
||||
|
||||
public static Uri uriForId(int id) {
|
||||
|
|
|
|||
|
|
@ -11,8 +11,11 @@ import fr.free.nrw.commons.R;
|
|||
|
||||
class ContributionsListAdapter extends CursorAdapter {
|
||||
|
||||
public ContributionsListAdapter(Context context, Cursor c, int flags) {
|
||||
private final ContributionDao contributionDao;
|
||||
|
||||
public ContributionsListAdapter(Context context, Cursor c, int flags, ContributionDao contributionDao) {
|
||||
super(context, c, flags);
|
||||
this.contributionDao = contributionDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -26,7 +29,7 @@ class ContributionsListAdapter extends CursorAdapter {
|
|||
@Override
|
||||
public void bindView(View view, Context context, Cursor cursor) {
|
||||
final ContributionViewHolder views = (ContributionViewHolder)view.getTag();
|
||||
final Contribution contribution = ContributionDao.fromCursor(cursor);
|
||||
final Contribution contribution = contributionDao.fromCursor(cursor);
|
||||
|
||||
views.imageView.setMedia(contribution);
|
||||
views.titleView.setText(contribution.getDisplayTitle());
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import fr.free.nrw.commons.nearby.NearbyPlaces;
|
|||
import fr.free.nrw.commons.upload.UploadController;
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
|
||||
|
||||
@Module
|
||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||
|
|
@ -42,10 +43,16 @@ public class CommonsApplicationModule {
|
|||
|
||||
@Provides
|
||||
@Named("category")
|
||||
public ContentProviderClient provideContentProviderClient() {
|
||||
public ContentProviderClient provideCategoryContentProviderClient() {
|
||||
return application.getContentResolver().acquireContentProviderClient(CATEGORY_AUTHORITY);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Named("contribution")
|
||||
public ContentProviderClient provideContributionContentProviderClient() {
|
||||
return application.getContentResolver().acquireContentProviderClient(CONTRIBUTION_AUTHORITY);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Named("application_preferences")
|
||||
public SharedPreferences providesApplicationSharedPreferences() {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import timber.log.Timber;
|
|||
public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
|
||||
@Inject MediaWikiApi mwApi;
|
||||
@Inject ContributionDao contributionDao;
|
||||
|
||||
public ModificationsSyncAdapter(Context context, boolean autoInitialize) {
|
||||
super(context, autoInitialize);
|
||||
|
|
@ -80,7 +81,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||
|
||||
ContentProviderClient contributionsClient = null;
|
||||
try {
|
||||
contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
|
||||
contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.CONTRIBUTION_AUTHORITY);
|
||||
|
||||
while (!allModifications.isAfterLast()) {
|
||||
ModifierSequence sequence = ModifierSequenceDao.fromCursor(allModifications);
|
||||
|
|
@ -94,7 +95,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
contributionCursor.moveToFirst();
|
||||
contrib = ContributionDao.fromCursor(contributionCursor);
|
||||
contrib = contributionDao.fromCursor(contributionCursor);
|
||||
|
||||
if (contrib.getState() == Contribution.STATE_COMPLETED) {
|
||||
String pageContent;
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
@Inject MediaWikiApi mwApi;
|
||||
@Inject SessionManager sessionManager;
|
||||
@Inject @Named("default_preferences") SharedPreferences prefs;
|
||||
@Inject ContributionDao contributionDao;
|
||||
|
||||
private NotificationManager notificationManager;
|
||||
private ContentProviderClient contributionsProviderClient;
|
||||
|
|
@ -67,7 +68,6 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
public static final int NOTIFICATION_UPLOAD_IN_PROGRESS = 1;
|
||||
public static final int NOTIFICATION_UPLOAD_COMPLETE = 2;
|
||||
public static final int NOTIFICATION_UPLOAD_FAILED = 3;
|
||||
private ContributionDao dao;
|
||||
|
||||
public UploadService() {
|
||||
super("UploadService");
|
||||
|
|
@ -107,7 +107,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
|
||||
|
||||
contribution.setTransferred(transferred);
|
||||
dao.save(contribution);
|
||||
contributionDao.save(contribution);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -124,8 +124,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
super.onCreate();
|
||||
|
||||
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
|
||||
dao = new ContributionDao(contributionsProviderClient);
|
||||
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.CONTRIBUTION_AUTHORITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -147,7 +146,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
|
||||
contribution.setState(Contribution.STATE_QUEUED);
|
||||
contribution.setTransferred(0);
|
||||
dao.save(contribution);
|
||||
contributionDao.save(contribution);
|
||||
toUpload++;
|
||||
if (curProgressNotification != null && toUpload != 1) {
|
||||
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
|
||||
|
|
@ -262,7 +261,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
contribution.setImageUrl(uploadResult.getImageUrl());
|
||||
contribution.setState(Contribution.STATE_COMPLETED);
|
||||
contribution.setDateUploaded(uploadResult.getDateUploaded());
|
||||
dao.save(contribution);
|
||||
contributionDao.save(contribution);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Timber.d("I have a network fuckup");
|
||||
|
|
@ -293,7 +292,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, failureNotification);
|
||||
|
||||
contribution.setState(Contribution.STATE_FAILED);
|
||||
dao.save(contribution);
|
||||
contributionDao.save(contribution);
|
||||
}
|
||||
|
||||
private String findUniqueFilename(String fileName) throws IOException {
|
||||
|
|
|
|||
|
|
@ -31,10 +31,13 @@ import static fr.free.nrw.commons.contributions.Contribution.SOURCE_CAMERA;
|
|||
import static fr.free.nrw.commons.contributions.Contribution.SOURCE_GALLERY;
|
||||
import static fr.free.nrw.commons.contributions.Contribution.STATE_COMPLETED;
|
||||
import static fr.free.nrw.commons.contributions.Contribution.STATE_QUEUED;
|
||||
import static fr.free.nrw.commons.contributions.ContributionDao.*;
|
||||
import static fr.free.nrw.commons.contributions.ContributionDao.Table;
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI;
|
||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.uriForId;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Matchers.isA;
|
||||
|
|
@ -49,22 +52,22 @@ public class ContributionDaoTest {
|
|||
|
||||
private static final String LOCAL_URI = "http://example.com/";
|
||||
@Mock
|
||||
ContentProviderClient client;
|
||||
private ContentProviderClient client;
|
||||
@Mock
|
||||
SQLiteDatabase database;
|
||||
private SQLiteDatabase database;
|
||||
@Captor
|
||||
ArgumentCaptor<ContentValues> captor;
|
||||
private ArgumentCaptor<ContentValues> captor;
|
||||
|
||||
private Uri contentUri;
|
||||
private ContributionDao testObject;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
contentUri = uriForId(111);
|
||||
|
||||
testObject = new ContributionDao(client);
|
||||
testObject = new ContributionDao(() -> client);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -288,7 +291,7 @@ public class ContributionDaoTest {
|
|||
long uploaded = 456L;
|
||||
MatrixCursor mc = createCursor(created, uploaded, false, LOCAL_URI);
|
||||
|
||||
Contribution c = ContributionDao.fromCursor(mc);
|
||||
Contribution c = testObject.fromCursor(mc);
|
||||
|
||||
assertEquals(uriForId(111), c.getContentUri());
|
||||
assertEquals("file", c.getFilename());
|
||||
|
|
@ -312,7 +315,7 @@ public class ContributionDaoTest {
|
|||
public void createFromCursor_nullableTimestamps() {
|
||||
MatrixCursor mc = createCursor(0L, 0L, false, LOCAL_URI);
|
||||
|
||||
Contribution c = ContributionDao.fromCursor(mc);
|
||||
Contribution c = testObject.fromCursor(mc);
|
||||
|
||||
assertNull(c.getTimestamp());
|
||||
assertNull(c.getDateCreated());
|
||||
|
|
@ -323,7 +326,7 @@ public class ContributionDaoTest {
|
|||
public void createFromCursor_nullableLocalUri() {
|
||||
MatrixCursor mc = createCursor(0L, 0L, false, "");
|
||||
|
||||
Contribution c = ContributionDao.fromCursor(mc);
|
||||
Contribution c = testObject.fromCursor(mc);
|
||||
|
||||
assertNull(c.getLocalUri());
|
||||
assertNull(c.getDateCreated());
|
||||
|
|
@ -333,10 +336,10 @@ public class ContributionDaoTest {
|
|||
@Test
|
||||
public void createFromCursor_booleanEncoding() {
|
||||
MatrixCursor mcFalse = createCursor(0L, 0L, false, LOCAL_URI);
|
||||
assertFalse(ContributionDao.fromCursor(mcFalse).getMultiple());
|
||||
assertFalse(testObject.fromCursor(mcFalse).getMultiple());
|
||||
|
||||
MatrixCursor mcHammer = createCursor(0L, 0L, true, LOCAL_URI);
|
||||
assertTrue(ContributionDao.fromCursor(mcHammer).getMultiple());
|
||||
assertTrue(testObject.fromCursor(mcHammer).getMultiple());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue