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