diff --git a/src/org/fdroid/fdroid/AppDetails.java b/src/org/fdroid/fdroid/AppDetails.java index 61c99b921..7965c8b24 100644 --- a/src/org/fdroid/fdroid/AppDetails.java +++ b/src/org/fdroid/fdroid/AppDetails.java @@ -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,30 +158,27 @@ public class AppDetails extends ListActivity { appid = i.getStringExtra("appid"); } - reset(false); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - db.close(); - } private boolean pref_cacheDownloaded; @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(); } diff --git a/src/org/fdroid/fdroid/DB.java b/src/org/fdroid/fdroid/DB.java index 286120a14..c40be9970 100644 --- a/src/org/fdroid/fdroid/DB.java +++ b/src/org/fdroid/fdroid/DB.java @@ -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. @@ -88,7 +84,7 @@ public class DB { // explicitly ignored. (We're currently not using the database // field for this - we make the decision on the fly in getApps(). public boolean hasUpdates; - + // Used internally for tracking during repo updates. public boolean updated; @@ -188,13 +184,40 @@ public class DB { // private static final String[][] DB_UPGRADES = { - // Version 2... - {"alter table " + TABLE_APP + " add marketVersion text", - "alter table "+ TABLE_APP + " add marketVercode integer" - } + // Version 2... + { "alter table " + TABLE_APP + " add marketVersion text", + "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 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(); diff --git a/src/org/fdroid/fdroid/Preferences.java b/src/org/fdroid/fdroid/Preferences.java index cd55af93b..3f2cb4758 100644 --- a/src/org/fdroid/fdroid/Preferences.java +++ b/src/org/fdroid/fdroid/Preferences.java @@ -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(),