Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 com.android.documentsui; |
| 18 | |
| 19 | import android.content.ContentProvider; |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 21 | import android.content.ContentValues; |
| 22 | import android.content.Context; |
| 23 | import android.content.UriMatcher; |
| 24 | import android.database.Cursor; |
| 25 | import android.database.sqlite.SQLiteDatabase; |
| 26 | import android.database.sqlite.SQLiteOpenHelper; |
| 27 | import android.net.Uri; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 28 | import android.provider.DocumentsContract.Document; |
| 29 | import android.provider.DocumentsContract.Root; |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 30 | import android.text.format.DateUtils; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | public class RecentsProvider extends ContentProvider { |
| 34 | private static final String TAG = "RecentsProvider"; |
| 35 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 36 | public static final long MAX_HISTORY_IN_MILLIS = DateUtils.DAY_IN_MILLIS * 45; |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 37 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 38 | private static final String AUTHORITY = "com.android.documentsui.recents"; |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 39 | |
| 40 | private static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 41 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 42 | private static final int URI_RECENT = 1; |
| 43 | private static final int URI_STATE = 2; |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 44 | private static final int URI_RESUME = 3; |
| 45 | |
| 46 | static { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 47 | sMatcher.addURI(AUTHORITY, "recent", URI_RECENT); |
| 48 | // state/authority/rootId/docId |
| 49 | sMatcher.addURI(AUTHORITY, "state/*/*/*", URI_STATE); |
| 50 | // resume/packageName |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 51 | sMatcher.addURI(AUTHORITY, "resume/*", URI_RESUME); |
| 52 | } |
| 53 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 54 | public static final String TABLE_RECENT = "recent"; |
| 55 | public static final String TABLE_STATE = "state"; |
| 56 | public static final String TABLE_RESUME = "resume"; |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 57 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 58 | public static class RecentColumns { |
| 59 | public static final String STACK = "stack"; |
| 60 | public static final String TIMESTAMP = "timestamp"; |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 63 | public static class StateColumns { |
| 64 | public static final String AUTHORITY = "authority"; |
| 65 | public static final String ROOT_ID = Root.COLUMN_ROOT_ID; |
| 66 | public static final String DOCUMENT_ID = Document.COLUMN_DOCUMENT_ID; |
| 67 | public static final String MODE = "mode"; |
| 68 | public static final String SORT_ORDER = "sortOrder"; |
| 69 | } |
| 70 | |
| 71 | public static class ResumeColumns { |
| 72 | public static final String PACKAGE_NAME = "package_name"; |
| 73 | public static final String STACK = "stack"; |
| 74 | public static final String TIMESTAMP = "timestamp"; |
| 75 | } |
| 76 | |
| 77 | public static Uri buildRecent() { |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 78 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 79 | .authority(AUTHORITY).appendPath("recent").build(); |
| 80 | } |
| 81 | |
| 82 | public static Uri buildState(String authority, String rootId, String documentId) { |
| 83 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(AUTHORITY) |
| 84 | .appendPath("state").appendPath(authority).appendPath(rootId).appendPath(documentId) |
| 85 | .build(); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | public static Uri buildResume(String packageName) { |
| 89 | return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 90 | .authority(AUTHORITY).appendPath("resume").appendPath(packageName).build(); |
| 91 | } |
| 92 | |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 93 | private DatabaseHelper mHelper; |
| 94 | |
| 95 | private static class DatabaseHelper extends SQLiteOpenHelper { |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 96 | private static final String DB_NAME = "recents.db"; |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 97 | |
| 98 | private static final int VERSION_INIT = 1; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 99 | private static final int VERSION_AS_BLOB = 3; |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 100 | |
| 101 | public DatabaseHelper(Context context) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 102 | super(context, DB_NAME, null, VERSION_AS_BLOB); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onCreate(SQLiteDatabase db) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 107 | |
| 108 | db.execSQL("CREATE TABLE " + TABLE_RECENT + " (" + |
| 109 | RecentColumns.STACK + " BLOB PRIMARY KEY ON CONFLICT REPLACE," + |
| 110 | RecentColumns.TIMESTAMP + " INTEGER" + |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 111 | ")"); |
| 112 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 113 | db.execSQL("CREATE TABLE " + TABLE_STATE + " (" + |
| 114 | StateColumns.AUTHORITY + " TEXT," + |
| 115 | StateColumns.ROOT_ID + " TEXT," + |
| 116 | StateColumns.DOCUMENT_ID + " TEXT," + |
| 117 | StateColumns.MODE + " INTEGER," + |
| 118 | StateColumns.SORT_ORDER + " INTEGER," + |
| 119 | "PRIMARY KEY (" + StateColumns.AUTHORITY + ", " + StateColumns.ROOT_ID + ", " |
| 120 | + StateColumns.DOCUMENT_ID + ")" + |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 121 | ")"); |
| 122 | |
| 123 | db.execSQL("CREATE TABLE " + TABLE_RESUME + " (" + |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 124 | ResumeColumns.PACKAGE_NAME + " TEXT PRIMARY KEY ON CONFLICT REPLACE," + |
| 125 | ResumeColumns.STACK + " BLOB," + |
| 126 | ResumeColumns.TIMESTAMP + " INTEGER" + |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 127 | ")"); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 132 | Log.w(TAG, "Upgrading database; wiping app data"); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 133 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECENT); |
| 134 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATE); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 135 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESUME); |
| 136 | onCreate(db); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean onCreate() { |
| 142 | mHelper = new DatabaseHelper(getContext()); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 148 | String sortOrder) { |
| 149 | final SQLiteDatabase db = mHelper.getReadableDatabase(); |
| 150 | switch (sMatcher.match(uri)) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 151 | case URI_RECENT: |
| 152 | return db.query(TABLE_RECENT, projection, |
| 153 | RecentColumns.TIMESTAMP + "<" + MAX_HISTORY_IN_MILLIS, null, null, null, |
| 154 | null); |
| 155 | case URI_STATE: |
| 156 | final String authority = uri.getPathSegments().get(1); |
| 157 | final String rootId = uri.getPathSegments().get(2); |
| 158 | final String documentId = uri.getPathSegments().get(3); |
| 159 | return db.query(TABLE_STATE, projection, StateColumns.AUTHORITY + "=? AND " |
| 160 | + StateColumns.ROOT_ID + "=? AND " + StateColumns.DOCUMENT_ID + "=?", |
| 161 | new String[] { authority, rootId, documentId }, null, null, null); |
| 162 | case URI_RESUME: |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 163 | final String packageName = uri.getPathSegments().get(1); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 164 | return db.query(TABLE_RESUME, projection, ResumeColumns.PACKAGE_NAME + "=?", |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 165 | new String[] { packageName }, null, null, null); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 166 | default: |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 167 | throw new UnsupportedOperationException("Unsupported Uri " + uri); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public String getType(Uri uri) { |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Uri insert(Uri uri, ContentValues values) { |
| 178 | final SQLiteDatabase db = mHelper.getWritableDatabase(); |
| 179 | switch (sMatcher.match(uri)) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 180 | case URI_RECENT: |
| 181 | values.put(RecentColumns.TIMESTAMP, System.currentTimeMillis()); |
| 182 | db.insert(TABLE_RECENT, null, values); |
| 183 | db.delete( |
| 184 | TABLE_RECENT, RecentColumns.TIMESTAMP + ">" + MAX_HISTORY_IN_MILLIS, null); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 185 | return uri; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 186 | case URI_STATE: |
| 187 | final String authority = uri.getPathSegments().get(1); |
| 188 | final String rootId = uri.getPathSegments().get(2); |
| 189 | final String documentId = uri.getPathSegments().get(3); |
| 190 | |
| 191 | final ContentValues key = new ContentValues(); |
| 192 | key.put(StateColumns.AUTHORITY, authority); |
| 193 | key.put(StateColumns.ROOT_ID, rootId); |
| 194 | key.put(StateColumns.DOCUMENT_ID, documentId); |
| 195 | |
| 196 | // Ensure that row exists, then update with changed values |
| 197 | db.insertWithOnConflict(TABLE_STATE, null, key, SQLiteDatabase.CONFLICT_IGNORE); |
| 198 | db.update(TABLE_STATE, values, StateColumns.AUTHORITY + "=? AND " |
| 199 | + StateColumns.ROOT_ID + "=? AND " + StateColumns.DOCUMENT_ID + "=?", |
| 200 | new String[] { authority, rootId, documentId }); |
| 201 | |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 202 | return uri; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 203 | case URI_RESUME: |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 204 | final String packageName = uri.getPathSegments().get(1); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 205 | values.put(ResumeColumns.PACKAGE_NAME, packageName); |
| 206 | values.put(ResumeColumns.TIMESTAMP, System.currentTimeMillis()); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 207 | db.insert(TABLE_RESUME, null, values); |
| 208 | return uri; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 209 | default: |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 210 | throw new UnsupportedOperationException("Unsupported Uri " + uri); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 216 | throw new UnsupportedOperationException("Unsupported Uri " + uri); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 221 | throw new UnsupportedOperationException("Unsupported Uri " + uri); |
| 222 | } |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 223 | } |