Only get icons for compatible apps, and behave properly after reset

There's no point wasting time and space on icons for incompatible
apps because they'll never be displayed!
This commit is contained in:
Ciaran Gultnieks 2012-09-15 11:24:47 +01:00
parent b46b6ed933
commit d239d2dabe
5 changed files with 79 additions and 59 deletions

View File

@ -47,8 +47,6 @@ import android.preference.PreferenceManager;
import android.text.TextUtils.SimpleStringSplitter; import android.text.TextUtils.SimpleStringSplitter;
import android.util.Log; import android.util.Log;
public class DB { public class DB {
private static Semaphore dbSync = new Semaphore(1, true); private static Semaphore dbSync = new Semaphore(1, true);
@ -360,16 +358,18 @@ public class DB {
private static void createAppApk(SQLiteDatabase db) { private static void createAppApk(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_APP); db.execSQL(CREATE_TABLE_APP);
db.execSQL("create index app_id on " + TABLE_APP + " (id);"); db.execSQL("create index app_id on " + TABLE_APP + " (id);");
db.execSQL("create index app_category on " + TABLE_APP + " (category);");
db.execSQL(CREATE_TABLE_APK); db.execSQL(CREATE_TABLE_APK);
db.execSQL("create index apk_vercode on " + TABLE_APK db.execSQL("create index apk_vercode on " + TABLE_APK + " (vercode);");
+ " (vercode);");
db.execSQL("create index apk_id on " + TABLE_APK + " (id);"); db.execSQL("create index apk_id on " + TABLE_APK + " (id);");
} }
public static void resetTransient(SQLiteDatabase db) { public static void resetTransient(SQLiteDatabase db) {
db.execSQL("drop table " + TABLE_APP); db.execSQL("drop table " + TABLE_APP);
db.execSQL("drop table " + TABLE_APK); db.execSQL("drop table " + TABLE_APK);
createAppApk(db); createAppApk(db);
} }
private class DBHelper extends SQLiteOpenHelper { private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) { public DBHelper(Context context) {
@ -399,7 +399,6 @@ public class DB {
db.execSQL("alter table " + TABLE_REPO + " add pubkey string"); db.execSQL("alter table " + TABLE_REPO + " add pubkey string");
} }
} }
public static File getDataPath() { public static File getDataPath() {
@ -482,11 +481,10 @@ public class DB {
+ " order by category", null); + " order by category", null);
c.moveToFirst(); c.moveToFirst();
while (!c.isAfterLast()) { while (!c.isAfterLast()) {
String s = c.getString(c.getColumnIndex("category")); String s = c.getString(0);
if (s == null) { if (s != null) {
s = "none"; result.add(s);
} }
result.add(s);
c.moveToNext(); c.moveToNext();
} }
} catch (Exception e) { } catch (Exception e) {
@ -757,7 +755,8 @@ public class DB {
public void endUpdate() { public void endUpdate() {
if (updateApps == null) if (updateApps == null)
return; return;
Log.d("FDroid", "Processing endUpdate - " + updateApps.size() + " apps before"); Log.d("FDroid", "Processing endUpdate - " + updateApps.size()
+ " apps before");
for (App app : updateApps) { for (App app : updateApps) {
if (!app.updated) { if (!app.updated) {
// The application hasn't been updated, so it's no longer // The application hasn't been updated, so it's no longer
@ -800,10 +799,12 @@ public class DB {
// Called during update to supply new details for an application (or // Called during update to supply new details for an application (or
// details of a completely new one). Calls to this must be wrapped by // details of a completely new one). Calls to this must be wrapped by
// a call to beginUpdate and a call to endUpdate. // a call to beginUpdate and a call to endUpdate.
public void updateApplication(App upapp) { // Returns true if the app was accepted. If it wasn't, it's probably
// because it's not compatible with the device.
public boolean updateApplication(App upapp) {
if (updateApps == null) { if (updateApps == null) {
return; return false;
} }
// Lazy initialise this... // Lazy initialise this...
@ -819,7 +820,7 @@ public class DB {
if (compatChecker.isCompatible(apk)) if (compatChecker.isCompatible(apk))
compatibleapks.add(apk); compatibleapks.add(apk);
if (compatibleapks.size() == 0) if (compatibleapks.size() == 0)
return; return false;
boolean found = false; boolean found = false;
for (App app : updateApps) { for (App app : updateApps) {
@ -859,6 +860,7 @@ public class DB {
upapp.updated = true; upapp.updated = true;
updateApps.add(upapp); updateApps.add(upapp);
} }
return true;
} }
@ -923,7 +925,8 @@ public class DB {
CommaSeparatedList.str(upapk.detail_permissions)); CommaSeparatedList.str(upapk.detail_permissions));
values.put("features", CommaSeparatedList.str(upapk.features)); values.put("features", CommaSeparatedList.str(upapk.features));
if (oldapk != null) { if (oldapk != null) {
db.update(TABLE_APK, values, "id = ? and vercode = " + Integer.toString(oldapk.vercode), db.update(TABLE_APK, values,
"id = ? and vercode = " + Integer.toString(oldapk.vercode),
new String[] { oldapk.id }); new String[] { oldapk.id });
} else { } else {
db.insert(TABLE_APK, null, values); db.insert(TABLE_APK, null, values);

View File

@ -260,7 +260,11 @@ public class FDroid extends TabActivity implements OnItemClickListener,
// unschedule) the service accordingly. It's cheap, so no need to // unschedule) the service accordingly. It's cheap, so no need to
// check if the particular setting has actually been changed. // check if the particular setting has actually been changed.
UpdateService.schedule(getBaseContext()); UpdateService.schedule(getBaseContext());
populateLists(); if (data.hasExtra("reset")) {
updateRepos();
} else {
populateLists();
}
break; break;
} }

View File

@ -42,9 +42,6 @@ public class Preferences extends PreferenceActivity {
// TODO: Progress dialog + thread is needed, it can take a // TODO: Progress dialog + thread is needed, it can take a
// while to delete all the icons and cached apks in a long // while to delete all the icons and cached apks in a long
// standing install! // standing install!
Toast.makeText(getBaseContext(),
"Hold on...", Toast.LENGTH_SHORT)
.show();
// TODO: This is going to cause problems if there is background // TODO: This is going to cause problems if there is background
// update in progress at the time! // update in progress at the time!
@ -65,19 +62,16 @@ public class Preferences extends PreferenceActivity {
Toast.makeText(getBaseContext(), Toast.makeText(getBaseContext(),
"Local cached data has been cleared", Toast.LENGTH_LONG) "Local cached data has been cleared", Toast.LENGTH_LONG)
.show(); .show();
Intent ret = new Intent();
ret.putExtra("reset", true);
setResult(RESULT_OK, ret);
finish();
return true; return true;
} }
}); });
} }
@Override
public void finish() {
Intent ret = new Intent();
this.setResult(RESULT_OK, ret);
super.finish();
}
private void deleteAll(File dir) { private void deleteAll(File dir) {

View File

@ -91,7 +91,6 @@ public class RepoXMLHandler extends DefaultHandler {
} }
if (curel.equals("application") && curapp != null) { if (curel.equals("application") && curapp != null) {
getIcon(curapp);
// If we already have this application (must be from scanning a // If we already have this application (must be from scanning a
// different repo) then just merge in the apks. // different repo) then just merge in the apks.
@ -241,36 +240,6 @@ public class RepoXMLHandler extends DefaultHandler {
curchars.setLength(0); curchars.setLength(0);
} }
private void getIcon(DB.App app) {
try {
File f = new File(DB.getIconsPath(), app.icon);
if (f.exists())
return;
URL u = new URL(server + "/icons/" + app.icon);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
if (uc.getResponseCode() == 200) {
BufferedInputStream getit = new BufferedInputStream(
uc.getInputStream());
FileOutputStream saveit = new FileOutputStream(f);
BufferedOutputStream bout = new BufferedOutputStream(saveit,
1024);
byte data[] = new byte[1024];
int readed = getit.read(data, 0, 1024);
while (readed != -1) {
bout.write(data, 0, readed);
readed = getit.read(data, 0, 1024);
}
bout.close();
getit.close();
saveit.close();
}
} catch (Exception e) {
}
}
private static void getRemoteFile(Context ctx, String url, String dest) private static void getRemoteFile(Context ctx, String url, String dest)
throws MalformedURLException, IOException { throws MalformedURLException, IOException {

View File

@ -18,6 +18,12 @@
package org.fdroid.fdroid; package org.fdroid.fdroid;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector; import java.util.Vector;
import android.app.AlarmManager; import android.app.AlarmManager;
@ -128,16 +134,17 @@ public class UpdateService extends IntentService {
} }
if (success) { if (success) {
Vector<DB.App> acceptedapps = new Vector<DB.App>();
Vector<DB.App> prevapps = ((FDroidApp) getApplication()) Vector<DB.App> prevapps = ((FDroidApp) getApplication())
.getApps(); .getApps();
db = DB.getDB(); db = DB.getDB();
try { try {
prevUpdates = db.beginUpdate(prevapps); prevUpdates = db.beginUpdate(prevapps);
for (DB.App app : apps) { for (DB.App app : apps) {
db.updateApplication(app); if(db.updateApplication(app))
acceptedapps.add(app);
} }
db.endUpdate(); db.endUpdate();
((FDroidApp) getApplication()).invalidateApps();
if (notify) if (notify)
newUpdates = db.getNumUpdates(); newUpdates = db.getNumUpdates();
} catch (Exception ex) { } catch (Exception ex) {
@ -149,6 +156,12 @@ public class UpdateService extends IntentService {
} finally { } finally {
DB.releaseDB(); DB.releaseDB();
} }
if (success) {
for (DB.App app : acceptedapps)
getIcon(app);
((FDroidApp) getApplication()).invalidateApps();
}
} }
if (success && notify) { if (success && notify) {
@ -204,4 +217,41 @@ public class UpdateService extends IntentService {
} }
} }
private void getIcon(DB.App app) {
try {
File f = new File(DB.getIconsPath(), app.icon);
if (f.exists())
return;
if(app.apks.size() == 0)
return;
String server = app.apks.get(0).detail_server;
URL u = new URL(server + "/icons/" + app.icon);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
if (uc.getResponseCode() == 200) {
BufferedInputStream getit = new BufferedInputStream(
uc.getInputStream());
FileOutputStream saveit = new FileOutputStream(f);
BufferedOutputStream bout = new BufferedOutputStream(saveit,
1024);
byte data[] = new byte[1024];
int readed = getit.read(data, 0, 1024);
while (readed != -1) {
bout.write(data, 0, readed);
readed = getit.read(data, 0, 1024);
}
bout.close();
getit.close();
saveit.close();
}
} catch (Exception e) {
}
}
} }