1526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen/*
2526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * Copyright (C) 2008 Esmertec AG.
3526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * Copyright (C) 2008 The Android Open Source Project
4526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen *
5526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * Licensed under the Apache License, Version 2.0 (the "License");
6526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * you may not use this file except in compliance with the License.
7526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * You may obtain a copy of the License at
8526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen *
9526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen *      http://www.apache.org/licenses/LICENSE-2.0
10526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen *
11526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * Unless required by applicable law or agreed to in writing, software
12526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * distributed under the License is distributed on an "AS IS" BASIS,
13526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * See the License for the specific language governing permissions and
15526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen * limitations under the License.
16526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen */
17526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
18526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenpackage com.google.android.mms.util;
19526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
20526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.app.ActivityManager;
21526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.content.ContentResolver;
22526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.content.ContentValues;
23526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.content.Context;
24526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.database.Cursor;
25526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.database.sqlite.SQLiteException;
26526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.net.Uri;
27526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.util.Log;
28526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenimport android.widget.Toast;
29526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
30526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wenpublic final class SqliteWrapper {
31526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    private static final String TAG = "SqliteWrapper";
32526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    private static final String SQLITE_EXCEPTION_DETAIL_MESSAGE
33526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen                = "unable to open database file";
34526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
35526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    private SqliteWrapper() {
36526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        // Forbidden being instantiated.
37526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
38526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
39526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    // FIXME: It looks like outInfo.lowMemory does not work well as we expected.
40526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    // after run command: adb shell fillup -p 100, outInfo.lowMemory is still false.
41526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    private static boolean isLowMemory(Context context) {
42526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        if (null == context) {
43526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return false;
44526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        }
45526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
46526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        ActivityManager am = (ActivityManager)
47526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen                        context.getSystemService(Context.ACTIVITY_SERVICE);
48526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
49526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        am.getMemoryInfo(outInfo);
50526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
51526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        return outInfo.lowMemory;
52526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
53526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
54526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    // FIXME: need to optimize this method.
55526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    private static boolean isLowMemory(SQLiteException e) {
56526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        return e.getMessage().equals(SQLITE_EXCEPTION_DETAIL_MESSAGE);
57526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
58526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
59526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    public static void checkSQLiteException(Context context, SQLiteException e) {
60526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        if (isLowMemory(e)) {
61526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            Toast.makeText(context, com.android.internal.R.string.low_memory,
62526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen                    Toast.LENGTH_SHORT).show();
63526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        } else {
64526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            throw e;
65526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        }
66526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
67526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
68526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    public static Cursor query(Context context, ContentResolver resolver, Uri uri,
69526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            String[] projection, String selection, String[] selectionArgs, String sortOrder) {
70526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        try {
71526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
72526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        } catch (SQLiteException e) {
73526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            Log.e(TAG, "Catch a SQLiteException when query: ", e);
74526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            checkSQLiteException(context, e);
75526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return null;
76526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        }
77526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
78526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
79526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    public static boolean requery(Context context, Cursor cursor) {
80526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        try {
81526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return cursor.requery();
82526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        } catch (SQLiteException e) {
83526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            Log.e(TAG, "Catch a SQLiteException when requery: ", e);
84526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            checkSQLiteException(context, e);
85526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return false;
86526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        }
87526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
88526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    public static int update(Context context, ContentResolver resolver, Uri uri,
89526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            ContentValues values, String where, String[] selectionArgs) {
90526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        try {
91526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return resolver.update(uri, values, where, selectionArgs);
92526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        } catch (SQLiteException e) {
93526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            Log.e(TAG, "Catch a SQLiteException when update: ", e);
94526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            checkSQLiteException(context, e);
95526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return -1;
96526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        }
97526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
98526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
99526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    public static int delete(Context context, ContentResolver resolver, Uri uri,
100526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            String where, String[] selectionArgs) {
101526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        try {
102526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return resolver.delete(uri, where, selectionArgs);
103526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        } catch (SQLiteException e) {
104526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            Log.e(TAG, "Catch a SQLiteException when delete: ", e);
105526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            checkSQLiteException(context, e);
106526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return -1;
107526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        }
108526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
109526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen
110526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    public static Uri insert(Context context, ContentResolver resolver,
111526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            Uri uri, ContentValues values) {
112526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        try {
113526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return resolver.insert(uri, values);
114526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        } catch (SQLiteException e) {
115526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            Log.e(TAG, "Catch a SQLiteException when insert: ", e);
116526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            checkSQLiteException(context, e);
117526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen            return null;
118526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen        }
119526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen    }
120526ecd1799a2fc467cfce114eae3578b42ccb786Ye Wen}
121