Move Utils#getApkUrl(String repoAddress, Apk apk) to Apk#getUrl()

Added assertions that both apkName and repoAddress need to be populated
in order to call `getUrl()`. Also verified that this is the case for all
usages of this method, which it should be. All `Apk` objects which currently
have `getUrl()` called on them are loaded using the `ApkProvider.Helper.findById()`
method without specifying which columns to load (which defaults to all).
This commit is contained in:
Peter Serwylo 2016-05-05 15:17:22 +10:00 committed by Hans-Christoph Steiner
parent e72ba25fe5
commit 3a1fcfd226
6 changed files with 17 additions and 30 deletions

View File

@ -815,9 +815,6 @@ public class AppDetails extends AppCompatActivity {
return;
}
final String repoaddress = getRepoAddress(apk);
if (repoaddress == null) return;
if (!apk.compatible) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.installIncompatible);
@ -826,7 +823,7 @@ public class AppDetails extends AppCompatActivity {
@Override
public void onClick(DialogInterface dialog,
int whichButton) {
startDownload(apk, repoaddress);
startDownload(apk);
}
});
builder.setNegativeButton(R.string.no,
@ -855,21 +852,11 @@ public class AppDetails extends AppCompatActivity {
alert.show();
return;
}
startDownload(apk, repoaddress);
startDownload(apk);
}
@Nullable
private String getRepoAddress(Apk apk) {
final String[] projection = {RepoProvider.DataColumns.ADDRESS};
Repo repo = RepoProvider.Helper.findById(this, apk.repo, projection);
if (repo == null || repo.address == null) {
return null;
}
return repo.address;
}
private void startDownload(Apk apk, String repoAddress) {
activeDownloadUrlString = Utils.getApkUrl(repoAddress, apk);
private void startDownload(Apk apk) {
activeDownloadUrlString = apk.getUrl();
registerDownloaderReceivers();
headerFragment.startProgress();
DownloaderService.queue(this, apk.packageName, activeDownloadUrlString);

View File

@ -393,7 +393,7 @@ public class UpdateService extends IntentService implements ProgressListener {
// now that downloading the index is done, start downloading updates
if (changes && fdroidPrefs.isAutoDownloadEnabled()) {
autoDownloadUpdates(repo.address);
autoDownloadUpdates();
}
}
@ -485,7 +485,7 @@ public class UpdateService extends IntentService implements ProgressListener {
return inboxStyle;
}
private void autoDownloadUpdates(String repoAddress) {
private void autoDownloadUpdates() {
Cursor cursor = getContentResolver().query(
AppProvider.getCanUpdateUri(),
new String[]{
@ -496,10 +496,8 @@ public class UpdateService extends IntentService implements ProgressListener {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
App app = new App(cursor);
Apk apk = ApkProvider.Helper.find(this, app.packageName, app.suggestedVersionCode, new String[]{
ApkProvider.DataColumns.NAME,
});
String urlString = Utils.getApkUrl(repoAddress, apk);
Apk apk = ApkProvider.Helper.find(this, app.packageName, app.suggestedVersionCode);
String urlString = apk.getUrl();
DownloaderService.queue(this, app.packageName, urlString);
cursor.moveToNext();
}

View File

@ -39,7 +39,6 @@ import com.nostra13.universalimageloader.utils.StorageUtils;
import org.apache.commons.io.FileUtils;
import org.fdroid.fdroid.compat.FileCompat;
import org.fdroid.fdroid.data.Apk;
import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.SanitizedFile;
import org.xml.sax.XMLReader;
@ -434,10 +433,6 @@ public final class Utils {
return new Locale(languageTag);
}
public static String getApkUrl(String repoAddress, Apk apk) {
return repoAddress + "/" + apk.apkName.replace(" ", "%20");
}
public static final class CommaSeparatedList implements Iterable<String> {
private final String value;

View File

@ -121,6 +121,13 @@ public class Apk extends ValueObject implements Comparable<Apk> {
}
}
public String getUrl() {
if (repoAddress == null || apkName == null) {
throw new IllegalStateException("Apk needs to have both ApkProvider.DataColumns.REPO_ADDRESS and ApkProvider.DataColumns.NAME set in order to calculate URL.");
}
return repoAddress + "/" + apkName.replace(" ", "%20");
}
@Override
public String toString() {
return packageName + " (version " + versionCode + ")";

View File

@ -297,7 +297,7 @@ public class SwapAppsView extends ListView implements
// TODO: Unregister receivers correctly...
Apk apk = getApkToInstall();
String url = Utils.getApkUrl(apk.repoAddress, apk);
String url = apk.getUrl();
localBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
localBroadcastManager.registerReceiver(appListViewResetReceiver,

View File

@ -778,7 +778,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
public void install(@NonNull final App app) {
final Apk apk = ApkProvider.Helper.find(this, app.packageName, app.suggestedVersionCode);
String urlString = Utils.getApkUrl(apk.repoAddress, apk);
String urlString = apk.getUrl();
downloadCompleteReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {