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 com.google.android.mms.util; 19 20 import android.app.ActivityManager; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.content.ContentResolver; 23 import android.content.ContentValues; 24 import android.content.Context; 25 import android.database.Cursor; 26 import android.database.sqlite.SQLiteException; 27 import android.net.Uri; 28 import android.util.Log; 29 import android.widget.Toast; 30 31 public final class SqliteWrapper { 32 private static final String TAG = "SqliteWrapper"; 33 private static final String SQLITE_EXCEPTION_DETAIL_MESSAGE 34 = "unable to open database file"; 35 SqliteWrapper()36 private SqliteWrapper() { 37 // Forbidden being instantiated. 38 } 39 40 // FIXME: It looks like outInfo.lowMemory does not work well as we expected. 41 // after run command: adb shell fillup -p 100, outInfo.lowMemory is still false. isLowMemory(Context context)42 private static boolean isLowMemory(Context context) { 43 if (null == context) { 44 return false; 45 } 46 47 ActivityManager am = (ActivityManager) 48 context.getSystemService(Context.ACTIVITY_SERVICE); 49 ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo(); 50 am.getMemoryInfo(outInfo); 51 52 return outInfo.lowMemory; 53 } 54 55 // FIXME: need to optimize this method. isLowMemory(SQLiteException e)56 private static boolean isLowMemory(SQLiteException e) { 57 return e.getMessage().equals(SQLITE_EXCEPTION_DETAIL_MESSAGE); 58 } 59 60 @UnsupportedAppUsage checkSQLiteException(Context context, SQLiteException e)61 public static void checkSQLiteException(Context context, SQLiteException e) { 62 if (isLowMemory(e)) { 63 Toast.makeText(context, com.android.internal.R.string.low_memory, 64 Toast.LENGTH_SHORT).show(); 65 } else { 66 throw e; 67 } 68 } 69 70 @UnsupportedAppUsage query(Context context, ContentResolver resolver, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)71 public static Cursor query(Context context, ContentResolver resolver, Uri uri, 72 String[] projection, String selection, String[] selectionArgs, String sortOrder) { 73 try { 74 return resolver.query(uri, projection, selection, selectionArgs, sortOrder); 75 } catch (SQLiteException e) { 76 Log.e(TAG, "Catch a SQLiteException when query: ", e); 77 checkSQLiteException(context, e); 78 return null; 79 } 80 } 81 82 @UnsupportedAppUsage requery(Context context, Cursor cursor)83 public static boolean requery(Context context, Cursor cursor) { 84 try { 85 return cursor.requery(); 86 } catch (SQLiteException e) { 87 Log.e(TAG, "Catch a SQLiteException when requery: ", e); 88 checkSQLiteException(context, e); 89 return false; 90 } 91 } 92 @UnsupportedAppUsage update(Context context, ContentResolver resolver, Uri uri, ContentValues values, String where, String[] selectionArgs)93 public static int update(Context context, ContentResolver resolver, Uri uri, 94 ContentValues values, String where, String[] selectionArgs) { 95 try { 96 return resolver.update(uri, values, where, selectionArgs); 97 } catch (SQLiteException e) { 98 Log.e(TAG, "Catch a SQLiteException when update: ", e); 99 checkSQLiteException(context, e); 100 return -1; 101 } 102 } 103 104 @UnsupportedAppUsage delete(Context context, ContentResolver resolver, Uri uri, String where, String[] selectionArgs)105 public static int delete(Context context, ContentResolver resolver, Uri uri, 106 String where, String[] selectionArgs) { 107 try { 108 return resolver.delete(uri, where, selectionArgs); 109 } catch (SQLiteException e) { 110 Log.e(TAG, "Catch a SQLiteException when delete: ", e); 111 checkSQLiteException(context, e); 112 return -1; 113 } 114 } 115 116 @UnsupportedAppUsage insert(Context context, ContentResolver resolver, Uri uri, ContentValues values)117 public static Uri insert(Context context, ContentResolver resolver, 118 Uri uri, ContentValues values) { 119 try { 120 return resolver.insert(uri, values); 121 } catch (SQLiteException e) { 122 Log.e(TAG, "Catch a SQLiteException when insert: ", e); 123 checkSQLiteException(context, e); 124 return null; 125 } 126 } 127 } 128