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