Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.dirlist; |
| 18 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.State.ACTION_BROWSE; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 21 | import static com.android.documentsui.State.ACTION_CREATE; |
| 22 | import static com.android.documentsui.State.ACTION_GET_CONTENT; |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 23 | import static com.android.documentsui.State.ACTION_MANAGE; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 24 | import static com.android.documentsui.State.ACTION_OPEN; |
| 25 | import static com.android.documentsui.State.ACTION_OPEN_TREE; |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 26 | import static com.android.internal.util.Preconditions.checkArgument; |
| 27 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.os.SystemProperties; |
| 30 | import android.provider.DocumentsContract.Document; |
| 31 | import android.util.Log; |
| 32 | import android.view.Menu; |
| 33 | import android.view.MenuItem; |
| 34 | |
| 35 | import com.android.documentsui.DocumentsActivity; |
| 36 | import com.android.documentsui.FilesActivity; |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 37 | import com.android.documentsui.Menus; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 38 | import com.android.documentsui.MimePredicate; |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 39 | import com.android.documentsui.R; |
| 40 | import com.android.documentsui.State; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 41 | import com.android.documentsui.dirlist.DirectoryFragment.ResultType; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 42 | |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Providers support for specializing the DirectoryFragment to the "host" Activity. |
| 45 | * Feel free to expand the role of this class to handle other specializations. |
| 46 | */ |
| 47 | public abstract class FragmentTuner { |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 48 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 49 | final Context mContext; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 50 | final State mState; |
| 51 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 52 | public FragmentTuner(Context context, State state) { |
| 53 | mContext = context; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 54 | mState = state; |
| 55 | } |
| 56 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 57 | public static FragmentTuner pick(Context context, State state) { |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 58 | switch (state.action) { |
| 59 | case ACTION_BROWSE: |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 60 | return new FilesTuner(context, state); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 61 | case ACTION_MANAGE: |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 62 | return new DownloadsTuner(context, state); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 63 | default: |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 64 | return new DocumentsTuner(context, state); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 68 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 69 | public abstract void updateActionMenu(Menu menu, int dirType, boolean canDelete, |
| 70 | boolean canRename); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 71 | |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 72 | // Subtly different from isDocumentEnabled. The reason may be illuminated as follows. |
| 73 | // A folder is enabled such that it may be double clicked, even in settings |
| 74 | // when the folder itself cannot be selected. This may also be true of container types. |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 75 | public boolean canSelectType(String docMimeType, int docFlags) { |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
| 79 | public boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 80 | if (isDirectory(docMimeType)) { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | return MimePredicate.mimeMatches(mState.acceptMimes, docMimeType); |
| 85 | } |
| 86 | |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 87 | abstract void onModelLoaded(Model model, @ResultType int resultType, boolean isSearch); |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 88 | |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Provides support for Platform specific specializations of DirectoryFragment. |
| 91 | */ |
| 92 | private static final class DocumentsTuner extends FragmentTuner { |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 93 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 94 | public DocumentsTuner(Context context, State state) { |
| 95 | super(context, state); |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 99 | public boolean canSelectType(String docMimeType, int docFlags) { |
| 100 | if (!isDocumentEnabled(docMimeType, docFlags)) { |
| 101 | return false; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 102 | } |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 103 | |
| 104 | if (isDirectory(docMimeType)) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if (mState.action == ACTION_OPEN_TREE) { |
| 109 | // In this case nothing *ever* is selectable...the expected user behavior is |
| 110 | // they navigate *into* a folder, then click a confirmation button indicating |
| 111 | // that the current directory is the directory they are picking. |
| 112 | return false; |
| 113 | } |
| 114 | |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean isDocumentEnabled(String docMimeType, int docFlags) { |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 120 | // Directories are always enabled. |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 121 | if (isDirectory(docMimeType)) { |
| 122 | return true; |
| 123 | } |
| 124 | |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 125 | switch (mState.action) { |
| 126 | case ACTION_CREATE: |
| 127 | // Read-only files are disabled when creating. |
| 128 | if ((docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) { |
| 129 | return false; |
| 130 | } |
| 131 | case ACTION_OPEN: |
| 132 | case ACTION_GET_CONTENT: |
| 133 | final boolean isVirtual = (docFlags & Document.FLAG_VIRTUAL_DOCUMENT) != 0; |
| 134 | if (isVirtual && mState.openableOnly) { |
| 135 | return false; |
| 136 | } |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return MimePredicate.mimeMatches(mState.acceptMimes, docMimeType); |
| 140 | } |
| 141 | |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 142 | @Override |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 143 | public void updateActionMenu(Menu menu, int dirType, boolean canDelete, |
| 144 | boolean canRename) { |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 145 | |
| 146 | boolean copyEnabled = dirType != DirectoryFragment.TYPE_RECENT_OPEN; |
| 147 | boolean moveEnabled = |
| 148 | SystemProperties.getBoolean("debug.documentsui.enable_move", false); |
| 149 | menu.findItem(R.id.menu_copy_to_clipboard).setEnabled(copyEnabled); |
| 150 | |
| 151 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 152 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 153 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 154 | final MenuItem copyTo = menu.findItem(R.id.menu_copy_to); |
| 155 | final MenuItem moveTo = menu.findItem(R.id.menu_move_to); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 156 | final MenuItem rename = menu.findItem(R.id.menu_rename); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 157 | |
| 158 | open.setVisible(true); |
| 159 | share.setVisible(false); |
| 160 | delete.setVisible(false); |
| 161 | copyTo.setVisible(copyEnabled); |
| 162 | copyTo.setEnabled(copyEnabled); |
| 163 | moveTo.setVisible(moveEnabled); |
| 164 | moveTo.setEnabled(moveEnabled); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 165 | rename.setVisible(false); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 166 | } |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 167 | |
| 168 | @Override |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 169 | void onModelLoaded(Model model, @ResultType int resultType, boolean isSearch) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 170 | // When launched into empty recents, show drawer |
| 171 | if (resultType == DirectoryFragment.TYPE_RECENT_OPEN |
| 172 | && model.isEmpty() |
| 173 | && !mState.hasLocationChanged()) { |
| 174 | ((DocumentsActivity) mContext).setRootsDrawerOpen(true); |
| 175 | } |
| 176 | } |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Provides support for Platform specific specializations of DirectoryFragment. |
| 181 | */ |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 182 | private static final class DownloadsTuner extends FragmentTuner { |
| 183 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 184 | public DownloadsTuner(Context context, State state) { |
| 185 | super(context, state); |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 186 | } |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 187 | |
| 188 | @Override |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 189 | public void updateActionMenu(Menu menu, int dirType, boolean canDelete, |
| 190 | boolean canRename) { |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 191 | checkArgument(dirType != DirectoryFragment.TYPE_RECENT_OPEN); |
| 192 | |
| 193 | boolean moveEnabled = |
| 194 | SystemProperties.getBoolean("debug.documentsui.enable_move", false); |
| 195 | menu.findItem(R.id.menu_copy_to_clipboard).setEnabled(true); |
| 196 | |
| 197 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 198 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 199 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 200 | final MenuItem copyTo = menu.findItem(R.id.menu_copy_to); |
| 201 | final MenuItem moveTo = menu.findItem(R.id.menu_move_to); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 202 | final MenuItem rename = menu.findItem(R.id.menu_rename); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 203 | |
| 204 | open.setVisible(false); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 205 | delete.setVisible(canDelete); |
| 206 | copyTo.setVisible(true); |
| 207 | copyTo.setEnabled(true); |
| 208 | moveTo.setVisible(moveEnabled); |
| 209 | moveTo.setEnabled(moveEnabled); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 210 | rename.setVisible(false); |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 211 | } |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 212 | |
| 213 | @Override |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 214 | void onModelLoaded(Model model, @ResultType int resultType, boolean isSearch) {} |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Provides support for Files activity specific specializations of DirectoryFragment. |
| 219 | */ |
| 220 | private static final class FilesTuner extends FragmentTuner { |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 221 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 222 | private static final String TAG = "FilesTuner"; |
| 223 | |
| 224 | public FilesTuner(Context context, State state) { |
| 225 | super(context, state); |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 226 | } |
| 227 | |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 228 | @Override |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 229 | public void updateActionMenu(Menu menu, int dirType, boolean canDelete, |
| 230 | boolean canRename) { |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 231 | |
| 232 | MenuItem copy = menu.findItem(R.id.menu_copy_to_clipboard); |
| 233 | MenuItem paste = menu.findItem(R.id.menu_paste_from_clipboard); |
| 234 | copy.setEnabled(dirType != DirectoryFragment.TYPE_RECENT_OPEN); |
| 235 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 236 | MenuItem rename = menu.findItem(R.id.menu_rename); |
| 237 | rename.setVisible(true); |
| 238 | rename.setEnabled(canRename); |
| 239 | |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 240 | menu.findItem(R.id.menu_share).setVisible(true); |
| 241 | menu.findItem(R.id.menu_delete).setVisible(canDelete); |
| 242 | |
| 243 | menu.findItem(R.id.menu_open).setVisible(false); |
| 244 | menu.findItem(R.id.menu_copy_to).setVisible(true); |
| 245 | menu.findItem(R.id.menu_move_to).setVisible(true); |
| 246 | |
| 247 | Menus.disableHiddenItems(menu, copy, paste); |
| 248 | } |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 249 | |
| 250 | @Override |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 251 | void onModelLoaded(Model model, @ResultType int resultType, boolean isSearch) { |
Aga Wronska | d4b1753 | 2016-02-12 09:15:32 -0800 | [diff] [blame] | 252 | if (DEBUG) Log.d(TAG, "Handling model loaded. Has Location shcnage: " + mState.initialLocationHasChanged()); |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 253 | // When launched into empty root, open drawer. |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 254 | if (model.isEmpty() && !mState.initialLocationHasChanged() && !isSearch) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 255 | if (DEBUG) Log.d(TAG, "Showing roots drawer cuz stuffs empty."); |
| 256 | |
| 257 | // This noops on layouts without drawer, so no need to guard. |
| 258 | ((FilesActivity) mContext).setRootsDrawerOpen(true); |
| 259 | } |
| 260 | if (DEBUG) Log.d(TAG, "Donezo."); |
| 261 | } |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 262 | } |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 263 | |
| 264 | private static boolean isDirectory(String mimeType) { |
| 265 | return Document.MIME_TYPE_DIR.equals(mimeType); |
| 266 | } |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 267 | |
Steve McKay | d3f6f14 | 2015-10-27 15:57:53 -0700 | [diff] [blame] | 268 | } |