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