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