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:
parent
a8bf9f7614
commit
ab6166c36d
@ -837,8 +837,7 @@ public class AppDetails extends ListActivity {
|
|||||||
// Install the version of this app denoted by 'app.curApk'.
|
// Install the version of this app denoted by 'app.curApk'.
|
||||||
private void install(final Apk apk) {
|
private void install(final Apk apk) {
|
||||||
String [] projection = { RepoProvider.DataColumns.ADDRESS };
|
String [] projection = { RepoProvider.DataColumns.ADDRESS };
|
||||||
Repo repo = RepoProvider.Helper.findById(
|
Repo repo = RepoProvider.Helper.findById(this, apk.repo, projection);
|
||||||
getContentResolver(), apk.repo, projection);
|
|
||||||
if (repo == null || repo.address == null) {
|
if (repo == null || repo.address == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -248,7 +248,7 @@ public class UpdateService extends IntentService implements ProgressListener {
|
|||||||
// Grab some preliminary information, then we can release the
|
// Grab some preliminary information, then we can release the
|
||||||
// database while we do all the downloading, etc...
|
// database while we do all the downloading, etc...
|
||||||
int updates = 0;
|
int updates = 0;
|
||||||
List<Repo> repos = RepoProvider.Helper.all(getContentResolver());
|
List<Repo> repos = RepoProvider.Helper.all(this);
|
||||||
|
|
||||||
// Process each repo...
|
// Process each repo...
|
||||||
Map<String, App> appsToUpdate = new HashMap<String, App>();
|
Map<String, App> appsToUpdate = new HashMap<String, App>();
|
||||||
|
@ -20,12 +20,13 @@ public class RepoProvider extends FDroidProvider {
|
|||||||
|
|
||||||
private Helper() {}
|
private Helper() {}
|
||||||
|
|
||||||
public static Repo findById(ContentResolver resolver, long repoId) {
|
public static Repo findById(Context context, long repoId) {
|
||||||
return findById(resolver, repoId, DataColumns.ALL);
|
return findById(context, repoId, DataColumns.ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Repo findById(ContentResolver resolver, long repoId,
|
public static Repo findById(Context context, long repoId,
|
||||||
String[] projection) {
|
String[] projection) {
|
||||||
|
ContentResolver resolver = context.getContentResolver();
|
||||||
Uri uri = RepoProvider.getContentUri(repoId);
|
Uri uri = RepoProvider.getContentUri(repoId);
|
||||||
Cursor cursor = resolver.query(uri, projection, null, null, null);
|
Cursor cursor = resolver.query(uri, projection, null, null, null);
|
||||||
Repo repo = null;
|
Repo repo = null;
|
||||||
@ -36,32 +37,33 @@ public class RepoProvider extends FDroidProvider {
|
|||||||
return repo;
|
return repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Repo findByAddress(ContentResolver resolver,
|
public static Repo findByAddress(Context context, String address) {
|
||||||
String address) {
|
return findByAddress(context, address, DataColumns.ALL);
|
||||||
return findByAddress(resolver, address, DataColumns.ALL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Repo findByAddress(ContentResolver resolver,
|
public static Repo findByAddress(Context context,
|
||||||
String address, String[] projection) {
|
String address, String[] projection) {
|
||||||
List<Repo> repos = findBy(
|
List<Repo> repos = findBy(
|
||||||
resolver, DataColumns.ADDRESS, address, projection);
|
context, DataColumns.ADDRESS, address, projection);
|
||||||
return repos.size() > 0 ? repos.get(0) : null;
|
return repos.size() > 0 ? repos.get(0) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Repo> all(ContentResolver resolver) {
|
public static List<Repo> all(Context context) {
|
||||||
return all(resolver, DataColumns.ALL);
|
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();
|
Uri uri = RepoProvider.getContentUri();
|
||||||
Cursor cursor = resolver.query(uri, projection, null, null, null);
|
Cursor cursor = resolver.query(uri, projection, null, null, null);
|
||||||
return cursorToList(cursor);
|
return cursorToList(cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Repo> findBy(ContentResolver resolver,
|
private static List<Repo> findBy(Context context,
|
||||||
String fieldName,
|
String fieldName,
|
||||||
String fieldValue,
|
String fieldValue,
|
||||||
String[] projection) {
|
String[] projection) {
|
||||||
|
ContentResolver resolver = context.getContentResolver();
|
||||||
Uri uri = RepoProvider.getContentUri();
|
Uri uri = RepoProvider.getContentUri();
|
||||||
String[] args = { fieldValue };
|
String[] args = { fieldValue };
|
||||||
Cursor cursor = resolver.query(
|
Cursor cursor = resolver.query(
|
||||||
@ -82,8 +84,9 @@ public class RepoProvider extends FDroidProvider {
|
|||||||
return repos;
|
return repos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void update(ContentResolver resolver, Repo repo,
|
public static void update(Context context, Repo repo,
|
||||||
ContentValues values) {
|
ContentValues values) {
|
||||||
|
ContentResolver resolver = context.getContentResolver();
|
||||||
|
|
||||||
// Change the name to the new address. Next time we update the repo
|
// Change the name to the new address. Next time we update the repo
|
||||||
// index file, it will populate the name field with the proper
|
// 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
|
* resolver, but I thought I'd put it here in the interests of having
|
||||||
* each of the CRUD methods available in the helper class.
|
* each of the CRUD methods available in the helper class.
|
||||||
*/
|
*/
|
||||||
public static void insert(ContentResolver resolver,
|
public static void insert(Context context,
|
||||||
ContentValues values) {
|
ContentValues values) {
|
||||||
|
ContentResolver resolver = context.getContentResolver();
|
||||||
Uri uri = RepoProvider.getContentUri();
|
Uri uri = RepoProvider.getContentUri();
|
||||||
resolver.insert(uri, values);
|
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);
|
Uri uri = RepoProvider.getContentUri(repoId);
|
||||||
resolver.delete(uri, null, null);
|
resolver.delete(uri, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void purgeApps(Context context, Repo repo, FDroidApp app) {
|
public static void purgeApps(Context context, Repo repo, FDroidApp app) {
|
||||||
Uri apkUri = ApkProvider.getRepoUri(repo.getId());
|
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);
|
Log.d("FDroid", "Removed " + apkCount + " apks from repo " + repo.name);
|
||||||
|
|
||||||
Uri appUri = AppProvider.getNoApksUri();
|
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.");
|
Log.d("Log", "Removed " + appCount + " apps with no apks.");
|
||||||
|
|
||||||
app.invalidateAllApps();
|
app.invalidateAllApps();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int countAppsForRepo(ContentResolver resolver,
|
public static int countAppsForRepo(Context context, long repoId) {
|
||||||
long repoId) {
|
ContentResolver resolver = context.getContentResolver();
|
||||||
String[] projection = { "COUNT(distinct id)" };
|
String[] projection = { "COUNT(distinct id)" };
|
||||||
String selection = "repo = ?";
|
String selection = "repo = ?";
|
||||||
String[] args = { Long.toString(repoId) };
|
String[] args = { Long.toString(repoId) };
|
||||||
|
@ -263,7 +263,7 @@ abstract public class RepoUpdater {
|
|||||||
values.put(RepoProvider.DataColumns.NAME, handler.getName());
|
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 {
|
public static class UpdateException extends Exception {
|
||||||
|
@ -54,7 +54,7 @@ public class RepoDetailsActivity extends FragmentActivity {
|
|||||||
RepoProvider.DataColumns.ADDRESS,
|
RepoProvider.DataColumns.ADDRESS,
|
||||||
RepoProvider.DataColumns.FINGERPRINT
|
RepoProvider.DataColumns.FINGERPRINT
|
||||||
};
|
};
|
||||||
repo = RepoProvider.Helper.findById(getContentResolver(), repoId, projection);
|
repo = RepoProvider.Helper.findById(this, repoId, projection);
|
||||||
|
|
||||||
ActionBarCompat.create(this).setDisplayHomeAsUpEnabled(true);
|
ActionBarCompat.create(this).setDisplayHomeAsUpEnabled(true);
|
||||||
setTitle(repo.getName());
|
setTitle(repo.getName());
|
||||||
|
@ -77,7 +77,7 @@ public class RepoDetailsFragment extends Fragment {
|
|||||||
* repo object directly from the database.
|
* repo object directly from the database.
|
||||||
*/
|
*/
|
||||||
private Repo loadRepoDetails() {
|
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) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
@ -156,8 +156,7 @@ public class RepoDetailsFragment extends Fragment {
|
|||||||
|
|
||||||
name.setText(repo.getName());
|
name.setText(repo.getName());
|
||||||
|
|
||||||
int appCount = RepoProvider.Helper.countAppsForRepo(
|
int appCount = RepoProvider.Helper.countAppsForRepo(getActivity(), repo.getId());
|
||||||
getActivity().getContentResolver(), repo.getId());
|
|
||||||
numApps.setText(Integer.toString(appCount));
|
numApps.setText(Integer.toString(appCount));
|
||||||
|
|
||||||
setupDescription(repoView, repo);
|
setupDescription(repoView, repo);
|
||||||
@ -197,7 +196,7 @@ public class RepoDetailsFragment extends Fragment {
|
|||||||
// Ensure repo is enabled before updating...
|
// Ensure repo is enabled before updating...
|
||||||
ContentValues values = new ContentValues(1);
|
ContentValues values = new ContentValues(1);
|
||||||
values.put(RepoProvider.DataColumns.IN_USE, 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() {
|
UpdateService.updateRepoNow(repo.address, getActivity()).setListener(new ProgressListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -230,7 +229,7 @@ public class RepoDetailsFragment extends Fragment {
|
|||||||
if (!repo.address.equals(s.toString())) {
|
if (!repo.address.equals(s.toString())) {
|
||||||
ContentValues values = new ContentValues(1);
|
ContentValues values = new ContentValues(1);
|
||||||
values.put(RepoProvider.DataColumns.ADDRESS, s.toString());
|
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
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
Repo repo = RepoDetailsFragment.this.repo;
|
Repo repo = RepoDetailsFragment.this.repo;
|
||||||
RepoProvider.Helper.remove(getActivity().getContentResolver(), repo.getId());
|
RepoProvider.Helper.remove(getActivity(), repo.getId());
|
||||||
getActivity().finish();
|
getActivity().finish();
|
||||||
}
|
}
|
||||||
}).setNegativeButton(android.R.string.cancel,
|
}).setNegativeButton(android.R.string.cancel,
|
||||||
|
@ -106,8 +106,7 @@ public class RepoListFragment extends ListFragment
|
|||||||
if (repo.inuse != isEnabled) {
|
if (repo.inuse != isEnabled) {
|
||||||
ContentValues values = new ContentValues(1);
|
ContentValues values = new ContentValues(1);
|
||||||
values.put(RepoProvider.DataColumns.IN_USE, isEnabled ? 1 : 0);
|
values.put(RepoProvider.DataColumns.IN_USE, isEnabled ? 1 : 0);
|
||||||
RepoProvider.Helper.update(
|
RepoProvider.Helper.update(getActivity(), repo, values);
|
||||||
getActivity().getContentResolver(), repo, values);
|
|
||||||
|
|
||||||
if (isEnabled) {
|
if (isEnabled) {
|
||||||
changed = true;
|
changed = true;
|
||||||
@ -312,7 +311,7 @@ public class RepoListFragment extends ListFragment
|
|||||||
final EditText fingerprintEditText = (EditText) view.findViewById(R.id.edit_fingerprint);
|
final EditText fingerprintEditText = (EditText) view.findViewById(R.id.edit_fingerprint);
|
||||||
|
|
||||||
final Repo repo = (newAddress != null && isImportingRepo)
|
final Repo repo = (newAddress != null && isImportingRepo)
|
||||||
? RepoProvider.Helper.findByAddress(getActivity().getContentResolver(), newAddress)
|
? RepoProvider.Helper.findByAddress(getActivity(), newAddress)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
alrt.setIcon(android.R.drawable.ic_menu_add);
|
alrt.setIcon(android.R.drawable.ic_menu_add);
|
||||||
@ -409,7 +408,7 @@ public class RepoListFragment extends ListFragment
|
|||||||
ContentValues values = new ContentValues(2);
|
ContentValues values = new ContentValues(2);
|
||||||
values.put(RepoProvider.DataColumns.ADDRESS, address);
|
values.put(RepoProvider.DataColumns.ADDRESS, address);
|
||||||
values.put(RepoProvider.DataColumns.FINGERPRINT, fingerprint.toUpperCase(Locale.ENGLISH));
|
values.put(RepoProvider.DataColumns.FINGERPRINT, fingerprint.toUpperCase(Locale.ENGLISH));
|
||||||
RepoProvider.Helper.insert(getActivity().getContentResolver(), values);
|
RepoProvider.Helper.insert(getActivity(), values);
|
||||||
finishedAddingRepo();
|
finishedAddingRepo();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,7 +418,7 @@ public class RepoListFragment extends ListFragment
|
|||||||
private void createNewRepo(Repo repo) {
|
private void createNewRepo(Repo repo) {
|
||||||
ContentValues values = new ContentValues(1);
|
ContentValues values = new ContentValues(1);
|
||||||
values.put(RepoProvider.DataColumns.IN_USE, 1);
|
values.put(RepoProvider.DataColumns.IN_USE, 1);
|
||||||
RepoProvider.Helper.update(getActivity().getContentResolver(), repo, values);
|
RepoProvider.Helper.update(getActivity(), repo, values);
|
||||||
repo.inuse = true;
|
repo.inuse = true;
|
||||||
finishedAddingRepo();
|
finishedAddingRepo();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user