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