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