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.app.Activity; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.database.Cursor; 23 import android.net.Uri; 24 25 public interface PickCountRecordStorage { getPickCountRecord(Context context, Uri uri)26 int getPickCountRecord(Context context, Uri uri); setPickCountRecord(Context context, Uri uri, int pickCount)27 void setPickCountRecord(Context context, Uri uri, int pickCount); increasePickCountRecord(Context context, Uri uri)28 int increasePickCountRecord(Context context, Uri uri); 29 create()30 static PickCountRecordStorage create() { 31 return new PickCountRecordStorage() { 32 private static final String TAG = "PickCountRecordStorage"; 33 34 @Override 35 public int getPickCountRecord(Context context, Uri uri) { 36 int fileHashId = uri.hashCode(); 37 Uri pickRecordUri = PickCountRecordProvider.buildPickRecordUri(fileHashId); 38 final ContentResolver resolver = context.getContentResolver(); 39 int count = 0; 40 try (Cursor cursor = resolver.query(pickRecordUri, null, null, null, null)) { 41 if (cursor != null && cursor.moveToFirst()) { 42 final int index = cursor 43 .getColumnIndex(PickCountRecordProvider.Columns.PICK_COUNT); 44 if (index != -1) { 45 count = cursor.getInt(index); 46 } 47 } 48 } 49 return count; 50 } 51 52 @Override 53 public void setPickCountRecord(Context context, Uri uri, int pickCount) { 54 PickCountRecordProvider.setPickRecord( 55 context.getContentResolver(), uri.hashCode(), pickCount); 56 } 57 58 @Override 59 public int increasePickCountRecord(Context context, Uri uri) { 60 int pickCount = getPickCountRecord(context, uri) + 1; 61 setPickCountRecord(context, uri, pickCount); 62 return pickCount; 63 } 64 }; 65 } 66 } 67