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