Home | History | Annotate | Download | only in mmslib
      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.android.messaging.mmslib;
     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 
     27 import com.android.messaging.util.LogUtil;
     28 
     29 // Wrapper around content resolver methods to catch exceptions
     30 public final class SqliteWrapper {
     31     private static final String TAG = LogUtil.BUGLE_TAG;
     32 
     33     private SqliteWrapper() {
     34         // Forbidden being instantiated.
     35     }
     36 
     37     public static Cursor query(Context context, ContentResolver resolver, Uri uri,
     38             String[] projection, String selection, String[] selectionArgs, String sortOrder) {
     39         try {
     40             return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
     41         } catch (SQLiteException e) {
     42             LogUtil.e(TAG, "SqliteWrapper: catch an exception when query", e);
     43             return null;
     44         } catch (IllegalArgumentException e) {
     45             LogUtil.e(TAG, "SqliteWrapper: catch an exception when query", e);
     46             return null;
     47         }
     48     }
     49 
     50     public static int update(Context context, ContentResolver resolver, Uri uri,
     51             ContentValues values, String where, String[] selectionArgs) {
     52         try {
     53             return resolver.update(uri, values, where, selectionArgs);
     54         } catch (SQLiteException e) {
     55             LogUtil.e(TAG, "SqliteWrapper: catch an exception when update", e);
     56             return -1;
     57         } catch (IllegalArgumentException e) {
     58             LogUtil.e(TAG, "SqliteWrapper: catch an exception when update", e);
     59             return -1;
     60         }
     61     }
     62 
     63     public static int delete(Context context, ContentResolver resolver, Uri uri,
     64             String where, String[] selectionArgs) {
     65         try {
     66             return resolver.delete(uri, where, selectionArgs);
     67         } catch (SQLiteException e) {
     68             LogUtil.e(TAG, "SqliteWrapper: catch an exception when delete", e);
     69             return -1;
     70         } catch (IllegalArgumentException e) {
     71             LogUtil.e(TAG, "SqliteWrapper: catch an exception when delete", e);
     72             return -1;
     73         }
     74     }
     75 
     76     public static Uri insert(Context context, ContentResolver resolver,
     77             Uri uri, ContentValues values) {
     78         try {
     79             return resolver.insert(uri, values);
     80         } catch (SQLiteException e) {
     81             LogUtil.e(TAG, "SqliteWrapper: catch an exception when insert", e);
     82             return null;
     83         } catch (IllegalArgumentException e) {
     84             LogUtil.e(TAG, "SqliteWrapper: catch an exception when insert", e);
     85             return null;
     86         }
     87     }
     88 }
     89