blob: b019618a0f4952f0be6aa6e582ad88c5c82683ba [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Tom Taylor5e342fa2010-01-28 15:54:15 -080018package android.database.sqlite;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.ContentValues;
22import android.content.Context;
23import android.database.Cursor;
24import android.database.sqlite.SQLiteException;
25import android.net.Uri;
26import android.util.Log;
27import android.widget.Toast;
28
Tom Taylor5e342fa2010-01-28 15:54:15 -080029/**
30 * @hide
31 */
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033public 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 // Forbidden being instantiated.
40 }
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 // FIXME: need to optimize this method.
43 private static boolean isLowMemory(SQLiteException e) {
44 return e.getMessage().equals(SQLITE_EXCEPTION_DETAIL_MESSAGE);
45 }
46
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
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
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 }
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
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
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}