1 /* 2 * Copyright (C) 2017 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.ContentValues; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.util.Log; 25 26 import com.android.documentsui.base.DocumentStack; 27 import com.android.documentsui.base.Shared; 28 import com.android.documentsui.base.State; 29 import com.android.documentsui.roots.ProvidersAccess; 30 31 import libcore.io.IoUtils; 32 33 import java.io.IOException; 34 35 import javax.annotation.Nullable; 36 37 /** 38 * An interface that stores the last accessed stack of the caller 39 */ 40 public interface LastAccessedStorage { 41 getLastAccessed( Activity activity, ProvidersAccess providers, State state)42 @Nullable DocumentStack getLastAccessed( 43 Activity activity, ProvidersAccess providers, State state); 44 setLastAccessed(Activity activity, DocumentStack stack)45 void setLastAccessed(Activity activity, DocumentStack stack); 46 setLastAccessedToExternalApp(Activity activity)47 void setLastAccessedToExternalApp(Activity activity); 48 create()49 static LastAccessedStorage create() { 50 return new RuntimeLastAccessedStorage(); 51 } 52 53 class RuntimeLastAccessedStorage implements LastAccessedStorage { 54 55 private static final String TAG = "LastAccessedStorage"; 56 RuntimeLastAccessedStorage()57 private RuntimeLastAccessedStorage() {} 58 59 @Override getLastAccessed( Activity activity, ProvidersAccess providers, State state)60 public @Nullable DocumentStack getLastAccessed( 61 Activity activity, ProvidersAccess providers, State state) { 62 final String packageName = Shared.getCallingPackageName(activity); 63 final Uri resumeUri = LastAccessedProvider.buildLastAccessed(packageName); 64 final ContentResolver resolver = activity.getContentResolver(); 65 Cursor cursor = resolver.query(resumeUri, null, null, null, null); 66 try { 67 return DocumentStack.fromLastAccessedCursor( 68 cursor, providers.getMatchingRootsBlocking(state), resolver); 69 } catch (IOException e) { 70 Log.w(TAG, "Failed to resume: ", e); 71 } finally { 72 IoUtils.closeQuietly(cursor); 73 } 74 75 return null; 76 } 77 78 @Override setLastAccessed(Activity activity, DocumentStack stack)79 public void setLastAccessed(Activity activity, DocumentStack stack) { 80 String packageName = Shared.getCallingPackageName(activity); 81 LastAccessedProvider.setLastAccessed(activity.getContentResolver(), packageName, stack); 82 } 83 84 @Override setLastAccessedToExternalApp(Activity activity)85 public void setLastAccessedToExternalApp(Activity activity) { 86 final String packageName = Shared.getCallingPackageName(activity); 87 final ContentValues values = new ContentValues(); 88 values.put(LastAccessedProvider.Columns.EXTERNAL, 1); 89 activity.getContentResolver().insert( 90 LastAccessedProvider.buildLastAccessed(packageName), values); 91 } 92 } 93 } 94