Fixed an inconsistency in the updates listed after a repo update

This commit is contained in:
Ciaran Gultnieks 2010-12-14 22:54:07 +00:00 committed by Ciaran Gultnieks
parent bc556e0e2b
commit 24f5f0a5d8
3 changed files with 617 additions and 606 deletions

File diff suppressed because it is too large Load Diff

View File

@ -184,8 +184,7 @@ public class RepoXMLHandler extends DefaultHandler {
} }
} }
// Returns the number of applications with updates. public static void doUpdates(Context ctx, DB db) {
public static int doUpdates(Context ctx, DB db) {
db.beginUpdate(); db.beginUpdate();
Vector<DB.Repo> repos = db.getRepos(); Vector<DB.Repo> repos = db.getRepos();
for (DB.Repo repo : repos) { for (DB.Repo repo : repos) {
@ -236,7 +235,7 @@ public class RepoXMLHandler extends DefaultHandler {
} }
} }
return db.endUpdate(); db.endUpdate();
} }

View File

@ -35,121 +35,129 @@ import android.util.Log;
public class UpdateService extends Service { public class UpdateService extends Service {
// Schedule (or cancel schedule for) this service, according to the // Schedule (or cancel schedule for) this service, according to the
// current preferences. Should be called a) at boot, or b) if the preference // current preferences. Should be called a) at boot, or b) if the preference
// is changed. // is changed.
// TODO: What if we get upgraded? // TODO: What if we get upgraded?
public static void schedule(Context ctx) { public static void schedule(Context ctx) {
SharedPreferences prefs = PreferenceManager SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(ctx); .getDefaultSharedPreferences(ctx);
String sint = prefs.getString("updateInterval", "0"); String sint = prefs.getString("updateInterval", "0");
int interval = Integer.parseInt(sint); int interval = Integer.parseInt(sint);
Intent intent = new Intent(ctx, UpdateService.class); Intent intent = new Intent(ctx, UpdateService.class);
PendingIntent pending = PendingIntent.getService(ctx, 0, intent, 0); PendingIntent pending = PendingIntent.getService(ctx, 0, intent, 0);
AlarmManager alarm = (AlarmManager) ctx AlarmManager alarm = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE); .getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending); alarm.cancel(pending);
if (interval > 0) { if (interval > 0) {
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 5000, SystemClock.elapsedRealtime() + 5000,
AlarmManager.INTERVAL_HOUR, pending); AlarmManager.INTERVAL_HOUR, pending);
} }
} }
// For API levels <5 // For API levels <5
@Override @Override
public void onStart(Intent intent, int startId) { public void onStart(Intent intent, int startId) {
handleCommand(); handleCommand();
} }
// For API levels >=5 // For API levels >=5
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(); handleCommand();
return START_REDELIVER_INTENT; return START_REDELIVER_INTENT;
} }
private void handleCommand() { private void handleCommand() {
new Thread() { new Thread() {
public void run() { public void run() {
// If we're in one of our list activities, we don't want // If we're in one of our list activities, we don't want
// to run an update because the database will be out of // to run an update because the database will be out of
// sync with the display. // sync with the display.
if (((FDroidApp) getApplication()).inActivity != 0) if (((FDroidApp) getApplication()).inActivity != 0)
return; return;
// See if it's time to actually do anything yet... // See if it's time to actually do anything yet...
SharedPreferences prefs = PreferenceManager SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext()); .getDefaultSharedPreferences(getBaseContext());
long lastUpdate = prefs.getLong("lastUpdateCheck", 0); long lastUpdate = prefs.getLong("lastUpdateCheck", 0);
String sint = prefs.getString("updateInterval", "0"); String sint = prefs.getString("updateInterval", "0");
int interval = Integer.parseInt(sint); int interval = Integer.parseInt(sint);
if (interval == 0) if (interval == 0)
return; return;
if (lastUpdate + (interval * 60 * 60) > System if (lastUpdate + (interval * 60 * 60) > System
.currentTimeMillis()) .currentTimeMillis())
return; return;
// Make sure we have a connection... // Make sure we have a connection...
ConnectivityManager netstate = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager netstate = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (netstate.getNetworkInfo(1).getState() != NetworkInfo.State.CONNECTED if (netstate.getNetworkInfo(1).getState() != NetworkInfo.State.CONNECTED
&& netstate.getNetworkInfo(0).getState() != NetworkInfo.State.CONNECTED) && netstate.getNetworkInfo(0).getState() != NetworkInfo.State.CONNECTED)
return; return;
// Do the update... // Do the update...
DB db = null; DB db = null;
try { try {
db = new DB(getBaseContext()); db = new DB(getBaseContext());
int updateNum = RepoXMLHandler.doUpdates(getBaseContext(), boolean notify = prefs.getBoolean("updateNotify", false);
db);
if (updateNum != 0) { // Get the number of updates available before we
// We have updates. // start, so we can notify if there are new ones.
if (prefs.getBoolean("updateNotify", false)) { // (But avoid doing it if the user doesn't want
// And the user wants to know. // notifications, since it may be time consuming)
NotificationManager n = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int prevUpdates = 0;
Notification notification = new Notification( if (notify)
R.drawable.icon, prevUpdates = db.getNumUpdates();
"FDroid Updates Available", System
.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "FDroid";
CharSequence contentText = "Updates are available.";
Intent notificationIntent = new Intent(
UpdateService.this, FDroid.class);
PendingIntent contentIntent = PendingIntent
.getActivity(UpdateService.this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context,
contentTitle, contentText, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
n.notify(1, notification);
}
}
} catch (Exception e) { RepoXMLHandler.doUpdates(getBaseContext(), db);
Log.d("FDroid", "Exception during handleCommand() - "
+ e.getMessage());
} finally {
if (db != null)
db.close();
stopSelf();
}
} if (notify) {
}.start(); if (db.getNumUpdates() > prevUpdates) {
// And the user wants to know.
NotificationManager n = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.icon,
"FDroid Updates Available", System
.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "FDroid";
CharSequence contentText = "Updates are available.";
Intent notificationIntent = new Intent(
UpdateService.this, FDroid.class);
PendingIntent contentIntent = PendingIntent
.getActivity(UpdateService.this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context,
contentTitle, contentText, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
n.notify(1, notification);
}
}
} } catch (Exception e) {
Log.d("FDroid", "Exception during handleCommand() - "
+ e.getMessage());
} finally {
if (db != null)
db.close();
stopSelf();
}
@Override }
public IBinder onBind(Intent intent) { }.start();
return null;
} }
@Override
public IBinder onBind(Intent intent) {
return null;
}
} }