Ben Kwa | 0574b18 | 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 | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.State.ACTION_MANAGE; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 20 | import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE; |
Ben Kwa | 0574b18 | 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; |
| 27 | import android.content.ContentResolver; |
| 28 | import android.content.ContentValues; |
| 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 | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 34 | import android.support.design.widget.Snackbar; |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.Menu; |
Steve McKay | 14e3caf | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 37 | import android.view.MenuItem; |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 38 | import android.view.View; |
| 39 | import android.widget.BaseAdapter; |
| 40 | import android.widget.Spinner; |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 41 | import android.widget.Toolbar; |
| 42 | |
| 43 | import com.android.documentsui.RecentsProvider.ResumeColumns; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 44 | import com.android.documentsui.dirlist.DirectoryFragment; |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 45 | import com.android.documentsui.model.DocumentInfo; |
| 46 | import com.android.documentsui.model.DurableUtils; |
| 47 | import com.android.documentsui.model.RootInfo; |
| 48 | import com.android.internal.util.Preconditions; |
| 49 | |
| 50 | import java.util.Arrays; |
| 51 | import java.util.List; |
| 52 | |
Steve McKay | 0fd6eb4 | 2015-12-01 16:20:41 -0800 | [diff] [blame] | 53 | // Let's face it. MANAGE_ROOT is used almost exclusively |
| 54 | // for downloads, and is specialized for this purpose. |
| 55 | // So it is now thusly christened. |
| 56 | public class DownloadsActivity extends BaseActivity { |
| 57 | private static final String TAG = "DownloadsActivity"; |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 58 | |
| 59 | private Toolbar mToolbar; |
| 60 | private Spinner mToolbarStack; |
| 61 | |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 62 | private ItemSelectedListener mStackListener; |
| 63 | private BaseAdapter mStackAdapter; |
| 64 | |
Steve McKay | 0fd6eb4 | 2015-12-01 16:20:41 -0800 | [diff] [blame] | 65 | public DownloadsActivity() { |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 66 | super(R.layout.manage_roots_activity, TAG); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onCreate(Bundle icicle) { |
| 71 | super.onCreate(icicle); |
| 72 | |
| 73 | final Context context = this; |
| 74 | |
| 75 | mDrawer = DrawerController.createDummy(); |
| 76 | |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 77 | mToolbar = (Toolbar) findViewById(R.id.toolbar); |
| 78 | mToolbar.setTitleTextAppearance(context, |
| 79 | android.R.style.TextAppearance_DeviceDefault_Widget_ActionBar_Title); |
| 80 | |
| 81 | mStackAdapter = new StackAdapter(); |
| 82 | mStackListener = new ItemSelectedListener(); |
| 83 | mToolbarStack = (Spinner) findViewById(R.id.stack); |
| 84 | mToolbarStack.setOnItemSelectedListener(mStackListener); |
| 85 | |
| 86 | setActionBar(mToolbar); |
| 87 | |
| 88 | if (!mState.restored) { |
| 89 | // In this case, we set the activity title in AsyncTask.onPostExecute(). To prevent |
| 90 | // talkback from reading aloud the default title, we clear it here. |
| 91 | setTitle(""); |
| 92 | final Uri rootUri = getIntent().getData(); |
Steve McKay | 459bc2b | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 93 | new RestoreRootTask(rootUri).executeOnExecutor(getExecutorForCurrentDirectory()); |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 94 | } else { |
| 95 | onCurrentDirectoryChanged(ANIM_NONE); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | State buildState() { |
| 101 | State state = buildDefaultState(); |
| 102 | |
| 103 | state.action = ACTION_MANAGE; |
| 104 | state.acceptMimes = new String[] { "*/*" }; |
| 105 | state.allowMultiple = true; |
| 106 | state.showSize = true; |
| 107 | state.excludedAuthorities = getExcludedAuthorities(); |
| 108 | |
| 109 | return state; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected void onPostCreate(Bundle savedInstanceState) { |
| 114 | super.onPostCreate(savedInstanceState); |
| 115 | updateActionBar(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void updateActionBar() { |
| 120 | // No navigation in manage root mode. |
| 121 | mToolbar.setNavigationIcon(null); |
| 122 | mToolbar.setNavigationOnClickListener(null); |
| 123 | |
| 124 | if (mSearchManager.isExpanded()) { |
| 125 | mToolbar.setTitle(null); |
| 126 | mToolbarStack.setVisibility(View.GONE); |
| 127 | mToolbarStack.setAdapter(null); |
| 128 | } else { |
| 129 | if (mState.stack.size() <= 1) { |
| 130 | mToolbar.setTitle(getCurrentRoot().title); |
| 131 | mToolbarStack.setVisibility(View.GONE); |
| 132 | mToolbarStack.setAdapter(null); |
| 133 | } else { |
| 134 | mToolbar.setTitle(null); |
| 135 | mToolbarStack.setVisibility(View.VISIBLE); |
| 136 | mToolbarStack.setAdapter(mStackAdapter); |
| 137 | |
| 138 | mStackListener.mIgnoreNextNavigation = true; |
| 139 | mToolbarStack.setSelection(mStackAdapter.getCount() - 1); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 146 | super.onPrepareOptionsMenu(menu); |
Steve McKay | 14e3caf | 2015-11-03 11:56:44 -0800 | [diff] [blame] | 147 | |
| 148 | final MenuItem advanced = menu.findItem(R.id.menu_advanced); |
| 149 | final MenuItem createDir = menu.findItem(R.id.menu_create_dir); |
| 150 | final MenuItem newWindow = menu.findItem(R.id.menu_new_window); |
| 151 | final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard); |
| 152 | final MenuItem fileSize = menu.findItem(R.id.menu_file_size); |
| 153 | final MenuItem search = menu.findItem(R.id.menu_search); |
| 154 | |
| 155 | advanced.setVisible(false); |
| 156 | createDir.setVisible(false); |
| 157 | pasteFromCb.setEnabled(false); |
| 158 | newWindow.setEnabled(false); |
| 159 | fileSize.setVisible(false); |
| 160 | search.setVisible(false); |
| 161 | |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 162 | Menus.disableHiddenItems(menu); |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | void onDirectoryChanged(int anim) { |
| 168 | final FragmentManager fm = getFragmentManager(); |
| 169 | final RootInfo root = getCurrentRoot(); |
| 170 | final DocumentInfo cwd = getCurrentDirectory(); |
| 171 | |
| 172 | // If started in manage roots mode, there has to be a cwd (i.e. the root dir of the managed |
| 173 | // root). |
| 174 | Preconditions.checkNotNull(cwd); |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 175 | |
| 176 | if (mState.currentSearch != null) { |
| 177 | // Ongoing search |
| 178 | DirectoryFragment.showSearch(fm, root, mState.currentSearch, anim); |
| 179 | } else { |
| 180 | // Normal boring directory |
| 181 | DirectoryFragment.showNormal(fm, root, cwd, anim); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame^] | 186 | public void onDocumentPicked(DocumentInfo doc, SiblingProvider siblings) { |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 187 | final FragmentManager fm = getFragmentManager(); |
Tomasz Mikolajewski | 734936a | 2015-11-25 13:01:18 +0900 | [diff] [blame] | 188 | if (doc.isContainer()) { |
| 189 | openContainerDocument(doc); |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 190 | } else { |
| 191 | // First try managing the document; we expect manager to filter |
| 192 | // based on authority, so we don't grant. |
| 193 | final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT); |
| 194 | manage.setData(doc.derivedUri); |
| 195 | |
| 196 | try { |
| 197 | startActivity(manage); |
| 198 | } catch (ActivityNotFoundException ex) { |
| 199 | // Fall back to viewing |
| 200 | final Intent view = new Intent(Intent.ACTION_VIEW); |
| 201 | view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 202 | view.setData(doc.derivedUri); |
| 203 | |
| 204 | try { |
| 205 | startActivity(view); |
| 206 | } catch (ActivityNotFoundException ex2) { |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 207 | Snackbars.makeSnackbar(this, R.string.toast_no_application, Snackbar.LENGTH_SHORT) |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 208 | .show(); |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void onDocumentsPicked(List<DocumentInfo> docs) {} |
| 216 | |
| 217 | @Override |
| 218 | void saveStackBlocking() { |
| 219 | final ContentResolver resolver = getContentResolver(); |
| 220 | final ContentValues values = new ContentValues(); |
| 221 | |
| 222 | final byte[] rawStack = DurableUtils.writeToArrayOrNull(mState.stack); |
| 223 | |
| 224 | // Remember location for next app launch |
| 225 | final String packageName = getCallingPackageMaybeExtra(); |
| 226 | values.clear(); |
| 227 | values.put(ResumeColumns.STACK, rawStack); |
| 228 | values.put(ResumeColumns.EXTERNAL, 0); |
| 229 | resolver.insert(RecentsProvider.buildResume(packageName), values); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | void onTaskFinished(Uri... uris) { |
| 234 | Log.d(TAG, "onFinished() " + Arrays.toString(uris)); |
| 235 | |
| 236 | final Intent intent = new Intent(); |
| 237 | if (uris.length == 1) { |
| 238 | intent.setData(uris[0]); |
| 239 | } else if (uris.length > 1) { |
| 240 | final ClipData clipData = new ClipData( |
| 241 | null, mState.acceptMimes, new ClipData.Item(uris[0])); |
| 242 | for (int i = 1; i < uris.length; i++) { |
| 243 | clipData.addItem(new ClipData.Item(uris[i])); |
| 244 | } |
| 245 | intent.setClipData(clipData); |
| 246 | } |
| 247 | |
| 248 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 249 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 250 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
| 251 | |
| 252 | setResult(Activity.RESULT_OK, intent); |
| 253 | finish(); |
| 254 | } |
| 255 | |
Steve McKay | 0fd6eb4 | 2015-12-01 16:20:41 -0800 | [diff] [blame] | 256 | public static DownloadsActivity get(Fragment fragment) { |
| 257 | return (DownloadsActivity) fragment.getActivity(); |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 258 | } |
| 259 | } |