blob: b5d3ea082d50481c9a722e9679d4757e36251051 [file] [log] [blame]
Ben Kwa0f7078f02015-09-08 07:31:19 -07001/*
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
17package com.android.documentsui;
18
Steve McKay7a3b88c2015-09-23 17:21:40 -070019import static com.android.documentsui.State.ACTION_MANAGE;
Steve McKayf8621552015-11-03 15:23:16 -080020import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE;
Ben Kwa0f7078f02015-09-08 07:31:19 -070021
22import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.content.ActivityNotFoundException;
26import android.content.ClipData;
Ben Kwa0f7078f02015-09-08 07:31:19 -070027import android.content.Context;
28import android.content.Intent;
29import android.net.Uri;
30import android.os.Bundle;
31import android.provider.DocumentsContract;
Ben Kwa94b486d2015-09-30 10:00:10 -070032import android.support.design.widget.Snackbar;
Ben Kwa0f7078f02015-09-08 07:31:19 -070033import android.util.Log;
34import android.view.Menu;
Steve McKay9c62fd82015-11-03 11:56:44 -080035import android.view.MenuItem;
Ben Kwa0f7078f02015-09-08 07:31:19 -070036import android.widget.Toolbar;
37
Steve McKayf8621552015-11-03 15:23:16 -080038import com.android.documentsui.dirlist.DirectoryFragment;
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +090039import com.android.documentsui.dirlist.Model;
Ben Kwa0f7078f02015-09-08 07:31:19 -070040import com.android.documentsui.model.DocumentInfo;
Ben Kwa0f7078f02015-09-08 07:31:19 -070041import com.android.documentsui.model.RootInfo;
Ben Kwa0f7078f02015-09-08 07:31:19 -070042
43import java.util.Arrays;
44import java.util.List;
45
Steve McKay02794192015-12-01 16:20:41 -080046// 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.
49public class DownloadsActivity extends BaseActivity {
50 private static final String TAG = "DownloadsActivity";
Ben Kwa0f7078f02015-09-08 07:31:19 -070051
Steve McKay02794192015-12-01 16:20:41 -080052 public DownloadsActivity() {
Steve McKay1f264a82016-02-03 11:15:57 -080053 super(R.layout.downloads_activity, TAG);
Ben Kwa0f7078f02015-09-08 07:31:19 -070054 }
55
56 @Override
57 public void onCreate(Bundle icicle) {
58 super.onCreate(icicle);
59
60 final Context context = this;
61
Steve McKay1f264a82016-02-03 11:15:57 -080062 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
63 toolbar.setTitleTextAppearance(context,
Ben Kwa0f7078f02015-09-08 07:31:19 -070064 android.R.style.TextAppearance_DeviceDefault_Widget_ActionBar_Title);
65
Ben Kwa0f7078f02015-09-08 07:31:19 -070066 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 McKayc95d87c2016-02-23 14:34:50 -080071 new LoadRootTask(this, rootUri).executeOnExecutor(getExecutorForCurrentDirectory());
Ben Kwa0f7078f02015-09-08 07:31:19 -070072 } else {
Aga Wronskaf6a31d32016-01-15 17:30:15 -080073 refreshCurrentRootAndDirectory(ANIM_NONE);
Ben Kwa0f7078f02015-09-08 07:31:19 -070074 }
75 }
76
77 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -080078 void includeState(State state) {
Ben Kwa0f7078f02015-09-08 07:31:19 -070079 state.action = ACTION_MANAGE;
80 state.acceptMimes = new String[] { "*/*" };
81 state.allowMultiple = true;
82 state.showSize = true;
83 state.excludedAuthorities = getExcludedAuthorities();
Ben Kwa0f7078f02015-09-08 07:31:19 -070084 }
85
86 @Override
87 protected void onPostCreate(Bundle savedInstanceState) {
88 super.onPostCreate(savedInstanceState);
Steve McKay1f264a82016-02-03 11:15:57 -080089 mNavigator.update();
Ben Kwa0f7078f02015-09-08 07:31:19 -070090 }
91
92 @Override
Steve McKay1f264a82016-02-03 11:15:57 -080093 public String getDrawerTitle() {
94 return null; // being and nothingness
Ben Kwa0f7078f02015-09-08 07:31:19 -070095 }
96
97 @Override
98 public boolean onPrepareOptionsMenu(Menu menu) {
99 super.onPrepareOptionsMenu(menu);
Steve McKay9c62fd82015-11-03 11:56:44 -0800100
101 final MenuItem advanced = menu.findItem(R.id.menu_advanced);
102 final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
Steve McKay9c62fd82015-11-03 11:56:44 -0800103 final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard);
104 final MenuItem fileSize = menu.findItem(R.id.menu_file_size);
Steve McKay9c62fd82015-11-03 11:56:44 -0800105
106 advanced.setVisible(false);
107 createDir.setVisible(false);
108 pasteFromCb.setEnabled(false);
Steve McKay9c62fd82015-11-03 11:56:44 -0800109 fileSize.setVisible(false);
Steve McKay9c62fd82015-11-03 11:56:44 -0800110
Steve McKay5bbae102015-10-01 11:39:24 -0700111 Menus.disableHiddenItems(menu);
Ben Kwa0f7078f02015-09-08 07:31:19 -0700112 return true;
113 }
114
115 @Override
Aga Wronskaf6a31d32016-01-15 17:30:15 -0800116 void refreshDirectory(int anim) {
Ben Kwa0f7078f02015-09-08 07:31:19 -0700117 final FragmentManager fm = getFragmentManager();
118 final RootInfo root = getCurrentRoot();
119 final DocumentInfo cwd = getCurrentDirectory();
120
Steve McKaya1f76802016-02-25 13:34:03 -0800121 assert(!mSearchManager.isSearching());
Aga Wronska893390b2016-02-17 13:50:42 -0800122
Ben Kwa0f7078f02015-09-08 07:31:19 -0700123 // If started in manage roots mode, there has to be a cwd (i.e. the root dir of the managed
124 // root).
Steve McKaya1f76802016-02-25 13:34:03 -0800125 assert(cwd != null);
Ben Kwa0f7078f02015-09-08 07:31:19 -0700126
Aga Wronska893390b2016-02-17 13:50:42 -0800127 // Normal boring directory
128 DirectoryFragment.showDirectory(fm, root, cwd, anim);
Ben Kwa0f7078f02015-09-08 07:31:19 -0700129 }
130
131 @Override
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900132 public void onDocumentPicked(DocumentInfo doc, Model model) {
Steve McKaya1f76802016-02-25 13:34:03 -0800133 assert(!doc.isDirectory());
134
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900135 // 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 Kwa0f7078f02015-09-08 07:31:19 -0700147
148 try {
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900149 startActivity(view);
150 } catch (ActivityNotFoundException ex2) {
151 Snackbars.makeSnackbar(this, R.string.toast_no_application, Snackbar.LENGTH_SHORT)
152 .show();
Ben Kwa0f7078f02015-09-08 07:31:19 -0700153 }
154 }
155 }
156
157 @Override
158 public void onDocumentsPicked(List<DocumentInfo> docs) {}
159
160 @Override
Ben Kwa0f7078f02015-09-08 07:31:19 -0700161 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 McKay02794192015-12-01 16:20:41 -0800184 public static DownloadsActivity get(Fragment fragment) {
185 return (DownloadsActivity) fragment.getActivity();
Ben Kwa0f7078f02015-09-08 07:31:19 -0700186 }
187}