Steve McKay | e934ce6 | 2015-03-25 14:35:33 -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; |
| 18 | |
Steve McKay | a7e923c | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_UNKNOWN; |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 21 | import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE; |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 22 | import static com.android.internal.util.Preconditions.checkArgument; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 23 | import static com.android.internal.util.Preconditions.checkState; |
Steve McKay | d0d9afc | 2015-05-06 12:16:40 -0700 | [diff] [blame] | 24 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 25 | import android.app.Activity; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 26 | import android.app.FragmentManager; |
| 27 | import android.content.ActivityNotFoundException; |
| 28 | import android.content.ClipData; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 29 | import android.content.ContentResolver; |
| 30 | import android.content.ContentValues; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 31 | import android.content.Intent; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 34 | import android.os.Parcelable; |
| 35 | import android.provider.DocumentsContract; |
Ben Kwa | 94b486d | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 36 | import android.support.design.widget.Snackbar; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 38 | import android.view.KeyEvent; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 39 | import android.view.Menu; |
| 40 | import android.view.MenuItem; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 41 | |
Steve McKay | a7e923c | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 42 | import com.android.documentsui.OperationDialogFragment.DialogType; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 43 | import com.android.documentsui.RecentsProvider.ResumeColumns; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 44 | import com.android.documentsui.dirlist.DirectoryFragment; |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 45 | import com.android.documentsui.dirlist.Model; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 46 | import com.android.documentsui.model.DocumentInfo; |
| 47 | import com.android.documentsui.model.DocumentStack; |
| 48 | import com.android.documentsui.model.DurableUtils; |
| 49 | import com.android.documentsui.model.RootInfo; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 50 | import com.android.documentsui.services.FileOperationService; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 51 | |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 52 | import java.io.FileNotFoundException; |
Tomasz Mikolajewski | 6168659 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 53 | import java.util.ArrayList; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 54 | import java.util.Arrays; |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 55 | import java.util.Collection; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 56 | import java.util.List; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 57 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 58 | /** |
Steve McKay | 273103b | 2015-05-12 12:49:58 -0700 | [diff] [blame] | 59 | * Standalone file management activity. |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 60 | */ |
Steve McKay | 1205547 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 61 | public class FilesActivity extends BaseActivity { |
Steve McKay | c78bcb8 | 2015-07-31 14:35:22 -0700 | [diff] [blame] | 62 | |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 63 | public static final String TAG = "FilesActivity"; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 64 | |
Steve McKay | c95d87c | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 65 | // See comments where this const is referenced for details. |
| 66 | private static final int DRAWER_NO_FIDDLE_DELAY = 1500; |
| 67 | |
| 68 | // Track the time we opened the drawer in response to back being pressed. |
| 69 | // We use the time gap to figure out whether to close app or reopen the drawer. |
| 70 | private long mDrawerLastFiddled; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 71 | private DocumentClipper mClipper; |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 72 | |
Steve McKay | 1205547 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 73 | public FilesActivity() { |
| 74 | super(R.layout.files_activity, TAG); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 75 | } |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void onCreate(Bundle icicle) { |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 79 | super.onCreate(icicle); |
| 80 | |
Steve McKay | 1205547 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 81 | mClipper = new DocumentClipper(this); |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 82 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 83 | RootsFragment.show(getFragmentManager(), null); |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 84 | |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 85 | final Intent intent = getIntent(); |
| 86 | final Uri uri = intent.getData(); |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 87 | |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 88 | if (mState.restored) { |
| 89 | if (DEBUG) Log.d(TAG, "Stack already resolved for uri: " + intent.getData()); |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 90 | } else if (!mState.stack.isEmpty()) { |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 91 | // If a non-empty stack is present in our state it was read (presumably) |
| 92 | // from EXTRA_STACK intent extra. In this case, we'll skip other means of |
| 93 | // loading or restoring the stack. |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 94 | // |
| 95 | // When restoring from a stack, if a URI is present, it should only ever |
Tomasz Mikolajewski | cd27015 | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 96 | // be a launch URI, or a fake Uri from notifications. |
| 97 | // Launch URIs support sensible activity management, but don't specify a real |
| 98 | // content target. |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 99 | if (DEBUG) Log.d(TAG, "Launching with non-empty stack."); |
Tomasz Mikolajewski | cd27015 | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 100 | checkState(uri == null || uri.getAuthority() == null || |
| 101 | LauncherActivity.isLaunchUri(uri)); |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 102 | refreshCurrentRootAndDirectory(ANIM_NONE); |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 103 | } else if (intent.getAction() == Intent.ACTION_VIEW) { |
| 104 | checkArgument(uri != null); |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 105 | new OpenUriForViewTask(this).executeOnExecutor( |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 106 | ProviderExecutor.forAuthority(uri.getAuthority()), uri); |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 107 | } else if (DocumentsContract.isRootUri(this, uri)) { |
| 108 | if (DEBUG) Log.d(TAG, "Launching with root URI."); |
| 109 | // If we've got a specific root to display, restore that root using a dedicated |
| 110 | // authority. That way a misbehaving provider won't result in an ANR. |
Steve McKay | c95d87c | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 111 | loadRoot(uri); |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 112 | } else { |
| 113 | if (DEBUG) Log.d(TAG, "Launching into Home directory."); |
| 114 | // If all else fails, try to load "Home" directory. |
| 115 | final Uri homeUri = DocumentsContract.buildHomeUri(); |
Steve McKay | c95d87c | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 116 | loadRoot(homeUri); |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 117 | } |
Tomasz Mikolajewski | f8c3f32 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 118 | |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 119 | final @DialogType int dialogType = intent.getIntExtra( |
| 120 | FileOperationService.EXTRA_DIALOG_TYPE, DIALOG_TYPE_UNKNOWN); |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 121 | // DialogFragment takes care of restoring the dialog on configuration change. |
| 122 | // Only show it manually for the first time (icicle is null). |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 123 | if (icicle == null && dialogType != DIALOG_TYPE_UNKNOWN) { |
| 124 | final int opType = intent.getIntExtra( |
| 125 | FileOperationService.EXTRA_OPERATION, |
| 126 | FileOperationService.OPERATION_COPY); |
| 127 | final ArrayList<DocumentInfo> srcList = |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 128 | intent.getParcelableArrayListExtra(FileOperationService.EXTRA_SRC_LIST); |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 129 | OperationDialogFragment.show( |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 130 | getFragmentManager(), |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 131 | dialogType, |
| 132 | srcList, |
Tomasz Mikolajewski | aa68445 | 2015-12-25 17:06:52 +0900 | [diff] [blame] | 133 | mState.stack, |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 134 | opType); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Steve McKay | 1205547 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 138 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 139 | void includeState(State state) { |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 140 | final Intent intent = getIntent(); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 141 | |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 142 | state.action = State.ACTION_BROWSE; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 143 | state.allowMultiple = true; |
Steve McKay | 273103b | 2015-05-12 12:49:58 -0700 | [diff] [blame] | 144 | |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 145 | // Options specific to the DocumentsActivity. |
| 146 | checkArgument(!intent.hasExtra(Intent.EXTRA_LOCAL_ONLY)); |
Steve McKay | 273103b | 2015-05-12 12:49:58 -0700 | [diff] [blame] | 147 | |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 148 | final DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK); |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 149 | if (stack != null) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 150 | state.stack = stack; |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 151 | } |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void onPostCreate(Bundle savedInstanceState) { |
| 156 | super.onPostCreate(savedInstanceState); |
Steve McKay | b67bfbf | 2015-12-08 17:02:03 -0800 | [diff] [blame] | 157 | // This check avoids a flicker from "Recents" to "Home". |
| 158 | // Only update action bar at this point if there is an active |
| 159 | // serach. Why? Because this avoid an early (undesired) load of |
| 160 | // the recents root...which is the default root in other activities. |
| 161 | // In Files app "Home" is the default, but it is loaded async. |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 162 | // update will be called once Home root is loaded. |
Steve McKay | b67bfbf | 2015-12-08 17:02:03 -0800 | [diff] [blame] | 163 | // Except while searching we need this call to ensure the |
| 164 | // search bits get layed out correctly. |
| 165 | if (mSearchManager.isSearching()) { |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 166 | mNavigator.update(); |
Steve McKay | b67bfbf | 2015-12-08 17:02:03 -0800 | [diff] [blame] | 167 | } |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 171 | public void onResume() { |
| 172 | super.onResume(); |
| 173 | |
| 174 | final RootInfo root = getCurrentRoot(); |
| 175 | |
| 176 | // If we're browsing a specific root, and that root went away, then we |
| 177 | // have no reason to hang around. |
| 178 | // TODO: Rather than just disappearing, maybe we should inform |
| 179 | // the user what has happened, let them close us. Less surprising. |
| 180 | if (mRoots.getRootBlocking(root.authority, root.rootId) == null) { |
| 181 | finish(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 186 | public String getDrawerTitle() { |
| 187 | return getResources().getString(R.string.files_label); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 191 | public boolean onPrepareOptionsMenu(Menu menu) { |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 192 | super.onPrepareOptionsMenu(menu); |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 193 | |
Steve McKay | efa1761 | 2016-01-29 18:15:39 -0800 | [diff] [blame] | 194 | final RootInfo root = getCurrentRoot(); |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 195 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 196 | final MenuItem createDir = menu.findItem(R.id.menu_create_dir); |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 197 | final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard); |
Steve McKay | efa1761 | 2016-01-29 18:15:39 -0800 | [diff] [blame] | 198 | final MenuItem settings = menu.findItem(R.id.menu_settings); |
Steve McKay | f873769 | 2016-02-04 19:40:45 -0800 | [diff] [blame] | 199 | final MenuItem newWindow = menu.findItem(R.id.menu_new_window); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 200 | |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 201 | createDir.setVisible(true); |
| 202 | createDir.setEnabled(canCreateDirectory()); |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 203 | pasteFromCb.setEnabled(mClipper.hasItemsToPaste()); |
Steve McKay | efa1761 | 2016-01-29 18:15:39 -0800 | [diff] [blame] | 204 | settings.setVisible(root.hasSettings()); |
Steve McKay | f873769 | 2016-02-04 19:40:45 -0800 | [diff] [blame] | 205 | newWindow.setVisible(true); |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 206 | |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 207 | Menus.disableHiddenItems(menu, pasteFromCb); |
| 208 | return true; |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 212 | public boolean onOptionsItemSelected(MenuItem item) { |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 213 | switch (item.getItemId()) { |
| 214 | case R.id.menu_create_dir: |
| 215 | checkState(canCreateDirectory()); |
| 216 | showCreateDirectoryDialog(); |
| 217 | return true; |
| 218 | case R.id.menu_new_window: |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 219 | createNewWindow(); |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 220 | return true; |
| 221 | case R.id.menu_paste_from_clipboard: |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 222 | DirectoryFragment dir = getDirectoryFragment(); |
| 223 | if (dir != null) { |
| 224 | dir.pasteFromClipboard(); |
| 225 | } |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 226 | return true; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return super.onOptionsItemSelected(item); |
| 230 | } |
| 231 | |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 232 | private void createNewWindow() { |
Ben Kwa | 7237998 | 2016-01-26 11:50:03 -0800 | [diff] [blame] | 233 | Metrics.logMultiWindow(this); |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 234 | Intent intent = LauncherActivity.createLaunchIntent(this); |
| 235 | intent.putExtra(Shared.EXTRA_STACK, (Parcelable) mState.stack); |
Steve McKay | a7e923c | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 236 | |
| 237 | // With new multi-window mode we have to pick how we are launched. |
| 238 | // By default we'd be launched in-place above the existing app. |
| 239 | // By setting launch-to-side ActivityManager will open us to side. |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 240 | if (inMultiWindow()) { |
Wale Ogunwale | 2a25a62 | 2016-01-30 11:27:21 -0800 | [diff] [blame] | 241 | intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT); |
Steve McKay | a7e923c | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 244 | startActivity(intent); |
| 245 | } |
| 246 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 247 | @Override |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 248 | void refreshDirectory(int anim) { |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 249 | final FragmentManager fm = getFragmentManager(); |
| 250 | final RootInfo root = getCurrentRoot(); |
| 251 | final DocumentInfo cwd = getCurrentDirectory(); |
| 252 | |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 253 | if (DEBUG) checkState(!mSearchManager.isSearching()); |
| 254 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 255 | if (cwd == null) { |
| 256 | DirectoryFragment.showRecentsOpen(fm, anim); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 257 | } else { |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 258 | // Normal boring directory |
| 259 | DirectoryFragment.showDirectory(fm, root, cwd, anim); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 260 | } |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
Steve McKay | 4f16040 | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 264 | void onRootPicked(RootInfo root) { |
| 265 | super.onRootPicked(root); |
| 266 | mDrawer.setOpen(false); |
| 267 | } |
| 268 | |
| 269 | @Override |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 270 | public void onDocumentsPicked(List<DocumentInfo> docs) { |
| 271 | throw new UnsupportedOperationException(); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 274 | @Override |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 275 | public void onDocumentPicked(DocumentInfo doc, Model model) { |
Tomasz Mikolajewski | 39acff5 | 2015-11-25 13:01:18 +0900 | [diff] [blame] | 276 | if (doc.isContainer()) { |
| 277 | openContainerDocument(doc); |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 278 | } else { |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 279 | openDocument(doc, model); |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 280 | } |
Steve McKay | c78bcb8 | 2015-07-31 14:35:22 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Launches an intent to view the specified document. |
| 285 | */ |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 286 | private void openDocument(DocumentInfo doc, Model model) { |
| 287 | Intent intent = new QuickViewIntentBuilder( |
| 288 | getPackageManager(), getResources(), doc, model).build(); |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 289 | |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 290 | if (intent != null) { |
| 291 | // TODO: un-work around issue b/24963914. Should be fixed soon. |
| 292 | try { |
| 293 | startActivity(intent); |
| 294 | return; |
| 295 | } catch (SecurityException e) { |
Steve McKay | 1eafb66 | 2015-10-23 09:04:09 -0700 | [diff] [blame] | 296 | // Carry on to regular view mode. |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 297 | Log.e(TAG, "Caught security error: " + e.getLocalizedMessage()); |
| 298 | } |
Steve McKay | c78bcb8 | 2015-07-31 14:35:22 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Steve McKay | 1eafb66 | 2015-10-23 09:04:09 -0700 | [diff] [blame] | 301 | // Fallback to traditional VIEW action... |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 302 | intent = new Intent(Intent.ACTION_VIEW); |
| 303 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 304 | intent.setData(doc.derivedUri); |
| 305 | |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 306 | if (DEBUG && intent.getClipData() != null) { |
| 307 | Log.d(TAG, "Starting intent w/ clip data: " + intent.getClipData()); |
| 308 | } |
| 309 | |
Steve McKay | c78bcb8 | 2015-07-31 14:35:22 -0700 | [diff] [blame] | 310 | try { |
| 311 | startActivity(intent); |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 312 | } catch (ActivityNotFoundException e) { |
| 313 | Snackbars.makeSnackbar( |
| 314 | this, R.string.toast_no_application, Snackbar.LENGTH_SHORT).show(); |
Steve McKay | c78bcb8 | 2015-07-31 14:35:22 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 318 | @Override |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 319 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
| 320 | DirectoryFragment dir; |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 321 | // TODO: All key events should be statically bound using alphabeticShortcut. |
| 322 | // But not working. |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 323 | switch (keyCode) { |
| 324 | case KeyEvent.KEYCODE_A: |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 325 | dir = getDirectoryFragment(); |
| 326 | if (dir != null) { |
| 327 | dir.selectAllFiles(); |
| 328 | } |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 329 | return true; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 330 | case KeyEvent.KEYCODE_C: |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 331 | dir = getDirectoryFragment(); |
| 332 | if (dir != null) { |
| 333 | dir.copySelectedToClipboard(); |
| 334 | } |
Steve McKay | 8fd086a | 2015-12-04 11:19:09 -0800 | [diff] [blame] | 335 | return true; |
| 336 | case KeyEvent.KEYCODE_V: |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 337 | dir = getDirectoryFragment(); |
| 338 | if (dir != null) { |
| 339 | dir.pasteFromClipboard(); |
| 340 | } |
Steve McKay | 8fd086a | 2015-12-04 11:19:09 -0800 | [diff] [blame] | 341 | return true; |
| 342 | default: |
| 343 | return super.onKeyShortcut(keyCode, event); |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Steve McKay | c95d87c | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 347 | // Do some "do what a I want" drawer fiddling, but don't |
| 348 | // do it if user already hit back recently and we recently |
| 349 | // did some fiddling. |
| 350 | @Override |
| 351 | boolean onBeforePopDir() { |
| 352 | int size = mState.stack.size(); |
| 353 | |
| 354 | if (mDrawer.isPresent() |
| 355 | && (System.currentTimeMillis() - mDrawerLastFiddled) > DRAWER_NO_FIDDLE_DELAY) { |
| 356 | // Close drawer if it is open. |
| 357 | if (mDrawer.isOpen()) { |
| 358 | mDrawer.setOpen(false); |
| 359 | mDrawerLastFiddled = System.currentTimeMillis(); |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | // Open the Close drawer if it is closed and we're at the top of a root. |
| 364 | if (size == 1) { |
| 365 | mDrawer.setOpen(true); |
| 366 | // Remember so we don't just close it again if back is pressed again. |
| 367 | mDrawerLastFiddled = System.currentTimeMillis(); |
| 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return false; |
| 373 | } |
| 374 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 375 | // Turns out only DocumentsActivity was ever calling saveStackBlocking. |
| 376 | // There may be a case where we want to contribute entries from |
| 377 | // Behavior here in FilesActivity, but it isn't yet obvious. |
| 378 | // TODO: Contribute to recents, or remove this. |
| 379 | void writeStackToRecentsBlocking() { |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 380 | final ContentResolver resolver = getContentResolver(); |
| 381 | final ContentValues values = new ContentValues(); |
| 382 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 383 | final byte[] rawStack = DurableUtils.writeToArrayOrNull(mState.stack); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 384 | |
| 385 | // Remember location for next app launch |
| 386 | final String packageName = getCallingPackageMaybeExtra(); |
| 387 | values.clear(); |
| 388 | values.put(ResumeColumns.STACK, rawStack); |
| 389 | values.put(ResumeColumns.EXTERNAL, 0); |
| 390 | resolver.insert(RecentsProvider.buildResume(packageName), values); |
| 391 | } |
| 392 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 393 | @Override |
| 394 | void onTaskFinished(Uri... uris) { |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 395 | Log.d(TAG, "onFinished() " + Arrays.toString(uris)); |
| 396 | |
| 397 | final Intent intent = new Intent(); |
| 398 | if (uris.length == 1) { |
| 399 | intent.setData(uris[0]); |
| 400 | } else if (uris.length > 1) { |
| 401 | final ClipData clipData = new ClipData( |
| 402 | null, mState.acceptMimes, new ClipData.Item(uris[0])); |
| 403 | for (int i = 1; i < uris.length; i++) { |
| 404 | clipData.addItem(new ClipData.Item(uris[i])); |
| 405 | } |
| 406 | intent.setClipData(clipData); |
| 407 | } |
| 408 | |
| 409 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 410 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 411 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
| 412 | |
| 413 | setResult(Activity.RESULT_OK, intent); |
| 414 | finish(); |
| 415 | } |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * Builds a stack for the specific Uris. Multi roots are not supported, as it's impossible |
| 419 | * to know which root to select. Also, the stack doesn't contain intermediate directories. |
| 420 | * It's primarly used for opening ZIP archives from Downloads app. |
| 421 | */ |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 422 | private static final class OpenUriForViewTask extends PairedTask<FilesActivity, Uri, Void> { |
| 423 | |
| 424 | private final State mState; |
| 425 | public OpenUriForViewTask(FilesActivity activity) { |
| 426 | super(activity); |
| 427 | mState = activity.mState; |
| 428 | } |
| 429 | |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 430 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 431 | protected Void run(Uri... params) { |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 432 | final Uri uri = params[0]; |
| 433 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 434 | final RootsCache rootsCache = DocumentsApplication.getRootsCache(mOwner); |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 435 | final String authority = uri.getAuthority(); |
| 436 | |
| 437 | final Collection<RootInfo> roots = |
| 438 | rootsCache.getRootsForAuthorityBlocking(authority); |
| 439 | if (roots.isEmpty()) { |
| 440 | Log.e(TAG, "Failed to find root for the requested Uri: " + uri); |
| 441 | return null; |
| 442 | } |
| 443 | |
| 444 | final RootInfo root = roots.iterator().next(); |
| 445 | mState.stack.root = root; |
| 446 | try { |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 447 | mState.stack.add(DocumentInfo.fromUri(mOwner.getContentResolver(), uri)); |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 448 | } catch (FileNotFoundException e) { |
| 449 | Log.e(TAG, "Failed to resolve DocumentInfo from Uri: " + uri); |
| 450 | } |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 451 | mState.stack.add(mOwner.getRootDocumentBlocking(root)); |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 452 | return null; |
| 453 | } |
| 454 | |
| 455 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 456 | protected void finish(Void result) { |
| 457 | mOwner.refreshCurrentRootAndDirectory(ANIM_NONE); |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 458 | } |
| 459 | } |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 460 | } |