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     /** Ignore documents older than this age. */
39     private static final long REJECT_OLDER_THAN = 45 * DateUtils.DAY_IN_MILLIS;
40 
41     /** MIME types that should always be excluded from recents. */
42     private static final String[] REJECT_MIMES = new String[]{Document.MIME_TYPE_DIR};
43 
44     /** Maximum documents from a single root. */
45     private static final int MAX_DOCS_FROM_ROOT = 64;
46 
47     private final UserId mUserId;
48 
RecentsLoader(Context context, ProvidersAccess providers, State state, Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap, UserId userId)49     public RecentsLoader(Context context, ProvidersAccess providers, State state,
50             Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap, UserId userId) {
51         super(context, providers, state, executors, fileTypeMap);
52         mUserId = userId;
53     }
54 
55     @Override
loadInBackground()56     public DirectoryResult loadInBackground() {
57         if (!mState.canInteractWith(mUserId)) {
58             DirectoryResult result = new DirectoryResult();
59             result.exception = new CrossProfileNoPermissionException();
60             return result;
61         } else if (mUserId.isQuietModeEnabled(getContext())) {
62             DirectoryResult result = new DirectoryResult();
63             result.exception = new CrossProfileQuietModeException(mUserId);
64             return result;
65         }
66         return super.loadInBackground();
67     }
68 
69     @Override
getRejectBeforeTime()70     protected long getRejectBeforeTime() {
71         return System.currentTimeMillis() - REJECT_OLDER_THAN;
72     }
73 
74     @Override
getRejectMimes()75     protected String[] getRejectMimes() {
76         return REJECT_MIMES;
77     }
78 
79     @Override
shouldIgnoreRoot(RootInfo root)80     protected boolean shouldIgnoreRoot(RootInfo root) {
81         // only query the root is local only, support recents, and is from the selected user.
82         return !root.isLocalOnly() || !root.supportsRecents() || !mUserId.equals(root.userId);
83     }
84 
85     @Override
getQueryTask(String authority, List<RootInfo> rootInfos)86     protected QueryTask getQueryTask(String authority, List<RootInfo> rootInfos) {
87         return new RecentsTask(authority, rootInfos);
88     }
89 
90     private class RecentsTask extends QueryTask {
91 
RecentsTask(String authority, List<RootInfo> rootInfos)92         public RecentsTask(String authority, List<RootInfo> rootInfos) {
93             super(authority, rootInfos);
94         }
95 
96         @Override
getQueryUri(RootInfo rootInfo)97         protected Uri getQueryUri(RootInfo rootInfo) {
98             return DocumentsContract.buildRecentDocumentsUri(authority, rootInfo.rootId);
99         }
100 
101         @Override
generateResultCursor(RootInfo rootInfo, Cursor oriCursor)102         protected RootCursorWrapper generateResultCursor(RootInfo rootInfo, Cursor oriCursor) {
103             return new RootCursorWrapper(rootInfo.userId, authority, rootInfo.rootId, oriCursor,
104                     MAX_DOCS_FROM_ROOT);
105         }
106     }
107 }
108