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