Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -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 | |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -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_MANAGE; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 21 | import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE; |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 22 | import static com.android.internal.util.Preconditions.checkState; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 23 | |
| 24 | import android.app.Activity; |
| 25 | import android.app.Fragment; |
| 26 | import android.app.FragmentManager; |
| 27 | import android.content.ActivityNotFoundException; |
| 28 | import android.content.ClipData; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Bundle; |
| 33 | import android.provider.DocumentsContract; |
Ben Kwa | 94b486d | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 34 | import android.support.design.widget.Snackbar; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.Menu; |
Steve McKay | 9c62fd8 | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 37 | import android.view.MenuItem; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 38 | import android.widget.Toolbar; |
| 39 | |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 40 | import com.android.documentsui.dirlist.DirectoryFragment; |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 41 | import com.android.documentsui.dirlist.Model; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 42 | import com.android.documentsui.model.DocumentInfo; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 43 | import com.android.documentsui.model.RootInfo; |
| 44 | import com.android.internal.util.Preconditions; |
| 45 | |
| 46 | import java.util.Arrays; |
| 47 | import java.util.List; |
| 48 | |
Steve McKay | 0279419 | 2015-12-01 16:20:41 -0800 | [diff] [blame] | 49 | // Let's face it. MANAGE_ROOT is used almost exclusively |
| 50 | // for downloads, and is specialized for this purpose. |
| 51 | // So it is now thusly christened. |
| 52 | public class DownloadsActivity extends BaseActivity { |
| 53 | private static final String TAG = "DownloadsActivity"; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 54 | |
Steve McKay | 0279419 | 2015-12-01 16:20:41 -0800 | [diff] [blame] | 55 | public DownloadsActivity() { |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 56 | super(R.layout.downloads_activity, TAG); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void onCreate(Bundle icicle) { |
| 61 | super.onCreate(icicle); |
| 62 | |
| 63 | final Context context = this; |
| 64 | |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 65 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 66 | toolbar.setTitleTextAppearance(context, |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 67 | android.R.style.TextAppearance_DeviceDefault_Widget_ActionBar_Title); |
| 68 | |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 69 | if (!mState.restored) { |
| 70 | // In this case, we set the activity title in AsyncTask.onPostExecute(). To prevent |
| 71 | // talkback from reading aloud the default title, we clear it here. |
| 72 | setTitle(""); |
| 73 | final Uri rootUri = getIntent().getData(); |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 74 | new RestoreRootTask(this, rootUri).executeOnExecutor(getExecutorForCurrentDirectory()); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 75 | } else { |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 76 | refreshCurrentRootAndDirectory(ANIM_NONE); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
Steve McKay | 95cd85a | 2016-02-04 12:15:22 -0800 | [diff] [blame] | 81 | void includeState(State state) { |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 82 | state.action = ACTION_MANAGE; |
| 83 | state.acceptMimes = new String[] { "*/*" }; |
| 84 | state.allowMultiple = true; |
| 85 | state.showSize = true; |
| 86 | state.excludedAuthorities = getExcludedAuthorities(); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected void onPostCreate(Bundle savedInstanceState) { |
| 91 | super.onPostCreate(savedInstanceState); |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 92 | mNavigator.update(); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Steve McKay | 1f264a8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 96 | public String getDrawerTitle() { |
| 97 | return null; // being and nothingness |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 102 | super.onPrepareOptionsMenu(menu); |
Steve McKay | 9c62fd8 | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 103 | |
| 104 | final MenuItem advanced = menu.findItem(R.id.menu_advanced); |
| 105 | final MenuItem createDir = menu.findItem(R.id.menu_create_dir); |
Steve McKay | 9c62fd8 | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 106 | final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard); |
| 107 | final MenuItem fileSize = menu.findItem(R.id.menu_file_size); |
Steve McKay | 9c62fd8 | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 108 | |
| 109 | advanced.setVisible(false); |
| 110 | createDir.setVisible(false); |
| 111 | pasteFromCb.setEnabled(false); |
Steve McKay | 9c62fd8 | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 112 | fileSize.setVisible(false); |
Steve McKay | 9c62fd8 | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 113 | |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 114 | Menus.disableHiddenItems(menu); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
| 118 | @Override |
Aga Wronska | f6a31d3 | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 119 | void refreshDirectory(int anim) { |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 120 | final FragmentManager fm = getFragmentManager(); |
| 121 | final RootInfo root = getCurrentRoot(); |
| 122 | final DocumentInfo cwd = getCurrentDirectory(); |
| 123 | |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 124 | if (DEBUG) checkState(!mSearchManager.isSearching()); |
| 125 | |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 126 | // If started in manage roots mode, there has to be a cwd (i.e. the root dir of the managed |
| 127 | // root). |
| 128 | Preconditions.checkNotNull(cwd); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 129 | |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 130 | // Normal boring directory |
| 131 | DirectoryFragment.showDirectory(fm, root, cwd, anim); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 135 | public void onDocumentPicked(DocumentInfo doc, Model model) { |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 136 | Preconditions.checkArgument(!doc.isDirectory()); |
| 137 | // First try managing the document; we expect manager to filter |
| 138 | // based on authority, so we don't grant. |
| 139 | final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT); |
| 140 | manage.setData(doc.derivedUri); |
| 141 | |
| 142 | try { |
| 143 | startActivity(manage); |
| 144 | } catch (ActivityNotFoundException ex) { |
| 145 | // Fall back to viewing. |
| 146 | final Intent view = new Intent(Intent.ACTION_VIEW); |
| 147 | view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 148 | view.setData(doc.derivedUri); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 149 | |
| 150 | try { |
Tomasz Mikolajewski | a6120da | 2016-01-27 17:36:51 +0900 | [diff] [blame] | 151 | startActivity(view); |
| 152 | } catch (ActivityNotFoundException ex2) { |
| 153 | Snackbars.makeSnackbar(this, R.string.toast_no_application, Snackbar.LENGTH_SHORT) |
| 154 | .show(); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onDocumentsPicked(List<DocumentInfo> docs) {} |
| 161 | |
| 162 | @Override |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 163 | void onTaskFinished(Uri... uris) { |
| 164 | Log.d(TAG, "onFinished() " + Arrays.toString(uris)); |
| 165 | |
| 166 | final Intent intent = new Intent(); |
| 167 | if (uris.length == 1) { |
| 168 | intent.setData(uris[0]); |
| 169 | } else if (uris.length > 1) { |
| 170 | final ClipData clipData = new ClipData( |
| 171 | null, mState.acceptMimes, new ClipData.Item(uris[0])); |
| 172 | for (int i = 1; i < uris.length; i++) { |
| 173 | clipData.addItem(new ClipData.Item(uris[i])); |
| 174 | } |
| 175 | intent.setClipData(clipData); |
| 176 | } |
| 177 | |
| 178 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 179 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 180 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
| 181 | |
| 182 | setResult(Activity.RESULT_OK, intent); |
| 183 | finish(); |
| 184 | } |
| 185 | |
Steve McKay | 0279419 | 2015-12-01 16:20:41 -0800 | [diff] [blame] | 186 | public static DownloadsActivity get(Fragment fragment) { |
| 187 | return (DownloadsActivity) fragment.getActivity(); |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 188 | } |
| 189 | } |