reorganize update process to set up updating individual repos

This keeps the same logic as was in place before, but splits out code from
onHandleIntent() so that it can be used to update specific repos, rather
than always updating all repos.  This is needed for transient repos, like
p2p repos connected via bluetooth or local wifi.
This commit is contained in:
Hans-Christoph Steiner 2014-01-21 20:54:59 -05:00
parent 62008e85c6
commit 10106bf6dd

View File

@ -21,22 +21,32 @@ package org.fdroid.fdroid;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import android.app.*; import android.app.AlarmManager;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.*; import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.os.ResultReceiver;
import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log;
import org.fdroid.fdroid.updater.RepoUpdater;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import org.fdroid.fdroid.updater.RepoUpdater;
public class UpdateService extends IntentService implements ProgressListener { public class UpdateService extends IntentService implements ProgressListener {
public static final String RESULT_MESSAGE = "msg"; public static final String RESULT_MESSAGE = "msg";
@ -232,9 +242,32 @@ public class UpdateService extends IntentService implements ProgressListener {
} else { } else {
Log.d("FDroid", "Unscheduled (manually requested) update"); Log.d("FDroid", "Unscheduled (manually requested) update");
} }
errmsg = updateRepos();
if (TextUtils.isEmpty(errmsg)) {
Editor e = prefs.edit();
e.putLong(Preferences.PREF_UPD_LAST, System.currentTimeMillis());
e.commit();
}
} catch (Exception e) {
Log.e("FDroid",
"Exception during update processing:\n"
+ Log.getStackTraceString(e));
if (TextUtils.isEmpty(errmsg))
errmsg = "Unknown error";
sendStatus(STATUS_ERROR, errmsg);
} finally {
Log.d("FDroid", "Update took "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " seconds.");
receiver = null;
}
}
protected String updateRepos() throws Exception {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
boolean notify = prefs.getBoolean(Preferences.PREF_UPD_NOTIFY, false); boolean notify = prefs.getBoolean(Preferences.PREF_UPD_NOTIFY, false);
String errmsg = "";
// 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;
@ -251,12 +284,10 @@ public class UpdateService extends IntentService implements ProgressListener {
// Process each repo... // Process each repo...
List<DB.App> updatingApps = new ArrayList<DB.App>(); List<DB.App> updatingApps = new ArrayList<DB.App>();
List<Integer> keeprepos = new ArrayList<Integer>(); List<Integer> keeprepos = new ArrayList<Integer>();
boolean success = true;
boolean changes = false; boolean changes = false;
for (DB.Repo repo : repos) { for (DB.Repo repo : repos) {
if (!repo.inuse) { if (!repo.inuse)
continue; continue;
}
sendStatus(STATUS_INFO, getString(R.string.status_connecting_to_repo, repo.address)); sendStatus(STATUS_INFO, getString(R.string.status_connecting_to_repo, repo.address));
RepoUpdater updater = RepoUpdater.createUpdaterFor(getBaseContext(), repo); RepoUpdater updater = RepoUpdater.createUpdaterFor(getBaseContext(), repo);
updater.setProgressListener(this); updater.setProgressListener(this);
@ -275,14 +306,12 @@ public class UpdateService extends IntentService implements ProgressListener {
} }
} }
if (!changes && success) { boolean success = true;
Log.d("FDroid", if (!changes) {
"Not checking app details or compatibility, " + Log.d("FDroid", "Not checking app details or compatibility, " +
"because all repos were up to date."); "because all repos were up to date.");
} else if (changes && success) { } else {
sendStatus(STATUS_INFO, getString(R.string.status_checking_compatibility));
sendStatus(STATUS_INFO,
getString(R.string.status_checking_compatibility));
DB db = DB.getDB(); DB db = DB.getDB();
try { try {
@ -336,7 +365,6 @@ public class UpdateService extends IntentService implements ProgressListener {
} finally { } finally {
DB.releaseDB(); DB.releaseDB();
} }
} }
if (success && changes) { if (success && changes) {
@ -345,71 +373,51 @@ public class UpdateService extends IntentService implements ProgressListener {
apps = ((FDroidApp) getApplication()).getApps(); apps = ((FDroidApp) getApplication()).getApps();
updates = getNumUpdates(apps); updates = getNumUpdates(apps);
} }
if (notify && updates > 0)
showAppUpdatesNotification(updates);
} }
if (success && changes && notify && updates > 0) { if (success) {
Log.d("FDroid", "Notifying "+updates+" updates.");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(
this)
.setAutoCancel(true)
.setContentTitle(
getString(R.string.fdroid_updates_available));
if (Build.VERSION.SDK_INT >= 11) {
mBuilder.setSmallIcon(R.drawable.ic_stat_notify_updates);
} else {
mBuilder.setSmallIcon(R.drawable.ic_launcher);
}
Intent notifyIntent = new Intent(this, FDroid.class)
.putExtra(FDroid.EXTRA_TAB_UPDATE, true);
if (updates > 1) {
mBuilder.setContentText(getString(
R.string.many_updates_available, updates));
} else {
mBuilder.setContentText(getString(R.string.one_update_available));
}
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(this).addParentStack(FDroid.class)
.addNextIntent(notifyIntent);
PendingIntent pendingIntent = stackBuilder
.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
if (!success) {
if (errmsg.length() == 0)
errmsg = "Unknown error";
sendStatus(STATUS_ERROR, errmsg);
} else {
Editor e = prefs.edit();
e.putLong(Preferences.PREF_UPD_LAST, System.currentTimeMillis());
e.commit();
if (changes) { if (changes) {
sendStatus(STATUS_COMPLETE_WITH_CHANGES); sendStatus(STATUS_COMPLETE_WITH_CHANGES);
} else { } else {
sendStatus(STATUS_COMPLETE_AND_SAME); sendStatus(STATUS_COMPLETE_AND_SAME);
} }
} } else {
if (TextUtils.isEmpty(errmsg))
} catch (Exception e) {
Log.e("FDroid",
"Exception during update processing:\n"
+ Log.getStackTraceString(e));
if (errmsg.length() == 0)
errmsg = "Unknown error"; errmsg = "Unknown error";
sendStatus(STATUS_ERROR, errmsg); sendStatus(STATUS_ERROR, errmsg);
} finally {
Log.d("FDroid", "Update took "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " seconds.");
receiver = null;
}
} }
return errmsg;
}
private void showAppUpdatesNotification(int updates) throws Exception {
Log.d("FDroid", "Notifying " + updates + " updates.");
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(getString(R.string.fdroid_updates_available));
if (Build.VERSION.SDK_INT >= 11) {
builder.setSmallIcon(R.drawable.ic_stat_notify_updates);
} else {
builder.setSmallIcon(R.drawable.ic_launcher);
}
Intent notifyIntent = new Intent(this, FDroid.class)
.putExtra(FDroid.EXTRA_TAB_UPDATE, true);
if (updates > 1) {
builder.setContentText(getString(R.string.many_updates_available, updates));
} else {
builder.setContentText(getString(R.string.one_update_available));
}
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(this).addParentStack(FDroid.class)
.addNextIntent(notifyIntent);
PendingIntent pi = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, builder.build());
}
/** /**
* Received progress event from the RepoXMLHandler. It could be progress * Received progress event from the RepoXMLHandler. It could be progress