1 /* 2 * Copyright (C) 2019 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.picker; 18 19 import android.content.ContentProvider; 20 import android.content.ContentResolver; 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; 28 import android.util.Log; 29 30 public class PickCountRecordProvider extends ContentProvider { 31 private static final String TAG = "PickCountRecordProvider"; 32 33 private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); 34 35 private static final int URI_PICK_RECORD = 1; 36 37 private static final String PATH_PICK_COUNT_RECORD = "pickCountRecord"; 38 39 private static final String TABLE_PICK_COUNT_RECORD = "pickCountRecordTable"; 40 41 static final String AUTHORITY = "com.android.documentsui.pickCountRecord"; 42 43 static { MATCHER.addURI(AUTHORITY, R, URI_PICK_RECORD)44 MATCHER.addURI(AUTHORITY, "pickCountRecord/*", URI_PICK_RECORD); 45 } 46 47 public static class Columns { 48 public static final String FILE_HASH_ID = "file_hash_id"; 49 public static final String PICK_COUNT = "pick_count"; 50 } 51 52 /** 53 * Build pickRecord uri. 54 * 55 * @param hashFileId the file hash id. 56 * @return return an pick record uri. 57 */ buildPickRecordUri(int hashFileId)58 public static Uri buildPickRecordUri(int hashFileId) { 59 return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) 60 .authority(AUTHORITY).appendPath(PATH_PICK_COUNT_RECORD) 61 .appendPath(Integer.toString(hashFileId)) 62 .build(); 63 } 64 65 private PickCountRecordProvider.DatabaseHelper mHelper; 66 67 private static class DatabaseHelper extends SQLiteOpenHelper { 68 private static final String DB_NAME = "pickCountRecord.db"; 69 70 private static final int VERSION_INIT = 1; 71 DatabaseHelper(Context context)72 public DatabaseHelper(Context context) { 73 super(context, DB_NAME, null, VERSION_INIT); 74 } 75 76 @Override onCreate(SQLiteDatabase db)77 public void onCreate(SQLiteDatabase db) { 78 db.execSQL("CREATE TABLE " + TABLE_PICK_COUNT_RECORD + " (" 79 + PickCountRecordProvider.Columns.FILE_HASH_ID + " TEXT NOT NULL PRIMARY KEY," 80 + PickCountRecordProvider.Columns.PICK_COUNT + " INTEGER" + ")"); 81 } 82 83 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)84 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 85 Log.w(TAG, "Upgrading database; wiping app data"); 86 db.execSQL("DROP TABLE IF EXISTS " + TABLE_PICK_COUNT_RECORD); 87 onCreate(db); 88 } 89 } 90 91 @Override onCreate()92 public boolean onCreate() { 93 mHelper = new DatabaseHelper(getContext()); 94 return true; 95 } 96 97 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)98 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 99 String sortOrder) { 100 if (MATCHER.match(uri) != URI_PICK_RECORD) { 101 throw new UnsupportedOperationException("Unsupported Uri " + uri); 102 } 103 final SQLiteDatabase db = mHelper.getReadableDatabase(); 104 final String fileHashId = uri.getPathSegments().get(1); 105 return db.query(TABLE_PICK_COUNT_RECORD, projection, Columns.FILE_HASH_ID + "=?", 106 new String[] { fileHashId }, null, null, sortOrder); 107 } 108 109 @Override insert(Uri uri, ContentValues values)110 public Uri insert(Uri uri, ContentValues values) { 111 if (MATCHER.match(uri) != URI_PICK_RECORD) { 112 throw new UnsupportedOperationException("Unsupported Uri " + uri); 113 } 114 final SQLiteDatabase db = mHelper.getWritableDatabase(); 115 final ContentValues key = new ContentValues(); 116 117 final String hashId = uri.getPathSegments().get(1); 118 key.put(Columns.FILE_HASH_ID, hashId); 119 120 // Ensure that row exists, then update with changed values 121 db.insertWithOnConflict(TABLE_PICK_COUNT_RECORD, null, key, SQLiteDatabase.CONFLICT_IGNORE); 122 db.update(TABLE_PICK_COUNT_RECORD, values, Columns.FILE_HASH_ID + "=?", 123 new String[] { hashId }); 124 return null; 125 } 126 setPickRecord(ContentResolver resolver, int fileHashId, int pickCount)127 static void setPickRecord(ContentResolver resolver, int fileHashId, int pickCount) { 128 final ContentValues values = new ContentValues(); 129 values.clear(); 130 values.put(Columns.PICK_COUNT, pickCount); 131 resolver.insert(buildPickRecordUri(fileHashId), values); 132 } 133 134 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)135 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 136 throw new UnsupportedOperationException("Unsupported Uri " + uri); 137 } 138 139 @Override delete(Uri uri, String selection, String[] selectionArgs)140 public int delete(Uri uri, String selection, String[] selectionArgs) { 141 final SQLiteDatabase db = mHelper.getWritableDatabase(); 142 final String hashId = uri.getPathSegments().get(1); 143 return db.delete(TABLE_PICK_COUNT_RECORD, Columns.FILE_HASH_ID + "=?", 144 new String[] { hashId }); 145 } 146 147 @Override getType(Uri uri)148 public String getType(Uri uri) { 149 return null; 150 } 151 }