Skip to content

Commit 9927879

Browse files
committed
Injected the ContributionDao where needed.
1 parent f2ed57a commit 9927879

File tree

9 files changed

+70
-46
lines changed

9 files changed

+70
-46
lines changed

app/src/main/java/fr/free/nrw/commons/auth/AccountUtil.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import android.os.Bundle;
99
import android.support.annotation.Nullable;
1010

11-
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
12-
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
1311
import timber.log.Timber;
1412

1513
import static android.accounts.AccountManager.ERROR_CODE_REMOTE_EXCEPTION;
1614
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
1715
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
16+
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
17+
import static fr.free.nrw.commons.modifications.ModificationsContentProvider.AUTHORITY;
1818

1919
public class AccountUtil {
2020

@@ -51,8 +51,8 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
5151
}
5252

5353
// FIXME: If the user turns it off, it shouldn't be auto turned back on
54-
ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
55-
ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
54+
ContentResolver.setSyncAutomatically(account, CONTRIBUTION_AUTHORITY, true); // Enable sync by default!
55+
ContentResolver.setSyncAutomatically(account, AUTHORITY, true); // Enable sync by default!
5656
}
5757

5858
private AccountManager accountManager() {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,51 @@
1111

1212
import java.util.Date;
1313

14+
import javax.inject.Inject;
15+
import javax.inject.Named;
16+
import javax.inject.Provider;
17+
1418
import fr.free.nrw.commons.settings.Prefs;
1519

1620
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI;
1721
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.uriForId;
1822

1923
public class ContributionDao {
20-
private final ContentProviderClient client;
24+
private final Provider<ContentProviderClient> clientProvider;
2125

22-
public ContributionDao(ContentProviderClient client) {
23-
this.client = client;
26+
@Inject
27+
public ContributionDao(@Named("contribution") Provider<ContentProviderClient> clientProvider) {
28+
this.clientProvider = clientProvider;
2429
}
2530

2631
public void save(Contribution contribution) {
32+
ContentProviderClient db = clientProvider.get();
2733
try {
2834
if (contribution.getContentUri() == null) {
29-
contribution.setContentUri(client.insert(BASE_URI, toContentValues(contribution)));
35+
contribution.setContentUri(db.insert(BASE_URI, toContentValues(contribution)));
3036
} else {
31-
client.update(contribution.getContentUri(), toContentValues(contribution), null, null);
37+
db.update(contribution.getContentUri(), toContentValues(contribution), null, null);
3238
}
3339
} catch (RemoteException e) {
3440
throw new RuntimeException(e);
41+
} finally {
42+
db.release();
3543
}
3644
}
3745

3846
public void delete(Contribution contribution) {
47+
ContentProviderClient db = clientProvider.get();
3948
try {
4049
if (contribution.getContentUri() == null) {
4150
// noooo
4251
throw new RuntimeException("tried to delete item with no content URI");
4352
} else {
44-
client.delete(contribution.getContentUri(), null, null);
53+
db.delete(contribution.getContentUri(), null, null);
4554
}
4655
} catch (RemoteException e) {
4756
throw new RuntimeException(e);
57+
} finally {
58+
db.release();
4859
}
4960
}
5061

@@ -74,7 +85,7 @@ public static ContentValues toContentValues(Contribution contribution) {
7485
return cv;
7586
}
7687

77-
public static Contribution fromCursor(Cursor cursor) {
88+
public Contribution fromCursor(Cursor cursor) {
7889
// Hardcoding column positions!
7990
//Check that cursor has a value to avoid CursorIndexOutOfBoundsException
8091
if (cursor.getCount() > 0) {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import static android.content.ContentResolver.requestSync;
4444
import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED;
4545
import static fr.free.nrw.commons.contributions.ContributionDao.Table.ALL_FIELDS;
46-
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.AUTHORITY;
4746
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI;
4847
import static fr.free.nrw.commons.settings.Prefs.UPLOADS_SHOWING;
4948

@@ -58,6 +57,7 @@ public class ContributionsActivity
5857
@Inject MediaWikiApi mediaWikiApi;
5958
@Inject SessionManager sessionManager;
6059
@Inject @Named("default_preferences") SharedPreferences prefs;
60+
@Inject ContributionDao contributionDao;
6161

6262
private Cursor allContributions;
6363
private ContributionsListFragment contributionsList;
@@ -121,7 +121,7 @@ protected void onResume() {
121121
@Override
122122
protected void onAuthCookieAcquired(String authCookie) {
123123
// Do a sync everytime we get here!
124-
requestSync(sessionManager.getCurrentAccount(), ContributionsContentProvider.AUTHORITY, new Bundle());
124+
requestSync(sessionManager.getCurrentAccount(), ContributionsContentProvider.CONTRIBUTION_AUTHORITY, new Bundle());
125125
Intent uploadServiceIntent = new Intent(this, UploadService.class);
126126
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
127127
startService(uploadServiceIntent);
@@ -186,7 +186,7 @@ private void showDetail(int i) {
186186

187187
public void retryUpload(int i) {
188188
allContributions.moveToPosition(i);
189-
Contribution c = ContributionDao.fromCursor(allContributions);
189+
Contribution c = contributionDao.fromCursor(allContributions);
190190
if (c.getState() == STATE_FAILED) {
191191
uploadService.queue(UploadService.ACTION_UPLOAD_FILE, c);
192192
Timber.d("Restarting for %s", c.toString());
@@ -197,10 +197,10 @@ public void retryUpload(int i) {
197197

198198
public void deleteUpload(int i) {
199199
allContributions.moveToPosition(i);
200-
Contribution c = ContributionDao.fromCursor(allContributions);
200+
Contribution c = contributionDao.fromCursor(allContributions);
201201
if (c.getState() == STATE_FAILED) {
202202
Timber.d("Deleting failed contrib %s", c.toString());
203-
new ContributionDao(getContentResolver().acquireContentProviderClient(AUTHORITY)).delete(c);
203+
contributionDao.delete(c);
204204
} else {
205205
Timber.d("Skipping deletion for non-failed contrib %s", c.toString());
206206
}
@@ -248,7 +248,7 @@ public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
248248

249249
if (contributionsList.getAdapter() == null) {
250250
contributionsList.setAdapter(new ContributionsListAdapter(getApplicationContext(),
251-
cursor, 0));
251+
cursor, 0, contributionDao));
252252
} else {
253253
((CursorAdapter) contributionsList.getAdapter()).swapCursor(cursor);
254254
}
@@ -269,7 +269,7 @@ public Media getMediaAtPosition(int i) {
269269
// not yet ready to return data
270270
return null;
271271
} else {
272-
return ContributionDao.fromCursor((Cursor) contributionsList.getAdapter().getItem(i));
272+
return contributionDao.fromCursor((Cursor) contributionsList.getAdapter().getItem(i));
273273
}
274274
}
275275

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsContentProvider.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public class ContributionsContentProvider extends ContentProvider {
2626
private static final int CONTRIBUTIONS_ID = 2;
2727
private static final String BASE_PATH = "contributions";
2828
private static final UriMatcher uriMatcher = new UriMatcher(NO_MATCH);
29-
public static final String AUTHORITY = "fr.free.nrw.commons.contributions.contentprovider";
29+
public static final String CONTRIBUTION_AUTHORITY = "fr.free.nrw.commons.contributions.contentprovider";
3030

31-
public static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
31+
public static final Uri BASE_URI = Uri.parse("content://" + CONTRIBUTION_AUTHORITY + "/" + BASE_PATH);
3232

3333
static {
34-
uriMatcher.addURI(AUTHORITY, BASE_PATH, CONTRIBUTIONS);
35-
uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CONTRIBUTIONS_ID);
34+
uriMatcher.addURI(CONTRIBUTION_AUTHORITY, BASE_PATH, CONTRIBUTIONS);
35+
uriMatcher.addURI(CONTRIBUTION_AUTHORITY, BASE_PATH + "/#", CONTRIBUTIONS_ID);
3636
}
3737

3838
public static Uri uriForId(int id) {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListAdapter.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
class ContributionsListAdapter extends CursorAdapter {
1313

14-
public ContributionsListAdapter(Context context, Cursor c, int flags) {
14+
private final ContributionDao contributionDao;
15+
16+
public ContributionsListAdapter(Context context, Cursor c, int flags, ContributionDao contributionDao) {
1517
super(context, c, flags);
18+
this.contributionDao = contributionDao;
1619
}
1720

1821
@Override
@@ -26,7 +29,7 @@ public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
2629
@Override
2730
public void bindView(View view, Context context, Cursor cursor) {
2831
final ContributionViewHolder views = (ContributionViewHolder)view.getTag();
29-
final Contribution contribution = ContributionDao.fromCursor(cursor);
32+
final Contribution contribution = contributionDao.fromCursor(cursor);
3033

3134
views.imageView.setMedia(contribution);
3235
views.titleView.setText(contribution.getDisplayTitle());

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationModule.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import fr.free.nrw.commons.upload.UploadController;
2424

2525
import static android.content.Context.MODE_PRIVATE;
26+
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
2627

2728
@Module
2829
@SuppressWarnings({"WeakerAccess", "unused"})
@@ -42,10 +43,16 @@ public AccountUtil providesAccountUtil() {
4243

4344
@Provides
4445
@Named("category")
45-
public ContentProviderClient provideContentProviderClient() {
46+
public ContentProviderClient provideCategoryContentProviderClient() {
4647
return application.getContentResolver().acquireContentProviderClient(CATEGORY_AUTHORITY);
4748
}
4849

50+
@Provides
51+
@Named("contribution")
52+
public ContentProviderClient provideContributionContentProviderClient() {
53+
return application.getContentResolver().acquireContentProviderClient(CONTRIBUTION_AUTHORITY);
54+
}
55+
4956
@Provides
5057
@Named("application_preferences")
5158
public SharedPreferences providesApplicationSharedPreferences() {

app/src/main/java/fr/free/nrw/commons/modifications/ModificationsSyncAdapter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
2727

2828
@Inject MediaWikiApi mwApi;
29+
@Inject ContributionDao contributionDao;
2930

3031
public ModificationsSyncAdapter(Context context, boolean autoInitialize) {
3132
super(context, autoInitialize);
@@ -80,7 +81,7 @@ public void onPerformSync(Account account, Bundle bundle, String s, ContentProvi
8081

8182
ContentProviderClient contributionsClient = null;
8283
try {
83-
contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
84+
contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.CONTRIBUTION_AUTHORITY);
8485

8586
while (!allModifications.isAfterLast()) {
8687
ModifierSequence sequence = ModifierSequenceDao.fromCursor(allModifications);
@@ -94,7 +95,7 @@ public void onPerformSync(Account account, Bundle bundle, String s, ContentProvi
9495
throw new RuntimeException(e);
9596
}
9697
contributionCursor.moveToFirst();
97-
contrib = ContributionDao.fromCursor(contributionCursor);
98+
contrib = contributionDao.fromCursor(contributionCursor);
9899

99100
if (contrib.getState() == Contribution.STATE_COMPLETED) {
100101
String pageContent;

app/src/main/java/fr/free/nrw/commons/upload/UploadService.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class UploadService extends HandlerService<Contribution> {
5252
@Inject MediaWikiApi mwApi;
5353
@Inject SessionManager sessionManager;
5454
@Inject @Named("default_preferences") SharedPreferences prefs;
55+
@Inject ContributionDao contributionDao;
5556

5657
private NotificationManager notificationManager;
5758
private ContentProviderClient contributionsProviderClient;
@@ -67,7 +68,6 @@ public class UploadService extends HandlerService<Contribution> {
6768
public static final int NOTIFICATION_UPLOAD_IN_PROGRESS = 1;
6869
public static final int NOTIFICATION_UPLOAD_COMPLETE = 2;
6970
public static final int NOTIFICATION_UPLOAD_FAILED = 3;
70-
private ContributionDao dao;
7171

7272
public UploadService() {
7373
super("UploadService");
@@ -107,7 +107,7 @@ public void onProgress(long transferred, long total) {
107107
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
108108

109109
contribution.setTransferred(transferred);
110-
dao.save(contribution);
110+
contributionDao.save(contribution);
111111
}
112112

113113
}
@@ -124,8 +124,7 @@ public void onCreate() {
124124
super.onCreate();
125125

126126
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
127-
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
128-
dao = new ContributionDao(contributionsProviderClient);
127+
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.CONTRIBUTION_AUTHORITY);
129128
}
130129

131130
@Override
@@ -147,7 +146,7 @@ public void queue(int what, Contribution contribution) {
147146

148147
contribution.setState(Contribution.STATE_QUEUED);
149148
contribution.setTransferred(0);
150-
dao.save(contribution);
149+
contributionDao.save(contribution);
151150
toUpload++;
152151
if (curProgressNotification != null && toUpload != 1) {
153152
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
@@ -262,7 +261,7 @@ private void uploadContribution(Contribution contribution) {
262261
contribution.setImageUrl(uploadResult.getImageUrl());
263262
contribution.setState(Contribution.STATE_COMPLETED);
264263
contribution.setDateUploaded(uploadResult.getDateUploaded());
265-
dao.save(contribution);
264+
contributionDao.save(contribution);
266265
}
267266
} catch (IOException e) {
268267
Timber.d("I have a network fuckup");
@@ -293,7 +292,7 @@ private void showFailedNotification(Contribution contribution) {
293292
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, failureNotification);
294293

295294
contribution.setState(Contribution.STATE_FAILED);
296-
dao.save(contribution);
295+
contributionDao.save(contribution);
297296
}
298297

299298
private String findUniqueFilename(String fileName) throws IOException {

app/src/test/java/fr/free/nrw/commons/contributions/ContributionDaoTest.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import static fr.free.nrw.commons.contributions.Contribution.SOURCE_GALLERY;
3232
import static fr.free.nrw.commons.contributions.Contribution.STATE_COMPLETED;
3333
import static fr.free.nrw.commons.contributions.Contribution.STATE_QUEUED;
34-
import static fr.free.nrw.commons.contributions.ContributionDao.*;
34+
import static fr.free.nrw.commons.contributions.ContributionDao.Table;
3535
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI;
3636
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.uriForId;
37-
import static org.junit.Assert.*;
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertFalse;
39+
import static org.junit.Assert.assertNull;
40+
import static org.junit.Assert.assertTrue;
3841
import static org.mockito.Matchers.any;
3942
import static org.mockito.Matchers.eq;
4043
import static org.mockito.Matchers.isA;
@@ -49,22 +52,22 @@ public class ContributionDaoTest {
4952

5053
private static final String LOCAL_URI = "http://example.com/";
5154
@Mock
52-
ContentProviderClient client;
55+
private ContentProviderClient client;
5356
@Mock
54-
SQLiteDatabase database;
57+
private SQLiteDatabase database;
5558
@Captor
56-
ArgumentCaptor<ContentValues> captor;
59+
private ArgumentCaptor<ContentValues> captor;
5760

5861
private Uri contentUri;
5962
private ContributionDao testObject;
6063

6164
@Before
62-
public void setUp() throws Exception {
65+
public void setUp() {
6366
MockitoAnnotations.initMocks(this);
6467

6568
contentUri = uriForId(111);
6669

67-
testObject = new ContributionDao(client);
70+
testObject = new ContributionDao(() -> client);
6871
}
6972

7073
@Test
@@ -288,7 +291,7 @@ public void createFromCursor() {
288291
long uploaded = 456L;
289292
MatrixCursor mc = createCursor(created, uploaded, false, LOCAL_URI);
290293

291-
Contribution c = ContributionDao.fromCursor(mc);
294+
Contribution c = testObject.fromCursor(mc);
292295

293296
assertEquals(uriForId(111), c.getContentUri());
294297
assertEquals("file", c.getFilename());
@@ -312,7 +315,7 @@ public void createFromCursor() {
312315
public void createFromCursor_nullableTimestamps() {
313316
MatrixCursor mc = createCursor(0L, 0L, false, LOCAL_URI);
314317

315-
Contribution c = ContributionDao.fromCursor(mc);
318+
Contribution c = testObject.fromCursor(mc);
316319

317320
assertNull(c.getTimestamp());
318321
assertNull(c.getDateCreated());
@@ -323,7 +326,7 @@ public void createFromCursor_nullableTimestamps() {
323326
public void createFromCursor_nullableLocalUri() {
324327
MatrixCursor mc = createCursor(0L, 0L, false, "");
325328

326-
Contribution c = ContributionDao.fromCursor(mc);
329+
Contribution c = testObject.fromCursor(mc);
327330

328331
assertNull(c.getLocalUri());
329332
assertNull(c.getDateCreated());
@@ -333,10 +336,10 @@ public void createFromCursor_nullableLocalUri() {
333336
@Test
334337
public void createFromCursor_booleanEncoding() {
335338
MatrixCursor mcFalse = createCursor(0L, 0L, false, LOCAL_URI);
336-
assertFalse(ContributionDao.fromCursor(mcFalse).getMultiple());
339+
assertFalse(testObject.fromCursor(mcFalse).getMultiple());
337340

338341
MatrixCursor mcHammer = createCursor(0L, 0L, true, LOCAL_URI);
339-
assertTrue(ContributionDao.fromCursor(mcHammer).getMultiple());
342+
assertTrue(testObject.fromCursor(mcHammer).getMultiple());
340343
}
341344

342345
@NonNull

0 commit comments

Comments
 (0)