1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 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 * @hide 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 SqliteWrapper()38 private SqliteWrapper() { 39 // Forbidden being instantiated. 40 } 41 42 // FIXME: need to optimize this method. isLowMemory(SQLiteException e)43 private static boolean isLowMemory(SQLiteException e) { 44 return e.getMessage().equals(SQLITE_EXCEPTION_DETAIL_MESSAGE); 45 } 46 47 @UnsupportedAppUsage checkSQLiteException(Context context, SQLiteException e)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 query(Context context, ContentResolver resolver, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)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 requery(Context context, Cursor cursor)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 update(Context context, ContentResolver resolver, Uri uri, ContentValues values, String where, String[] selectionArgs)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 delete(Context context, ContentResolver resolver, Uri uri, String where, String[] selectionArgs)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 insert(Context context, ContentResolver resolver, Uri uri, ContentValues values)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 } 114