add "Auto Download" pref to enable auto-downloading of updates

Now that we have a nice background service, let's put it to work!  This
makes update APKs be automatically downloaded after the index is updated.
Then when the user goes to install the updates, they will not have to wait
for the download part.

#601 https://gitlab.com/fdroid/fdroidclient/issues/601
This commit is contained in:
Hans-Christoph Steiner 2016-04-06 13:45:29 +02:00
parent adcdc417ab
commit d114f428e7
4 changed files with 36 additions and 0 deletions

View File

@ -45,6 +45,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
public static final String PREF_UPD_INTERVAL = "updateInterval";
public static final String PREF_UPD_WIFI_ONLY = "updateOnWifiOnly";
public static final String PREF_UPD_AUTO_DOWNLOAD = "updateAutoDownload";
public static final String PREF_UPD_NOTIFY = "updateNotify";
public static final String PREF_UPD_HISTORY = "updateHistoryDays";
public static final String PREF_ROOTED = "rooted";
@ -182,6 +183,10 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return preferences.getBoolean(PREF_UPD_NOTIFY, true);
}
public boolean isAutoDownloadEnabled() {
return preferences.getBoolean(PREF_UPD_AUTO_DOWNLOAD, false);
}
public boolean isUpdateOnlyOnWifi() {
return preferences.getBoolean(PREF_UPD_WIFI_ONLY, false);
}

View File

@ -42,6 +42,7 @@ import android.util.Log;
import android.widget.Toast;
import org.fdroid.fdroid.compat.PreferencesCompat;
import org.fdroid.fdroid.data.Apk;
import org.fdroid.fdroid.data.ApkProvider;
import org.fdroid.fdroid.data.App;
import org.fdroid.fdroid.data.AppProvider;
@ -386,6 +387,11 @@ public class UpdateService extends IntentService implements ProgressListener {
Log.e(TAG, "Error updating repository " + repo.address, e);
}
localBroadcastManager.unregisterReceiver(downloadProgressReceiver);
// now that downloading the index is done, start downloading updates
if (changes && Preferences.get().isAutoDownloadEnabled()) {
autoDownloadUpdates(repo.address);
}
}
if (!changes) {
@ -476,6 +482,25 @@ public class UpdateService extends IntentService implements ProgressListener {
return inboxStyle;
}
private void autoDownloadUpdates(String repoAddress) {
Cursor cursor = getContentResolver().query(
AppProvider.getCanUpdateUri(),
new String[]{
AppProvider.DataColumns.PACKAGE_NAME,
AppProvider.DataColumns.SUGGESTED_VERSION_CODE,
}, null, null, null);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
App app = new App(cursor);
Apk apk = ApkProvider.Helper.find(this, app.packageName, app.suggestedVercode, new String[]{
ApkProvider.DataColumns.NAME,
});
String urlString = Utils.getApkUrl(repoAddress, apk);
DownloaderService.queue(this, urlString);
cursor.moveToNext();
}
}
private void showAppUpdatesNotification(Cursor hasUpdates) {
Utils.debugLog(TAG, "Notifying " + hasUpdates.getCount() + " updates.");

View File

@ -21,6 +21,8 @@
<string name="update_interval_zero">No automatic app list updates</string>
<string name="automatic_scan_wifi">Only on Wi-Fi</string>
<string name="automatic_scan_wifi_on">Update app lists automatically only on Wi-Fi</string>
<string name="update_auto_download">Automatically download updates</string>
<string name="update_auto_download_summary">Download the update files in the background</string>
<string name="notify">Update notifications</string>
<string name="notify_on">Show a notification when updates are available</string>
<string name="update_history">Update history</string>

View File

@ -9,6 +9,10 @@
<CheckBoxPreference android:title="@string/automatic_scan_wifi"
android:defaultValue="false"
android:key="updateOnWifiOnly" />
<CheckBoxPreference android:title="@string/update_auto_download"
android:summary="@string/update_auto_download_summary"
android:defaultValue="false"
android:key="updateAutoDownload" />
<CheckBoxPreference android:title="@string/notify"
android:defaultValue="true"
android:key="updateNotify" />