1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.providers.telephony; 18 19 import android.content.ContentProvider; 20 import android.content.ContentValues; 21 import android.database.Cursor; 22 import android.database.sqlite.SQLiteDatabase; 23 import android.database.sqlite.SQLiteQueryBuilder; 24 import android.net.Uri; 25 import android.util.Log; 26 27 import android.content.ContentUris; 28 import android.database.SQLException; 29 30 import java.util.Arrays; 31 32 /** 33 * The class to provide base facility to access Carrier related content, 34 * which is stored in a SQLite database. 35 */ 36 public class CarrierProvider extends ContentProvider { 37 38 private static final boolean VDBG = false; // STOPSHIP if true 39 private static final String TAG = "CarrierProvider"; 40 41 private CarrierDatabaseHelper mDbHelper; 42 private SQLiteDatabase mDatabase; 43 44 static final String PROVIDER_NAME = "carrier_information"; 45 static final String URL = "content://" + PROVIDER_NAME + "/carrier"; 46 static final Uri CONTENT_URI = Uri.parse(URL); 47 48 @Override onCreate()49 public boolean onCreate() { 50 Log.d(TAG, "onCreate"); 51 mDbHelper = new CarrierDatabaseHelper(getContext()); 52 return (mDatabase == null ? false : true); 53 } 54 55 @Override getType(Uri uri)56 public String getType(Uri uri) { 57 return null; 58 } 59 60 @Override query(Uri uri, String[] projectionIn, String selection, String[] selectionArgs, String sortOrder)61 public Cursor query(Uri uri, String[] projectionIn, String selection, 62 String[] selectionArgs, String sortOrder) { 63 if (VDBG) { 64 Log.d(TAG, "query:" 65 + " uri=" + uri 66 + " values=" + Arrays.toString(projectionIn) 67 + " selection=" + selection 68 + " selectionArgs=" + Arrays.toString(selectionArgs)); 69 } 70 SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 71 qb.setTables(CarrierDatabaseHelper.CARRIER_KEY_TABLE); 72 73 SQLiteDatabase db = getReadableDatabase(); 74 Cursor c = qb.query(db, projectionIn, selection, selectionArgs, null, null, sortOrder); 75 return c; 76 } 77 78 @Override insert(Uri uri, ContentValues values)79 public Uri insert(Uri uri, ContentValues values) { 80 values.put(CarrierDatabaseHelper.LAST_MODIFIED, System.currentTimeMillis()); 81 long row = getWritableDatabase().insertOrThrow(CarrierDatabaseHelper.CARRIER_KEY_TABLE, 82 null, values); 83 if (row > 0) { 84 Uri newUri = ContentUris.withAppendedId(CONTENT_URI, row); 85 getContext().getContentResolver().notifyChange(CONTENT_URI, null); 86 return newUri; 87 } 88 return null; 89 } 90 91 @Override delete(Uri uri, String selection, String[] selectionArgs)92 public int delete(Uri uri, String selection, String[] selectionArgs) { 93 if (VDBG) { 94 Log.d(TAG, "delete:" 95 + " uri=" + uri 96 + " selection={" + selection + "}" 97 + " selection=" + selection 98 + " selectionArgs=" + Arrays.toString(selectionArgs)); 99 } 100 final int count = getWritableDatabase().delete(CarrierDatabaseHelper.CARRIER_KEY_TABLE, 101 selection, selectionArgs); 102 Log.d(TAG, " delete.count=" + count); 103 return count; 104 } 105 106 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)107 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 108 values.put(CarrierDatabaseHelper.LAST_MODIFIED, System.currentTimeMillis()); 109 if (VDBG) { 110 Log.d(TAG, "update:" 111 + " uri=" + uri 112 + " values={" + values + "}" 113 + " selection=" + selection 114 + " selectionArgs=" + Arrays.toString(selectionArgs)); 115 } 116 final int count = getWritableDatabase().update(CarrierDatabaseHelper.CARRIER_KEY_TABLE, 117 values, selection, selectionArgs); 118 if (count > 0) { 119 getContext().getContentResolver().notifyChange(CONTENT_URI, null); 120 } 121 Log.d(TAG, " update.count=" + count); 122 return count; 123 } 124 125 /** 126 * These methods can be overridden in a subclass for testing TelephonyProvider using an 127 * in-memory database. 128 */ getReadableDatabase()129 SQLiteDatabase getReadableDatabase() { 130 return mDbHelper.getReadableDatabase(); 131 } getWritableDatabase()132 SQLiteDatabase getWritableDatabase() { 133 return mDbHelper.getWritableDatabase(); 134 } 135 } 136