Auto update from repos when empty, plus database rejig and less unnecessary list repopulation
This commit is contained in:
parent
fb772cb015
commit
a2c1b3da17
@ -150,9 +150,6 @@ public class AppDetails extends ListActivity {
|
|||||||
|
|
||||||
setContentView(R.layout.appdetails);
|
setContentView(R.layout.appdetails);
|
||||||
|
|
||||||
db = new DB(this);
|
|
||||||
mPm = getPackageManager();
|
|
||||||
|
|
||||||
Intent i = getIntent();
|
Intent i = getIntent();
|
||||||
appid = "";
|
appid = "";
|
||||||
if (!i.hasExtra("appid")) {
|
if (!i.hasExtra("appid")) {
|
||||||
@ -161,14 +158,6 @@ public class AppDetails extends ListActivity {
|
|||||||
appid = i.getStringExtra("appid");
|
appid = i.getStringExtra("appid");
|
||||||
}
|
}
|
||||||
|
|
||||||
reset(false);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean pref_cacheDownloaded;
|
private boolean pref_cacheDownloaded;
|
||||||
@ -176,15 +165,20 @@ public class AppDetails extends ListActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
|
db = new DB(this);
|
||||||
|
mPm = getPackageManager();
|
||||||
((FDroidApp) getApplication()).inActivity++;
|
((FDroidApp) getApplication()).inActivity++;
|
||||||
// Get the preferences we're going to use in this Activity...
|
// Get the preferences we're going to use in this Activity...
|
||||||
SharedPreferences prefs = PreferenceManager
|
SharedPreferences prefs = PreferenceManager
|
||||||
.getDefaultSharedPreferences(getBaseContext());
|
.getDefaultSharedPreferences(getBaseContext());
|
||||||
pref_cacheDownloaded = prefs.getBoolean("cacheDownloaded", true);
|
pref_cacheDownloaded = prefs.getBoolean("cacheDownloaded", true);
|
||||||
|
|
||||||
|
reset(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStop() {
|
protected void onStop() {
|
||||||
|
db.close();
|
||||||
((FDroidApp) getApplication()).inActivity--;
|
((FDroidApp) getApplication()).inActivity--;
|
||||||
super.onStop();
|
super.onStop();
|
||||||
}
|
}
|
||||||
|
@ -30,19 +30,15 @@ import android.content.pm.PackageInfo;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class DB {
|
public class DB {
|
||||||
|
|
||||||
private static final String DATABASE_NAME = "fdroid_db";
|
private static final String DATABASE_NAME = "fdroid";
|
||||||
|
|
||||||
private SQLiteDatabase db;
|
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.
|
// The TABLE_APP table stores details of all the applications we know about.
|
||||||
// This information is retrieved from the repositories.
|
// This information is retrieved from the repositories.
|
||||||
@ -188,13 +184,40 @@ public class DB {
|
|||||||
//
|
//
|
||||||
private static final String[][] DB_UPGRADES = {
|
private static final String[][] DB_UPGRADES = {
|
||||||
|
|
||||||
// Version 2...
|
// Version 2...
|
||||||
{"alter table " + TABLE_APP + " add marketVersion text",
|
{ "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() {
|
public static String getIconsPath() {
|
||||||
return "/sdcard/.fdroid/icons/";
|
return "/sdcard/.fdroid/icons/";
|
||||||
}
|
}
|
||||||
@ -202,84 +225,29 @@ public class DB {
|
|||||||
private PackageManager mPm;
|
private PackageManager mPm;
|
||||||
|
|
||||||
public DB(Context ctx) {
|
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();
|
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() {
|
public void close() {
|
||||||
db.close();
|
db.close();
|
||||||
db = null;
|
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.
|
// Return a list of apps matching the given criteria.
|
||||||
// 'appid' - specific app id to retrieve, or null
|
// 'appid' - specific app id to retrieve, or null
|
||||||
// 'filter' - search text to filter on.
|
// 'filter' - search text to filter on.
|
||||||
@ -421,6 +389,8 @@ public class DB {
|
|||||||
// Returns the number of new updates (installed applications for which
|
// Returns the number of new updates (installed applications for which
|
||||||
// there is a new version available)
|
// there is a new version available)
|
||||||
public int endUpdate() {
|
public int endUpdate() {
|
||||||
|
if (updateApps == null)
|
||||||
|
return 0;
|
||||||
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
|
||||||
@ -485,7 +455,7 @@ public class DB {
|
|||||||
updateApkIfDifferent(null, upapk);
|
updateApkIfDifferent(null, upapk);
|
||||||
upapk.updated = true;
|
upapk.updated = true;
|
||||||
app.apks.add(upapk);
|
app.apks.add(upapk);
|
||||||
if(!app.hasUpdates && app.installedVersion != null)
|
if (!app.hasUpdates && app.installedVersion != null)
|
||||||
updateNewUpdates++;
|
updateNewUpdates++;
|
||||||
app.hasUpdates = true;
|
app.hasUpdates = true;
|
||||||
}
|
}
|
||||||
|
@ -176,8 +176,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
|
|||||||
if (!icon_path.exists())
|
if (!icon_path.exists())
|
||||||
icon_path.mkdir();
|
icon_path.mkdir();
|
||||||
|
|
||||||
db = new DB(this);
|
|
||||||
|
|
||||||
tabHost = getTabHost();
|
tabHost = getTabHost();
|
||||||
createTabs();
|
createTabs();
|
||||||
|
|
||||||
@ -190,21 +188,17 @@ public class FDroid extends TabActivity implements OnItemClickListener {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
((FDroidApp) getApplication()).inActivity++;
|
((FDroidApp) getApplication()).inActivity++;
|
||||||
|
db = new DB(this);
|
||||||
populateLists(true);
|
populateLists(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStop() {
|
protected void onStop() {
|
||||||
|
db.close();
|
||||||
((FDroidApp) getApplication()).inActivity--;
|
((FDroidApp) getApplication()).inActivity--;
|
||||||
super.onStop();
|
super.onStop();
|
||||||
}
|
}
|
||||||
@ -241,7 +235,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
|
|||||||
case PREFERENCES:
|
case PREFERENCES:
|
||||||
Intent prefs = new Intent(getBaseContext(), Preferences.class);
|
Intent prefs = new Intent(getBaseContext(), Preferences.class);
|
||||||
startActivityForResult(prefs, REQUEST_PREFS);
|
startActivityForResult(prefs, REQUEST_PREFS);
|
||||||
populateLists(true);
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case ABOUT:
|
case ABOUT:
|
||||||
@ -289,7 +282,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
|
|||||||
|
|
||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
case REQUEST_APPDETAILS:
|
case REQUEST_APPDETAILS:
|
||||||
populateLists(false);
|
|
||||||
break;
|
break;
|
||||||
case REQUEST_MANAGEREPOS:
|
case REQUEST_MANAGEREPOS:
|
||||||
if (data.hasExtra("update")) {
|
if (data.hasExtra("update")) {
|
||||||
@ -322,7 +314,6 @@ public class FDroid extends TabActivity implements OnItemClickListener {
|
|||||||
// particular setting has
|
// particular setting has
|
||||||
// actually been changed.
|
// actually been changed.
|
||||||
UpdateService.schedule(getBaseContext());
|
UpdateService.schedule(getBaseContext());
|
||||||
populateLists(false);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -382,6 +373,13 @@ public class FDroid extends TabActivity implements OnItemClickListener {
|
|||||||
private void populateLists(boolean update) {
|
private void populateLists(boolean update) {
|
||||||
|
|
||||||
Vector<DB.App> apps = db.getApps(null, null, 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");
|
Log.d("FDroid", "Updating lists - " + apps.size() + " apps in total");
|
||||||
|
|
||||||
apps_in.clear();
|
apps_in.clear();
|
||||||
|
@ -35,8 +35,7 @@ public class Preferences extends PreferenceActivity {
|
|||||||
r.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
r.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||||
|
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
DB db = new DB(Preferences.this);
|
DB.delete(getApplicationContext());
|
||||||
db.upgrade(true);
|
|
||||||
// TODO: Clear cached apks and icons too.
|
// TODO: Clear cached apks and icons too.
|
||||||
Toast
|
Toast
|
||||||
.makeText(getBaseContext(),
|
.makeText(getBaseContext(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user