Fix repo update notifications
* Get rid of getAppsBasic(boolean) * Use FDroidApp to read apps from UpdateService - Don't read the SQL tables twice if updating manually - Use two app lists, not three * Notify regardless of the previous updates count
This commit is contained in:
		
							parent
							
								
									60dcf7f12c
								
							
						
					
					
						commit
						fa1b53a81c
					
				| @ -609,8 +609,7 @@ public class DB { | ||||
| 
 | ||||
|     // Get the number of apps that have updates available. This can be a | ||||
|     // time consuming operation. | ||||
|     public int getNumUpdates() { | ||||
|         List<App> apps = getAppsBasic(true); | ||||
|     public int getNumUpdates(List<DB.App> apps) { | ||||
|         int count = 0; | ||||
|         for (App app : apps) { | ||||
|             if (!app.ignoreUpdates && app.hasUpdates) | ||||
| @ -924,100 +923,6 @@ public class DB { | ||||
|         return apps; | ||||
|     } | ||||
| 
 | ||||
|     private List<App> getAppsBasic(boolean getinstalledinfo) { | ||||
| 
 | ||||
|         // If we're going to need it, get info in what's currently installed | ||||
|         Map<String, PackageInfo> systemApks = null; | ||||
|         if (getinstalledinfo) { | ||||
|             Log.d("FDroid", "Reading installed packages"); | ||||
|             systemApks = new HashMap<String, PackageInfo>(); | ||||
|             List<PackageInfo> installedPackages = mContext.getPackageManager() | ||||
|                     .getInstalledPackages(0); | ||||
|             for (PackageInfo appInfo : installedPackages) { | ||||
|                 systemApks.put(appInfo.packageName, appInfo); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         Map<String, App> apps = new HashMap<String, App>(); | ||||
|         Cursor c = null; | ||||
|         long startTime = System.currentTimeMillis(); | ||||
|         try { | ||||
| 
 | ||||
|             String cols[] = new String[] { "id", "curVercode" }; | ||||
|             c = db.query(TABLE_APP, cols, null, null, null, null, null); | ||||
|             c.moveToFirst(); | ||||
|             while (!c.isAfterLast()) { | ||||
| 
 | ||||
|                 App app = new App(); | ||||
|                 app.id = c.getString(0); | ||||
|                 app.curVercode = c.getInt(1); | ||||
|                 app.hasUpdates = false; | ||||
| 
 | ||||
|                 if (getinstalledinfo && systemApks.containsKey(app.id)) { | ||||
|                     PackageInfo sysapk = systemApks.get(app.id); | ||||
|                     app.installedVerCode = sysapk.versionCode; | ||||
|                 } else { | ||||
|                     app.installedVerCode = 0; | ||||
|                 } | ||||
| 
 | ||||
|                 apps.put(app.id, app); | ||||
|                 c.moveToNext(); | ||||
|             } | ||||
|             c.close(); | ||||
|             c = null; | ||||
| 
 | ||||
|             Log.d("FDroid", "Read basic app data from database " + " (took " | ||||
|                     + (System.currentTimeMillis() - startTime) + " ms)"); | ||||
| 
 | ||||
|             cols = new String[] { "id", "vercode", "repo" }; | ||||
|             c = db.query(TABLE_APK, cols, null, null, null, null, | ||||
|                     "vercode desc"); | ||||
|             c.moveToFirst(); | ||||
|             while (!c.isAfterLast()) { | ||||
|                 Apk apk = new Apk(); | ||||
|                 apk.id = c.getString(0); | ||||
|                 apk.vercode = c.getInt(1); | ||||
|                 apk.repo = c.getInt(2); | ||||
|                 apps.get(apk.id).apks.add(apk); | ||||
|                 c.moveToNext(); | ||||
|             } | ||||
|             c.close(); | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             Log.e("FDroid", "Exception during database reading:\n" | ||||
|                             + Log.getStackTraceString(e)); | ||||
|         } finally { | ||||
|             if (c != null) { | ||||
|                 c.close(); | ||||
|             } | ||||
| 
 | ||||
|             Log.d("FDroid", "Read basic app and apk data from database " + | ||||
|                     " (took " + (System.currentTimeMillis() - startTime) + | ||||
|                     " ms)"); | ||||
|         } | ||||
| 
 | ||||
|         List<App> result = new ArrayList<App>(apps.values()); | ||||
|         Collections.sort(result); | ||||
| 
 | ||||
|         // Fill in the hasUpdates fields if we have the necessary information... | ||||
|         if (getinstalledinfo) { | ||||
| 
 | ||||
|             // We'll say an application has updates if it's installed AND the | ||||
|             // version is older than the current one | ||||
|             for (App app : result) { | ||||
|                 Apk curver = app.getCurrentVersion(); | ||||
|                 if (curver != null | ||||
|                         && app.installedVerCode > 0 | ||||
|                         && app.installedVerCode < curver.vercode) { | ||||
|                     app.hasUpdates = true; | ||||
|                     app.updateVersion = curver.version; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     public List<String> doSearch(String query) { | ||||
| 
 | ||||
|         List<String> ids = new ArrayList<String>(); | ||||
| @ -1070,27 +975,18 @@ public class DB { | ||||
| 
 | ||||
|     private List<App> updateApps = null; | ||||
| 
 | ||||
|     // Called before a repo update starts. Returns the number of updates | ||||
|     // available beforehand. | ||||
|     public int beginUpdate(List<DB.App> apps) { | ||||
|     // Called before a repo update starts. | ||||
|     public void beginUpdate(List<DB.App> apps) { | ||||
|         // Get a list of all apps. All the apps and apks in this list will | ||||
|         // have 'updated' set to false at this point, and we will only set | ||||
|         // it to true when we see the app/apk in a repository. Thus, at the | ||||
|         // end, any that are still false can be removed. | ||||
|         updateApps = apps; | ||||
|         Log.d("FDroid", "AppUpdate: " + updateApps.size() | ||||
|                 + " apps before starting."); | ||||
|         Log.d("FDroid", "AppUpdate: " + updateApps.size() + " apps before starting."); | ||||
|         // Wrap the whole update in a transaction. Make sure to call | ||||
|         // either endUpdate or cancelUpdate to commit or discard it, | ||||
|         // respectively. | ||||
|         db.beginTransaction(); | ||||
| 
 | ||||
|         int count = 0; | ||||
|         for (App app : updateApps) { | ||||
|             if (!app.ignoreUpdates && app.hasUpdates) | ||||
|                 count++; | ||||
|         } | ||||
|         return count; | ||||
|     } | ||||
| 
 | ||||
|     // Called when a repo update ends. Any applications that have not been | ||||
| @ -1408,7 +1304,7 @@ public class DB { | ||||
|                     db.delete(TABLE_REPO, "address = ?", | ||||
|                             new String[] { address }); | ||||
|             } | ||||
|             List<App> apps = getAppsBasic(true); | ||||
|             List<App> apps = getApps(false); | ||||
|             for (App app : apps) { | ||||
|                 if (app.apks.isEmpty()) { | ||||
|                     db.delete(TABLE_APP, "id = ?", new String[] { app.id }); | ||||
|  | ||||
| @ -134,8 +134,7 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
| 
 | ||||
|             // Grab some preliminary information, then we can release the | ||||
|             // database while we do all the downloading, etc... | ||||
|             int prevUpdates = 0; | ||||
|             int newUpdates = 0; | ||||
|             int updates = 0; | ||||
|             List<DB.Repo> repos; | ||||
|             try { | ||||
|                 DB db = DB.getDB(); | ||||
| @ -145,7 +144,7 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|             } | ||||
| 
 | ||||
|             // Process each repo... | ||||
|             List<DB.App> apps = new ArrayList<DB.App>(); | ||||
|             List<DB.App> updatingApps = new ArrayList<DB.App>(); | ||||
|             List<Integer> keeprepos = new ArrayList<Integer>(); | ||||
|             boolean success = true; | ||||
|             boolean changes = false; | ||||
| @ -159,7 +158,7 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
| 
 | ||||
|                     StringBuilder newetag = new StringBuilder(); | ||||
|                     String err = RepoXMLHandler.doUpdate(getBaseContext(), | ||||
|                             repo, apps, newetag, keeprepos, this); | ||||
|                             repo, updatingApps, newetag, keeprepos, this); | ||||
|                     if (err == null) { | ||||
|                         String nt = newetag.toString(); | ||||
|                         if (!nt.equals(repo.lastetag)) { | ||||
| @ -178,7 +177,6 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             List<DB.App> acceptedapps = new ArrayList<DB.App>(); | ||||
|             if (!changes && success) { | ||||
|                 Log.d("FDroid", | ||||
|                         "Not checking app details or compatibility, because all repos were up to date."); | ||||
| @ -186,7 +184,7 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
| 
 | ||||
|                 sendStatus(STATUS_INFO, | ||||
|                         getString(R.string.status_checking_compatibility)); | ||||
|                 List<DB.App> prevapps = ((FDroidApp) getApplication()).getApps(); | ||||
|                 List<DB.App> apps = ((FDroidApp) getApplication()).getApps(); | ||||
| 
 | ||||
|                 DB db = DB.getDB(); | ||||
|                 try { | ||||
| @ -195,7 +193,7 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|                     // no data about during the update. (i.e. stuff from a repo | ||||
|                     // that we know is unchanged due to the etag) | ||||
|                     for (int keep : keeprepos) { | ||||
|                         for (DB.App app : prevapps) { | ||||
|                         for (DB.App app : apps) { | ||||
|                             boolean keepapp = false; | ||||
|                             for (DB.Apk apk : app.apks) { | ||||
|                                 if (apk.repo == keep) { | ||||
| @ -205,14 +203,14 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|                             } | ||||
|                             if (keepapp) { | ||||
|                                 DB.App app_k = null; | ||||
|                                 for (DB.App app2 : apps) { | ||||
|                                 for (DB.App app2 : updatingApps) { | ||||
|                                     if (app2.id.equals(app.id)) { | ||||
|                                         app_k = app2; | ||||
|                                         break; | ||||
|                                     } | ||||
|                                 } | ||||
|                                 if (app_k == null) { | ||||
|                                     apps.add(app); | ||||
|                                     updatingApps.add(app); | ||||
|                                     app_k = app; | ||||
|                                 } | ||||
|                                 app_k.updated = true; | ||||
| @ -224,14 +222,15 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|                         } | ||||
|                     } | ||||
| 
 | ||||
|                     prevUpdates = db.beginUpdate(prevapps); | ||||
|                     for (DB.App app : apps) { | ||||
|                         if (db.updateApplication(app)) | ||||
|                             acceptedapps.add(app); | ||||
|                     db.beginUpdate(apps); | ||||
|                     for (DB.App app : updatingApps) { | ||||
|                         db.updateApplication(app); | ||||
|                     } | ||||
|                     db.endUpdate(); | ||||
|                     if (notify) | ||||
|                         newUpdates = db.getNumUpdates(); | ||||
|                     if (notify) { | ||||
|                 		apps = ((FDroidApp) getApplication()).getApps(); | ||||
|                         updates = db.getNumUpdates(apps); | ||||
| 					} | ||||
|                     for (DB.Repo repo : repos) | ||||
|                         db.writeLastEtag(repo); | ||||
|                 } catch (Exception ex) { | ||||
| @ -249,9 +248,8 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|             if (success && changes) | ||||
|                 ((FDroidApp) getApplication()).invalidateAllApps(); | ||||
| 
 | ||||
|             if (success && changes && notify && (newUpdates > prevUpdates)) { | ||||
|                 Log.d("FDroid", "Notifying updates. Apps before:" + prevUpdates | ||||
|                         + ", apps after: " + newUpdates); | ||||
|             if (success && changes && notify && updates > 0) { | ||||
|                 Log.d("FDroid", "Notifying "+updates+" updates."); | ||||
|                 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( | ||||
|                         this) | ||||
|                         .setSmallIcon(R.drawable.icon) | ||||
| @ -263,9 +261,9 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|                                 getString(R.string.fdroid_updates_available)); | ||||
|                 Intent notifyIntent = new Intent(this, FDroid.class) | ||||
|                         .putExtra(FDroid.EXTRA_TAB_UPDATE, true); | ||||
|                 if (newUpdates > 1) { | ||||
|                 if (updates > 1) { | ||||
|                     mBuilder.setContentText(getString( | ||||
|                             R.string.many_updates_available, newUpdates)); | ||||
|                             R.string.many_updates_available, updates)); | ||||
| 
 | ||||
|                 } else { | ||||
|                     mBuilder.setContentText(getString(R.string.one_update_available)); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Daniel Martí
						Daniel Martí