Auto update from repos when empty, plus database rejig and less unnecessary list repopulation

This commit is contained in:
Ciaran Gultnieks 2010-11-14 18:28:01 +00:00
parent fb772cb015
commit a2c1b3da17
4 changed files with 67 additions and 106 deletions

View File

@ -150,9 +150,6 @@ public class AppDetails extends ListActivity {
setContentView(R.layout.appdetails);
db = new DB(this);
mPm = getPackageManager();
Intent i = getIntent();
appid = "";
if (!i.hasExtra("appid")) {
@ -161,14 +158,6 @@ public class AppDetails extends ListActivity {
appid = i.getStringExtra("appid");
}
reset(false);
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
}
private boolean pref_cacheDownloaded;
@ -176,15 +165,20 @@ public class AppDetails extends ListActivity {
@Override
protected void onStart() {
super.onStart();
db = new DB(this);
mPm = getPackageManager();
((FDroidApp) getApplication()).inActivity++;
// Get the preferences we're going to use in this Activity...
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
pref_cacheDownloaded = prefs.getBoolean("cacheDownloaded", true);
reset(false);
}
@Override
protected void onStop() {
db.close();
((FDroidApp) getApplication()).inActivity--;
super.onStop();
}

View File

@ -30,19 +30,15 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
public class DB {
private static final String DATABASE_NAME = "fdroid_db";
private static final String DATABASE_NAME = "fdroid";
private SQLiteDatabase db;
private Context mctx;
// The TABLE_VERSION table tracks the database version.
private static final String TABLE_VERSION = "fdroid_version";
private static final String CREATE_TABLE_VERSION = "create table "
+ TABLE_VERSION + " (version int not null);";
// The TABLE_APP table stores details of all the applications we know about.
// This information is retrieved from the repositories.
@ -190,11 +186,38 @@ public class DB {
// Version 2...
{ "alter table " + TABLE_APP + " add marketVersion text",
"alter table "+ TABLE_APP + " add marketVercode integer"
}
"alter table " + TABLE_APP + " add marketVercode integer" }
};
private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DB_UPGRADES.length + 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_REPO);
db.execSQL(CREATE_TABLE_APP);
db.execSQL(CREATE_TABLE_APK);
ContentValues values = new ContentValues();
values.put("address", "http://f-droid.org/repo");
values.put("inuse", 1);
values.put("priority", 10);
db.insert(TABLE_REPO, null, values);
onUpgrade(db, 1, DB_UPGRADES.length + 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for(int v=oldVersion+1;v<=newVersion;v++)
for (int i = 0; i < DB_UPGRADES[v - 2].length; i++)
db.execSQL(DB_UPGRADES[v - 2][i]);
}
}
public static String getIconsPath() {
return "/sdcard/.fdroid/icons/";
}
@ -202,84 +225,29 @@ public class DB {
private PackageManager mPm;
public DB(Context ctx) {
mctx = ctx;
db = ctx.openOrCreateDatabase(DATABASE_NAME, 0, null);
// Check if we already have a database and create or upgrade as
// appropriate...
Cursor c = db.rawQuery(
"SELECT name FROM sqlite_master WHERE type='table' AND name= '"
+ TABLE_VERSION + "'", null);
boolean newinst = (c.getCount() == 0);
c.close();
upgrade(newinst);
DBHelper h=new DBHelper(ctx);
db = h.getWritableDatabase();
mPm = ctx.getPackageManager();
}
// Upgrade the database to the latest version. (Or, if 'reset' is true,
// completely reset it, i.e. (re-)create all the tables from scratch and
// populate any initial data.
public void upgrade(boolean reset) {
int version;
if (reset) {
db.execSQL("drop table if exists " + TABLE_VERSION);
db.execSQL("drop table if exists " + TABLE_REPO);
db.execSQL("drop table if exists " + TABLE_APP);
db.execSQL("drop table if exists " + TABLE_APK);
db.execSQL(CREATE_TABLE_VERSION);
db.execSQL("insert into " + TABLE_VERSION
+ " (version) values (1);");
db.execSQL(CREATE_TABLE_REPO);
db.execSQL(CREATE_TABLE_APP);
db.execSQL(CREATE_TABLE_APK);
addServer("http://f-droid.org/repo", 10);
version = 1;
} else {
// See what database version we have...
Cursor c = db
.rawQuery("SELECT version from " + TABLE_VERSION, null);
c.moveToFirst();
if (c.isAfterLast()) {
c.close();
Log.d("FDroid", "Missing version record - assuming 1");
db.execSQL("INSERT into " + TABLE_VERSION
+ " (version) values (1);");
version = 1;
} else {
version = c.getInt(0);
c.close();
}
}
// Run upgrade scripts if necessary...
boolean modified = false;
while (version < DB_UPGRADES.length + 1) {
for(int i=0;i<DB_UPGRADES[version -1].length;i++)
db.execSQL(DB_UPGRADES[version - 1][i]);
version++;
db.execSQL("update " + TABLE_VERSION + " set version = " + version
+ ";");
modified = true;
}
if (modified || reset) {
// Close and reopen to ensure underlying prepared statements are
// dropped, otherwise
// they will fail to execute.
db.close();
db = mctx.openOrCreateDatabase(DATABASE_NAME, 0, null);
}
}
public void close() {
db.close();
db = null;
}
// Delete the database, which should cause it to be re-created next time it's
// used.
public static void delete(Context ctx) {
try {
ctx.deleteDatabase(DATABASE_NAME);
// Also try and delete the old one, from versions 0.13 and earlier.
ctx.deleteDatabase("fdroid_db");
} catch(Exception ex) {
Log.d("FDroid","Exception in DB.delete: "+ex.getMessage());
}
}
// Return a list of apps matching the given criteria.
// 'appid' - specific app id to retrieve, or null
// 'filter' - search text to filter on.
@ -421,6 +389,8 @@ public class DB {
// Returns the number of new updates (installed applications for which
// there is a new version available)
public int endUpdate() {
if (updateApps == null)
return 0;
for (App app : updateApps) {
if (!app.updated) {
// The application hasn't been updated, so it's no longer

View File

@ -176,8 +176,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
if (!icon_path.exists())
icon_path.mkdir();
db = new DB(this);
tabHost = getTabHost();
createTabs();
@ -190,21 +188,17 @@ public class FDroid extends TabActivity implements OnItemClickListener {
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
}
@Override
protected void onStart() {
super.onStart();
((FDroidApp) getApplication()).inActivity++;
db = new DB(this);
populateLists(true);
}
@Override
protected void onStop() {
db.close();
((FDroidApp) getApplication()).inActivity--;
super.onStop();
}
@ -241,7 +235,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
case PREFERENCES:
Intent prefs = new Intent(getBaseContext(), Preferences.class);
startActivityForResult(prefs, REQUEST_PREFS);
populateLists(true);
return true;
case ABOUT:
@ -289,7 +282,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
switch (requestCode) {
case REQUEST_APPDETAILS:
populateLists(false);
break;
case REQUEST_MANAGEREPOS:
if (data.hasExtra("update")) {
@ -322,7 +314,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
// particular setting has
// actually been changed.
UpdateService.schedule(getBaseContext());
populateLists(false);
break;
}
@ -382,6 +373,13 @@ public class FDroid extends TabActivity implements OnItemClickListener {
private void populateLists(boolean update) {
Vector<DB.App> apps = db.getApps(null, null, update);
if(apps.isEmpty()) {
// If there are no apps, update from the repos - it must be a
// new installation.
Log.d("FDroid", "Empty app list forces repo update");
updateRepos();
return;
}
Log.d("FDroid", "Updating lists - " + apps.size() + " apps in total");
apps_in.clear();

View File

@ -35,8 +35,7 @@ public class Preferences extends PreferenceActivity {
r.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
DB db = new DB(Preferences.this);
db.upgrade(true);
DB.delete(getApplicationContext());
// TODO: Clear cached apks and icons too.
Toast
.makeText(getBaseContext(),