Skip to content

Commit 4bd7246

Browse files
committed
More cleanup now that clean Dao classes are available from Dagger.
1 parent 874627e commit 4bd7246

File tree

6 files changed

+48
-41
lines changed

6 files changed

+48
-41
lines changed

app/src/main/java/fr/free/nrw/commons/category/CategoryDao.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void save(Category category) {
4646
* @param name Category's name
4747
* @return category from database, or null if not found
4848
*/
49-
public @Nullable
49+
@Nullable
5050
Category find(String name) {
5151
Cursor cursor = null;
5252
ContentProviderClient db = clientProvider.get();
@@ -77,7 +77,7 @@ Category find(String name) {
7777
*
7878
* @return a list containing recent categories
7979
*/
80-
public @NonNull
80+
@NonNull
8181
List<String> recentCategories(int limit) {
8282
List<String> items = new ArrayList<>();
8383
Cursor cursor = null;

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,43 @@
1717

1818
import fr.free.nrw.commons.settings.Prefs;
1919

20+
import static fr.free.nrw.commons.contributions.ContributionDao.Table.ALL_FIELDS;
2021
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI;
2122
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.uriForId;
2223

2324
public class ContributionDao {
25+
/*
26+
This sorts in the following order:
27+
Currently Uploading
28+
Failed (Sorted in ascending order of time added - FIFO)
29+
Queued to Upload (Sorted in ascending order of time added - FIFO)
30+
Completed (Sorted in descending order of time added)
31+
32+
This is why Contribution.STATE_COMPLETED is -1.
33+
*/
34+
static final String CONTRIBUTION_SORT = Table.COLUMN_STATE + " DESC, "
35+
+ Table.COLUMN_UPLOADED + " DESC , ("
36+
+ Table.COLUMN_TIMESTAMP + " * "
37+
+ Table.COLUMN_STATE + ")";
38+
2439
private final Provider<ContentProviderClient> clientProvider;
2540

2641
@Inject
2742
public ContributionDao(@Named("contribution") Provider<ContentProviderClient> clientProvider) {
2843
this.clientProvider = clientProvider;
2944
}
3045

46+
Cursor loadAllContributions() {
47+
ContentProviderClient db = clientProvider.get();
48+
try {
49+
return db.query(BASE_URI, ALL_FIELDS, "", null, CONTRIBUTION_SORT);
50+
} catch (RemoteException e) {
51+
return null;
52+
} finally {
53+
db.release();
54+
}
55+
}
56+
3157
public void save(Contribution contribution) {
3258
ContentProviderClient db = clientProvider.get();
3359
try {
@@ -59,7 +85,7 @@ public void delete(Contribution contribution) {
5985
}
6086
}
6187

62-
public static ContentValues toContentValues(Contribution contribution) {
88+
ContentValues toContentValues(Contribution contribution) {
6389
ContentValues cv = new ContentValues();
6490
cv.put(Table.COLUMN_FILENAME, contribution.getFilename());
6591
if (contribution.getLocalUri() != null) {

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

+3-19
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,6 @@ public class ContributionsActivity
6565
private UploadService uploadService;
6666
private boolean isUploadServiceConnected;
6767
private ArrayList<DataSetObserver> observersWaitingForLoad = new ArrayList<>();
68-
private String CONTRIBUTION_SELECTION = "";
69-
70-
/*
71-
This sorts in the following order:
72-
Currently Uploading
73-
Failed (Sorted in ascending order of time added - FIFO)
74-
Queued to Upload (Sorted in ascending order of time added - FIFO)
75-
Completed (Sorted in descending order of time added)
76-
77-
This is why Contribution.STATE_COMPLETED is -1.
78-
*/
79-
private String CONTRIBUTION_SORT = ContributionDao.Table.COLUMN_STATE + " DESC, "
80-
+ ContributionDao.Table.COLUMN_UPLOADED + " DESC , ("
81-
+ ContributionDao.Table.COLUMN_TIMESTAMP + " * "
82-
+ ContributionDao.Table.COLUMN_STATE + ")";
8368

8469
private CompositeDisposable compositeDisposable = new CompositeDisposable();
8570

@@ -127,8 +112,7 @@ protected void onAuthCookieAcquired(String authCookie) {
127112
startService(uploadServiceIntent);
128113
bindService(uploadServiceIntent, uploadServiceConnection, Context.BIND_AUTO_CREATE);
129114

130-
allContributions = getContentResolver().query(BASE_URI, ALL_FIELDS,
131-
CONTRIBUTION_SELECTION, null, CONTRIBUTION_SORT);
115+
allContributions = contributionDao.loadAllContributions();
132116

133117
getSupportLoaderManager().initLoader(0, null, this);
134118
}
@@ -238,8 +222,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
238222
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
239223
int uploads = prefs.getInt(UPLOADS_SHOWING, 100);
240224
return new CursorLoader(this, BASE_URI,
241-
ALL_FIELDS, CONTRIBUTION_SELECTION, null,
242-
CONTRIBUTION_SORT + "LIMIT " + uploads);
225+
ALL_FIELDS, "", null,
226+
ContributionDao.CONTRIBUTION_SORT + "LIMIT " + uploads);
243227
}
244228

245229
@Override

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public void onPerformSync(Account account, Bundle bundle, String authority,
8989
LogEventResult result;
9090
Boolean done = false;
9191
String queryContinue = null;
92+
ContributionDao contributionDao = new ContributionDao(() -> contentProviderClient);
9293
while (!done) {
9394

9495
try {
@@ -121,7 +122,7 @@ public void onPerformSync(Account account, Bundle bundle, String authority,
121122
"", -1, dateUpdated, dateUpdated, user,
122123
"", "");
123124
contrib.setState(STATE_COMPLETED);
124-
imageValues.add(ContributionDao.toContentValues(contrib));
125+
imageValues.add(contributionDao.toContentValues(contrib));
125126

126127
if (imageValues.size() % COMMIT_THRESHOLD == 0) {
127128
try {

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ public ModifierSequenceDao(@Named("modification") Provider<ContentProviderClient
2424
this.clientProvider = clientProvider;
2525
}
2626

27-
public ModifierSequence fromCursor(Cursor cursor) {
28-
// Hardcoding column positions!
29-
ModifierSequence ms = null;
30-
try {
31-
ms = new ModifierSequence(Uri.parse(cursor.getString(1)),
32-
new JSONObject(cursor.getString(2)));
33-
} catch (JSONException e) {
34-
throw new RuntimeException(e);
35-
}
36-
ms.setContentUri( ModificationsContentProvider.uriForId(cursor.getInt(0)));
37-
38-
return ms;
39-
}
40-
4127
public void save(ModifierSequence sequence) {
4228
ContentProviderClient db = clientProvider.get();
4329
try {
@@ -64,6 +50,20 @@ public void delete(ModifierSequence sequence) {
6450
}
6551
}
6652

53+
ModifierSequence fromCursor(Cursor cursor) {
54+
// Hardcoding column positions!
55+
ModifierSequence ms = null;
56+
try {
57+
ms = new ModifierSequence(Uri.parse(cursor.getString(1)),
58+
new JSONObject(cursor.getString(2)));
59+
} catch (JSONException e) {
60+
throw new RuntimeException(e);
61+
}
62+
ms.setContentUri( ModificationsContentProvider.uriForId(cursor.getInt(0)));
63+
64+
return ms;
65+
}
66+
6767
private JSONObject toJSON(ModifierSequence sequence) {
6868
JSONObject data = new JSONObject();
6969
try {

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

-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.app.Notification;
55
import android.app.NotificationManager;
66
import android.app.PendingIntent;
7-
import android.content.ContentProviderClient;
87
import android.content.ContentResolver;
98
import android.content.ContentValues;
109
import android.content.Intent;
@@ -55,7 +54,6 @@ public class UploadService extends HandlerService<Contribution> {
5554
@Inject ContributionDao contributionDao;
5655

5756
private NotificationManager notificationManager;
58-
private ContentProviderClient contributionsProviderClient;
5957
private NotificationCompat.Builder curProgressNotification;
6058
private int toUpload;
6159

@@ -115,7 +113,6 @@ public void onProgress(long transferred, long total) {
115113
@Override
116114
public void onDestroy() {
117115
super.onDestroy();
118-
contributionsProviderClient.release();
119116
Timber.d("UploadService.onDestroy; %s are yet to be uploaded", unfinishedUploads);
120117
}
121118

@@ -124,7 +121,6 @@ public void onCreate() {
124121
super.onCreate();
125122

126123
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
127-
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.CONTRIBUTION_AUTHORITY);
128124
}
129125

130126
@Override

0 commit comments

Comments
 (0)