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