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