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.util.Log;
public class DB {
private static Semaphore dbSync = new Semaphore(1, true);
@ -360,16 +358,18 @@ public class DB {
private static void createAppApk(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_APP);
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 index apk_vercode on " + TABLE_APK
+ " (vercode);");
db.execSQL("create index apk_vercode on " + TABLE_APK + " (vercode);");
db.execSQL("create index apk_id on " + TABLE_APK + " (id);");
}
public static void resetTransient(SQLiteDatabase db) {
db.execSQL("drop table " + TABLE_APP);
db.execSQL("drop table " + TABLE_APK);
createAppApk(db);
}
private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
@ -399,7 +399,6 @@ public class DB {
db.execSQL("alter table " + TABLE_REPO + " add pubkey string");
}
}
public static File getDataPath() {
@ -482,11 +481,10 @@ public class DB {
+ " order by category", null);
c.moveToFirst();
while (!c.isAfterLast()) {
String s = c.getString(c.getColumnIndex("category"));
if (s == null) {
s = "none";
String s = c.getString(0);
if (s != null) {
result.add(s);
}
result.add(s);
c.moveToNext();
}
} catch (Exception e) {
@ -757,7 +755,8 @@ public class DB {
public void endUpdate() {
if (updateApps == null)
return;
Log.d("FDroid", "Processing endUpdate - " + updateApps.size() + " apps before");
Log.d("FDroid", "Processing endUpdate - " + updateApps.size()
+ " apps before");
for (App app : updateApps) {
if (!app.updated) {
// 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
// details of a completely new one). Calls to this must be wrapped by
// 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) {
return;
return false;
}
// Lazy initialise this...
@ -819,7 +820,7 @@ public class DB {
if (compatChecker.isCompatible(apk))
compatibleapks.add(apk);
if (compatibleapks.size() == 0)
return;
return false;
boolean found = false;
for (App app : updateApps) {
@ -859,6 +860,7 @@ public class DB {
upapp.updated = true;
updateApps.add(upapp);
}
return true;
}
@ -923,7 +925,8 @@ public class DB {
CommaSeparatedList.str(upapk.detail_permissions));
values.put("features", CommaSeparatedList.str(upapk.features));
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 });
} else {
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
// check if the particular setting has actually been changed.
UpdateService.schedule(getBaseContext());
populateLists();
if (data.hasExtra("reset")) {
updateRepos();
} else {
populateLists();
}
break;
}

View File

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

View File

@ -91,7 +91,6 @@ public class RepoXMLHandler extends DefaultHandler {
}
if (curel.equals("application") && curapp != null) {
getIcon(curapp);
// If we already have this application (must be from scanning a
// different repo) then just merge in the apks.
@ -241,36 +240,6 @@ public class RepoXMLHandler extends DefaultHandler {
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)
throws MalformedURLException, IOException {

View File

@ -18,6 +18,12 @@
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 android.app.AlarmManager;
@ -128,16 +134,17 @@ public class UpdateService extends IntentService {
}
if (success) {
Vector<DB.App> acceptedapps = new Vector<DB.App>();
Vector<DB.App> prevapps = ((FDroidApp) getApplication())
.getApps();
db = DB.getDB();
try {
prevUpdates = db.beginUpdate(prevapps);
for (DB.App app : apps) {
db.updateApplication(app);
if(db.updateApplication(app))
acceptedapps.add(app);
}
db.endUpdate();
((FDroidApp) getApplication()).invalidateApps();
if (notify)
newUpdates = db.getNumUpdates();
} catch (Exception ex) {
@ -149,6 +156,12 @@ public class UpdateService extends IntentService {
} finally {
DB.releaseDB();
}
if (success) {
for (DB.App app : acceptedapps)
getIcon(app);
((FDroidApp) getApplication()).invalidateApps();
}
}
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) {
}
}
}