blob: 536feeb4ecc8a44c0293e6e47f57e90e79835157 [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
Aga Wronska893390b2016-02-17 13:50:42 -080019import static com.android.documentsui.Shared.DEBUG;
Steve McKay7a3b88c2015-09-23 17:21:40 -070020import static com.android.documentsui.State.ACTION_MANAGE;
Steve McKayf8621552015-11-03 15:23:16 -080021import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE;
Aga Wronska893390b2016-02-17 13:50:42 -080022import static com.android.internal.util.Preconditions.checkState;
Ben Kwa0f7078f02015-09-08 07:31:19 -070023
24import android.app.Activity;
25import android.app.Fragment;
26import android.app.FragmentManager;
27import android.content.ActivityNotFoundException;
28import android.content.ClipData;
Ben Kwa0f7078f02015-09-08 07:31:19 -070029import android.content.Context;
30import android.content.Intent;
31import android.net.Uri;
32import android.os.Bundle;
33import android.provider.DocumentsContract;
Ben Kwa94b486d2015-09-30 10:00:10 -070034import android.support.design.widget.Snackbar;
Ben Kwa0f7078f02015-09-08 07:31:19 -070035import android.util.Log;
36import android.view.Menu;
Steve McKay9c62fd82015-11-03 11:56:44 -080037import android.view.MenuItem;
Ben Kwa0f7078f02015-09-08 07:31:19 -070038import android.widget.Toolbar;
39
Steve McKayf8621552015-11-03 15:23:16 -080040import com.android.documentsui.dirlist.DirectoryFragment;
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +090041import com.android.documentsui.dirlist.Model;
Ben Kwa0f7078f02015-09-08 07:31:19 -070042import com.android.documentsui.model.DocumentInfo;
Ben Kwa0f7078f02015-09-08 07:31:19 -070043import com.android.documentsui.model.RootInfo;
44import com.android.internal.util.Preconditions;
45
46import java.util.Arrays;
47import java.util.List;
48
Steve McKay02794192015-12-01 16:20:41 -080049// 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.
52public class DownloadsActivity extends BaseActivity {
53 private static final String TAG = "DownloadsActivity";
Ben Kwa0f7078f02015-09-08 07:31:19 -070054
Steve McKay02794192015-12-01 16:20:41 -080055 public DownloadsActivity() {
Steve McKay1f264a82016-02-03 11:15:57 -080056 super(R.layout.downloads_activity, TAG);
Ben Kwa0f7078f02015-09-08 07:31:19 -070057 }
58
59 @Override
60 public void onCreate(Bundle icicle) {
61 super.onCreate(icicle);
62
63 final Context context = this;
64
Steve McKay1f264a82016-02-03 11:15:57 -080065 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
66 toolbar.setTitleTextAppearance(context,
Ben Kwa0f7078f02015-09-08 07:31:19 -070067 android.R.style.TextAppearance_DeviceDefault_Widget_ActionBar_Title);
68
Ben Kwa0f7078f02015-09-08 07:31:19 -070069 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 McKayc95d87c2016-02-23 14:34:50 -080074 new LoadRootTask(this, rootUri).executeOnExecutor(getExecutorForCurrentDirectory());
Ben Kwa0f7078f02015-09-08 07:31:19 -070075 } else {
Aga Wronskaf6a31d32016-01-15 17:30:15 -080076 refreshCurrentRootAndDirectory(ANIM_NONE);
Ben Kwa0f7078f02015-09-08 07:31:19 -070077 }
78 }
79
80 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -080081 void includeState(State state) {
Ben Kwa0f7078f02015-09-08 07:31:19 -070082 state.action = ACTION_MANAGE;
83 state.acceptMimes = new String[] { "*/*" };
84 state.allowMultiple = true;
85 state.showSize = true;
86 state.excludedAuthorities = getExcludedAuthorities();
Ben Kwa0f7078f02015-09-08 07:31:19 -070087 }
88
89 @Override
90 protected void onPostCreate(Bundle savedInstanceState) {
91 super.onPostCreate(savedInstanceState);
Steve McKay1f264a82016-02-03 11:15:57 -080092 mNavigator.update();
Ben Kwa0f7078f02015-09-08 07:31:19 -070093 }
94
95 @Override
Steve McKay1f264a82016-02-03 11:15:57 -080096 public String getDrawerTitle() {
97 return null; // being and nothingness
Ben Kwa0f7078f02015-09-08 07:31:19 -070098 }
99
100 @Override
101 public boolean onPrepareOptionsMenu(Menu menu) {
102 super.onPrepareOptionsMenu(menu);
Steve McKay9c62fd82015-11-03 11:56:44 -0800103
104 final MenuItem advanced = menu.findItem(R.id.menu_advanced);
105 final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
Steve McKay9c62fd82015-11-03 11:56:44 -0800106 final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard);
107 final MenuItem fileSize = menu.findItem(R.id.menu_file_size);
Steve McKay9c62fd82015-11-03 11:56:44 -0800108
109 advanced.setVisible(false);
110 createDir.setVisible(false);
111 pasteFromCb.setEnabled(false);
Steve McKay9c62fd82015-11-03 11:56:44 -0800112 fileSize.setVisible(false);
Steve McKay9c62fd82015-11-03 11:56:44 -0800113
Steve McKay5bbae102015-10-01 11:39:24 -0700114 Menus.disableHiddenItems(menu);
Ben Kwa0f7078f02015-09-08 07:31:19 -0700115 return true;
116 }
117
118 @Override
Aga Wronskaf6a31d32016-01-15 17:30:15 -0800119 void refreshDirectory(int anim) {
Ben Kwa0f7078f02015-09-08 07:31:19 -0700120 final FragmentManager fm = getFragmentManager();
121 final RootInfo root = getCurrentRoot();
122 final DocumentInfo cwd = getCurrentDirectory();
123
Aga Wronska893390b2016-02-17 13:50:42 -0800124 if (DEBUG) checkState(!mSearchManager.isSearching());
125
Ben Kwa0f7078f02015-09-08 07:31:19 -0700126 // 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 Kwa0f7078f02015-09-08 07:31:19 -0700129
Aga Wronska893390b2016-02-17 13:50:42 -0800130 // Normal boring directory
131 DirectoryFragment.showDirectory(fm, root, cwd, anim);
Ben Kwa0f7078f02015-09-08 07:31:19 -0700132 }
133
134 @Override
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900135 public void onDocumentPicked(DocumentInfo doc, Model model) {
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900136 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 Kwa0f7078f02015-09-08 07:31:19 -0700149
150 try {
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900151 startActivity(view);
152 } catch (ActivityNotFoundException ex2) {
153 Snackbars.makeSnackbar(this, R.string.toast_no_application, Snackbar.LENGTH_SHORT)
154 .show();
Ben Kwa0f7078f02015-09-08 07:31:19 -0700155 }
156 }
157 }
158
159 @Override
160 public void onDocumentsPicked(List<DocumentInfo> docs) {}
161
162 @Override
Ben Kwa0f7078f02015-09-08 07:31:19 -0700163 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 McKay02794192015-12-01 16:20:41 -0800186 public static DownloadsActivity get(Fragment fragment) {
187 return (DownloadsActivity) fragment.getActivity();
Ben Kwa0f7078f02015-09-08 07:31:19 -0700188 }
189}