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.content.ContentResolver;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.database.sqlite.SQLiteException;
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 
checkSQLiteException(Context context, SQLiteException e)47     public static void checkSQLiteException(Context context, SQLiteException e) {
48         if (isLowMemory(e)) {
49             Toast.makeText(context, com.android.internal.R.string.low_memory,
50                     Toast.LENGTH_SHORT).show();
51         } else {
52             throw e;
53         }
54     }
55 
query(Context context, ContentResolver resolver, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)56     public static Cursor query(Context context, ContentResolver resolver, Uri uri,
57             String[] projection, String selection, String[] selectionArgs, String sortOrder) {
58         try {
59             return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
60         } catch (SQLiteException e) {
61             Log.e(TAG, "Catch a SQLiteException when query: ", e);
62             checkSQLiteException(context, e);
63             return null;
64         }
65     }
66 
requery(Context context, Cursor cursor)67     public static boolean requery(Context context, Cursor cursor) {
68         try {
69             return cursor.requery();
70         } catch (SQLiteException e) {
71             Log.e(TAG, "Catch a SQLiteException when requery: ", e);
72             checkSQLiteException(context, e);
73             return false;
74         }
75     }
update(Context context, ContentResolver resolver, Uri uri, ContentValues values, String where, String[] selectionArgs)76     public static int update(Context context, ContentResolver resolver, Uri uri,
77             ContentValues values, String where, String[] selectionArgs) {
78         try {
79             return resolver.update(uri, values, where, selectionArgs);
80         } catch (SQLiteException e) {
81             Log.e(TAG, "Catch a SQLiteException when update: ", e);
82             checkSQLiteException(context, e);
83             return -1;
84         }
85     }
86 
delete(Context context, ContentResolver resolver, Uri uri, String where, String[] selectionArgs)87     public static int delete(Context context, ContentResolver resolver, Uri uri,
88             String where, String[] selectionArgs) {
89         try {
90             return resolver.delete(uri, where, selectionArgs);
91         } catch (SQLiteException e) {
92             Log.e(TAG, "Catch a SQLiteException when delete: ", e);
93             checkSQLiteException(context, e);
94             return -1;
95         }
96     }
97 
insert(Context context, ContentResolver resolver, Uri uri, ContentValues values)98     public static Uri insert(Context context, ContentResolver resolver,
99             Uri uri, ContentValues values) {
100         try {
101             return resolver.insert(uri, values);
102         } catch (SQLiteException e) {
103             Log.e(TAG, "Catch a SQLiteException when insert: ", e);
104             checkSQLiteException(context, e);
105             return null;
106         }
107     }
108 }
109