Dianne Hackborn | a59a238 | 2016-04-22 17:30:16 -0700 | [diff] [blame] | 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 | |
| 17 | package android.jobscheduler; |
| 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 | * Stub content provider used for generating content change reports |
| 30 | */ |
| 31 | public class DummyJobContentProvider extends ContentProvider { |
| 32 | private static final String DATABASE_NAME = "dummy.db"; |
| 33 | private static final String NAME_VALUE_TABLE = "name_value"; |
| 34 | |
| 35 | private DatabaseHelper mDbHelper; |
| 36 | private static UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 37 | |
| 38 | private static final int MATCH_NAME_VALUE = 1; |
| 39 | |
| 40 | public static final String AUTHORITY = "android.jobscheduler.dummyprovider"; |
| 41 | public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY); |
| 42 | |
| 43 | public static final String _ID = "_id"; |
| 44 | public static final String NAME = "name"; |
| 45 | public static final String VALUE = "value"; |
| 46 | |
| 47 | static { |
| 48 | sMatcher.addURI(AUTHORITY, null, MATCH_NAME_VALUE); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * (non-Javadoc) |
| 53 | * @see android.content.ContentProvider#onCreate() |
| 54 | */ |
| 55 | @Override |
| 56 | public boolean onCreate() { |
| 57 | mDbHelper = new DatabaseHelper(getContext()); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | private class DatabaseHelper extends SQLiteOpenHelper { |
| 62 | private static final int DATABASE_VERSION = 1; |
| 63 | |
| 64 | DatabaseHelper(Context context) { |
| 65 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void onCreate(SQLiteDatabase db) { |
| 70 | // create an empty name_value table |
| 71 | db.execSQL("CREATE TABLE " + NAME_VALUE_TABLE + " (" + _ID + " INTEGER PRIMARY KEY," |
| 72 | + NAME + " TEXT," + VALUE + " TEXT"+ ");"); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * (non-Javadoc) |
| 82 | * @see android.content.ContentProvider#insert(android.net.Uri, |
| 83 | * android.content.ContentValues) |
| 84 | */ |
| 85 | @Override |
| 86 | public Uri insert(Uri uri, ContentValues values) { |
| 87 | String tbName = getTableName(uri); |
| 88 | if (tbName == null) { |
| 89 | return null; |
| 90 | } |
| 91 | SQLiteDatabase db = mDbHelper.getWritableDatabase(); |
| 92 | db.insert(tbName, VALUE, values); |
| 93 | getContext().getContentResolver().notifyChange(uri, null); |
| 94 | return uri; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * (non-Javadoc) |
| 99 | * @see android.content.ContentProvider#query(android.net.Uri, |
| 100 | * java.lang.String[], java.lang.String, java.lang.String[], |
| 101 | * java.lang.String) |
| 102 | */ |
| 103 | @Override |
| 104 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 105 | String sortOrder) { |
| 106 | String tbName = getTableName(uri); |
| 107 | if (tbName == null) { |
| 108 | return null; |
| 109 | } |
| 110 | SQLiteDatabase db = mDbHelper.getReadableDatabase(); |
| 111 | Cursor c = db.query(tbName, projection, selection, selectionArgs, null, null, sortOrder); |
| 112 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 113 | return c; |
| 114 | } |
| 115 | |
| 116 | private String getTableName(Uri uri) { |
| 117 | switch (sMatcher.match(uri)) { |
| 118 | case MATCH_NAME_VALUE: |
| 119 | return NAME_VALUE_TABLE; |
| 120 | default: |
| 121 | throw new UnsupportedOperationException(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * (non-Javadoc) |
| 127 | * @see android.content.ContentProvider#update(android.net.Uri, |
| 128 | * android.content.ContentValues, java.lang.String, java.lang.String[]) |
| 129 | */ |
| 130 | @Override |
| 131 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 132 | String tbName = getTableName(uri); |
| 133 | if (tbName == null) { |
| 134 | return 0; |
| 135 | } |
| 136 | SQLiteDatabase db = mDbHelper.getWritableDatabase(); |
| 137 | int count = db.update(tbName, values, selection, selectionArgs); |
| 138 | getContext().getContentResolver().notifyChange(uri, null); |
| 139 | return count; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * (non-Javadoc) |
| 144 | * @see android.content.ContentProvider#delete(android.net.Uri, |
| 145 | * java.lang.String, java.lang.String[]) |
| 146 | */ |
| 147 | @Override |
| 148 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 149 | String tbName = getTableName(uri); |
| 150 | if (tbName == null) { |
| 151 | return 0; |
| 152 | } |
| 153 | SQLiteDatabase db = mDbHelper.getWritableDatabase(); |
| 154 | int count = db.delete(tbName, selection, selectionArgs); |
| 155 | getContext().getContentResolver().notifyChange(uri, null); |
| 156 | return count; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * (non-Javadoc) |
| 161 | * @see android.content.ContentProvider#getType(android.net.Uri) |
| 162 | */ |
| 163 | @Override |
| 164 | public String getType(Uri uri) { |
| 165 | return null; |
| 166 | } |
| 167 | } |