• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.net.Uri;
22 import android.provider.DocumentsContract;
23 import android.provider.DocumentsContract.Document;
24 import android.text.format.DateUtils;
25 
26 import com.android.documentsui.base.Lookup;
27 import com.android.documentsui.base.RootInfo;
28 import com.android.documentsui.base.State;
29 import com.android.documentsui.base.UserId;
30 import com.android.documentsui.roots.ProvidersAccess;
31 import com.android.documentsui.roots.RootCursorWrapper;
32 
33 import java.util.List;
34 import java.util.concurrent.Executor;
35 
36 public class RecentsLoader extends MultiRootDocumentsLoader {
37 
38     private static final String TAG = "RecentsLoader";
39     /** Ignore documents older than this age. */
40     private static final long REJECT_OLDER_THAN = 45 * DateUtils.DAY_IN_MILLIS;
41 
42     /** MIME types that should always be excluded from recents. */
43     private static final String[] REJECT_MIMES = new String[]{Document.MIME_TYPE_DIR};
44 
45     /** Maximum documents from a single root. */
46     private static final int MAX_DOCS_FROM_ROOT = 64;
47 
48     private final UserId mUserId;
49 
RecentsLoader(Context context, ProvidersAccess providers, State state, Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap, UserId userId)50     public RecentsLoader(Context context, ProvidersAccess providers, State state,
51             Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap, UserId userId) {
52         super(context, providers, state, executors, fileTypeMap);
53         mUserId = userId;
54     }
55 
56     @Override
loadInBackground()57     public DirectoryResult loadInBackground() {
58         if (!mState.canInteractWith(mUserId)) {
59             DirectoryResult result = new DirectoryResult();
60             result.exception = new CrossProfileNoPermissionException();
61             return result;
62         } else if (mUserId.isQuietModeEnabled(getContext())) {
63             DirectoryResult result = new DirectoryResult();
64             result.exception = new CrossProfileQuietModeException(mUserId);
65             return result;
66         }
67         return super.loadInBackground();
68     }
69 
70     @Override
getRejectBeforeTime()71     protected long getRejectBeforeTime() {
72         return System.currentTimeMillis() - REJECT_OLDER_THAN;
73     }
74 
75     @Override
getRejectMimes()76     protected String[] getRejectMimes() {
77         return REJECT_MIMES;
78     }
79 
80     @Override
shouldIgnoreRoot(RootInfo root)81     protected boolean shouldIgnoreRoot(RootInfo root) {
82         // only query the root is local only, support recents, and is from the selected user.
83         return !root.isLocalOnly() || !root.supportsRecents() || !mUserId.equals(root.userId);
84     }
85 
86     @Override
getQueryTask(String authority, List<RootInfo> rootInfos)87     protected QueryTask getQueryTask(String authority, List<RootInfo> rootInfos) {
88         return new RecentsTask(authority, rootInfos);
89     }
90 
91     private class RecentsTask extends QueryTask {
92 
RecentsTask(String authority, List<RootInfo> rootInfos)93         public RecentsTask(String authority, List<RootInfo> rootInfos) {
94             super(authority, rootInfos);
95         }
96 
97         @Override
getQueryUri(RootInfo rootInfo)98         protected Uri getQueryUri(RootInfo rootInfo) {
99             return DocumentsContract.buildRecentDocumentsUri(authority, rootInfo.rootId);
100         }
101 
102         @Override
generateResultCursor(RootInfo rootInfo, Cursor oriCursor)103         protected RootCursorWrapper generateResultCursor(RootInfo rootInfo, Cursor oriCursor) {
104             return new RootCursorWrapper(rootInfo.userId, authority, rootInfo.rootId, oriCursor,
105                     MAX_DOCS_FROM_ROOT);
106         }
107     }
108 }
109