Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.State.ACTION_CREATE; |
| 21 | import static com.android.documentsui.State.ACTION_GET_CONTENT; |
| 22 | import static com.android.documentsui.State.ACTION_OPEN; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 23 | import static com.android.documentsui.State.ACTION_OPEN_TREE; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 24 | import static com.android.documentsui.State.ACTION_PICK_COPY_DESTINATION; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 25 | import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 26 | |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 27 | import android.app.Activity; |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 28 | import android.app.Fragment; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 29 | import android.app.FragmentManager; |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 30 | import android.content.ClipData; |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 31 | import android.content.ComponentName; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 32 | import android.content.ContentProviderClient; |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 33 | import android.content.ContentResolver; |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 34 | import android.content.ContentValues; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 35 | import android.content.Intent; |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 36 | import android.content.pm.ResolveInfo; |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 37 | import android.database.Cursor; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 38 | import android.net.Uri; |
| 39 | import android.os.Bundle; |
Tomasz Mikolajewski | e1a03f8 | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 40 | import android.os.Parcelable; |
Jeff Sharkey | a5599ef | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 41 | import android.provider.DocumentsContract; |
Ben Kwa | 94b486d | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 42 | import android.support.design.widget.Snackbar; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 43 | import android.util.Log; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 44 | import android.view.Menu; |
| 45 | import android.view.MenuItem; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 46 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 47 | import com.android.documentsui.RecentsProvider.RecentColumns; |
| 48 | import com.android.documentsui.RecentsProvider.ResumeColumns; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 49 | import com.android.documentsui.dirlist.DirectoryFragment; |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 50 | import com.android.documentsui.dirlist.Model; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 51 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 52 | import com.android.documentsui.model.DurableUtils; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 53 | import com.android.documentsui.model.RootInfo; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 54 | import com.android.documentsui.services.FileOperationService; |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 55 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 56 | import libcore.io.IoUtils; |
| 57 | |
| 58 | import java.io.FileNotFoundException; |
| 59 | import java.io.IOException; |
Ben Kwa | cc34521 | 2015-05-18 13:18:05 -0700 | [diff] [blame] | 60 | import java.util.Arrays; |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 61 | import java.util.Collection; |
Ben Kwa | cc34521 | 2015-05-18 13:18:05 -0700 | [diff] [blame] | 62 | import java.util.List; |
| 63 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 64 | public class DocumentsActivity extends BaseActivity { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 65 | private static final int CODE_FORWARD = 42; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 66 | private static final String TAG = "DocumentsActivity"; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 67 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 68 | public DocumentsActivity() { |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 69 | super(R.layout.documents_activity, TAG); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 72 | @Override |
| 73 | public void onCreate(Bundle icicle) { |
Steve McKay | 1205547 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 74 | super.onCreate(icicle); |
Oren Blasberg | 9e9c246 | 2015-06-25 19:00:43 -0700 | [diff] [blame] | 75 | |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 76 | if (mState.action == ACTION_CREATE) { |
| 77 | final String mimeType = getIntent().getType(); |
| 78 | final String title = getIntent().getStringExtra(Intent.EXTRA_TITLE); |
| 79 | SaveFragment.show(getFragmentManager(), mimeType, title); |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 80 | } else if (mState.action == ACTION_OPEN_TREE || |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 81 | mState.action == ACTION_PICK_COPY_DESTINATION) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 82 | PickFragment.show(getFragmentManager()); |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (mState.action == ACTION_GET_CONTENT) { |
| 86 | final Intent moreApps = new Intent(getIntent()); |
| 87 | moreApps.setComponent(null); |
| 88 | moreApps.setPackage(null); |
| 89 | RootsFragment.show(getFragmentManager(), moreApps); |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 90 | } else if (mState.action == ACTION_OPEN || |
| 91 | mState.action == ACTION_CREATE || |
| 92 | mState.action == ACTION_OPEN_TREE || |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 93 | mState.action == ACTION_PICK_COPY_DESTINATION) { |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 94 | RootsFragment.show(getFragmentManager(), null); |
| 95 | } |
| 96 | |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 97 | if (!mState.restored) { |
Makoto Onuki | 9feac14 | 2015-06-23 14:29:25 -0700 | [diff] [blame] | 98 | // In this case, we set the activity title in AsyncTask.onPostExecute(). To prevent |
| 99 | // talkback from reading aloud the default title, we clear it here. |
| 100 | setTitle(""); |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 101 | new RestoreStackTask(this).execute(); |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 102 | } else { |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 103 | refreshCurrentRootAndDirectory(ANIM_NONE); |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 104 | } |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Steve McKay | 1205547 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 107 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 108 | void includeState(State state) { |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 109 | final Intent intent = getIntent(); |
| 110 | final String action = intent.getAction(); |
| 111 | if (Intent.ACTION_OPEN_DOCUMENT.equals(action)) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 112 | state.action = ACTION_OPEN; |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 113 | } else if (Intent.ACTION_CREATE_DOCUMENT.equals(action)) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 114 | state.action = ACTION_CREATE; |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 115 | } else if (Intent.ACTION_GET_CONTENT.equals(action)) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 116 | state.action = ACTION_GET_CONTENT; |
Jeff Sharkey | b9fbb72 | 2014-06-04 16:42:47 -0700 | [diff] [blame] | 117 | } else if (Intent.ACTION_OPEN_DOCUMENT_TREE.equals(action)) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 118 | state.action = ACTION_OPEN_TREE; |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 119 | } else if (Shared.ACTION_PICK_COPY_DESTINATION.equals(action)) { |
| 120 | state.action = ACTION_PICK_COPY_DESTINATION; |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 123 | if (state.action == ACTION_OPEN || state.action == ACTION_GET_CONTENT) { |
| 124 | state.allowMultiple = intent.getBooleanExtra( |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 125 | Intent.EXTRA_ALLOW_MULTIPLE, false); |
| 126 | } |
| 127 | |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 128 | if (state.action == ACTION_OPEN || state.action == ACTION_GET_CONTENT |
| 129 | || state.action == ACTION_CREATE) { |
| 130 | state.openableOnly = intent.hasCategory(Intent.CATEGORY_OPENABLE); |
| 131 | } |
| 132 | |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 133 | if (state.action == ACTION_PICK_COPY_DESTINATION) { |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 134 | // Indicates that a copy operation (or move) includes a directory. |
| 135 | // Why? Directory creation isn't supported by some roots (like Downloads). |
| 136 | // This allows us to restrict available roots to just those with support. |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 137 | state.directoryCopy = intent.getBooleanExtra( |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 138 | Shared.EXTRA_DIRECTORY_COPY, false); |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 139 | state.copyOperationSubType = intent.getIntExtra( |
| 140 | FileOperationService.EXTRA_OPERATION, |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 141 | FileOperationService.OPERATION_COPY); |
Daichi Hirono | 9be3429 | 2015-04-14 17:12:54 +0900 | [diff] [blame] | 142 | } |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 143 | } |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 144 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 145 | private void onStackRestored(boolean restored, boolean external) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 146 | // Show drawer when no stack restored, but only when requesting |
| 147 | // non-visual content. However, if we last used an external app, |
| 148 | // drawer is always shown. |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 149 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 150 | boolean showDrawer = false; |
| 151 | if (!restored) { |
| 152 | showDrawer = true; |
| 153 | } |
| 154 | if (MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, mState.acceptMimes)) { |
| 155 | showDrawer = false; |
| 156 | } |
| 157 | if (external && mState.action == ACTION_GET_CONTENT) { |
| 158 | showDrawer = true; |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 159 | } |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 160 | if (mState.action == ACTION_PICK_COPY_DESTINATION) { |
| 161 | showDrawer = true; |
| 162 | } |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 163 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 164 | if (showDrawer) { |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 165 | mNavigator.revealRootsDrawer(true); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 168 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 169 | public void onAppPicked(ResolveInfo info) { |
| 170 | final Intent intent = new Intent(getIntent()); |
| 171 | intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_FORWARD_RESULT); |
| 172 | intent.setComponent(new ComponentName( |
| 173 | info.activityInfo.applicationInfo.packageName, info.activityInfo.name)); |
| 174 | startActivityForResult(intent, CODE_FORWARD); |
| 175 | } |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 176 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 177 | @Override |
| 178 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 179 | Log.d(TAG, "onActivityResult() code=" + resultCode); |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 180 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 181 | // Only relay back results when not canceled; otherwise stick around to |
| 182 | // let the user pick another app/backend. |
| 183 | if (requestCode == CODE_FORWARD && resultCode != RESULT_CANCELED) { |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 184 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 185 | // Remember that we last picked via external app |
| 186 | final String packageName = getCallingPackageMaybeExtra(); |
| 187 | final ContentValues values = new ContentValues(); |
| 188 | values.put(ResumeColumns.EXTERNAL, 1); |
| 189 | getContentResolver().insert(RecentsProvider.buildResume(packageName), values); |
| 190 | |
| 191 | // Pass back result to original caller |
| 192 | setResult(resultCode, data); |
| 193 | finish(); |
| 194 | } else { |
| 195 | super.onActivityResult(requestCode, resultCode, data); |
Jeff Sharkey | a5599ef | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Jeff Sharkey | bc2971d | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 199 | @Override |
| 200 | protected void onPostCreate(Bundle savedInstanceState) { |
| 201 | super.onPostCreate(savedInstanceState); |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 202 | mDrawer.update(); |
| 203 | mNavigator.update(); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 206 | @Override |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 207 | public String getDrawerTitle() { |
| 208 | String title = getIntent().getStringExtra(DocumentsContract.EXTRA_PROMPT); |
| 209 | if (title == null) { |
| 210 | if (mState.action == ACTION_OPEN || |
| 211 | mState.action == ACTION_GET_CONTENT || |
| 212 | mState.action == ACTION_OPEN_TREE) { |
| 213 | title = getResources().getString(R.string.title_open); |
| 214 | } else if (mState.action == ACTION_CREATE || |
| 215 | mState.action == ACTION_PICK_COPY_DESTINATION) { |
| 216 | title = getResources().getString(R.string.title_save); |
Tomasz Mikolajewski | 0e591f9 | 2015-06-12 16:22:17 -0700 | [diff] [blame] | 217 | } else { |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 218 | // If all else fails, just call it "Files". |
| 219 | title = getResources().getString(R.string.files_label); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 220 | } |
Jeff Sharkey | f52773f | 2014-07-28 16:38:52 -0700 | [diff] [blame] | 221 | } |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 222 | |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 223 | return title; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 227 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 228 | super.onPrepareOptionsMenu(menu); |
| 229 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 230 | final DocumentInfo cwd = getCurrentDirectory(); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 231 | |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 232 | boolean picking = mState.action == ACTION_CREATE |
| 233 | || mState.action == ACTION_OPEN_TREE |
| 234 | || mState.action == ACTION_PICK_COPY_DESTINATION; |
| 235 | |
| 236 | if (picking) { |
| 237 | // May already be hidden because the root |
| 238 | // doesn't support search. |
| 239 | mSearchManager.showMenu(false); |
| 240 | } |
| 241 | |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 242 | final MenuItem createDir = menu.findItem(R.id.menu_create_dir); |
Jeff Sharkey | ded7718 | 2013-09-03 14:17:06 -0700 | [diff] [blame] | 243 | final MenuItem grid = menu.findItem(R.id.menu_grid); |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 244 | final MenuItem list = menu.findItem(R.id.menu_list); |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 245 | final MenuItem fileSize = menu.findItem(R.id.menu_file_size); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 246 | |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 247 | boolean recents = cwd == null; |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 248 | |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 249 | createDir.setVisible(picking && !recents && cwd.isCreateSupported()); |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 250 | |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 251 | // No display options in recent directories |
Steve McKay | 7c7e684 | 2016-01-08 14:58:33 -0800 | [diff] [blame] | 252 | if (picking && recents) { |
| 253 | grid.setVisible(false); |
| 254 | list.setVisible(false); |
| 255 | } |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 256 | |
| 257 | fileSize.setVisible(fileSize.isVisible() && !picking); |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 258 | |
| 259 | if (mState.action == ACTION_CREATE) { |
| 260 | final FragmentManager fm = getFragmentManager(); |
Steve McKay | 0c2a0a8 | 2015-10-14 17:41:13 -0700 | [diff] [blame] | 261 | SaveFragment.get(fm).prepareForDirectory(cwd); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 262 | } |
Jeff Sharkey | bc2971d | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 263 | |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 264 | Menus.disableHiddenItems(menu); |
Jeff Sharkey | 1407d4c | 2015-04-12 21:52:24 -0700 | [diff] [blame] | 265 | |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | |
| 269 | @Override |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 270 | void refreshDirectory(int anim) { |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 271 | final FragmentManager fm = getFragmentManager(); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 272 | final RootInfo root = getCurrentRoot(); |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 273 | final DocumentInfo cwd = getCurrentDirectory(); |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 274 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 275 | if (cwd == null) { |
| 276 | // No directory means recents |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 277 | if (mState.action == ACTION_CREATE || |
| 278 | mState.action == ACTION_OPEN_TREE || |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 279 | mState.action == ACTION_PICK_COPY_DESTINATION) { |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 280 | RecentsCreateFragment.show(fm); |
| 281 | } else { |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 282 | DirectoryFragment.showRecentsOpen(fm, anim); |
Jeff Sharkey | 88f322c | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 283 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 284 | // In recents we pick layout mode based on the mimetype, |
| 285 | // picking GRID for visual types. We intentionally don't |
| 286 | // consult a user's saved preferences here since they are |
| 287 | // set per root (not per root and per mimetype). |
| 288 | boolean visualMimes = MimePredicate.mimeMatches( |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 289 | MimePredicate.VISUAL_MIMES, mState.acceptMimes); |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 290 | mState.derivedMode = visualMimes ? State.MODE_GRID : State.MODE_LIST; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 291 | } |
| 292 | } else { |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 293 | if (mSearchManager.isSearching()) { |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 294 | // Ongoing search |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 295 | DirectoryFragment.showSearch(fm, root, mState.currentSearch, anim); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 296 | } else { |
| 297 | // Normal boring directory |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 298 | DirectoryFragment.showDirectory(fm, root, cwd, anim); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 299 | } |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 300 | } |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 301 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 302 | // Forget any replacement target |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 303 | if (mState.action == ACTION_CREATE) { |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 304 | final SaveFragment save = SaveFragment.get(fm); |
| 305 | if (save != null) { |
| 306 | save.setReplaceTarget(null); |
| 307 | } |
| 308 | } |
| 309 | |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 310 | if (mState.action == ACTION_OPEN_TREE || |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 311 | mState.action == ACTION_PICK_COPY_DESTINATION) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 312 | final PickFragment pick = PickFragment.get(fm); |
| 313 | if (pick != null) { |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 314 | pick.setPickTarget(mState.action, mState.copyOperationSubType, cwd); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 317 | } |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 318 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 319 | void onSaveRequested(DocumentInfo replaceTarget) { |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 320 | new ExistingFinishTask(this, replaceTarget.derivedUri) |
| 321 | .executeOnExecutor(getExecutorForCurrentDirectory()); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 322 | } |
Jeff Sharkey | 28c05ee | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 323 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 324 | void onSaveRequested(String mimeType, String displayName) { |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 325 | new CreateFinishTask(this, mimeType, displayName) |
| 326 | .executeOnExecutor(getExecutorForCurrentDirectory()); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 329 | @Override |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 330 | void onRootPicked(RootInfo root) { |
| 331 | super.onRootPicked(root); |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 332 | mNavigator.revealRootsDrawer(false); |
| 333 | } |
| 334 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 335 | @Override |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 336 | public void onDocumentPicked(DocumentInfo doc, Model model) { |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 337 | final FragmentManager fm = getFragmentManager(); |
Tomasz Mikolajewski | 39acff5 | 2015-11-25 13:01:18 +0900 | [diff] [blame] | 338 | if (doc.isContainer()) { |
| 339 | openContainerDocument(doc); |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 340 | } else if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) { |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 341 | // Explicit file picked, return |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 342 | new ExistingFinishTask(this, doc.derivedUri) |
| 343 | .executeOnExecutor(getExecutorForCurrentDirectory()); |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 344 | } else if (mState.action == ACTION_CREATE) { |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 345 | // Replace selected file |
| 346 | SaveFragment.get(fm).setReplaceTarget(doc); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 350 | @Override |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 351 | public void onDocumentsPicked(List<DocumentInfo> docs) { |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 352 | if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) { |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 353 | final int size = docs.size(); |
| 354 | final Uri[] uris = new Uri[size]; |
| 355 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 356 | uris[i] = docs.get(i).derivedUri; |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 357 | } |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 358 | new ExistingFinishTask(this, uris) |
| 359 | .executeOnExecutor(getExecutorForCurrentDirectory()); |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 360 | } |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 363 | public void onPickRequested(DocumentInfo pickTarget) { |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 364 | Uri result; |
| 365 | if (mState.action == ACTION_OPEN_TREE) { |
| 366 | result = DocumentsContract.buildTreeDocumentUri( |
| 367 | pickTarget.authority, pickTarget.documentId); |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 368 | } else if (mState.action == ACTION_PICK_COPY_DESTINATION) { |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 369 | result = pickTarget.derivedUri; |
| 370 | } else { |
| 371 | // Should not be reached. |
| 372 | throw new IllegalStateException("Invalid mState.action."); |
| 373 | } |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 374 | new PickFinishTask(this, result).executeOnExecutor(getExecutorForCurrentDirectory()); |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 377 | void writeStackToRecentsBlocking() { |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 378 | final ContentResolver resolver = getContentResolver(); |
| 379 | final ContentValues values = new ContentValues(); |
| 380 | |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 381 | final byte[] rawStack = DurableUtils.writeToArrayOrNull(mState.stack); |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 382 | if (mState.action == ACTION_CREATE || |
| 383 | mState.action == ACTION_OPEN_TREE || |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 384 | mState.action == ACTION_PICK_COPY_DESTINATION) { |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 385 | // Remember stack for last create |
| 386 | values.clear(); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 387 | values.put(RecentColumns.KEY, mState.stack.buildKey()); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 388 | values.put(RecentColumns.STACK, rawStack); |
| 389 | resolver.insert(RecentsProvider.buildRecent(), values); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Remember location for next app launch |
Jeff Sharkey | 15be836 | 2013-10-09 13:52:17 -0700 | [diff] [blame] | 393 | final String packageName = getCallingPackageMaybeExtra(); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 394 | values.clear(); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 395 | values.put(ResumeColumns.STACK, rawStack); |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 396 | values.put(ResumeColumns.EXTERNAL, 0); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 397 | resolver.insert(RecentsProvider.buildResume(packageName), values); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 400 | @Override |
| 401 | void onTaskFinished(Uri... uris) { |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 402 | Log.d(TAG, "onFinished() " + Arrays.toString(uris)); |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 403 | |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 404 | final Intent intent = new Intent(); |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 405 | if (uris.length == 1) { |
| 406 | intent.setData(uris[0]); |
| 407 | } else if (uris.length > 1) { |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 408 | final ClipData clipData = new ClipData( |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 409 | null, mState.acceptMimes, new ClipData.Item(uris[0])); |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 410 | for (int i = 1; i < uris.length; i++) { |
| 411 | clipData.addItem(new ClipData.Item(uris[i])); |
| 412 | } |
| 413 | intent.setClipData(clipData); |
| 414 | } |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 415 | |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 416 | if (mState.action == ACTION_GET_CONTENT) { |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 417 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Jeff Sharkey | 04ec6f4 | 2015-07-15 09:13:14 -0700 | [diff] [blame] | 418 | } else if (mState.action == ACTION_OPEN_TREE) { |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 419 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 420 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 421 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 422 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 423 | } else if (mState.action == ACTION_PICK_COPY_DESTINATION) { |
Jeff Sharkey | 04ec6f4 | 2015-07-15 09:13:14 -0700 | [diff] [blame] | 424 | // Picking a copy destination is only used internally by us, so we |
| 425 | // don't need to extend permissions to the caller. |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 426 | intent.putExtra(Shared.EXTRA_STACK, (Parcelable) mState.stack); |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 427 | intent.putExtra(FileOperationService.EXTRA_OPERATION, mState.copyOperationSubType); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 428 | } else { |
| 429 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 430 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 431 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 432 | } |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 433 | |
| 434 | setResult(Activity.RESULT_OK, intent); |
| 435 | finish(); |
| 436 | } |
| 437 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 438 | |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 439 | public static DocumentsActivity get(Fragment fragment) { |
| 440 | return (DocumentsActivity) fragment.getActivity(); |
| 441 | } |
| 442 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 443 | /** |
| 444 | * Restores the stack from Recents for the specified package. |
| 445 | */ |
| 446 | private static final class RestoreStackTask |
| 447 | extends PairedTask<DocumentsActivity, Void, Void> { |
| 448 | |
| 449 | private volatile boolean mRestoredStack; |
| 450 | private volatile boolean mExternal; |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 451 | private State mState; |
| 452 | |
| 453 | public RestoreStackTask(DocumentsActivity activity) { |
| 454 | super(activity); |
| 455 | mState = activity.mState; |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | protected Void run(Void... params) { |
| 460 | if (DEBUG && !mState.stack.isEmpty()) { |
| 461 | Log.w(TAG, "Overwriting existing stack."); |
| 462 | } |
Ben Kwa | a4761f0 | 2016-02-05 06:44:15 -0800 | [diff] [blame] | 463 | RootsCache roots = DocumentsApplication.getRootsCache(mOwner); |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 464 | |
| 465 | String packageName = mOwner.getCallingPackageMaybeExtra(); |
| 466 | Uri resumeUri = RecentsProvider.buildResume(packageName); |
| 467 | Cursor cursor = mOwner.getContentResolver().query(resumeUri, null, null, null, null); |
| 468 | try { |
| 469 | if (cursor.moveToFirst()) { |
| 470 | mExternal = cursor.getInt(cursor.getColumnIndex(ResumeColumns.EXTERNAL)) != 0; |
| 471 | final byte[] rawStack = cursor.getBlob( |
| 472 | cursor.getColumnIndex(ResumeColumns.STACK)); |
| 473 | DurableUtils.readFromArray(rawStack, mState.stack); |
| 474 | mRestoredStack = true; |
| 475 | } |
| 476 | } catch (IOException e) { |
| 477 | Log.w(TAG, "Failed to resume: " + e); |
| 478 | } finally { |
| 479 | IoUtils.closeQuietly(cursor); |
| 480 | } |
| 481 | |
| 482 | if (mRestoredStack) { |
| 483 | // Update the restored stack to ensure we have freshest data |
| 484 | final Collection<RootInfo> matchingRoots = roots.getMatchingRootsBlocking(mState); |
| 485 | try { |
| 486 | mState.stack.updateRoot(matchingRoots); |
| 487 | mState.stack.updateDocuments(mOwner.getContentResolver()); |
| 488 | } catch (FileNotFoundException e) { |
| 489 | Log.w(TAG, "Failed to restore stack for package: " + packageName |
| 490 | + " because of error: "+ e); |
| 491 | mState.stack.reset(); |
| 492 | mRestoredStack = false; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return null; |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | protected void finish(Void result) { |
| 501 | mState.restored = true; |
| 502 | mOwner.refreshCurrentRootAndDirectory(ANIM_NONE); |
| 503 | mOwner.onStackRestored(mRestoredStack, mExternal); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | private static final class PickFinishTask extends PairedTask<DocumentsActivity, Void, Void> { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 508 | private final Uri mUri; |
| 509 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 510 | public PickFinishTask(DocumentsActivity activity, Uri uri) { |
| 511 | super(activity); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 512 | mUri = uri; |
| 513 | } |
| 514 | |
| 515 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 516 | protected Void run(Void... params) { |
| 517 | mOwner.writeStackToRecentsBlocking(); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 518 | return null; |
| 519 | } |
| 520 | |
| 521 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 522 | protected void finish(Void result) { |
| 523 | mOwner.onTaskFinished(mUri); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 527 | private static final class ExistingFinishTask extends PairedTask<DocumentsActivity, Void, Void> { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 528 | private final Uri[] mUris; |
| 529 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 530 | public ExistingFinishTask(DocumentsActivity activity, Uri... uris) { |
| 531 | super(activity); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 532 | mUris = uris; |
| 533 | } |
| 534 | |
| 535 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 536 | protected Void run(Void... params) { |
| 537 | mOwner.writeStackToRecentsBlocking(); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 538 | return null; |
| 539 | } |
| 540 | |
| 541 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 542 | protected void finish(Void result) { |
| 543 | mOwner.onTaskFinished(mUris); |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Task that creates a new document in the background. |
| 549 | */ |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 550 | private static final class CreateFinishTask extends PairedTask<DocumentsActivity, Void, Uri> { |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 551 | private final String mMimeType; |
| 552 | private final String mDisplayName; |
| 553 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 554 | public CreateFinishTask(DocumentsActivity activity, String mimeType, String displayName) { |
| 555 | super(activity); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 556 | mMimeType = mimeType; |
| 557 | mDisplayName = displayName; |
| 558 | } |
| 559 | |
| 560 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 561 | protected void prepare() { |
| 562 | mOwner.setPending(true); |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 566 | protected Uri run(Void... params) { |
| 567 | final ContentResolver resolver = mOwner.getContentResolver(); |
| 568 | final DocumentInfo cwd = mOwner.getCurrentDirectory(); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 569 | |
| 570 | ContentProviderClient client = null; |
| 571 | Uri childUri = null; |
| 572 | try { |
| 573 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 574 | resolver, cwd.derivedUri.getAuthority()); |
| 575 | childUri = DocumentsContract.createDocument( |
| 576 | client, cwd.derivedUri, mMimeType, mDisplayName); |
| 577 | } catch (Exception e) { |
| 578 | Log.w(TAG, "Failed to create document", e); |
| 579 | } finally { |
| 580 | ContentProviderClient.releaseQuietly(client); |
| 581 | } |
| 582 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 583 | if (childUri != null) { |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 584 | mOwner.writeStackToRecentsBlocking(); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 585 | } |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 586 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 587 | return childUri; |
| 588 | } |
| 589 | |
| 590 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 591 | protected void finish(Uri result) { |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 592 | if (result != null) { |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 593 | mOwner.onTaskFinished(result); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 594 | } else { |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 595 | Snackbars.makeSnackbar( |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 596 | mOwner, R.string.save_error, Snackbar.LENGTH_SHORT).show(); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 597 | } |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 598 | |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 599 | mOwner.setPending(false); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 602 | } |