1/* 2 * Copyright (C) 2016 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 17package com.android.internal.telephony; 18 19import android.annotation.NonNull; 20import android.annotation.Nullable; 21import android.content.ContentValues; 22import android.content.UriMatcher; 23import android.database.Cursor; 24import android.database.sqlite.SQLiteDatabase; 25import android.database.sqlite.SQLiteOpenHelper; 26import android.net.Uri; 27import android.telephony.SubscriptionManager; 28import android.test.mock.MockContentProvider; 29 30public class FakeSmsContentProvider extends MockContentProvider { 31 private static final String RAW_TABLE_NAME = "raw"; 32 public SQLiteOpenHelper mDbHelper = new InMemorySmsDbHelper(); 33 34 private static final UriMatcher sURLMatcher = 35 new UriMatcher(UriMatcher.NO_MATCH); 36 private static final int SMS_RAW_MESSAGE = 1; 37 private static final int SMS_RAW_MESSAGE_PERMANENT_DELETE = 2; 38 static { 39 sURLMatcher.addURI("sms", "raw", SMS_RAW_MESSAGE); 40 sURLMatcher.addURI("sms", "raw/permanentDelete", SMS_RAW_MESSAGE_PERMANENT_DELETE); 41 } 42 43 private class InMemorySmsDbHelper extends SQLiteOpenHelper { 44 public InMemorySmsDbHelper() { 45 super(getContext(), 46 null, //db file name - null for in-memory db 47 null, //CursorFactory - null for default 48 1); //db version - no-op for tests 49 } 50 51 @Override 52 public void onCreate(SQLiteDatabase db) { 53 db.execSQL("CREATE TABLE raw (" + 54 "_id INTEGER PRIMARY KEY," + 55 "date INTEGER," + 56 "reference_number INTEGER," + // one per full message 57 "count INTEGER," + // the number of parts 58 "sequence INTEGER," + // the part number of this message 59 "destination_port INTEGER," + 60 "address TEXT," + 61 "sub_id INTEGER DEFAULT " + 62 SubscriptionManager.INVALID_SUBSCRIPTION_ID + ", " + 63 "pdu TEXT," + // the raw PDU for this part 64 "deleted INTEGER DEFAULT 0," + // bool to indicate if row is deleted 65 "message_body TEXT," + // message body 66 "display_originating_addr TEXT);");// display address 67 } 68 69 @Override 70 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 71 } 72 } 73 74 @Override 75 public Cursor query(@NonNull Uri uri, @Nullable String[] projection, 76 @Nullable String selection, @Nullable String[] selectionArgs, 77 @Nullable String sortOrder) { 78 SQLiteDatabase db = mDbHelper.getReadableDatabase(); 79 return db.query(RAW_TABLE_NAME, projection, selection, selectionArgs, null, null, 80 sortOrder); 81 } 82 83 @Override 84 public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { 85 SQLiteDatabase db = mDbHelper.getWritableDatabase(); 86 long rowId = db.insert(RAW_TABLE_NAME, null, values); 87 return Uri.parse("content://raw/" + rowId); 88 } 89 90 @Override 91 public int delete(@NonNull Uri uri, @Nullable String selection, 92 @Nullable String[] selectionArgs) { 93 SQLiteDatabase db = mDbHelper.getWritableDatabase(); 94 int match = sURLMatcher.match(uri); 95 int count = 0; 96 switch (match) { 97 case SMS_RAW_MESSAGE: 98 ContentValues cv = new ContentValues(); 99 cv.put("deleted", 1); 100 count = db.update(RAW_TABLE_NAME, cv, selection, selectionArgs); 101 break; 102 103 case SMS_RAW_MESSAGE_PERMANENT_DELETE: 104 count = db.delete(RAW_TABLE_NAME, selection, selectionArgs); 105 break; 106 } 107 return count; 108 } 109 110 @Override 111 public void shutdown() { 112 mDbHelper.close(); 113 } 114 115 public int getNumRows() { 116 int numRows = 0; 117 Cursor c = query(null, null, null, null, null); 118 if (c != null) { 119 numRows = c.getCount(); 120 c.close(); 121 } 122 return numRows; 123 } 124}