diff options
author | Sunny Goyal <sunnygoyal@google.com> | 2015-12-17 17:09:36 -0800 |
---|---|---|
committer | Sunny Goyal <sunnygoyal@google.com> | 2015-12-18 10:20:52 -0800 |
commit | 6f70936f7fb7cb45ef6a2a3d1019b6ecf3e49440 (patch) | |
tree | 54e0b982fb3cfc3fd6d50f8388a8149c4f1649fe /src/com/android/launcher3/util/SQLiteCacheHelper.java | |
parent | d896ee43cd9195a2a2742324733b036ee72875bb (diff) | |
download | android_packages_apps_Trebuchet-6f70936f7fb7cb45ef6a2a3d1019b6ecf3e49440.tar.gz android_packages_apps_Trebuchet-6f70936f7fb7cb45ef6a2a3d1019b6ecf3e49440.tar.bz2 android_packages_apps_Trebuchet-6f70936f7fb7cb45ef6a2a3d1019b6ecf3e49440.zip |
Preventing launcher crashes due to low disk space.
In case of low disk space, all write operations to the IconCache are
silently ignored. This will not affect the Launcher behavior and
user will still see the latest icons, but in some cases, icon loading
would appear slow
Bug: 24585352
Change-Id: I85ccc519046fc3708403388bba89e019a3f2ce3d
Diffstat (limited to 'src/com/android/launcher3/util/SQLiteCacheHelper.java')
-rw-r--r-- | src/com/android/launcher3/util/SQLiteCacheHelper.java | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/com/android/launcher3/util/SQLiteCacheHelper.java b/src/com/android/launcher3/util/SQLiteCacheHelper.java new file mode 100644 index 000000000..62a30d0d4 --- /dev/null +++ b/src/com/android/launcher3/util/SQLiteCacheHelper.java @@ -0,0 +1,128 @@ +package com.android.launcher3.util; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteException; +import android.database.sqlite.SQLiteFullException; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +/** + * An extension of {@link SQLiteOpenHelper} with utility methods for a single table cache DB. + * Any exception during write operations are ignored, and any version change causes a DB reset. + */ +public abstract class SQLiteCacheHelper { + private static final String TAG = "SQLiteCacheHelper"; + + private final String mTableName; + private final MySQLiteOpenHelper mOpenHelper; + + private boolean mIgnoreWrites; + + public SQLiteCacheHelper(Context context, String name, int version, String tableName) { + mTableName = tableName; + mOpenHelper = new MySQLiteOpenHelper(context, name, version); + + mIgnoreWrites = false; + } + + /** + * @see SQLiteDatabase#update(String, ContentValues, String, String[]) + */ + public void update(ContentValues values, String whereClause, String[] whereArgs) { + if (mIgnoreWrites) { + return; + } + try { + mOpenHelper.getWritableDatabase().update(mTableName, values, whereClause, whereArgs); + } catch (SQLiteFullException e) { + onDiskFull(e); + } catch (SQLiteException e) { + Log.d(TAG, "Ignoring sqlite exception", e); + } + } + + /** + * @see SQLiteDatabase#delete(String, String, String[]) + */ + public void delete(String whereClause, String[] whereArgs) { + if (mIgnoreWrites) { + return; + } + try { + mOpenHelper.getWritableDatabase().delete(mTableName, whereClause, whereArgs); + } catch (SQLiteFullException e) { + onDiskFull(e); + } catch (SQLiteException e) { + Log.d(TAG, "Ignoring sqlite exception", e); + } + } + + /** + * @see SQLiteDatabase#insertWithOnConflict(String, String, ContentValues, int) + */ + public void insertOrReplace(ContentValues values) { + if (mIgnoreWrites) { + return; + } + try { + mOpenHelper.getWritableDatabase().insertWithOnConflict( + mTableName, null, values, SQLiteDatabase.CONFLICT_REPLACE); + } catch (SQLiteFullException e) { + onDiskFull(e); + } catch (SQLiteException e) { + Log.d(TAG, "Ignoring sqlite exception", e); + } + } + + private void onDiskFull(SQLiteFullException e) { + Log.e(TAG, "Disk full, all write operations will be ignored", e); + mIgnoreWrites = true; + } + + /** + * @see SQLiteDatabase#query(String, String[], String, String[], String, String, String) + */ + public Cursor query(String[] columns, String selection, String[] selectionArgs) { + return mOpenHelper.getReadableDatabase().query( + mTableName, columns, selection, selectionArgs, null, null, null); + } + + protected abstract void onCreateTable(SQLiteDatabase db); + + /** + * A private inner class to prevent direct DB access. + */ + private class MySQLiteOpenHelper extends SQLiteOpenHelper { + + public MySQLiteOpenHelper(Context context, String name, int version) { + super(context, name, null, version); + } + + @Override + public void onCreate(SQLiteDatabase db) { + onCreateTable(db); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (oldVersion != newVersion) { + clearDB(db); + } + } + + @Override + public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (oldVersion != newVersion) { + clearDB(db); + } + } + + private void clearDB(SQLiteDatabase db) { + db.execSQL("DROP TABLE IF EXISTS " + mTableName); + onCreate(db); + } + } +} |