make RepoProvider.Helper take Context rather than ContentResolver

This makes the code a bit neater, and passing the Context around is a
common pattern.

https://dev.guardianproject.info/issues/2926
refs #2926
This commit is contained in:
Hans-Christoph Steiner 2014-02-18 16:33:36 -05:00
parent a8bf9f7614
commit ab6166c36d
7 changed files with 38 additions and 35 deletions

View File

@ -837,8 +837,7 @@ public class AppDetails extends ListActivity {
// Install the version of this app denoted by 'app.curApk'.
private void install(final Apk apk) {
String [] projection = { RepoProvider.DataColumns.ADDRESS };
Repo repo = RepoProvider.Helper.findById(
getContentResolver(), apk.repo, projection);
Repo repo = RepoProvider.Helper.findById(this, apk.repo, projection);
if (repo == null || repo.address == null) {
return;
}

View File

@ -248,7 +248,7 @@ public class UpdateService extends IntentService implements ProgressListener {
// Grab some preliminary information, then we can release the
// database while we do all the downloading, etc...
int updates = 0;
List<Repo> repos = RepoProvider.Helper.all(getContentResolver());
List<Repo> repos = RepoProvider.Helper.all(this);
// Process each repo...
Map<String, App> appsToUpdate = new HashMap<String, App>();

View File

@ -20,12 +20,13 @@ public class RepoProvider extends FDroidProvider {
private Helper() {}
public static Repo findById(ContentResolver resolver, long repoId) {
return findById(resolver, repoId, DataColumns.ALL);
public static Repo findById(Context context, long repoId) {
return findById(context, repoId, DataColumns.ALL);
}
public static Repo findById(ContentResolver resolver, long repoId,
public static Repo findById(Context context, long repoId,
String[] projection) {
ContentResolver resolver = context.getContentResolver();
Uri uri = RepoProvider.getContentUri(repoId);
Cursor cursor = resolver.query(uri, projection, null, null, null);
Repo repo = null;
@ -36,32 +37,33 @@ public class RepoProvider extends FDroidProvider {
return repo;
}
public static Repo findByAddress(ContentResolver resolver,
String address) {
return findByAddress(resolver, address, DataColumns.ALL);
public static Repo findByAddress(Context context, String address) {
return findByAddress(context, address, DataColumns.ALL);
}
public static Repo findByAddress(ContentResolver resolver,
public static Repo findByAddress(Context context,
String address, String[] projection) {
List<Repo> repos = findBy(
resolver, DataColumns.ADDRESS, address, projection);
context, DataColumns.ADDRESS, address, projection);
return repos.size() > 0 ? repos.get(0) : null;
}
public static List<Repo> all(ContentResolver resolver) {
return all(resolver, DataColumns.ALL);
public static List<Repo> all(Context context) {
return all(context, DataColumns.ALL);
}
public static List<Repo> all(ContentResolver resolver, String[] projection) {
public static List<Repo> all(Context context, String[] projection) {
ContentResolver resolver = context.getContentResolver();
Uri uri = RepoProvider.getContentUri();
Cursor cursor = resolver.query(uri, projection, null, null, null);
return cursorToList(cursor);
}
private static List<Repo> findBy(ContentResolver resolver,
private static List<Repo> findBy(Context context,
String fieldName,
String fieldValue,
String[] projection) {
ContentResolver resolver = context.getContentResolver();
Uri uri = RepoProvider.getContentUri();
String[] args = { fieldValue };
Cursor cursor = resolver.query(
@ -82,8 +84,9 @@ public class RepoProvider extends FDroidProvider {
return repos;
}
public static void update(ContentResolver resolver, Repo repo,
public static void update(Context context, Repo repo,
ContentValues values) {
ContentResolver resolver = context.getContentResolver();
// Change the name to the new address. Next time we update the repo
// index file, it will populate the name field with the proper
@ -143,31 +146,34 @@ public class RepoProvider extends FDroidProvider {
* resolver, but I thought I'd put it here in the interests of having
* each of the CRUD methods available in the helper class.
*/
public static void insert(ContentResolver resolver,
public static void insert(Context context,
ContentValues values) {
ContentResolver resolver = context.getContentResolver();
Uri uri = RepoProvider.getContentUri();
resolver.insert(uri, values);
}
public static void remove(ContentResolver resolver, long repoId) {
public static void remove(Context context, long repoId) {
ContentResolver resolver = context.getContentResolver();
Uri uri = RepoProvider.getContentUri(repoId);
resolver.delete(uri, null, null);
}
public static void purgeApps(Context context, Repo repo, FDroidApp app) {
Uri apkUri = ApkProvider.getRepoUri(repo.getId());
int apkCount = context.getContentResolver().delete(apkUri, null, null);
ContentResolver resolver = context.getContentResolver();
int apkCount = resolver.delete(apkUri, null, null);
Log.d("FDroid", "Removed " + apkCount + " apks from repo " + repo.name);
Uri appUri = AppProvider.getNoApksUri();
int appCount = context.getContentResolver().delete(appUri, null, null);
int appCount = resolver.delete(appUri, null, null);
Log.d("Log", "Removed " + appCount + " apps with no apks.");
app.invalidateAllApps();
}
public static int countAppsForRepo(ContentResolver resolver,
long repoId) {
public static int countAppsForRepo(Context context, long repoId) {
ContentResolver resolver = context.getContentResolver();
String[] projection = { "COUNT(distinct id)" };
String selection = "repo = ?";
String[] args = { Long.toString(repoId) };

View File

@ -263,7 +263,7 @@ abstract public class RepoUpdater {
values.put(RepoProvider.DataColumns.NAME, handler.getName());
}
RepoProvider.Helper.update(context.getContentResolver(), repo, values);
RepoProvider.Helper.update(context, repo, values);
}
public static class UpdateException extends Exception {

View File

@ -54,7 +54,7 @@ public class RepoDetailsActivity extends FragmentActivity {
RepoProvider.DataColumns.ADDRESS,
RepoProvider.DataColumns.FINGERPRINT
};
repo = RepoProvider.Helper.findById(getContentResolver(), repoId, projection);
repo = RepoProvider.Helper.findById(this, repoId, projection);
ActionBarCompat.create(this).setDisplayHomeAsUpEnabled(true);
setTitle(repo.getName());

View File

@ -77,7 +77,7 @@ public class RepoDetailsFragment extends Fragment {
* repo object directly from the database.
*/
private Repo loadRepoDetails() {
return RepoProvider.Helper.findById(getActivity().getContentResolver(), getRepoId());
return RepoProvider.Helper.findById(getActivity(), getRepoId());
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -156,8 +156,7 @@ public class RepoDetailsFragment extends Fragment {
name.setText(repo.getName());
int appCount = RepoProvider.Helper.countAppsForRepo(
getActivity().getContentResolver(), repo.getId());
int appCount = RepoProvider.Helper.countAppsForRepo(getActivity(), repo.getId());
numApps.setText(Integer.toString(appCount));
setupDescription(repoView, repo);
@ -197,7 +196,7 @@ public class RepoDetailsFragment extends Fragment {
// Ensure repo is enabled before updating...
ContentValues values = new ContentValues(1);
values.put(RepoProvider.DataColumns.IN_USE, 1);
RepoProvider.Helper.update(getActivity().getContentResolver(), repo, values);
RepoProvider.Helper.update(getActivity(), repo, values);
UpdateService.updateRepoNow(repo.address, getActivity()).setListener(new ProgressListener() {
@Override
@ -230,7 +229,7 @@ public class RepoDetailsFragment extends Fragment {
if (!repo.address.equals(s.toString())) {
ContentValues values = new ContentValues(1);
values.put(RepoProvider.DataColumns.ADDRESS, s.toString());
RepoProvider.Helper.update(getActivity().getContentResolver(), repo, values);
RepoProvider.Helper.update(getActivity(), repo, values);
}
}
}
@ -310,7 +309,7 @@ public class RepoDetailsFragment extends Fragment {
@Override
public void onClick(DialogInterface dialog, int which) {
Repo repo = RepoDetailsFragment.this.repo;
RepoProvider.Helper.remove(getActivity().getContentResolver(), repo.getId());
RepoProvider.Helper.remove(getActivity(), repo.getId());
getActivity().finish();
}
}).setNegativeButton(android.R.string.cancel,

View File

@ -106,8 +106,7 @@ public class RepoListFragment extends ListFragment
if (repo.inuse != isEnabled) {
ContentValues values = new ContentValues(1);
values.put(RepoProvider.DataColumns.IN_USE, isEnabled ? 1 : 0);
RepoProvider.Helper.update(
getActivity().getContentResolver(), repo, values);
RepoProvider.Helper.update(getActivity(), repo, values);
if (isEnabled) {
changed = true;
@ -312,7 +311,7 @@ public class RepoListFragment extends ListFragment
final EditText fingerprintEditText = (EditText) view.findViewById(R.id.edit_fingerprint);
final Repo repo = (newAddress != null && isImportingRepo)
? RepoProvider.Helper.findByAddress(getActivity().getContentResolver(), newAddress)
? RepoProvider.Helper.findByAddress(getActivity(), newAddress)
: null;
alrt.setIcon(android.R.drawable.ic_menu_add);
@ -409,7 +408,7 @@ public class RepoListFragment extends ListFragment
ContentValues values = new ContentValues(2);
values.put(RepoProvider.DataColumns.ADDRESS, address);
values.put(RepoProvider.DataColumns.FINGERPRINT, fingerprint.toUpperCase(Locale.ENGLISH));
RepoProvider.Helper.insert(getActivity().getContentResolver(), values);
RepoProvider.Helper.insert(getActivity(), values);
finishedAddingRepo();
}
@ -419,7 +418,7 @@ public class RepoListFragment extends ListFragment
private void createNewRepo(Repo repo) {
ContentValues values = new ContentValues(1);
values.put(RepoProvider.DataColumns.IN_USE, 1);
RepoProvider.Helper.update(getActivity().getContentResolver(), repo, values);
RepoProvider.Helper.update(getActivity(), repo, values);
repo.inuse = true;
finishedAddingRepo();
}