Merge pull request #170 from whym/close-cursor

Close cursors (+tidy redundant class qualifiers)
This commit is contained in:
Nicolas Raoul 2016-07-11 21:57:04 +09:00 committed by GitHub
commit c50d6aa954
3 changed files with 28 additions and 18 deletions

View file

@ -64,8 +64,8 @@ public class CategorizationFragment extends Fragment {
// LHS guarantees ordered insertions, allowing for prioritized method A results // LHS guarantees ordered insertions, allowing for prioritized method A results
private final Set<String> results = new LinkedHashSet<String>(); private final Set<String> results = new LinkedHashSet<String>();
fr.free.nrw.commons.category.PrefixUpdater prefixUpdaterSub; PrefixUpdater prefixUpdaterSub;
fr.free.nrw.commons.category.MethodAUpdater methodAUpdaterSub; MethodAUpdater methodAUpdaterSub;
private ContentProviderClient client; private ContentProviderClient client;
@ -116,14 +116,14 @@ public class CategorizationFragment extends Fragment {
try { try {
Cursor cursor = client.query( Cursor cursor = client.query(
fr.free.nrw.commons.category.CategoryContentProvider.BASE_URI, CategoryContentProvider.BASE_URI,
fr.free.nrw.commons.category.Category.Table.ALL_FIELDS, Category.Table.ALL_FIELDS,
null, null,
new String[]{}, new String[]{},
fr.free.nrw.commons.category.Category.Table.COLUMN_LAST_USED + " DESC"); Category.Table.COLUMN_LAST_USED + " DESC");
// fixme add a limit on the original query instead of falling out of the loop? // fixme add a limit on the original query instead of falling out of the loop?
while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) { while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) {
fr.free.nrw.commons.category.Category cat = fr.free.nrw.commons.category.Category.fromCursor(cursor); Category cat = Category.fromCursor(cursor);
items.add(cat.getName()); items.add(cat.getName());
} }
cursor.close(); cursor.close();
@ -249,25 +249,30 @@ public class CategorizationFragment extends Fragment {
return count; return count;
} }
private fr.free.nrw.commons.category.Category lookupCategory(String name) { private Category lookupCategory(String name) {
Cursor cursor = null;
try { try {
Cursor cursor = client.query( cursor = client.query(
fr.free.nrw.commons.category.CategoryContentProvider.BASE_URI, CategoryContentProvider.BASE_URI,
fr.free.nrw.commons.category.Category.Table.ALL_FIELDS, Category.Table.ALL_FIELDS,
fr.free.nrw.commons.category.Category.Table.COLUMN_NAME + "=?", Category.Table.COLUMN_NAME + "=?",
new String[] {name}, new String[] {name},
null); null);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
fr.free.nrw.commons.category.Category cat = fr.free.nrw.commons.category.Category.fromCursor(cursor); Category cat = Category.fromCursor(cursor);
return cat; return cat;
} }
} catch (RemoteException e) { } catch (RemoteException e) {
// This feels lazy, but to hell with checked exceptions. :) // This feels lazy, but to hell with checked exceptions. :)
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
if ( cursor != null ) {
cursor.close();
}
} }
// Newly used category... // Newly used category...
fr.free.nrw.commons.category.Category cat = new fr.free.nrw.commons.category.Category(); Category cat = new Category();
cat.setName(name); cat.setName(name);
cat.setLastUsed(new Date()); cat.setLastUsed(new Date());
cat.setTimesUsed(0); cat.setTimesUsed(0);
@ -284,7 +289,7 @@ public class CategorizationFragment extends Fragment {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
fr.free.nrw.commons.category.Category cat = lookupCategory(name); Category cat = lookupCategory(name);
cat.incTimesUsed(); cat.incTimesUsed();
cat.setContentProviderClient(client); cat.setContentProviderClient(client);
@ -366,7 +371,7 @@ public class CategorizationFragment extends Fragment {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
prefixUpdaterSub = new fr.free.nrw.commons.category.PrefixUpdater(this) { prefixUpdaterSub = new PrefixUpdater(this) {
@Override @Override
protected ArrayList<String> doInBackground(Void... voids) { protected ArrayList<String> doInBackground(Void... voids) {
ArrayList<String> result = new ArrayList<String>(); ArrayList<String> result = new ArrayList<String>();
@ -398,7 +403,7 @@ public class CategorizationFragment extends Fragment {
} }
}; };
methodAUpdaterSub = new fr.free.nrw.commons.category.MethodAUpdater(this) { methodAUpdaterSub = new MethodAUpdater(this) {
@Override @Override
protected void onPostExecute(ArrayList<String> result) { protected void onPostExecute(ArrayList<String> result) {
results.clear(); results.clear();
@ -439,7 +444,7 @@ public class CategorizationFragment extends Fragment {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setHasOptionsMenu(true); setHasOptionsMenu(true);
getActivity().setTitle(R.string.categories_activity_title); getActivity().setTitle(R.string.categories_activity_title);
client = getActivity().getContentResolver().acquireContentProviderClient(fr.free.nrw.commons.category.CategoryContentProvider.AUTHORITY); client = getActivity().getContentResolver().acquireContentProviderClient(CategoryContentProvider.AUTHORITY);
} }
@Override @Override

View file

@ -37,10 +37,14 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
new String[] { filename }, new String[] { filename },
"" ""
); );
return cursor.getCount() != 0;
} catch (RemoteException e) { } catch (RemoteException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
if ( cursor != null ) {
cursor.close();
}
} }
return cursor != null && cursor.getCount() != 0;
} }
@Override @Override

View file

@ -145,6 +145,7 @@ public class UploadController {
} else { } else {
contribution.setDateCreated(new Date()); contribution.setDateCreated(new Date());
} }
cursor.close();
} }
return contribution; return contribution;