1 /* 2 * Copyright (C) 2007 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.app.activity; 18 19 import android.content.UriMatcher; 20 import android.content.*; 21 import android.database.Cursor; 22 import android.database.sqlite.SQLiteDatabase; 23 import android.database.sqlite.SQLiteOpenHelper; 24 import android.database.sqlite.SQLiteQueryBuilder; 25 import android.net.Uri; 26 import android.util.Log; 27 28 /** Simple test provider that runs in the local process. */ 29 public class LocalProvider extends ContentProvider { 30 private static final String TAG = "LocalProvider"; 31 32 private static final String AUTHORITY = "com.android.frameworks.coretests.LocalProvider"; 33 private static final String TABLE_DATA_NAME = "data"; 34 public static final Uri TABLE_DATA_URI = 35 Uri.parse("content://" + AUTHORITY + "/" + TABLE_DATA_NAME); 36 37 public static final String COLUMN_TEXT_NAME = "text"; 38 public static final String COLUMN_INTEGER_NAME = "integer"; 39 40 public static final String TEXT1 = "first data"; 41 public static final String TEXT2 = "second data"; 42 public static final int INTEGER1 = 100; 43 public static final int INTEGER2 = 101; 44 45 private SQLiteOpenHelper mOpenHelper; 46 47 private static final int DATA = 1; 48 private static final int DATA_ID = 2; 49 private static final UriMatcher sURLMatcher = new UriMatcher( 50 UriMatcher.NO_MATCH); 51 52 static { 53 sURLMatcher.addURI("*", "data", DATA); 54 sURLMatcher.addURI("*", "data/#", DATA_ID); 55 } 56 57 private static class DatabaseHelper extends SQLiteOpenHelper { 58 private static final String DATABASE_NAME = "local.db"; 59 private static final int DATABASE_VERSION = 1; 60 DatabaseHelper(Context context)61 public DatabaseHelper(Context context) { 62 super(context, DATABASE_NAME, null, DATABASE_VERSION); 63 } 64 65 @Override onCreate(SQLiteDatabase db)66 public void onCreate(SQLiteDatabase db) { 67 db.execSQL("CREATE TABLE " + TABLE_DATA_NAME + " (" + 68 "_id INTEGER PRIMARY KEY," + 69 COLUMN_TEXT_NAME + " TEXT, " + 70 COLUMN_INTEGER_NAME + " INTEGER);"); 71 72 // insert alarms 73 db.execSQL(getInsertCommand(TEXT1, INTEGER1)); 74 db.execSQL(getInsertCommand(TEXT2, INTEGER2)); 75 } 76 getInsertCommand(String textValue, int integerValue)77 private String getInsertCommand(String textValue, int integerValue) { 78 return "INSERT INTO " + TABLE_DATA_NAME 79 + " (" + COLUMN_TEXT_NAME + ", " + COLUMN_INTEGER_NAME + ") " 80 + "VALUES ('" + textValue + "', " + integerValue + ");"; 81 } 82 83 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion)84 public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) { 85 Log.w(TAG, "Upgrading test database from version " + 86 oldVersion + " to " + currentVersion + 87 ", which will destroy all old data"); 88 db.execSQL("DROP TABLE IF EXISTS data"); 89 onCreate(db); 90 } 91 } 92 93 LocalProvider()94 public LocalProvider() { 95 } 96 getTableDataUriForRow(int rowId)97 static public Uri getTableDataUriForRow(int rowId) { 98 return Uri.parse("content://" + AUTHORITY + "/" + TABLE_DATA_NAME + "/" + rowId); 99 } 100 101 @Override onCreate()102 public boolean onCreate() { 103 mOpenHelper = new DatabaseHelper(getContext()); 104 return true; 105 } 106 107 @Override query(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sort)108 public Cursor query(Uri url, String[] projectionIn, String selection, 109 String[] selectionArgs, String sort) { 110 SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 111 112 // Generate the body of the query 113 int match = sURLMatcher.match(url); 114 switch (match) { 115 case DATA: 116 qb.setTables("data"); 117 break; 118 case DATA_ID: 119 qb.setTables("data"); 120 qb.appendWhere("_id="); 121 qb.appendWhere(url.getPathSegments().get(1)); 122 break; 123 default: 124 throw new IllegalArgumentException("Unknown URL " + url); 125 } 126 127 SQLiteDatabase db = mOpenHelper.getReadableDatabase(); 128 Cursor ret = qb.query(db, projectionIn, selection, selectionArgs, 129 null, null, sort); 130 131 if (ret == null) { 132 if (false) Log.d(TAG, "Alarms.query: failed"); 133 } else { 134 ret.setNotificationUri(getContext().getContentResolver(), url); 135 } 136 137 return ret; 138 } 139 140 @Override getType(Uri url)141 public String getType(Uri url) { 142 int match = sURLMatcher.match(url); 143 switch (match) { 144 case DATA: 145 return "vnd.android.cursor.dir/vnd.google.unit_tests.local"; 146 case DATA_ID: 147 return "vnd.android.cursor.item/vnd.google.unit_tests.local"; 148 default: 149 throw new IllegalArgumentException("Unknown URL"); 150 } 151 } 152 153 @Override update(Uri url, ContentValues values, String where, String[] whereArgs)154 public int update(Uri url, ContentValues values, String where, String[] whereArgs) { 155 int count; 156 long rowId = 0; 157 int match = sURLMatcher.match(url); 158 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 159 switch (match) { 160 case DATA_ID: { 161 String segment = url.getPathSegments().get(1); 162 rowId = Long.parseLong(segment); 163 count = db.update("data", values, "_id=" + rowId, null); 164 break; 165 } 166 default: { 167 throw new UnsupportedOperationException( 168 "Cannot update URL: " + url); 169 } 170 } 171 if (false) Log.d(TAG, "*** notifyChange() rowId: " + rowId); 172 getContext().getContentResolver().notifyChange(url, null); 173 return count; 174 } 175 176 177 @Override insert(Uri url, ContentValues initialValues)178 public Uri insert(Uri url, ContentValues initialValues) { 179 return null; 180 } 181 182 @Override delete(Uri url, String where, String[] whereArgs)183 public int delete(Uri url, String where, String[] whereArgs) { 184 throw new UnsupportedOperationException("delete not supported"); 185 } 186 } 187