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