mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
Injected the ModifierSequenceDao where needed.
This commit is contained in:
parent
9927879680
commit
874627e3f5
9 changed files with 49 additions and 32 deletions
|
|
@ -14,7 +14,7 @@ import static android.accounts.AccountManager.ERROR_CODE_REMOTE_EXCEPTION;
|
||||||
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
|
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
|
||||||
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
|
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
|
||||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
|
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
|
||||||
import static fr.free.nrw.commons.modifications.ModificationsContentProvider.AUTHORITY;
|
import static fr.free.nrw.commons.modifications.ModificationsContentProvider.MODIFICATIONS_AUTHORITY;
|
||||||
|
|
||||||
public class AccountUtil {
|
public class AccountUtil {
|
||||||
|
|
||||||
|
|
@ -52,7 +52,7 @@ public class AccountUtil {
|
||||||
|
|
||||||
// FIXME: If the user turns it off, it shouldn't be auto turned back on
|
// FIXME: If the user turns it off, it shouldn't be auto turned back on
|
||||||
ContentResolver.setSyncAutomatically(account, CONTRIBUTION_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!
|
ContentResolver.setSyncAutomatically(account, MODIFICATIONS_AUTHORITY, true); // Enable sync by default!
|
||||||
}
|
}
|
||||||
|
|
||||||
private AccountManager accountManager() {
|
private AccountManager accountManager() {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import fr.free.nrw.commons.upload.UploadController;
|
||||||
|
|
||||||
import static android.content.Context.MODE_PRIVATE;
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
|
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
|
||||||
|
import static fr.free.nrw.commons.modifications.ModificationsContentProvider.MODIFICATIONS_AUTHORITY;
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||||
|
|
@ -53,6 +54,12 @@ public class CommonsApplicationModule {
|
||||||
return application.getContentResolver().acquireContentProviderClient(CONTRIBUTION_AUTHORITY);
|
return application.getContentResolver().acquireContentProviderClient(CONTRIBUTION_AUTHORITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Named("modification")
|
||||||
|
public ContentProviderClient provideModificationContentProviderClient() {
|
||||||
|
return application.getContentResolver().acquireContentProviderClient(MODIFICATIONS_AUTHORITY);
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Named("application_preferences")
|
@Named("application_preferences")
|
||||||
public SharedPreferences providesApplicationSharedPreferences() {
|
public SharedPreferences providesApplicationSharedPreferences() {
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,15 @@ public class ModificationsContentProvider extends ContentProvider {
|
||||||
private static final int MODIFICATIONS = 1;
|
private static final int MODIFICATIONS = 1;
|
||||||
private static final int MODIFICATIONS_ID = 2;
|
private static final int MODIFICATIONS_ID = 2;
|
||||||
|
|
||||||
public static final String AUTHORITY = "fr.free.nrw.commons.modifications.contentprovider";
|
public static final String MODIFICATIONS_AUTHORITY = "fr.free.nrw.commons.modifications.contentprovider";
|
||||||
public static final String BASE_PATH = "modifications";
|
public static final String BASE_PATH = "modifications";
|
||||||
|
|
||||||
public static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
|
public static final Uri BASE_URI = Uri.parse("content://" + MODIFICATIONS_AUTHORITY + "/" + BASE_PATH);
|
||||||
|
|
||||||
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
||||||
static {
|
static {
|
||||||
uriMatcher.addURI(AUTHORITY, BASE_PATH, MODIFICATIONS);
|
uriMatcher.addURI(MODIFICATIONS_AUTHORITY, BASE_PATH, MODIFICATIONS);
|
||||||
uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", MODIFICATIONS_ID);
|
uriMatcher.addURI(MODIFICATIONS_AUTHORITY, BASE_PATH + "/#", MODIFICATIONS_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Uri uriForId(int id) {
|
public static Uri uriForId(int id) {
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
|
||||||
|
|
||||||
@Inject MediaWikiApi mwApi;
|
@Inject MediaWikiApi mwApi;
|
||||||
@Inject ContributionDao contributionDao;
|
@Inject ContributionDao contributionDao;
|
||||||
|
@Inject ModifierSequenceDao modifierSequenceDao;
|
||||||
|
|
||||||
public ModificationsSyncAdapter(Context context, boolean autoInitialize) {
|
public ModificationsSyncAdapter(Context context, boolean autoInitialize) {
|
||||||
super(context, autoInitialize);
|
super(context, autoInitialize);
|
||||||
|
|
@ -84,8 +85,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
|
||||||
contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.CONTRIBUTION_AUTHORITY);
|
contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.CONTRIBUTION_AUTHORITY);
|
||||||
|
|
||||||
while (!allModifications.isAfterLast()) {
|
while (!allModifications.isAfterLast()) {
|
||||||
ModifierSequence sequence = ModifierSequenceDao.fromCursor(allModifications);
|
ModifierSequence sequence = modifierSequenceDao.fromCursor(allModifications);
|
||||||
ModifierSequenceDao dao = new ModifierSequenceDao(contributionsClient);
|
|
||||||
Contribution contrib;
|
Contribution contrib;
|
||||||
|
|
||||||
Cursor contributionCursor;
|
Cursor contributionCursor;
|
||||||
|
|
@ -123,7 +123,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
|
||||||
// FIXME: Log this somewhere else
|
// FIXME: Log this somewhere else
|
||||||
Timber.d("Non success result! %s", editResult);
|
Timber.d("Non success result! %s", editResult);
|
||||||
} else {
|
} else {
|
||||||
dao.delete(sequence);
|
modifierSequenceDao.delete(sequence);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
allModifications.moveToNext();
|
allModifications.moveToNext();
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,20 @@ import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Named;
|
||||||
|
import javax.inject.Provider;
|
||||||
|
|
||||||
public class ModifierSequenceDao {
|
public class ModifierSequenceDao {
|
||||||
|
|
||||||
private final ContentProviderClient client;
|
private final Provider<ContentProviderClient> clientProvider;
|
||||||
|
|
||||||
public ModifierSequenceDao(ContentProviderClient client) {
|
@Inject
|
||||||
this.client = client;
|
public ModifierSequenceDao(@Named("modification") Provider<ContentProviderClient> clientProvider) {
|
||||||
|
this.clientProvider = clientProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ModifierSequence fromCursor(Cursor cursor) {
|
public ModifierSequence fromCursor(Cursor cursor) {
|
||||||
// Hardcoding column positions!
|
// Hardcoding column positions!
|
||||||
ModifierSequence ms = null;
|
ModifierSequence ms = null;
|
||||||
try {
|
try {
|
||||||
|
|
@ -34,22 +39,28 @@ public class ModifierSequenceDao {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(ModifierSequence sequence) {
|
public void save(ModifierSequence sequence) {
|
||||||
|
ContentProviderClient db = clientProvider.get();
|
||||||
try {
|
try {
|
||||||
if (sequence.getContentUri() == null) {
|
if (sequence.getContentUri() == null) {
|
||||||
sequence.setContentUri(client.insert(ModificationsContentProvider.BASE_URI, toContentValues(sequence)));
|
sequence.setContentUri(db.insert(ModificationsContentProvider.BASE_URI, toContentValues(sequence)));
|
||||||
} else {
|
} else {
|
||||||
client.update(sequence.getContentUri(), toContentValues(sequence), null, null);
|
db.update(sequence.getContentUri(), toContentValues(sequence), null, null);
|
||||||
}
|
}
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
db.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(ModifierSequence sequence) {
|
public void delete(ModifierSequence sequence) {
|
||||||
|
ContentProviderClient db = clientProvider.get();
|
||||||
try {
|
try {
|
||||||
client.delete(sequence.getContentUri(), null, null);
|
db.delete(sequence.getContentUri(), null, null);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
db.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package fr.free.nrw.commons.upload;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.ContentProviderClient;
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
@ -55,6 +54,7 @@ public class MultipleShareActivity extends AuthenticatedActivity
|
||||||
@Inject MediaWikiApi mwApi;
|
@Inject MediaWikiApi mwApi;
|
||||||
@Inject SessionManager sessionManager;
|
@Inject SessionManager sessionManager;
|
||||||
@Inject UploadController uploadController;
|
@Inject UploadController uploadController;
|
||||||
|
@Inject ModifierSequenceDao modifierSequenceDao;
|
||||||
@Inject @Named("default_preferences") SharedPreferences prefs;
|
@Inject @Named("default_preferences") SharedPreferences prefs;
|
||||||
|
|
||||||
private ArrayList<Contribution> photosList = null;
|
private ArrayList<Contribution> photosList = null;
|
||||||
|
|
@ -166,19 +166,18 @@ public class MultipleShareActivity extends AuthenticatedActivity
|
||||||
@Override
|
@Override
|
||||||
public void onCategoriesSave(List<String> categories) {
|
public void onCategoriesSave(List<String> categories) {
|
||||||
if (categories.size() > 0) {
|
if (categories.size() > 0) {
|
||||||
ModifierSequenceDao dao = new ModifierSequenceDao(getContentResolver().acquireContentProviderClient(ModificationsContentProvider.AUTHORITY));
|
|
||||||
for (Contribution contribution : photosList) {
|
for (Contribution contribution : photosList) {
|
||||||
ModifierSequence categoriesSequence = new ModifierSequence(contribution.getContentUri());
|
ModifierSequence categoriesSequence = new ModifierSequence(contribution.getContentUri());
|
||||||
|
|
||||||
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
||||||
categoriesSequence.queueModifier(new TemplateRemoveModifier("Uncategorized"));
|
categoriesSequence.queueModifier(new TemplateRemoveModifier("Uncategorized"));
|
||||||
|
|
||||||
dao.save(categoriesSequence);
|
modifierSequenceDao.save(categoriesSequence);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// FIXME: Make sure that the content provider is up
|
// FIXME: Make sure that the content provider is up
|
||||||
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
||||||
ContentResolver.setSyncAutomatically(sessionManager.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
|
ContentResolver.setSyncAutomatically(sessionManager.getCurrentAccount(), ModificationsContentProvider.MODIFICATIONS_AUTHORITY, true); // Enable sync by default!
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,7 @@ public class ShareActivity
|
||||||
@Inject CacheController cacheController;
|
@Inject CacheController cacheController;
|
||||||
@Inject SessionManager sessionManager;
|
@Inject SessionManager sessionManager;
|
||||||
@Inject UploadController uploadController;
|
@Inject UploadController uploadController;
|
||||||
|
@Inject ModifierSequenceDao modifierSequenceDao;
|
||||||
@Inject @Named("default_preferences") SharedPreferences prefs;
|
@Inject @Named("default_preferences") SharedPreferences prefs;
|
||||||
|
|
||||||
private String source;
|
private String source;
|
||||||
|
|
@ -166,13 +167,12 @@ public class ShareActivity
|
||||||
|
|
||||||
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
||||||
categoriesSequence.queueModifier(new TemplateRemoveModifier("Uncategorized"));
|
categoriesSequence.queueModifier(new TemplateRemoveModifier("Uncategorized"));
|
||||||
ModifierSequenceDao dao = new ModifierSequenceDao(getContentResolver().acquireContentProviderClient(ModificationsContentProvider.AUTHORITY));
|
modifierSequenceDao.save(categoriesSequence);
|
||||||
dao.save(categoriesSequence);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Make sure that the content provider is up
|
// FIXME: Make sure that the content provider is up
|
||||||
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
||||||
ContentResolver.setSyncAutomatically(sessionManager.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
|
ContentResolver.setSyncAutomatically(sessionManager.getCurrentAccount(), ModificationsContentProvider.MODIFICATIONS_AUTHORITY, true); // Enable sync by default!
|
||||||
|
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -273,7 +273,7 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
toUpload--;
|
toUpload--;
|
||||||
if (toUpload == 0) {
|
if (toUpload == 0) {
|
||||||
// Sync modifications right after all uplaods are processed
|
// Sync modifications right after all uplaods are processed
|
||||||
ContentResolver.requestSync(sessionManager.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, new Bundle());
|
ContentResolver.requestSync(sessionManager.getCurrentAccount(), ModificationsContentProvider.MODIFICATIONS_AUTHORITY, new Bundle());
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,14 +52,14 @@ public class ModifierSequenceDaoTest {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
testObject = new ModifierSequenceDao(client);
|
testObject = new ModifierSequenceDao(() -> client);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createFromCursorWithEmptyModifiers() {
|
public void createFromCursorWithEmptyModifiers() {
|
||||||
MatrixCursor cursor = createCursor("");
|
MatrixCursor cursor = createCursor("");
|
||||||
|
|
||||||
ModifierSequence seq = ModifierSequenceDao.fromCursor(cursor);
|
ModifierSequence seq = testObject.fromCursor(cursor);
|
||||||
|
|
||||||
assertEquals(EXPECTED_MEDIA_URI, seq.getMediaUri().toString());
|
assertEquals(EXPECTED_MEDIA_URI, seq.getMediaUri().toString());
|
||||||
assertEquals(BASE_URI.buildUpon().appendPath("1").toString(), seq.getContentUri().toString());
|
assertEquals(BASE_URI.buildUpon().appendPath("1").toString(), seq.getContentUri().toString());
|
||||||
|
|
@ -70,7 +70,7 @@ public class ModifierSequenceDaoTest {
|
||||||
public void createFromCursorWtihCategoryModifier() {
|
public void createFromCursorWtihCategoryModifier() {
|
||||||
MatrixCursor cursor = createCursor("{\"name\": \"CategoriesModifier\", \"data\": {}}");
|
MatrixCursor cursor = createCursor("{\"name\": \"CategoriesModifier\", \"data\": {}}");
|
||||||
|
|
||||||
ModifierSequence seq = ModifierSequenceDao.fromCursor(cursor);
|
ModifierSequence seq = testObject.fromCursor(cursor);
|
||||||
|
|
||||||
assertEquals(1, seq.getModifiers().size());
|
assertEquals(1, seq.getModifiers().size());
|
||||||
assertTrue(seq.getModifiers().get(0) instanceof CategoryModifier);
|
assertTrue(seq.getModifiers().get(0) instanceof CategoryModifier);
|
||||||
|
|
@ -80,7 +80,7 @@ public class ModifierSequenceDaoTest {
|
||||||
public void createFromCursorWithRemoveModifier() {
|
public void createFromCursorWithRemoveModifier() {
|
||||||
MatrixCursor cursor = createCursor("{\"name\": \"TemplateRemoverModifier\", \"data\": {}}");
|
MatrixCursor cursor = createCursor("{\"name\": \"TemplateRemoverModifier\", \"data\": {}}");
|
||||||
|
|
||||||
ModifierSequence seq = ModifierSequenceDao.fromCursor(cursor);
|
ModifierSequence seq = testObject.fromCursor(cursor);
|
||||||
|
|
||||||
assertEquals(1, seq.getModifiers().size());
|
assertEquals(1, seq.getModifiers().size());
|
||||||
assertTrue(seq.getModifiers().get(0) instanceof TemplateRemoveModifier);
|
assertTrue(seq.getModifiers().get(0) instanceof TemplateRemoveModifier);
|
||||||
|
|
@ -89,7 +89,7 @@ public class ModifierSequenceDaoTest {
|
||||||
@Test
|
@Test
|
||||||
public void deleteSequence() throws Exception {
|
public void deleteSequence() throws Exception {
|
||||||
when(client.delete(isA(Uri.class), isNull(String.class), isNull(String[].class))).thenReturn(1);
|
when(client.delete(isA(Uri.class), isNull(String.class), isNull(String[].class))).thenReturn(1);
|
||||||
ModifierSequence seq = ModifierSequenceDao.fromCursor(createCursor(""));
|
ModifierSequence seq = testObject.fromCursor(createCursor(""));
|
||||||
|
|
||||||
testObject.delete(seq);
|
testObject.delete(seq);
|
||||||
|
|
||||||
|
|
@ -99,7 +99,7 @@ public class ModifierSequenceDaoTest {
|
||||||
@Test(expected = RuntimeException.class)
|
@Test(expected = RuntimeException.class)
|
||||||
public void deleteTranslatesRemoteExceptions() throws Exception {
|
public void deleteTranslatesRemoteExceptions() throws Exception {
|
||||||
when(client.delete(isA(Uri.class), isNull(String.class), isNull(String[].class))).thenThrow(new RemoteException(""));
|
when(client.delete(isA(Uri.class), isNull(String.class), isNull(String[].class))).thenThrow(new RemoteException(""));
|
||||||
ModifierSequence seq = ModifierSequenceDao.fromCursor(createCursor(""));
|
ModifierSequence seq = testObject.fromCursor(createCursor(""));
|
||||||
|
|
||||||
testObject.delete(seq);
|
testObject.delete(seq);
|
||||||
}
|
}
|
||||||
|
|
@ -110,9 +110,9 @@ public class ModifierSequenceDaoTest {
|
||||||
String expectedData = "{\"modifiers\":[" + modifierJson + "]}";
|
String expectedData = "{\"modifiers\":[" + modifierJson + "]}";
|
||||||
MatrixCursor cursor = createCursor(modifierJson);
|
MatrixCursor cursor = createCursor(modifierJson);
|
||||||
|
|
||||||
testObject.save(ModifierSequenceDao.fromCursor(cursor));
|
testObject.save(testObject.fromCursor(cursor));
|
||||||
|
|
||||||
verify(client).update(eq(ModifierSequenceDao.fromCursor(cursor).getContentUri()), contentValuesCaptor.capture(), isNull(String.class), isNull(String[].class));
|
verify(client).update(eq(testObject.fromCursor(cursor).getContentUri()), contentValuesCaptor.capture(), isNull(String.class), isNull(String[].class));
|
||||||
ContentValues cv = contentValuesCaptor.getValue();
|
ContentValues cv = contentValuesCaptor.getValue();
|
||||||
assertEquals(2, cv.size());
|
assertEquals(2, cv.size());
|
||||||
assertEquals(EXPECTED_MEDIA_URI, cv.get(ModifierSequenceDao.Table.COLUMN_MEDIA_URI));
|
assertEquals(EXPECTED_MEDIA_URI, cv.get(ModifierSequenceDao.Table.COLUMN_MEDIA_URI));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue