1 /* 2 * Copyright (C) 2009 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 android.content.cts; 18 19 import android.content.ContentProvider; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.content.UriMatcher; 23 import android.database.Cursor; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.database.sqlite.SQLiteOpenHelper; 26 import android.net.Uri; 27 28 /** 29 * A simple content provider for AsyncQueryHandlerTest and ContentQueryMapTest. 30 * 31 * @see AsyncQueryHandlerTest 32 * @see ContentQueryMapTest 33 */ 34 public class DummyProvider extends ContentProvider { 35 private static final String MOCK_OPERATION = "mockOperation"; 36 37 private static final String DATABASE_NAME = "dummy.db"; 38 private static final String NAME_VALUE_TABLE = "name_value"; 39 40 private DatabaseHelper mDbHelper; 41 private static UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH); 42 43 private static final int MATCH_NAME_VALUE = 1; 44 private static final int MATCH_MOCK_OPERATION = 2; 45 46 public static final long MOCK_OPERATION_SLEEP_TIME = 2000; 47 48 public static final String AUTHORITY = "android.content.cts.dummyprovider"; 49 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY); 50 public static final Uri CONTENT_URI_MOCK_OPERATION = Uri.withAppendedPath(CONTENT_URI, 51 MOCK_OPERATION); 52 53 public static final String _ID = "_id"; 54 public static final String NAME = "name"; 55 public static final String VALUE = "value"; 56 57 static { 58 sMatcher.addURI(AUTHORITY, null, MATCH_NAME_VALUE); 59 sMatcher.addURI(AUTHORITY, MOCK_OPERATION, MATCH_MOCK_OPERATION); 60 } 61 62 /* 63 * (non-Javadoc) 64 * @see android.content.ContentProvider#onCreate() 65 */ 66 @Override 67 public boolean onCreate() { 68 mDbHelper = new DatabaseHelper(getContext()); 69 return true; 70 } 71 72 private class DatabaseHelper extends SQLiteOpenHelper { 73 private static final int DATABASE_VERSION = 1; 74 75 DatabaseHelper(Context context) { 76 super(context, DATABASE_NAME, null, DATABASE_VERSION); 77 } 78 79 @Override 80 public void onCreate(SQLiteDatabase db) { 81 // create an empty name_value table 82 db.execSQL("CREATE TABLE " + NAME_VALUE_TABLE + " (" + _ID + " INTEGER PRIMARY KEY," 83 + NAME + " TEXT," + VALUE + " TEXT"+ ");"); 84 } 85 86 @Override 87 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 88 } 89 } 90 91 /* 92 * (non-Javadoc) 93 * @see android.content.ContentProvider#insert(android.net.Uri, 94 * android.content.ContentValues) 95 */ 96 @Override 97 public Uri insert(Uri uri, ContentValues values) { 98 String tbName = getTableName(uri); 99 if (tbName == null) { 100 return null; 101 } 102 SQLiteDatabase db = mDbHelper.getWritableDatabase(); 103 db.insert(tbName, VALUE, values); 104 getContext().getContentResolver().notifyChange(uri, null); 105 return uri; 106 } 107 108 /* 109 * (non-Javadoc) 110 * @see android.content.ContentProvider#query(android.net.Uri, 111 * java.lang.String[], java.lang.String, java.lang.String[], 112 * java.lang.String) 113 */ 114 @Override 115 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 116 String sortOrder) { 117 String tbName = getTableName(uri); 118 if (tbName == null) { 119 return null; 120 } 121 SQLiteDatabase db = mDbHelper.getReadableDatabase(); 122 Cursor c = db.query(tbName, projection, selection, selectionArgs, null, null, sortOrder); 123 c.setNotificationUri(getContext().getContentResolver(), uri); 124 return c; 125 } 126 127 private String getTableName(Uri uri) { 128 switch (sMatcher.match(uri)) { 129 case MATCH_NAME_VALUE: 130 return NAME_VALUE_TABLE; 131 case MATCH_MOCK_OPERATION: 132 doMockOperation(); 133 return null; 134 default: 135 throw new UnsupportedOperationException(); 136 } 137 } 138 139 /** 140 * Simulate a long-running DB operation by sleeping. 141 */ 142 private void doMockOperation() { 143 long time = System.currentTimeMillis(); 144 long target = time + MOCK_OPERATION_SLEEP_TIME; 145 while (time < target) { 146 try { 147 Thread.sleep(target - time); 148 } catch (InterruptedException ignored) { 149 // ignore and continue sleeping, since a true long-running DB operation would not 150 // be affected by interrupt() either 151 } 152 time = System.currentTimeMillis(); 153 } 154 } 155 156 /* 157 * (non-Javadoc) 158 * @see android.content.ContentProvider#update(android.net.Uri, 159 * android.content.ContentValues, java.lang.String, java.lang.String[]) 160 */ 161 @Override 162 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 163 String tbName = getTableName(uri); 164 if (tbName == null) { 165 return 0; 166 } 167 SQLiteDatabase db = mDbHelper.getWritableDatabase(); 168 int count = db.update(tbName, values, selection, selectionArgs); 169 getContext().getContentResolver().notifyChange(uri, null); 170 return count; 171 } 172 173 /* 174 * (non-Javadoc) 175 * @see android.content.ContentProvider#delete(android.net.Uri, 176 * java.lang.String, java.lang.String[]) 177 */ 178 @Override 179 public int delete(Uri uri, String selection, String[] selectionArgs) { 180 String tbName = getTableName(uri); 181 if (tbName == null) { 182 return 0; 183 } 184 SQLiteDatabase db = mDbHelper.getWritableDatabase(); 185 int count = db.delete(tbName, selection, selectionArgs); 186 getContext().getContentResolver().notifyChange(uri, null); 187 return count; 188 } 189 190 /* 191 * (non-Javadoc) 192 * @see android.content.ContentProvider#getType(android.net.Uri) 193 */ 194 @Override 195 public String getType(Uri uri) { 196 return null; 197 } 198 } 199