blob: d4439d8b480f53a57e60bc29951219476c9c133f [file] [log] [blame]
Steve McKaye934ce62015-03-25 14:35:33 -07001/*
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
17package com.android.documentsui;
18
Steve McKaya7e923c2016-01-28 12:02:57 -080019import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_UNKNOWN;
Steve McKay83df8c02015-09-16 15:07:31 -070020import static com.android.documentsui.Shared.DEBUG;
Steve McKayd0d9afc2015-05-06 12:16:40 -070021
Steve McKaye934ce62015-03-25 14:35:33 -070022import android.app.Activity;
Steve McKaye934ce62015-03-25 14:35:33 -070023import android.app.FragmentManager;
24import android.content.ActivityNotFoundException;
25import android.content.ClipData;
Steve McKaye934ce62015-03-25 14:35:33 -070026import android.content.ContentResolver;
27import android.content.ContentValues;
Steve McKaye934ce62015-03-25 14:35:33 -070028import android.content.Intent;
Steve McKaye934ce62015-03-25 14:35:33 -070029import android.net.Uri;
Steve McKaye934ce62015-03-25 14:35:33 -070030import android.os.Bundle;
Steve McKay323ee3e2015-09-25 16:02:56 -070031import android.os.Parcelable;
32import android.provider.DocumentsContract;
Ben Kwa94b486d2015-09-30 10:00:10 -070033import android.support.design.widget.Snackbar;
Steve McKaye934ce62015-03-25 14:35:33 -070034import android.util.Log;
Steve McKay3da8afc2015-05-05 14:50:00 -070035import android.view.KeyEvent;
Steve McKaye934ce62015-03-25 14:35:33 -070036import android.view.Menu;
37import android.view.MenuItem;
Steve McKaye934ce62015-03-25 14:35:33 -070038
Steve McKaya7e923c2016-01-28 12:02:57 -080039import com.android.documentsui.OperationDialogFragment.DialogType;
Steve McKaye934ce62015-03-25 14:35:33 -070040import com.android.documentsui.RecentsProvider.ResumeColumns;
Steve McKayce710822016-03-11 10:49:32 -080041import com.android.documentsui.dirlist.AnimationView;
Steve McKayf8621552015-11-03 15:23:16 -080042import com.android.documentsui.dirlist.DirectoryFragment;
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +090043import com.android.documentsui.dirlist.Model;
Steve McKaye934ce62015-03-25 14:35:33 -070044import com.android.documentsui.model.DocumentInfo;
45import com.android.documentsui.model.DocumentStack;
46import com.android.documentsui.model.DurableUtils;
47import com.android.documentsui.model.RootInfo;
Steve McKay14e827a2016-01-06 18:32:13 -080048import com.android.documentsui.services.FileOperationService;
Steve McKaye934ce62015-03-25 14:35:33 -070049
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +090050import java.io.FileNotFoundException;
Tomasz Mikolajewski61686592015-04-13 19:38:43 +090051import java.util.ArrayList;
Steve McKaye934ce62015-03-25 14:35:33 -070052import java.util.Arrays;
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +090053import java.util.Collection;
Steve McKaye934ce62015-03-25 14:35:33 -070054import java.util.List;
Steve McKaye934ce62015-03-25 14:35:33 -070055
Steve McKayef3e2cf2015-04-20 17:18:15 -070056/**
Steve McKay273103b2015-05-12 12:49:58 -070057 * Standalone file management activity.
Steve McKayef3e2cf2015-04-20 17:18:15 -070058 */
Steve McKay12055472015-08-20 16:48:49 -070059public class FilesActivity extends BaseActivity {
Steve McKayc78bcb82015-07-31 14:35:22 -070060
Ben Kwa0f7078f02015-09-08 07:31:19 -070061 public static final String TAG = "FilesActivity";
Steve McKaye934ce62015-03-25 14:35:33 -070062
Steve McKayc95d87c2016-02-23 14:34:50 -080063 // See comments where this const is referenced for details.
64 private static final int DRAWER_NO_FIDDLE_DELAY = 1500;
65
66 // Track the time we opened the drawer in response to back being pressed.
67 // We use the time gap to figure out whether to close app or reopen the drawer.
68 private long mDrawerLastFiddled;
Steve McKaybdbd0ff2015-05-20 15:58:42 -070069 private DocumentClipper mClipper;
Steve McKayef3e2cf2015-04-20 17:18:15 -070070
Steve McKay12055472015-08-20 16:48:49 -070071 public FilesActivity() {
72 super(R.layout.files_activity, TAG);
Steve McKayef3e2cf2015-04-20 17:18:15 -070073 }
Steve McKaye934ce62015-03-25 14:35:33 -070074
75 @Override
76 public void onCreate(Bundle icicle) {
Steve McKaye934ce62015-03-25 14:35:33 -070077 super.onCreate(icicle);
78
Steve McKay12055472015-08-20 16:48:49 -070079 mClipper = new DocumentClipper(this);
Steve McKaybdbd0ff2015-05-20 15:58:42 -070080
Steve McKaye934ce62015-03-25 14:35:33 -070081 RootsFragment.show(getFragmentManager(), null);
Steve McKay83df8c02015-09-16 15:07:31 -070082
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +090083 final Intent intent = getIntent();
84 final Uri uri = intent.getData();
Steve McKay323ee3e2015-09-25 16:02:56 -070085
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +090086 if (mState.restored) {
87 if (DEBUG) Log.d(TAG, "Stack already resolved for uri: " + intent.getData());
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +090088 } else if (!mState.stack.isEmpty()) {
Steve McKay323ee3e2015-09-25 16:02:56 -070089 // If a non-empty stack is present in our state it was read (presumably)
90 // from EXTRA_STACK intent extra. In this case, we'll skip other means of
91 // loading or restoring the stack.
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +090092 //
93 // When restoring from a stack, if a URI is present, it should only ever
Tomasz Mikolajewskicd270152016-02-01 12:01:14 +090094 // be a launch URI, or a fake Uri from notifications.
95 // Launch URIs support sensible activity management, but don't specify a real
96 // content target.
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +090097 if (DEBUG) Log.d(TAG, "Launching with non-empty stack.");
Steve McKaya1f76802016-02-25 13:34:03 -080098 assert(uri == null || uri.getAuthority() == null ||
Tomasz Mikolajewskicd270152016-02-01 12:01:14 +090099 LauncherActivity.isLaunchUri(uri));
Steve McKayce710822016-03-11 10:49:32 -0800100 refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900101 } else if (intent.getAction() == Intent.ACTION_VIEW) {
Steve McKaya1f76802016-02-25 13:34:03 -0800102 assert(uri != null);
Steve McKay95cd85a2016-02-04 12:15:22 -0800103 new OpenUriForViewTask(this).executeOnExecutor(
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900104 ProviderExecutor.forAuthority(uri.getAuthority()), uri);
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +0900105 } else if (DocumentsContract.isRootUri(this, uri)) {
106 if (DEBUG) Log.d(TAG, "Launching with root URI.");
107 // If we've got a specific root to display, restore that root using a dedicated
108 // authority. That way a misbehaving provider won't result in an ANR.
Steve McKayc95d87c2016-02-23 14:34:50 -0800109 loadRoot(uri);
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +0900110 } else {
Steve McKay6ce903d2016-03-09 15:20:00 -0800111 if (DEBUG) Log.d(TAG, "All other means skipped. Launching into default directory.");
Aga Wronska4e627162016-03-22 14:18:43 -0700112 loadRoot(getDefaultRoot());
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +0900113 }
Tomasz Mikolajewskif8c3f322015-04-14 16:32:41 +0900114
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900115 final @DialogType int dialogType = intent.getIntExtra(
116 FileOperationService.EXTRA_DIALOG_TYPE, DIALOG_TYPE_UNKNOWN);
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +0900117 // DialogFragment takes care of restoring the dialog on configuration change.
118 // Only show it manually for the first time (icicle is null).
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900119 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 McKay14e827a2016-01-06 18:32:13 -0800124 intent.getParcelableArrayListExtra(FileOperationService.EXTRA_SRC_LIST);
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900125 OperationDialogFragment.show(
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +0900126 getFragmentManager(),
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900127 dialogType,
128 srcList,
Tomasz Mikolajewskiaa684452015-12-25 17:06:52 +0900129 mState.stack,
Steve McKay14e827a2016-01-06 18:32:13 -0800130 opType);
Steve McKaye934ce62015-03-25 14:35:33 -0700131 }
132 }
133
Steve McKay12055472015-08-20 16:48:49 -0700134 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -0800135 void includeState(State state) {
Steve McKaye934ce62015-03-25 14:35:33 -0700136 final Intent intent = getIntent();
Ben Kwa0f7078f02015-09-08 07:31:19 -0700137
Steve McKay83df8c02015-09-16 15:07:31 -0700138 state.action = State.ACTION_BROWSE;
Ben Kwa0f7078f02015-09-08 07:31:19 -0700139 state.allowMultiple = true;
Steve McKay273103b2015-05-12 12:49:58 -0700140
Steve McKay83df8c02015-09-16 15:07:31 -0700141 // Options specific to the DocumentsActivity.
Steve McKaya1f76802016-02-25 13:34:03 -0800142 assert(!intent.hasExtra(Intent.EXTRA_LOCAL_ONLY));
Steve McKay273103b2015-05-12 12:49:58 -0700143
Steve McKay323ee3e2015-09-25 16:02:56 -0700144 final DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK);
Steve McKay83df8c02015-09-16 15:07:31 -0700145 if (stack != null) {
Steve McKayef3e2cf2015-04-20 17:18:15 -0700146 state.stack = stack;
Steve McKay83df8c02015-09-16 15:07:31 -0700147 }
Steve McKaye934ce62015-03-25 14:35:33 -0700148 }
149
150 @Override
151 protected void onPostCreate(Bundle savedInstanceState) {
152 super.onPostCreate(savedInstanceState);
Steve McKayb67bfbf2015-12-08 17:02:03 -0800153 // 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 McKay1f264a82016-02-03 11:15:57 -0800158 // update will be called once Home root is loaded.
Steve McKayb67bfbf2015-12-08 17:02:03 -0800159 // Except while searching we need this call to ensure the
160 // search bits get layed out correctly.
161 if (mSearchManager.isSearching()) {
Steve McKay1f264a82016-02-03 11:15:57 -0800162 mNavigator.update();
Steve McKayb67bfbf2015-12-08 17:02:03 -0800163 }
Steve McKaye934ce62015-03-25 14:35:33 -0700164 }
165
166 @Override
Steve McKay83df8c02015-09-16 15:07:31 -0700167 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 McKay1f264a82016-02-03 11:15:57 -0800182 public String getDrawerTitle() {
Daichi Hirono2917fa92016-03-18 17:46:57 +0900183 return getResources().getString(R.string.downloads_label);
Steve McKaye934ce62015-03-25 14:35:33 -0700184 }
185
186 @Override
Steve McKaye934ce62015-03-25 14:35:33 -0700187 public boolean onPrepareOptionsMenu(Menu menu) {
Steve McKay5bbae102015-10-01 11:39:24 -0700188 super.onPrepareOptionsMenu(menu);
Steve McKay3ce95952016-02-02 11:41:03 -0800189
Steve McKayefa17612016-01-29 18:15:39 -0800190 final RootInfo root = getCurrentRoot();
Steve McKay9f9d5b42015-09-23 15:44:24 -0700191
Steve McKaye934ce62015-03-25 14:35:33 -0700192 final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
Steve McKay9f9d5b42015-09-23 15:44:24 -0700193 final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard);
Steve McKayefa17612016-01-29 18:15:39 -0800194 final MenuItem settings = menu.findItem(R.id.menu_settings);
Steve McKayf8737692016-02-04 19:40:45 -0800195 final MenuItem newWindow = menu.findItem(R.id.menu_new_window);
Steve McKaye934ce62015-03-25 14:35:33 -0700196
Steve McKay5bbae102015-10-01 11:39:24 -0700197 createDir.setVisible(true);
198 createDir.setEnabled(canCreateDirectory());
Steve McKay5bbae102015-10-01 11:39:24 -0700199 pasteFromCb.setEnabled(mClipper.hasItemsToPaste());
Steve McKayefa17612016-01-29 18:15:39 -0800200 settings.setVisible(root.hasSettings());
Steve McKayf8737692016-02-04 19:40:45 -0800201 newWindow.setVisible(true);
Steve McKaya521d0d2015-05-19 16:10:25 -0700202
Steve McKay5bbae102015-10-01 11:39:24 -0700203 Menus.disableHiddenItems(menu, pasteFromCb);
Aga Wronskab0998562016-03-24 18:22:09 -0700204 // It hides icon if searching in progress
205 mSearchManager.updateMenu();
Steve McKay5bbae102015-10-01 11:39:24 -0700206 return true;
Steve McKaye934ce62015-03-25 14:35:33 -0700207 }
208
209 @Override
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700210 public boolean onOptionsItemSelected(MenuItem item) {
Steve McKay9f9d5b42015-09-23 15:44:24 -0700211 switch (item.getItemId()) {
212 case R.id.menu_create_dir:
Steve McKaya1f76802016-02-25 13:34:03 -0800213 assert(canCreateDirectory());
Steve McKay9f9d5b42015-09-23 15:44:24 -0700214 showCreateDirectoryDialog();
Aga Wronskacf966ae2016-03-30 13:55:19 -0700215 break;
Steve McKay9f9d5b42015-09-23 15:44:24 -0700216 case R.id.menu_new_window:
Steve McKay323ee3e2015-09-25 16:02:56 -0700217 createNewWindow();
Aga Wronskacf966ae2016-03-30 13:55:19 -0700218 break;
Steve McKay9f9d5b42015-09-23 15:44:24 -0700219 case R.id.menu_paste_from_clipboard:
Steve McKay3ce95952016-02-02 11:41:03 -0800220 DirectoryFragment dir = getDirectoryFragment();
221 if (dir != null) {
222 dir.pasteFromClipboard();
223 }
Aga Wronskacf966ae2016-03-30 13:55:19 -0700224 break;
225 default:
226 return super.onOptionsItemSelected(item);
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700227 }
228
Aga Wronskacf966ae2016-03-30 13:55:19 -0700229 Metrics.logMenuAction(this, item.getItemId());
230 return true;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700231 }
232
Steve McKay323ee3e2015-09-25 16:02:56 -0700233 private void createNewWindow() {
Ben Kwa72379982016-01-26 11:50:03 -0800234 Metrics.logMultiWindow(this);
Steve McKay323ee3e2015-09-25 16:02:56 -0700235 Intent intent = LauncherActivity.createLaunchIntent(this);
236 intent.putExtra(Shared.EXTRA_STACK, (Parcelable) mState.stack);
Steve McKaya7e923c2016-01-28 12:02:57 -0800237
238 // With new multi-window mode we have to pick how we are launched.
239 // By default we'd be launched in-place above the existing app.
240 // By setting launch-to-side ActivityManager will open us to side.
Andrii Kulian933076d2016-03-29 17:04:42 -0700241 if (isInMultiWindowMode()) {
Wale Ogunwale2a25a622016-01-30 11:27:21 -0800242 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
Steve McKaya7e923c2016-01-28 12:02:57 -0800243 }
244
Steve McKay323ee3e2015-09-25 16:02:56 -0700245 startActivity(intent);
246 }
247
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700248 @Override
Aga Wronskaf6a31d32016-01-15 17:30:15 -0800249 void refreshDirectory(int anim) {
Steve McKaye934ce62015-03-25 14:35:33 -0700250 final FragmentManager fm = getFragmentManager();
251 final RootInfo root = getCurrentRoot();
252 final DocumentInfo cwd = getCurrentDirectory();
253
Steve McKaya1f76802016-02-25 13:34:03 -0800254 assert(!mSearchManager.isSearching());
Aga Wronska893390b2016-02-17 13:50:42 -0800255
Steve McKaye934ce62015-03-25 14:35:33 -0700256 if (cwd == null) {
257 DirectoryFragment.showRecentsOpen(fm, anim);
Steve McKaye934ce62015-03-25 14:35:33 -0700258 } else {
Aga Wronska893390b2016-02-17 13:50:42 -0800259 // Normal boring directory
260 DirectoryFragment.showDirectory(fm, root, cwd, anim);
Steve McKaye934ce62015-03-25 14:35:33 -0700261 }
Steve McKaye934ce62015-03-25 14:35:33 -0700262 }
263
264 @Override
Steve McKay4f160402015-08-17 13:18:05 -0700265 void onRootPicked(RootInfo root) {
266 super.onRootPicked(root);
267 mDrawer.setOpen(false);
268 }
269
270 @Override
Steve McKay6eaf38632015-08-04 10:11:01 -0700271 public void onDocumentsPicked(List<DocumentInfo> docs) {
272 throw new UnsupportedOperationException();
Steve McKaye934ce62015-03-25 14:35:33 -0700273 }
274
Steve McKay6eaf38632015-08-04 10:11:01 -0700275 @Override
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900276 public void onDocumentPicked(DocumentInfo doc, Model model) {
Tomasz Mikolajewski39acff52015-11-25 13:01:18 +0900277 if (doc.isContainer()) {
278 openContainerDocument(doc);
Steve McKay6eaf38632015-08-04 10:11:01 -0700279 } else {
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900280 openDocument(doc, model);
Steve McKay6eaf38632015-08-04 10:11:01 -0700281 }
Steve McKayc78bcb82015-07-31 14:35:22 -0700282 }
283
284 /**
285 * Launches an intent to view the specified document.
286 */
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900287 private void openDocument(DocumentInfo doc, Model model) {
Steve McKay9bb98f32016-03-07 11:49:05 -0800288
Steve McKay3e63e7d2016-03-11 10:11:52 -0800289 // Anything on downloads goes through the back through downloads manager
290 // (that's the MANAGE_DOCUMENT bit).
291 // This is done for two reasons:
292 // 1) The file in question might be a failed/queued or otherwise have some
293 // specialized download handling.
294 // 2) For APKs, the download manager will add on some important security stuff
295 // like origin URL.
296 // All other files not on downloads, event APKs, would get no benefit from this
297 // treatment, thusly the "isDownloads" check.
298 if (getCurrentRoot().isDownloads()) {
Steve McKay9bb98f32016-03-07 11:49:05 -0800299 // First try managing the document; we expect manager to filter
300 // based on authority, so we don't grant.
301 final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT);
302 manage.setData(doc.derivedUri);
303
304 try {
305 startActivity(manage);
306 return;
307 } catch (ActivityNotFoundException ex) {
308 // fall back to regular handling below.
309 }
310 }
311
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900312 Intent intent = new QuickViewIntentBuilder(
313 getPackageManager(), getResources(), doc, model).build();
Steve McKay6eaf38632015-08-04 10:11:01 -0700314
Steve McKay3b2ad112015-10-15 15:27:30 -0700315 if (intent != null) {
316 // TODO: un-work around issue b/24963914. Should be fixed soon.
317 try {
318 startActivity(intent);
319 return;
320 } catch (SecurityException e) {
Steve McKay1eafb662015-10-23 09:04:09 -0700321 // Carry on to regular view mode.
Steve McKay3b2ad112015-10-15 15:27:30 -0700322 Log.e(TAG, "Caught security error: " + e.getLocalizedMessage());
323 }
Steve McKayc78bcb82015-07-31 14:35:22 -0700324 }
325
Steve McKay9bb98f32016-03-07 11:49:05 -0800326 // Fall back to traditional VIEW action...
Steve McKay3b2ad112015-10-15 15:27:30 -0700327 intent = new Intent(Intent.ACTION_VIEW);
328 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
329 intent.setData(doc.derivedUri);
330
Steve McKay6eaf38632015-08-04 10:11:01 -0700331 if (DEBUG && intent.getClipData() != null) {
332 Log.d(TAG, "Starting intent w/ clip data: " + intent.getClipData());
333 }
334
Steve McKayc78bcb82015-07-31 14:35:22 -0700335 try {
336 startActivity(intent);
Steve McKay3b2ad112015-10-15 15:27:30 -0700337 } catch (ActivityNotFoundException e) {
338 Snackbars.makeSnackbar(
339 this, R.string.toast_no_application, Snackbar.LENGTH_SHORT).show();
Steve McKayc78bcb82015-07-31 14:35:22 -0700340 }
341 }
342
Steve McKaye934ce62015-03-25 14:35:33 -0700343 @Override
Steve McKay3da8afc2015-05-05 14:50:00 -0700344 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
345 DirectoryFragment dir;
Steve McKay3ce95952016-02-02 11:41:03 -0800346 // TODO: All key events should be statically bound using alphabeticShortcut.
347 // But not working.
Steve McKay3da8afc2015-05-05 14:50:00 -0700348 switch (keyCode) {
349 case KeyEvent.KEYCODE_A:
Steve McKay3ce95952016-02-02 11:41:03 -0800350 dir = getDirectoryFragment();
351 if (dir != null) {
Aga Wronska0614c162016-03-31 15:08:43 -0700352 Metrics.logKeyboardAction(this, keyCode);
Steve McKay3ce95952016-02-02 11:41:03 -0800353 dir.selectAllFiles();
354 }
Steve McKay3da8afc2015-05-05 14:50:00 -0700355 return true;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700356 case KeyEvent.KEYCODE_C:
Steve McKay3ce95952016-02-02 11:41:03 -0800357 dir = getDirectoryFragment();
358 if (dir != null) {
Aga Wronska0614c162016-03-31 15:08:43 -0700359 Metrics.logKeyboardAction(this, keyCode);
Steve McKay3ce95952016-02-02 11:41:03 -0800360 dir.copySelectedToClipboard();
361 }
Steve McKay8fd086a2015-12-04 11:19:09 -0800362 return true;
363 case KeyEvent.KEYCODE_V:
Steve McKay3ce95952016-02-02 11:41:03 -0800364 dir = getDirectoryFragment();
365 if (dir != null) {
Aga Wronska0614c162016-03-31 15:08:43 -0700366 Metrics.logKeyboardAction(this, keyCode);
Steve McKay3ce95952016-02-02 11:41:03 -0800367 dir.pasteFromClipboard();
368 }
Steve McKay8fd086a2015-12-04 11:19:09 -0800369 return true;
370 default:
371 return super.onKeyShortcut(keyCode, event);
Steve McKay3da8afc2015-05-05 14:50:00 -0700372 }
373 }
374
Steve McKayc95d87c2016-02-23 14:34:50 -0800375 // Do some "do what a I want" drawer fiddling, but don't
376 // do it if user already hit back recently and we recently
377 // did some fiddling.
378 @Override
379 boolean onBeforePopDir() {
380 int size = mState.stack.size();
381
382 if (mDrawer.isPresent()
383 && (System.currentTimeMillis() - mDrawerLastFiddled) > DRAWER_NO_FIDDLE_DELAY) {
384 // Close drawer if it is open.
385 if (mDrawer.isOpen()) {
386 mDrawer.setOpen(false);
387 mDrawerLastFiddled = System.currentTimeMillis();
388 return true;
389 }
390
391 // Open the Close drawer if it is closed and we're at the top of a root.
Aga Wronska96e30eb2016-03-23 17:12:33 -0700392 if (size <= 1) {
Steve McKayc95d87c2016-02-23 14:34:50 -0800393 mDrawer.setOpen(true);
394 // Remember so we don't just close it again if back is pressed again.
395 mDrawerLastFiddled = System.currentTimeMillis();
396 return true;
397 }
398 }
399
400 return false;
401 }
402
Steve McKay95cd85a2016-02-04 12:15:22 -0800403 // Turns out only DocumentsActivity was ever calling saveStackBlocking.
404 // There may be a case where we want to contribute entries from
405 // Behavior here in FilesActivity, but it isn't yet obvious.
406 // TODO: Contribute to recents, or remove this.
407 void writeStackToRecentsBlocking() {
Steve McKaye934ce62015-03-25 14:35:33 -0700408 final ContentResolver resolver = getContentResolver();
409 final ContentValues values = new ContentValues();
410
Steve McKay95cd85a2016-02-04 12:15:22 -0800411 final byte[] rawStack = DurableUtils.writeToArrayOrNull(mState.stack);
Steve McKaye934ce62015-03-25 14:35:33 -0700412
413 // Remember location for next app launch
414 final String packageName = getCallingPackageMaybeExtra();
415 values.clear();
416 values.put(ResumeColumns.STACK, rawStack);
417 values.put(ResumeColumns.EXTERNAL, 0);
418 resolver.insert(RecentsProvider.buildResume(packageName), values);
419 }
420
Steve McKayef3e2cf2015-04-20 17:18:15 -0700421 @Override
422 void onTaskFinished(Uri... uris) {
Ben Kwaffa829f2016-03-22 11:11:46 -0700423 if (DEBUG) Log.d(TAG, "onFinished() " + Arrays.toString(uris));
Steve McKaye934ce62015-03-25 14:35:33 -0700424
425 final Intent intent = new Intent();
426 if (uris.length == 1) {
427 intent.setData(uris[0]);
428 } else if (uris.length > 1) {
429 final ClipData clipData = new ClipData(
430 null, mState.acceptMimes, new ClipData.Item(uris[0]));
431 for (int i = 1; i < uris.length; i++) {
432 clipData.addItem(new ClipData.Item(uris[i]));
433 }
434 intent.setClipData(clipData);
435 }
436
437 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
438 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
439 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
440
441 setResult(Activity.RESULT_OK, intent);
442 finish();
443 }
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900444
445 /**
446 * Builds a stack for the specific Uris. Multi roots are not supported, as it's impossible
447 * to know which root to select. Also, the stack doesn't contain intermediate directories.
448 * It's primarly used for opening ZIP archives from Downloads app.
449 */
Steve McKay95cd85a2016-02-04 12:15:22 -0800450 private static final class OpenUriForViewTask extends PairedTask<FilesActivity, Uri, Void> {
451
452 private final State mState;
453 public OpenUriForViewTask(FilesActivity activity) {
454 super(activity);
455 mState = activity.mState;
456 }
457
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900458 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -0800459 protected Void run(Uri... params) {
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900460 final Uri uri = params[0];
461
Steve McKay95cd85a2016-02-04 12:15:22 -0800462 final RootsCache rootsCache = DocumentsApplication.getRootsCache(mOwner);
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900463 final String authority = uri.getAuthority();
464
465 final Collection<RootInfo> roots =
466 rootsCache.getRootsForAuthorityBlocking(authority);
467 if (roots.isEmpty()) {
468 Log.e(TAG, "Failed to find root for the requested Uri: " + uri);
469 return null;
470 }
471
472 final RootInfo root = roots.iterator().next();
473 mState.stack.root = root;
474 try {
Steve McKay95cd85a2016-02-04 12:15:22 -0800475 mState.stack.add(DocumentInfo.fromUri(mOwner.getContentResolver(), uri));
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900476 } catch (FileNotFoundException e) {
477 Log.e(TAG, "Failed to resolve DocumentInfo from Uri: " + uri);
478 }
Steve McKay95cd85a2016-02-04 12:15:22 -0800479 mState.stack.add(mOwner.getRootDocumentBlocking(root));
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900480 return null;
481 }
482
483 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -0800484 protected void finish(Void result) {
Steve McKayce710822016-03-11 10:49:32 -0800485 mOwner.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900486 }
487 }
Steve McKaye934ce62015-03-25 14:35:33 -0700488}