LastAccessedStorage.java revision 23ac60cd2b1beb59b6cd760b91c7e1b1f7e8fe49
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
17package com.android.documentsui.picker;
18
19import android.app.Activity;
20import android.content.ContentResolver;
21import android.content.ContentValues;
22import android.database.Cursor;
23import android.net.Uri;
24import android.util.Log;
25
26import com.android.documentsui.base.DocumentStack;
27import com.android.documentsui.base.Shared;
28import com.android.documentsui.base.State;
29import com.android.documentsui.roots.RootsAccess;
30
31import libcore.io.IoUtils;
32
33import java.io.IOException;
34
35import javax.annotation.Nullable;
36
37/**
38 * An interface that stores the last accessed stack of the caller
39 */
40public interface LastAccessedStorage {
41
42    @Nullable DocumentStack getLastAccessed(
43            Activity activity, RootsAccess roots, State state);
44
45    void setLastAccessed(Activity activity, DocumentStack stack);
46
47    void setLastAccessedToExternalApp(Activity activity);
48
49    static LastAccessedStorage create() {
50        return new RuntimeLastAccessedStorage();
51    }
52
53    class RuntimeLastAccessedStorage implements LastAccessedStorage {
54
55        private static final String TAG = "LastAccessedStorage";
56
57        private RuntimeLastAccessedStorage() {}
58
59        @Override
60        public @Nullable DocumentStack getLastAccessed(
61                Activity activity, RootsAccess roots, 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, roots.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
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
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