blob: f7a45e218a82fc6056b4797e0394ec43511027a9 [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;
42import com.android.internal.util.Preconditions;
43
44import java.util.Arrays;
45import java.util.List;
46
Steve McKay02794192015-12-01 16:20:41 -080047// 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.
50public class DownloadsActivity extends BaseActivity {
51 private static final String TAG = "DownloadsActivity";
Ben Kwa0f7078f02015-09-08 07:31:19 -070052
Steve McKay02794192015-12-01 16:20:41 -080053 public DownloadsActivity() {
Steve McKay1f264a82016-02-03 11:15:57 -080054 super(R.layout.downloads_activity, TAG);
Ben Kwa0f7078f02015-09-08 07:31:19 -070055 }
56
57 @Override
58 public void onCreate(Bundle icicle) {
59 super.onCreate(icicle);
60
61 final Context context = this;
62
Steve McKay1f264a82016-02-03 11:15:57 -080063 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
64 toolbar.setTitleTextAppearance(context,
Ben Kwa0f7078f02015-09-08 07:31:19 -070065 android.R.style.TextAppearance_DeviceDefault_Widget_ActionBar_Title);
66
Ben Kwa0f7078f02015-09-08 07:31:19 -070067 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 McKay95cd85a2016-02-04 12:15:22 -080072 new RestoreRootTask(this, rootUri).executeOnExecutor(getExecutorForCurrentDirectory());
Ben Kwa0f7078f02015-09-08 07:31:19 -070073 } else {
Aga Wronskaf6a31d32016-01-15 17:30:15 -080074 refreshCurrentRootAndDirectory(ANIM_NONE);
Ben Kwa0f7078f02015-09-08 07:31:19 -070075 }
76 }
77
78 @Override
Steve McKay95cd85a2016-02-04 12:15:22 -080079 void includeState(State state) {
Ben Kwa0f7078f02015-09-08 07:31:19 -070080 state.action = ACTION_MANAGE;
81 state.acceptMimes = new String[] { "*/*" };
82 state.allowMultiple = true;
83 state.showSize = true;
84 state.excludedAuthorities = getExcludedAuthorities();
Ben Kwa0f7078f02015-09-08 07:31:19 -070085 }
86
87 @Override
88 protected void onPostCreate(Bundle savedInstanceState) {
89 super.onPostCreate(savedInstanceState);
Steve McKay1f264a82016-02-03 11:15:57 -080090 mNavigator.update();
Ben Kwa0f7078f02015-09-08 07:31:19 -070091 }
92
93 @Override
Steve McKay1f264a82016-02-03 11:15:57 -080094 public String getDrawerTitle() {
95 return null; // being and nothingness
Ben Kwa0f7078f02015-09-08 07:31:19 -070096 }
97
98 @Override
99 public boolean onPrepareOptionsMenu(Menu menu) {
100 super.onPrepareOptionsMenu(menu);
Steve McKay9c62fd82015-11-03 11:56:44 -0800101
102 final MenuItem advanced = menu.findItem(R.id.menu_advanced);
103 final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
Steve McKay9c62fd82015-11-03 11:56:44 -0800104 final MenuItem pasteFromCb = menu.findItem(R.id.menu_paste_from_clipboard);
105 final MenuItem fileSize = menu.findItem(R.id.menu_file_size);
Steve McKay9c62fd82015-11-03 11:56:44 -0800106
107 advanced.setVisible(false);
108 createDir.setVisible(false);
109 pasteFromCb.setEnabled(false);
Steve McKay9c62fd82015-11-03 11:56:44 -0800110 fileSize.setVisible(false);
Steve McKay9c62fd82015-11-03 11:56:44 -0800111
Steve McKay5bbae102015-10-01 11:39:24 -0700112 Menus.disableHiddenItems(menu);
Ben Kwa0f7078f02015-09-08 07:31:19 -0700113 return true;
114 }
115
116 @Override
Aga Wronskaf6a31d32016-01-15 17:30:15 -0800117 void refreshDirectory(int anim) {
Ben Kwa0f7078f02015-09-08 07:31:19 -0700118 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 Kwa0f7078f02015-09-08 07:31:19 -0700125
126 if (mState.currentSearch != null) {
127 // Ongoing search
128 DirectoryFragment.showSearch(fm, root, mState.currentSearch, anim);
129 } else {
130 // Normal boring directory
Aga Wronskaf6a31d32016-01-15 17:30:15 -0800131 DirectoryFragment.showDirectory(fm, root, cwd, anim);
Ben Kwa0f7078f02015-09-08 07:31:19 -0700132 }
133 }
134
135 @Override
Tomasz Mikolajewski3d988a92016-02-16 12:28:43 +0900136 public void onDocumentPicked(DocumentInfo doc, Model model) {
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900137 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 Kwa0f7078f02015-09-08 07:31:19 -0700150
151 try {
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900152 startActivity(view);
153 } catch (ActivityNotFoundException ex2) {
154 Snackbars.makeSnackbar(this, R.string.toast_no_application, Snackbar.LENGTH_SHORT)
155 .show();
Ben Kwa0f7078f02015-09-08 07:31:19 -0700156 }
157 }
158 }
159
160 @Override
161 public void onDocumentsPicked(List<DocumentInfo> docs) {}
162
163 @Override
Ben Kwa0f7078f02015-09-08 07:31:19 -0700164 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 McKay02794192015-12-01 16:20:41 -0800187 public static DownloadsActivity get(Fragment fragment) {
188 return (DownloadsActivity) fragment.getActivity();
Ben Kwa0f7078f02015-09-08 07:31:19 -0700189 }
190}