| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package android.database.sqlite; |
| 19 |
|
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.ContentResolver; |
| 22 |
import android.content.ContentValues; |
| 23 |
import android.content.Context; |
| 24 |
import android.database.Cursor; |
| 25 |
import android.net.Uri; |
| 26 |
import android.util.Log; |
| 27 |
import android.widget.Toast; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public final class SqliteWrapper { |
| 34 |
private static final String TAG = "SqliteWrapper"; |
| 35 |
private static final String SQLITE_EXCEPTION_DETAIL_MESSAGE |
| 36 |
= "unable to open database file"; |
| 37 |
|
| 38 |
private SqliteWrapper() { |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
private static boolean isLowMemory(SQLiteException e) { |
| 44 |
return e.getMessage().equals(SQLITE_EXCEPTION_DETAIL_MESSAGE); |
| 45 |
} |
| 46 |
|
| 47 |
@UnsupportedAppUsage |
| 48 |
public static void checkSQLiteException(Context context, SQLiteException e) { |
| 49 |
if (isLowMemory(e)) { |
| 50 |
Toast.makeText(context, com.android.internal.R.string.low_memory, |
| 51 |
Toast.LENGTH_SHORT).show(); |
| 52 |
} else { |
| 53 |
throw e; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
@UnsupportedAppUsage |
| 58 |
public static Cursor query(Context context, ContentResolver resolver, Uri uri, |
| 59 |
String[] projection, String selection, String[] selectionArgs, String sortOrder) { |
| 60 |
try { |
| 61 |
return resolver.query(uri, projection, selection, selectionArgs, sortOrder); |
| 62 |
} catch (SQLiteException e) { |
| 63 |
Log.e(TAG, "Catch a SQLiteException when query: ", e); |
| 64 |
checkSQLiteException(context, e); |
| 65 |
return null; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public static boolean requery(Context context, Cursor cursor) { |
| 70 |
try { |
| 71 |
return cursor.requery(); |
| 72 |
} catch (SQLiteException e) { |
| 73 |
Log.e(TAG, "Catch a SQLiteException when requery: ", e); |
| 74 |
checkSQLiteException(context, e); |
| 75 |
return false; |
| 76 |
} |
| 77 |
} |
| 78 |
@UnsupportedAppUsage |
| 79 |
public static int update(Context context, ContentResolver resolver, Uri uri, |
| 80 |
ContentValues values, String where, String[] selectionArgs) { |
| 81 |
try { |
| 82 |
return resolver.update(uri, values, where, selectionArgs); |
| 83 |
} catch (SQLiteException e) { |
| 84 |
Log.e(TAG, "Catch a SQLiteException when update: ", e); |
| 85 |
checkSQLiteException(context, e); |
| 86 |
return -1; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
@UnsupportedAppUsage |
| 91 |
public static int delete(Context context, ContentResolver resolver, Uri uri, |
| 92 |
String where, String[] selectionArgs) { |
| 93 |
try { |
| 94 |
return resolver.delete(uri, where, selectionArgs); |
| 95 |
} catch (SQLiteException e) { |
| 96 |
Log.e(TAG, "Catch a SQLiteException when delete: ", e); |
| 97 |
checkSQLiteException(context, e); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
@UnsupportedAppUsage |
| 103 |
public static Uri insert(Context context, ContentResolver resolver, |
| 104 |
Uri uri, ContentValues values) { |
| 105 |
try { |
| 106 |
return resolver.insert(uri, values); |
| 107 |
} catch (SQLiteException e) { |
| 108 |
Log.e(TAG, "Catch a SQLiteException when insert: ", e); |
| 109 |
checkSQLiteException(context, e); |
| 110 |
return null; |
| 111 |
} |
| 112 |
} |
| 113 |
} |