blob: 84fc6fe2dee16cf709f6c16967e47cbe9268cd7e [file] [log] [blame]
Steve McKayd0a2a2c2015-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 McKaydf5add42016-01-28 12:02:57 -080019import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_UNKNOWN;
Steve McKay459bc2b2015-09-16 15:07:31 -070020import static com.android.documentsui.Shared.DEBUG;
Steve McKay7ef09a22015-05-06 12:16:40 -070021
Steve McKayd0a2a2c2015-03-25 14:35:33 -070022import android.app.Activity;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070023import android.app.FragmentManager;
24import android.content.ActivityNotFoundException;
25import android.content.ClipData;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070026import android.content.ContentResolver;
27import android.content.ContentValues;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070028import android.content.Intent;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070029import android.net.Uri;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070030import android.os.Bundle;
Steve McKay4d0255f2015-09-25 16:02:56 -070031import android.os.Parcelable;
32import android.provider.DocumentsContract;
Ben Kwac4693342015-09-30 10:00:10 -070033import android.support.design.widget.Snackbar;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070034import android.util.Log;
Steve McKay0599a442015-05-05 14:50:00 -070035import android.view.KeyEvent;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070036import android.view.Menu;
37import android.view.MenuItem;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070038
Steve McKaydf5add42016-01-28 12:02:57 -080039import com.android.documentsui.OperationDialogFragment.DialogType;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070040import com.android.documentsui.RecentsProvider.ResumeColumns;
Steve McKayfb4fd2f2016-03-11 10:49:32 -080041import com.android.documentsui.dirlist.AnimationView;
Steve McKayf68210e2015-11-03 15:23:16 -080042import com.android.documentsui.dirlist.DirectoryFragment;
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090043import com.android.documentsui.dirlist.Model;
Steve McKayd0a2a2c2015-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 McKayc83baa02016-01-06 18:32:13 -080048import com.android.documentsui.services.FileOperationService;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070049
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +090050import java.io.FileNotFoundException;
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090051import java.util.ArrayList;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070052import java.util.Arrays;
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +090053import java.util.Collection;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070054import java.util.List;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070055
Steve McKayb68dd222015-04-20 17:18:15 -070056/**
Steve McKay64ac2512015-05-12 12:49:58 -070057 * Standalone file management activity.
Steve McKayb68dd222015-04-20 17:18:15 -070058 */
Steve McKay0fbfc652015-08-20 16:48:49 -070059public class FilesActivity extends BaseActivity {
Steve McKayedc65bb2015-07-31 14:35:22 -070060
Ben Kwa0574b182015-09-08 07:31:19 -070061 public static final String TAG = "FilesActivity";
Steve McKayd0a2a2c2015-03-25 14:35:33 -070062
Steve McKayfd8425a2016-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 McKay1f199482015-05-20 15:58:42 -070069 private DocumentClipper mClipper;
Steve McKayb68dd222015-04-20 17:18:15 -070070
Steve McKay0fbfc652015-08-20 16:48:49 -070071 public FilesActivity() {
72 super(R.layout.files_activity, TAG);
Steve McKayb68dd222015-04-20 17:18:15 -070073 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -070074
75 @Override
76 public void onCreate(Bundle icicle) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -070077 super.onCreate(icicle);
78
Steve McKay0fbfc652015-08-20 16:48:49 -070079 mClipper = new DocumentClipper(this);
Steve McKay1f199482015-05-20 15:58:42 -070080
Steve McKayd0a2a2c2015-03-25 14:35:33 -070081 RootsFragment.show(getFragmentManager(), null);
Steve McKay459bc2b2015-09-16 15:07:31 -070082
Tomasz Mikolajewskicdbbbe02015-12-25 17:06:52 +090083 final Intent intent = getIntent();
84 final Uri uri = intent.getData();
Steve McKay4d0255f2015-09-25 16:02:56 -070085
Tomasz Mikolajewskicdbbbe02015-12-25 17:06:52 +090086 if (mState.restored) {
87 if (DEBUG) Log.d(TAG, "Stack already resolved for uri: " + intent.getData());
Tomasz Mikolajewskicdbbbe02015-12-25 17:06:52 +090088 } else if (!mState.stack.isEmpty()) {
Steve McKay4d0255f2015-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 Mikolajewskicdbbbe02015-12-25 17:06:52 +090092 //
93 // When restoring from a stack, if a URI is present, it should only ever
Tomasz Mikolajewski63e2aae2016-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 Mikolajewskicdbbbe02015-12-25 17:06:52 +090097 if (DEBUG) Log.d(TAG, "Launching with non-empty stack.");
Steve McKay0af8afd2016-02-25 13:34:03 -080098 assert(uri == null || uri.getAuthority() == null ||
Tomasz Mikolajewski63e2aae2016-02-01 12:01:14 +090099 LauncherActivity.isLaunchUri(uri));
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800100 refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
Tomasz Mikolajewskibe4e38b2016-04-07 18:00:08 +0900101 } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Steve McKay0af8afd2016-02-25 13:34:03 -0800102 assert(uri != null);
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800103 new OpenUriForViewTask(this).executeOnExecutor(
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900104 ProviderExecutor.forAuthority(uri.getAuthority()), uri);
Tomasz Mikolajewskicdbbbe02015-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 McKayfd8425a2016-02-23 14:34:50 -0800109 loadRoot(uri);
Tomasz Mikolajewskicdbbbe02015-12-25 17:06:52 +0900110 } else {
Steve McKay15b92782016-03-09 15:20:00 -0800111 if (DEBUG) Log.d(TAG, "All other means skipped. Launching into default directory.");
Aga Wronska64ae1f42016-03-22 14:18:43 -0700112 loadRoot(getDefaultRoot());
Tomasz Mikolajewskicdbbbe02015-12-25 17:06:52 +0900113 }
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900114
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +0900115 final @DialogType int dialogType = intent.getIntExtra(
116 FileOperationService.EXTRA_DIALOG_TYPE, DIALOG_TYPE_UNKNOWN);
Tomasz Mikolajewskicdbbbe02015-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 Mikolajewskidd2b31c2016-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 McKayc83baa02016-01-06 18:32:13 -0800124 intent.getParcelableArrayListExtra(FileOperationService.EXTRA_SRC_LIST);
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +0900125 OperationDialogFragment.show(
Tomasz Mikolajewskicdbbbe02015-12-25 17:06:52 +0900126 getFragmentManager(),
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +0900127 dialogType,
128 srcList,
Tomasz Mikolajewskicdbbbe02015-12-25 17:06:52 +0900129 mState.stack,
Steve McKayc83baa02016-01-06 18:32:13 -0800130 opType);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700131 }
132 }
133
Steve McKay0fbfc652015-08-20 16:48:49 -0700134 @Override
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800135 void includeState(State state) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700136 final Intent intent = getIntent();
Ben Kwa0574b182015-09-08 07:31:19 -0700137
Steve McKay459bc2b2015-09-16 15:07:31 -0700138 state.action = State.ACTION_BROWSE;
Ben Kwa0574b182015-09-08 07:31:19 -0700139 state.allowMultiple = true;
Steve McKay64ac2512015-05-12 12:49:58 -0700140
Steve McKay459bc2b2015-09-16 15:07:31 -0700141 // Options specific to the DocumentsActivity.
Steve McKay0af8afd2016-02-25 13:34:03 -0800142 assert(!intent.hasExtra(Intent.EXTRA_LOCAL_ONLY));
Steve McKay64ac2512015-05-12 12:49:58 -0700143
Steve McKay4d0255f2015-09-25 16:02:56 -0700144 final DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK);
Steve McKay459bc2b2015-09-16 15:07:31 -0700145 if (stack != null) {
Steve McKayb68dd222015-04-20 17:18:15 -0700146 state.stack = stack;
Steve McKay459bc2b2015-09-16 15:07:31 -0700147 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700148 }
149
150 @Override
151 protected void onPostCreate(Bundle savedInstanceState) {
152 super.onPostCreate(savedInstanceState);
Steve McKaya8fa58d2015-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 McKay18d01e82016-02-03 11:15:57 -0800158 // update will be called once Home root is loaded.
Steve McKaya8fa58d2015-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 McKay18d01e82016-02-03 11:15:57 -0800162 mNavigator.update();
Steve McKaya8fa58d2015-12-08 17:02:03 -0800163 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700164 }
165
166 @Override
Steve McKay459bc2b2015-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 McKay18d01e82016-02-03 11:15:57 -0800182 public String getDrawerTitle() {
Steve McKay17f7e582016-04-04 15:26:48 -0700183 Intent intent = getIntent();
184 return (intent != null && intent.hasExtra(Intent.EXTRA_TITLE))
185 ? intent.getStringExtra(Intent.EXTRA_TITLE)
186 : getTitle().toString();
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700187 }
188
189 @Override
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700190 public boolean onPrepareOptionsMenu(Menu menu) {
Steve McKaye9809272015-10-01 11:39:24 -0700191 super.onPrepareOptionsMenu(menu);
Steve McKayd4800812016-02-02 11:41:03 -0800192
Steve McKay36787542016-01-29 18:15:39 -0800193 final RootInfo root = getCurrentRoot();
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700194
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700195 final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700196 final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard);
Steve McKay36787542016-01-29 18:15:39 -0800197 final MenuItem settings = menu.findItem(R.id.menu_settings);
Steve McKay95c79f52016-02-04 19:40:45 -0800198 final MenuItem newWindow = menu.findItem(R.id.menu_new_window);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700199
Steve McKaye9809272015-10-01 11:39:24 -0700200 createDir.setVisible(true);
201 createDir.setEnabled(canCreateDirectory());
Steve McKaye9809272015-10-01 11:39:24 -0700202 pasteFromCb.setEnabled(mClipper.hasItemsToPaste());
Steve McKay36787542016-01-29 18:15:39 -0800203 settings.setVisible(root.hasSettings());
Steve McKay95c79f52016-02-04 19:40:45 -0800204 newWindow.setVisible(true);
Steve McKayceeb3f72015-05-19 16:10:25 -0700205
Steve McKaye9809272015-10-01 11:39:24 -0700206 Menus.disableHiddenItems(menu, pasteFromCb);
Aga Wronska8e21daa2016-03-24 18:22:09 -0700207 // It hides icon if searching in progress
208 mSearchManager.updateMenu();
Steve McKaye9809272015-10-01 11:39:24 -0700209 return true;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700210 }
211
212 @Override
Steve McKay1f199482015-05-20 15:58:42 -0700213 public boolean onOptionsItemSelected(MenuItem item) {
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700214 switch (item.getItemId()) {
215 case R.id.menu_create_dir:
Steve McKay0af8afd2016-02-25 13:34:03 -0800216 assert(canCreateDirectory());
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700217 showCreateDirectoryDialog();
Aga Wronska4972d712016-03-30 13:55:19 -0700218 break;
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700219 case R.id.menu_new_window:
Steve McKay4d0255f2015-09-25 16:02:56 -0700220 createNewWindow();
Aga Wronska4972d712016-03-30 13:55:19 -0700221 break;
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700222 case R.id.menu_paste_from_clipboard:
Steve McKayd4800812016-02-02 11:41:03 -0800223 DirectoryFragment dir = getDirectoryFragment();
224 if (dir != null) {
225 dir.pasteFromClipboard();
226 }
Aga Wronska4972d712016-03-30 13:55:19 -0700227 break;
228 default:
229 return super.onOptionsItemSelected(item);
Steve McKay1f199482015-05-20 15:58:42 -0700230 }
Aga Wronska4972d712016-03-30 13:55:19 -0700231 return true;
Steve McKay1f199482015-05-20 15:58:42 -0700232 }
233
Steve McKay4d0255f2015-09-25 16:02:56 -0700234 private void createNewWindow() {
Aga Wronska94e53e42016-04-07 13:09:58 -0700235 Metrics.logUserAction(this, Metrics.USER_ACTION_NEW_WINDOW);
236
Steve McKay4d0255f2015-09-25 16:02:56 -0700237 Intent intent = LauncherActivity.createLaunchIntent(this);
238 intent.putExtra(Shared.EXTRA_STACK, (Parcelable) mState.stack);
Steve McKaydf5add42016-01-28 12:02:57 -0800239
240 // With new multi-window mode we have to pick how we are launched.
241 // By default we'd be launched in-place above the existing app.
242 // By setting launch-to-side ActivityManager will open us to side.
Andrii Kulian130e0612016-03-29 17:04:42 -0700243 if (isInMultiWindowMode()) {
Wale Ogunwale7f81a692016-01-30 11:27:21 -0800244 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
Steve McKaydf5add42016-01-28 12:02:57 -0800245 }
246
Steve McKay4d0255f2015-09-25 16:02:56 -0700247 startActivity(intent);
248 }
249
Steve McKay1f199482015-05-20 15:58:42 -0700250 @Override
Aga Wronska8788dad2016-01-15 17:30:15 -0800251 void refreshDirectory(int anim) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700252 final FragmentManager fm = getFragmentManager();
253 final RootInfo root = getCurrentRoot();
254 final DocumentInfo cwd = getCurrentDirectory();
255
Steve McKay0af8afd2016-02-25 13:34:03 -0800256 assert(!mSearchManager.isSearching());
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800257
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700258 if (cwd == null) {
259 DirectoryFragment.showRecentsOpen(fm, anim);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700260 } else {
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800261 // Normal boring directory
262 DirectoryFragment.showDirectory(fm, root, cwd, anim);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700263 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700264 }
265
266 @Override
Steve McKay6bbed4d2015-08-17 13:18:05 -0700267 void onRootPicked(RootInfo root) {
268 super.onRootPicked(root);
269 mDrawer.setOpen(false);
270 }
271
272 @Override
Steve McKay351a7492015-08-04 10:11:01 -0700273 public void onDocumentsPicked(List<DocumentInfo> docs) {
274 throw new UnsupportedOperationException();
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700275 }
276
Steve McKay351a7492015-08-04 10:11:01 -0700277 @Override
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900278 public void onDocumentPicked(DocumentInfo doc, Model model) {
Steve McKaybeac2232016-03-11 10:11:52 -0800279 // Anything on downloads goes through the back through downloads manager
280 // (that's the MANAGE_DOCUMENT bit).
281 // This is done for two reasons:
282 // 1) The file in question might be a failed/queued or otherwise have some
283 // specialized download handling.
284 // 2) For APKs, the download manager will add on some important security stuff
285 // like origin URL.
286 // All other files not on downloads, event APKs, would get no benefit from this
287 // treatment, thusly the "isDownloads" check.
Tomasz Mikolajewskibe4e38b2016-04-07 18:00:08 +0900288
289 // Launch MANAGE_DOCUMENTS only for the root level files, so it's not called for
290 // files in archives. Also, if the activity is already browsing a ZIP from downloads,
291 // then skip MANAGE_DOCUMENTS.
292 final boolean isViewing = Intent.ACTION_VIEW.equals(getIntent().getAction());
293 final boolean isInArchive = mState.stack.size() > 1;
294 if (getCurrentRoot().isDownloads() && !isInArchive && !isViewing) {
Steve McKay26444292016-03-07 11:49:05 -0800295 // First try managing the document; we expect manager to filter
296 // based on authority, so we don't grant.
297 final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT);
298 manage.setData(doc.derivedUri);
299
300 try {
301 startActivity(manage);
302 return;
303 } catch (ActivityNotFoundException ex) {
304 // fall back to regular handling below.
305 }
306 }
307
Tomasz Mikolajewskibe4e38b2016-04-07 18:00:08 +0900308 if (doc.isContainer()) {
309 openContainerDocument(doc);
310 } else {
311 openDocument(doc, model);
312 }
313 }
314
315 /**
316 * Launches an intent to view the specified document.
317 */
318 private void openDocument(DocumentInfo doc, Model model) {
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900319 Intent intent = new QuickViewIntentBuilder(
320 getPackageManager(), getResources(), doc, model).build();
Steve McKay351a7492015-08-04 10:11:01 -0700321
Steve McKay3bd316c2015-10-15 15:27:30 -0700322 if (intent != null) {
323 // TODO: un-work around issue b/24963914. Should be fixed soon.
324 try {
325 startActivity(intent);
326 return;
327 } catch (SecurityException e) {
Steve McKayb4a6b362015-10-23 09:04:09 -0700328 // Carry on to regular view mode.
Steve McKay3bd316c2015-10-15 15:27:30 -0700329 Log.e(TAG, "Caught security error: " + e.getLocalizedMessage());
330 }
Steve McKayedc65bb2015-07-31 14:35:22 -0700331 }
332
Steve McKay26444292016-03-07 11:49:05 -0800333 // Fall back to traditional VIEW action...
Steve McKay3bd316c2015-10-15 15:27:30 -0700334 intent = new Intent(Intent.ACTION_VIEW);
335 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
336 intent.setData(doc.derivedUri);
337
Steve McKay351a7492015-08-04 10:11:01 -0700338 if (DEBUG && intent.getClipData() != null) {
339 Log.d(TAG, "Starting intent w/ clip data: " + intent.getClipData());
340 }
341
Steve McKayedc65bb2015-07-31 14:35:22 -0700342 try {
343 startActivity(intent);
Steve McKay3bd316c2015-10-15 15:27:30 -0700344 } catch (ActivityNotFoundException e) {
345 Snackbars.makeSnackbar(
346 this, R.string.toast_no_application, Snackbar.LENGTH_SHORT).show();
Steve McKayedc65bb2015-07-31 14:35:22 -0700347 }
348 }
349
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700350 @Override
Steve McKay0599a442015-05-05 14:50:00 -0700351 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
352 DirectoryFragment dir;
Steve McKayd4800812016-02-02 11:41:03 -0800353 // TODO: All key events should be statically bound using alphabeticShortcut.
354 // But not working.
Steve McKay0599a442015-05-05 14:50:00 -0700355 switch (keyCode) {
356 case KeyEvent.KEYCODE_A:
Steve McKayd4800812016-02-02 11:41:03 -0800357 dir = getDirectoryFragment();
358 if (dir != null) {
359 dir.selectAllFiles();
360 }
Steve McKay0599a442015-05-05 14:50:00 -0700361 return true;
Steve McKay1f199482015-05-20 15:58:42 -0700362 case KeyEvent.KEYCODE_C:
Steve McKayd4800812016-02-02 11:41:03 -0800363 dir = getDirectoryFragment();
364 if (dir != null) {
365 dir.copySelectedToClipboard();
366 }
Steve McKay6035b3c2015-12-04 11:19:09 -0800367 return true;
368 case KeyEvent.KEYCODE_V:
Steve McKayd4800812016-02-02 11:41:03 -0800369 dir = getDirectoryFragment();
370 if (dir != null) {
371 dir.pasteFromClipboard();
372 }
Steve McKay6035b3c2015-12-04 11:19:09 -0800373 return true;
374 default:
375 return super.onKeyShortcut(keyCode, event);
Steve McKay0599a442015-05-05 14:50:00 -0700376 }
377 }
378
Steve McKayfd8425a2016-02-23 14:34:50 -0800379 // Do some "do what a I want" drawer fiddling, but don't
380 // do it if user already hit back recently and we recently
381 // did some fiddling.
382 @Override
383 boolean onBeforePopDir() {
384 int size = mState.stack.size();
385
386 if (mDrawer.isPresent()
387 && (System.currentTimeMillis() - mDrawerLastFiddled) > DRAWER_NO_FIDDLE_DELAY) {
388 // Close drawer if it is open.
389 if (mDrawer.isOpen()) {
390 mDrawer.setOpen(false);
391 mDrawerLastFiddled = System.currentTimeMillis();
392 return true;
393 }
394
395 // Open the Close drawer if it is closed and we're at the top of a root.
Aga Wronska58cb5382016-03-23 17:12:33 -0700396 if (size <= 1) {
Steve McKayfd8425a2016-02-23 14:34:50 -0800397 mDrawer.setOpen(true);
398 // Remember so we don't just close it again if back is pressed again.
399 mDrawerLastFiddled = System.currentTimeMillis();
400 return true;
401 }
402 }
403
404 return false;
405 }
406
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800407 // Turns out only DocumentsActivity was ever calling saveStackBlocking.
408 // There may be a case where we want to contribute entries from
409 // Behavior here in FilesActivity, but it isn't yet obvious.
410 // TODO: Contribute to recents, or remove this.
411 void writeStackToRecentsBlocking() {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700412 final ContentResolver resolver = getContentResolver();
413 final ContentValues values = new ContentValues();
414
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800415 final byte[] rawStack = DurableUtils.writeToArrayOrNull(mState.stack);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700416
417 // Remember location for next app launch
418 final String packageName = getCallingPackageMaybeExtra();
419 values.clear();
420 values.put(ResumeColumns.STACK, rawStack);
421 values.put(ResumeColumns.EXTERNAL, 0);
422 resolver.insert(RecentsProvider.buildResume(packageName), values);
423 }
424
Steve McKayb68dd222015-04-20 17:18:15 -0700425 @Override
426 void onTaskFinished(Uri... uris) {
Ben Kwa543a2922016-03-22 11:11:46 -0700427 if (DEBUG) Log.d(TAG, "onFinished() " + Arrays.toString(uris));
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700428
429 final Intent intent = new Intent();
430 if (uris.length == 1) {
431 intent.setData(uris[0]);
432 } else if (uris.length > 1) {
433 final ClipData clipData = new ClipData(
434 null, mState.acceptMimes, new ClipData.Item(uris[0]));
435 for (int i = 1; i < uris.length; i++) {
436 clipData.addItem(new ClipData.Item(uris[i]));
437 }
438 intent.setClipData(clipData);
439 }
440
441 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
442 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
443 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
444
445 setResult(Activity.RESULT_OK, intent);
446 finish();
447 }
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900448
449 /**
450 * Builds a stack for the specific Uris. Multi roots are not supported, as it's impossible
451 * to know which root to select. Also, the stack doesn't contain intermediate directories.
452 * It's primarly used for opening ZIP archives from Downloads app.
453 */
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800454 private static final class OpenUriForViewTask extends PairedTask<FilesActivity, Uri, Void> {
455
456 private final State mState;
457 public OpenUriForViewTask(FilesActivity activity) {
458 super(activity);
459 mState = activity.mState;
460 }
461
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900462 @Override
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800463 protected Void run(Uri... params) {
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900464 final Uri uri = params[0];
465
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800466 final RootsCache rootsCache = DocumentsApplication.getRootsCache(mOwner);
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900467 final String authority = uri.getAuthority();
468
469 final Collection<RootInfo> roots =
470 rootsCache.getRootsForAuthorityBlocking(authority);
471 if (roots.isEmpty()) {
472 Log.e(TAG, "Failed to find root for the requested Uri: " + uri);
473 return null;
474 }
475
476 final RootInfo root = roots.iterator().next();
477 mState.stack.root = root;
478 try {
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800479 mState.stack.add(DocumentInfo.fromUri(mOwner.getContentResolver(), uri));
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900480 } catch (FileNotFoundException e) {
481 Log.e(TAG, "Failed to resolve DocumentInfo from Uri: " + uri);
482 }
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800483 mState.stack.add(mOwner.getRootDocumentBlocking(root));
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900484 return null;
485 }
486
487 @Override
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800488 protected void finish(Void result) {
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800489 mOwner.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900490 }
491 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700492}