blob: 3e04e2a5a2181b6af3cb9d4aa3083153ddb15c74 [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();
215 return true;
216 case R.id.menu_new_window:
Steve McKay323ee3e2015-09-25 16:02:56 -0700217 createNewWindow();
Steve McKay9f9d5b42015-09-23 15:44:24 -0700218 return true;
219 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 }
Steve McKay9f9d5b42015-09-23 15:44:24 -0700224 return true;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700225 }
226
227 return super.onOptionsItemSelected(item);
228 }
229
Steve McKay323ee3e2015-09-25 16:02:56 -0700230 private void createNewWindow() {
Ben Kwa72379982016-01-26 11:50:03 -0800231 Metrics.logMultiWindow(this);
Steve McKay323ee3e2015-09-25 16:02:56 -0700232 Intent intent = LauncherActivity.createLaunchIntent(this);
233 intent.putExtra(Shared.EXTRA_STACK, (Parcelable) mState.stack);
Steve McKaya7e923c2016-01-28 12:02:57 -0800234
235 // With new multi-window mode we have to pick how we are launched.
236 // By default we'd be launched in-place above the existing app.
237 // By setting launch-to-side ActivityManager will open us to side.
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -0800238 if (inMultiWindow()) {
Wale Ogunwale2a25a622016-01-30 11:27:21 -0800239 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
Steve McKaya7e923c2016-01-28 12:02:57 -0800240 }
241
Steve McKay323ee3e2015-09-25 16:02:56 -0700242 startActivity(intent);
243 }
244
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700245 @Override
Aga Wronskaf6a31d32016-01-15 17:30:15 -0800246 void refreshDirectory(int anim) {
Steve McKaye934ce62015-03-25 14:35:33 -0700247 final FragmentManager fm = getFragmentManager();
248 final RootInfo root = getCurrentRoot();
249 final DocumentInfo cwd = getCurrentDirectory();
250
Steve McKaya1f76802016-02-25 13:34:03 -0800251 assert(!mSearchManager.isSearching());
Aga Wronska893390b2016-02-17 13:50:42 -0800252
Steve McKaye934ce62015-03-25 14:35:33 -0700253 if (cwd == null) {
254 DirectoryFragment.showRecentsOpen(fm, anim);
Steve McKaye934ce62015-03-25 14:35:33 -0700255 } else {
Aga Wronska893390b2016-02-17 13:50:42 -0800256 // Normal boring directory
257 DirectoryFragment.showDirectory(fm, root, cwd, anim);
Steve McKaye934ce62015-03-25 14:35:33 -0700258 }
Steve McKaye934ce62015-03-25 14:35:33 -0700259 }
260
261 @Override
Steve McKay4f160402015-08-17 13:18:05 -0700262 void onRootPicked(RootInfo root) {
263 super.onRootPicked(root);
264 mDrawer.setOpen(false);
265 }
266
267 @Override
Steve McKay6eaf38632015-08-04 10:11:01 -0700268 public void onDocumentsPicked(List<DocumentInfo> docs) {
269 throw new UnsupportedOperationException();
Steve McKaye934ce62015-03-25 14:35:33 -0700270 }
271
Steve McKay6eaf38632015-08-04 10:11:01 -0700272 @Override
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900273 public void onDocumentPicked(DocumentInfo doc, Model model) {
Tomasz Mikolajewski39acff52015-11-25 13:01:18 +0900274 if (doc.isContainer()) {
275 openContainerDocument(doc);
Steve McKay6eaf38632015-08-04 10:11:01 -0700276 } else {
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900277 openDocument(doc, model);
Steve McKay6eaf38632015-08-04 10:11:01 -0700278 }
Steve McKayc78bcb82015-07-31 14:35:22 -0700279 }
280
281 /**
282 * Launches an intent to view the specified document.
283 */
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900284 private void openDocument(DocumentInfo doc, Model model) {
Steve McKay9bb98f32016-03-07 11:49:05 -0800285
Steve McKay3e63e7d2016-03-11 10:11:52 -0800286 // Anything on downloads goes through the back through downloads manager
287 // (that's the MANAGE_DOCUMENT bit).
288 // This is done for two reasons:
289 // 1) The file in question might be a failed/queued or otherwise have some
290 // specialized download handling.
291 // 2) For APKs, the download manager will add on some important security stuff
292 // like origin URL.
293 // All other files not on downloads, event APKs, would get no benefit from this
294 // treatment, thusly the "isDownloads" check.
295 if (getCurrentRoot().isDownloads()) {
Steve McKay9bb98f32016-03-07 11:49:05 -0800296 // First try managing the document; we expect manager to filter
297 // based on authority, so we don't grant.
298 final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT);
299 manage.setData(doc.derivedUri);
300
301 try {
302 startActivity(manage);
303 return;
304 } catch (ActivityNotFoundException ex) {
305 // fall back to regular handling below.
306 }
307 }
308
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900309 Intent intent = new QuickViewIntentBuilder(
310 getPackageManager(), getResources(), doc, model).build();
Steve McKay6eaf38632015-08-04 10:11:01 -0700311
Steve McKay3b2ad112015-10-15 15:27:30 -0700312 if (intent != null) {
313 // TODO: un-work around issue b/24963914. Should be fixed soon.
314 try {
315 startActivity(intent);
316 return;
317 } catch (SecurityException e) {
Steve McKay1eafb662015-10-23 09:04:09 -0700318 // Carry on to regular view mode.
Steve McKay3b2ad112015-10-15 15:27:30 -0700319 Log.e(TAG, "Caught security error: " + e.getLocalizedMessage());
320 }
Steve McKayc78bcb82015-07-31 14:35:22 -0700321 }
322
Steve McKay9bb98f32016-03-07 11:49:05 -0800323 // Fall back to traditional VIEW action...
Steve McKay3b2ad112015-10-15 15:27:30 -0700324 intent = new Intent(Intent.ACTION_VIEW);
325 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
326 intent.setData(doc.derivedUri);
327
Steve McKay6eaf38632015-08-04 10:11:01 -0700328 if (DEBUG && intent.getClipData() != null) {
329 Log.d(TAG, "Starting intent w/ clip data: " + intent.getClipData());
330 }
331
Steve McKayc78bcb82015-07-31 14:35:22 -0700332 try {
333 startActivity(intent);
Steve McKay3b2ad112015-10-15 15:27:30 -0700334 } catch (ActivityNotFoundException e) {
335 Snackbars.makeSnackbar(
336 this, R.string.toast_no_application, Snackbar.LENGTH_SHORT).show();
Steve McKayc78bcb82015-07-31 14:35:22 -0700337 }
338 }
339
Steve McKaye934ce62015-03-25 14:35:33 -0700340 @Override
Steve McKay3da8afc2015-05-05 14:50:00 -0700341 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
342 DirectoryFragment dir;
Steve McKay3ce95952016-02-02 11:41:03 -0800343 // TODO: All key events should be statically bound using alphabeticShortcut.
344 // But not working.
Steve McKay3da8afc2015-05-05 14:50:00 -0700345 switch (keyCode) {
346 case KeyEvent.KEYCODE_A:
Steve McKay3ce95952016-02-02 11:41:03 -0800347 dir = getDirectoryFragment();
348 if (dir != null) {
349 dir.selectAllFiles();
350 }
Steve McKay3da8afc2015-05-05 14:50:00 -0700351 return true;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700352 case KeyEvent.KEYCODE_C:
Steve McKay3ce95952016-02-02 11:41:03 -0800353 dir = getDirectoryFragment();
354 if (dir != null) {
355 dir.copySelectedToClipboard();
356 }
Steve McKay8fd086a2015-12-04 11:19:09 -0800357 return true;
358 case KeyEvent.KEYCODE_V:
Steve McKay3ce95952016-02-02 11:41:03 -0800359 dir = getDirectoryFragment();
360 if (dir != null) {
361 dir.pasteFromClipboard();
362 }
Steve McKay8fd086a2015-12-04 11:19:09 -0800363 return true;
364 default:
365 return super.onKeyShortcut(keyCode, event);
Steve McKay3da8afc2015-05-05 14:50:00 -0700366 }
367 }
368
Steve McKayc95d87c2016-02-23 14:34:50 -0800369 // Do some "do what a I want" drawer fiddling, but don't
370 // do it if user already hit back recently and we recently
371 // did some fiddling.
372 @Override
373 boolean onBeforePopDir() {
374 int size = mState.stack.size();
375
376 if (mDrawer.isPresent()
377 && (System.currentTimeMillis() - mDrawerLastFiddled) > DRAWER_NO_FIDDLE_DELAY) {
378 // Close drawer if it is open.
379 if (mDrawer.isOpen()) {
380 mDrawer.setOpen(false);
381 mDrawerLastFiddled = System.currentTimeMillis();
382 return true;
383 }
384
385 // Open the Close drawer if it is closed and we're at the top of a root.
Aga Wronska96e30eb2016-03-23 17:12:33 -0700386 if (size <= 1) {
Steve McKayc95d87c2016-02-23 14:34:50 -0800387 mDrawer.setOpen(true);
388 // Remember so we don't just close it again if back is pressed again.
389 mDrawerLastFiddled = System.currentTimeMillis();
390 return true;
391 }
392 }
393
394 return false;
395 }
396
Steve McKay95cd85a2016-02-04 12:15:22 -0800397 // Turns out only DocumentsActivity was ever calling saveStackBlocking.
398 // There may be a case where we want to contribute entries from
399 // Behavior here in FilesActivity, but it isn't yet obvious.
400 // TODO: Contribute to recents, or remove this.
401 void writeStackToRecentsBlocking() {
Steve McKaye934ce62015-03-25 14:35:33 -0700402 final ContentResolver resolver = getContentResolver();
403 final ContentValues values = new ContentValues();
404
Steve McKay95cd85a2016-02-04 12:15:22 -0800405 final byte[] rawStack = DurableUtils.writeToArrayOrNull(mState.stack);
Steve McKaye934ce62015-03-25 14:35:33 -0700406
407 // Remember location for next app launch
408 final String packageName = getCallingPackageMaybeExtra();
409 values.clear();
410 values.put(ResumeColumns.STACK, rawStack);
411 values.put(ResumeColumns.EXTERNAL, 0);
412 resolver.insert(RecentsProvider.buildResume(packageName), values);
413 }
414
Steve McKayef3e2cf2015-04-20 17:18:15 -0700415 @Override
416 void onTaskFinished(Uri... uris) {
Ben Kwaffa829f2016-03-22 11:11:46 -0700417 if (DEBUG) Log.d(TAG, "onFinished() " + Arrays.toString(uris));
Steve McKaye934ce62015-03-25 14:35:33 -0700418
419 final Intent intent = new Intent();
420 if (uris.length == 1) {
421 intent.setData(uris[0]);
422 } else if (uris.length > 1) {
423 final ClipData clipData = new ClipData(
424 null, mState.acceptMimes, new ClipData.Item(uris[0]));
425 for (int i = 1; i < uris.length; i++) {
426 clipData.addItem(new ClipData.Item(uris[i]));
427 }
428 intent.setClipData(clipData);
429 }
430
431 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
432 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
433 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
434
435 setResult(Activity.RESULT_OK, intent);
436 finish();
437 }
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900438
439 /**
440 * Builds a stack for the specific Uris. Multi roots are not supported, as it's impossible
441 * to know which root to select. Also, the stack doesn't contain intermediate directories.
442 * It's primarly used for opening ZIP archives from Downloads app.
443 */
Steve McKay95cd85a2016-02-04 12:15:22 -0800444 private static final class OpenUriForViewTask extends PairedTask<FilesActivity, Uri, Void> {
445
446 private final State mState;
447 public OpenUriForViewTask(FilesActivity activity) {
448 super(activity);
449 mState = activity.mState;
450 }
451
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900452 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -0800453 protected Void run(Uri... params) {
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900454 final Uri uri = params[0];
455
Steve McKay95cd85a2016-02-04 12:15:22 -0800456 final RootsCache rootsCache = DocumentsApplication.getRootsCache(mOwner);
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900457 final String authority = uri.getAuthority();
458
459 final Collection<RootInfo> roots =
460 rootsCache.getRootsForAuthorityBlocking(authority);
461 if (roots.isEmpty()) {
462 Log.e(TAG, "Failed to find root for the requested Uri: " + uri);
463 return null;
464 }
465
466 final RootInfo root = roots.iterator().next();
467 mState.stack.root = root;
468 try {
Steve McKay95cd85a2016-02-04 12:15:22 -0800469 mState.stack.add(DocumentInfo.fromUri(mOwner.getContentResolver(), uri));
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900470 } catch (FileNotFoundException e) {
471 Log.e(TAG, "Failed to resolve DocumentInfo from Uri: " + uri);
472 }
Steve McKay95cd85a2016-02-04 12:15:22 -0800473 mState.stack.add(mOwner.getRootDocumentBlocking(root));
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900474 return null;
475 }
476
477 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -0800478 protected void finish(Void result) {
Steve McKayce710822016-03-11 10:49:32 -0800479 mOwner.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900480 }
481 }
Steve McKaye934ce62015-03-25 14:35:33 -0700482}