blob: 44d7136258ae0a15328add9105a711e2d498d96f [file] [log] [blame]
Steve McKayd0a2a2c2015-03-25 14:35:33 -07001/*
2 * Copyright (C) 2015 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 McKay4d0255f2015-09-25 16:02:56 -070019import static com.android.documentsui.Shared.DEBUG;
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +090020import static com.android.documentsui.Shared.EXTRA_BENCHMARK;
Aga Wronskaceb990e2016-03-30 18:07:59 -070021import static com.android.documentsui.State.ACTION_CREATE;
Steve McKay17f7e582016-04-04 15:26:48 -070022import static com.android.documentsui.State.ACTION_GET_CONTENT;
Aga Wronskaceb990e2016-03-30 18:07:59 -070023import static com.android.documentsui.State.ACTION_OPEN;
24import static com.android.documentsui.State.ACTION_OPEN_TREE;
Steve McKay7776aa52016-01-25 19:00:22 -080025import static com.android.documentsui.State.MODE_GRID;
Steve McKayb68dd222015-04-20 17:18:15 -070026
Steve McKayd0a2a2c2015-03-25 14:35:33 -070027import android.app.Activity;
28import android.app.Fragment;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080029import android.app.FragmentManager;
Steve McKayb68dd222015-04-20 17:18:15 -070030import android.content.Intent;
Ben Kwa0bcdec32015-05-29 15:40:31 -070031import android.content.pm.ApplicationInfo;
32import android.content.pm.PackageInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.ProviderInfo;
Steve McKayb68dd222015-04-20 17:18:15 -070035import android.net.Uri;
36import android.os.AsyncTask;
37import android.os.Bundle;
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +090038import android.os.Handler;
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +090039import android.os.MessageQueue.IdleHandler;
Steve McKayb68dd222015-04-20 17:18:15 -070040import android.provider.DocumentsContract;
41import android.provider.DocumentsContract.Root;
Steve McKayd4800812016-02-02 11:41:03 -080042import android.support.annotation.CallSuper;
Steve McKay0fbfc652015-08-20 16:48:49 -070043import android.support.annotation.LayoutRes;
Steve McKayfefcd702015-08-20 16:19:38 +000044import android.support.annotation.Nullable;
Steve McKayb68dd222015-04-20 17:18:15 -070045import android.util.Log;
Ben Kwa74e5d412016-02-10 07:46:35 -080046import android.view.KeyEvent;
Steve McKayb68dd222015-04-20 17:18:15 -070047import android.view.Menu;
48import android.view.MenuItem;
Aga Wronska7b168802016-03-29 12:56:56 -070049import android.view.WindowManager;
Steve McKay18d01e82016-02-03 11:15:57 -080050import android.widget.Spinner;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070051
Aga Wronskaaf5ace52016-02-17 13:50:42 -080052import com.android.documentsui.SearchViewManager.SearchManagerListener;
Steve McKay7776aa52016-01-25 19:00:22 -080053import com.android.documentsui.State.ViewMode;
Steve McKayfb4fd2f2016-03-11 10:49:32 -080054import com.android.documentsui.dirlist.AnimationView;
Steve McKayf68210e2015-11-03 15:23:16 -080055import com.android.documentsui.dirlist.DirectoryFragment;
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090056import com.android.documentsui.dirlist.Model;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070057import com.android.documentsui.model.DocumentInfo;
58import com.android.documentsui.model.DocumentStack;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070059import com.android.documentsui.model.RootInfo;
Steve McKay64ac2512015-05-12 12:49:58 -070060
Steve McKay64ac2512015-05-12 12:49:58 -070061import java.io.FileNotFoundException;
Steve McKay64ac2512015-05-12 12:49:58 -070062import java.util.ArrayList;
63import java.util.Collection;
Daichi Hirono320a08f2016-03-25 19:04:39 +090064import java.util.Date;
Steve McKay64ac2512015-05-12 12:49:58 -070065import java.util.List;
66import java.util.concurrent.Executor;
67
Steve McKay18d01e82016-02-03 11:15:57 -080068public abstract class BaseActivity extends Activity
69 implements SearchManagerListener, NavigationView.Environment {
Steve McKayb68dd222015-04-20 17:18:15 -070070
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +090071 private static final String BENCHMARK_TESTING_PACKAGE = "com.android.documentsui.appperftests";
72
Steve McKay0fbfc652015-08-20 16:48:49 -070073 State mState;
Steve McKayb68dd222015-04-20 17:18:15 -070074 RootsCache mRoots;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080075 SearchViewManager mSearchManager;
Steve McKay0fbfc652015-08-20 16:48:49 -070076 DrawerController mDrawer;
Steve McKay18d01e82016-02-03 11:15:57 -080077 NavigationView mNavigator;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090078 List<EventListener> mEventListeners = new ArrayList<>();
Steve McKay0269fb62015-04-22 15:55:34 -070079
Steve McKayf2c8b0d2015-09-23 15:44:24 -070080 private final String mTag;
Steve McKay95c79f52016-02-04 19:40:45 -080081
Steve McKay0fbfc652015-08-20 16:48:49 -070082 @LayoutRes
83 private int mLayoutId;
Steve McKayb68dd222015-04-20 17:18:15 -070084
Ben Kwa74e5d412016-02-10 07:46:35 -080085 private boolean mNavDrawerHasFocus;
Daichi Hirono320a08f2016-03-25 19:04:39 +090086 private long mStartTime;
Ben Kwa74e5d412016-02-10 07:46:35 -080087
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090088 public abstract void onDocumentPicked(DocumentInfo doc, Model model);
Steve McKayd0a2a2c2015-03-25 14:35:33 -070089 public abstract void onDocumentsPicked(List<DocumentInfo> docs);
Steve McKay351a7492015-08-04 10:11:01 -070090
Steve McKayb68dd222015-04-20 17:18:15 -070091 abstract void onTaskFinished(Uri... uris);
Aga Wronska8788dad2016-01-15 17:30:15 -080092 abstract void refreshDirectory(int anim);
Steve McKayc7dc0cf2016-02-04 12:15:22 -080093 /** Allows sub-classes to include information in a newly created State instance. */
94 abstract void includeState(State initialState);
Steve McKayb68dd222015-04-20 17:18:15 -070095
Steve McKay0fbfc652015-08-20 16:48:49 -070096 public BaseActivity(@LayoutRes int layoutId, String tag) {
97 mLayoutId = layoutId;
Steve McKayb68dd222015-04-20 17:18:15 -070098 mTag = tag;
99 }
100
Steve McKay18d01e82016-02-03 11:15:57 -0800101 @CallSuper
Steve McKayb68dd222015-04-20 17:18:15 -0700102 @Override
103 public void onCreate(Bundle icicle) {
Daichi Hirono320a08f2016-03-25 19:04:39 +0900104 // Record the time when onCreate is invoked for metric.
105 mStartTime = new Date().getTime();
106
Steve McKayb68dd222015-04-20 17:18:15 -0700107 super.onCreate(icicle);
Steve McKay0fbfc652015-08-20 16:48:49 -0700108
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +0900109 final Intent intent = getIntent();
110
Daichi Hirono320a08f2016-03-25 19:04:39 +0900111 addListenerForLaunchCompletion();
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +0900112
Steve McKay18d01e82016-02-03 11:15:57 -0800113 setContentView(mLayoutId);
114
115 mDrawer = DrawerController.create(this);
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800116 mState = getState(icicle);
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +0900117 Metrics.logActivityLaunch(this, mState, intent);
Ben Kwa1c0a3892016-01-26 11:50:03 -0800118
Steve McKayb68dd222015-04-20 17:18:15 -0700119 mRoots = DocumentsApplication.getRootsCache(this);
Steve McKay18d01e82016-02-03 11:15:57 -0800120
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900121 mRoots.setOnCacheUpdateListener(
122 new RootsCache.OnCacheUpdateListener() {
123 @Override
124 public void onCacheUpdate() {
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800125 new HandleRootsChangedTask(BaseActivity.this)
126 .execute(getCurrentRoot());
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900127 }
128 });
Steve McKay18d01e82016-02-03 11:15:57 -0800129
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800130 mSearchManager = new SearchViewManager(this, icicle);
Steve McKay0fbfc652015-08-20 16:48:49 -0700131
Steve McKay18d01e82016-02-03 11:15:57 -0800132 DocumentsToolbar toolbar = (DocumentsToolbar) findViewById(R.id.toolbar);
133 setActionBar(toolbar);
134 mNavigator = new NavigationView(
135 mDrawer,
136 toolbar,
137 (Spinner) findViewById(R.id.stack),
138 mState,
139 this);
140
Steve McKay0fbfc652015-08-20 16:48:49 -0700141 // Base classes must update result in their onCreate.
142 setResult(Activity.RESULT_CANCELED);
Steve McKay0269fb62015-04-22 15:55:34 -0700143 }
144
145 @Override
146 public boolean onCreateOptionsMenu(Menu menu) {
147 boolean showMenu = super.onCreateOptionsMenu(menu);
148
149 getMenuInflater().inflate(R.menu.activity, menu);
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800150 mNavigator.update();
Aga Wronska8e21daa2016-03-24 18:22:09 -0700151 boolean fullBarSearch = getResources().getBoolean(R.bool.full_bar_search_view);
152 mSearchManager.install((DocumentsToolbar) findViewById(R.id.toolbar), fullBarSearch);
Steve McKay0269fb62015-04-22 15:55:34 -0700153
154 return showMenu;
Steve McKayb68dd222015-04-20 17:18:15 -0700155 }
156
Steve McKay7bd32e12015-04-30 16:12:59 -0700157 @Override
Steve McKayd4800812016-02-02 11:41:03 -0800158 @CallSuper
Steve McKay7bd32e12015-04-30 16:12:59 -0700159 public boolean onPrepareOptionsMenu(Menu menu) {
Steve McKaye9809272015-10-01 11:39:24 -0700160 super.onPrepareOptionsMenu(menu);
Steve McKay7bd32e12015-04-30 16:12:59 -0700161
Steve McKayd4800812016-02-02 11:41:03 -0800162 mSearchManager.showMenu(canSearchRoot());
163
Steve McKaye9809272015-10-01 11:39:24 -0700164 final boolean inRecents = getCurrentDirectory() == null;
Steve McKay7bd32e12015-04-30 16:12:59 -0700165
166 final MenuItem sort = menu.findItem(R.id.menu_sort);
167 final MenuItem sortSize = menu.findItem(R.id.menu_sort_size);
168 final MenuItem grid = menu.findItem(R.id.menu_grid);
169 final MenuItem list = menu.findItem(R.id.menu_list);
Aga Wronskaceb990e2016-03-30 18:07:59 -0700170 final MenuItem advanced = menu.findItem(R.id.menu_advanced);
Steve McKay7bd32e12015-04-30 16:12:59 -0700171 final MenuItem fileSize = menu.findItem(R.id.menu_file_size);
172
Steve McKayd4800812016-02-02 11:41:03 -0800173 // Search uses backend ranking; no sorting, recents doesn't support sort.
Steve McKayf2eb8d92016-03-08 14:01:47 -0800174 sort.setEnabled(!inRecents && !mSearchManager.isSearching());
Steve McKayd4800812016-02-02 11:41:03 -0800175 sortSize.setVisible(mState.showSize); // Only sort by size when file sizes are visible
176 fileSize.setVisible(!mState.forceSize);
Steve McKaye9809272015-10-01 11:39:24 -0700177
178 // grid/list is effectively a toggle.
179 grid.setVisible(mState.derivedMode != State.MODE_GRID);
180 list.setVisible(mState.derivedMode != State.MODE_LIST);
181
Aga Wronskaceb990e2016-03-30 18:07:59 -0700182 advanced.setVisible(mState.showAdvancedOption);
183 advanced.setTitle(mState.showAdvancedOption && mState.showAdvanced
184 ? R.string.menu_advanced_hide : R.string.menu_advanced_show);
Steve McKay7bd32e12015-04-30 16:12:59 -0700185 fileSize.setTitle(LocalPreferences.getDisplayFileSize(this)
186 ? R.string.menu_file_size_hide : R.string.menu_file_size_show);
187
Steve McKaye9809272015-10-01 11:39:24 -0700188 return true;
Steve McKay7bd32e12015-04-30 16:12:59 -0700189 }
190
Daichi Hironoe28c3c82016-01-13 13:19:02 +0900191 @Override
192 protected void onDestroy() {
193 mRoots.setOnCacheUpdateListener(null);
194 super.onDestroy();
195 }
196
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800197 private State getState(@Nullable Bundle icicle) {
198 if (icicle != null) {
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800199 State state = icicle.<State>getParcelable(Shared.EXTRA_STATE);
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800200 if (DEBUG) Log.d(mTag, "Recovered existing state object: " + state);
201 return state;
202 }
203
Ben Kwa0574b182015-09-08 07:31:19 -0700204 State state = new State();
205
206 final Intent intent = getIntent();
Ben Kwa0574b182015-09-08 07:31:19 -0700207
208 state.localOnly = intent.getBooleanExtra(Intent.EXTRA_LOCAL_ONLY, false);
Steve McKay459bc2b2015-09-16 15:07:31 -0700209 state.forceSize = intent.getBooleanExtra(DocumentsContract.EXTRA_SHOW_FILESIZE, false);
210 state.showSize = state.forceSize || LocalPreferences.getDisplayFileSize(this);
Steve McKay459bc2b2015-09-16 15:07:31 -0700211 state.initAcceptMimes(intent);
Ben Kwa0574b182015-09-08 07:31:19 -0700212 state.excludedAuthorities = getExcludedAuthorities();
213
Aga Wronskaceb990e2016-03-30 18:07:59 -0700214 includeState(state);
215
216 // Advanced roots are shown by deafult without menu option if forced by config or intent.
Steve McKay17f7e582016-04-04 15:26:48 -0700217 state.showAdvanced = Shared.shouldShowDeviceRoot(this, intent);
Aga Wronskaceb990e2016-03-30 18:07:59 -0700218 // Menu option is shown for whitelisted intents if advanced roots are not shown by default.
219 state.showAdvancedOption = !state.showAdvanced &&
220 (state.action == ACTION_OPEN ||
221 state.action == ACTION_CREATE ||
222 state.action == ACTION_OPEN_TREE ||
223 state.action == ACTION_GET_CONTENT);
224
225 if (DEBUG) Log.d(mTag, "Created new state object: " + state);
226
Ben Kwa0574b182015-09-08 07:31:19 -0700227 return state;
228 }
229
Steve McKaycb9eb422016-02-09 16:17:24 -0800230 public void setRootsDrawerOpen(boolean open) {
231 mNavigator.revealRootsDrawer(open);
232 }
Steve McKayb68dd222015-04-20 17:18:15 -0700233
234 void onRootPicked(RootInfo root) {
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800235 // Clicking on the current root removes search
236 mSearchManager.cancelSearch();
237
Aga Wronskafa421722016-02-08 12:00:38 -0800238 // Skip refreshing if root nor directory didn't change
239 if (root.equals(getCurrentRoot()) && mState.stack.size() == 1) {
Aga Wronskafd26e8d2016-02-02 18:01:58 -0800240 return;
241 }
242
Steve McKay7776aa52016-01-25 19:00:22 -0800243 mState.derivedMode = LocalPreferences.getViewMode(this, root, MODE_GRID);
244
Steve McKayb68dd222015-04-20 17:18:15 -0700245 // Clear entire backstack and start in new root
Daichi Hirono2806beb2016-01-07 15:29:12 +0900246 mState.onRootChanged(root);
Steve McKay0269fb62015-04-22 15:55:34 -0700247
248 // Recents is always in memory, so we just load it directly.
249 // Otherwise we delegate loading data from disk to a task
250 // to ensure a responsive ui.
251 if (mRoots.isRecentsRoot(root)) {
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800252 refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
Steve McKay0269fb62015-04-22 15:55:34 -0700253 } else {
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800254 new PickRootTask(this, root).executeOnExecutor(getExecutorForCurrentDirectory());
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900255 }
256 }
257
Steve McKayb68dd222015-04-20 17:18:15 -0700258 @Override
259 public boolean onOptionsItemSelected(MenuItem item) {
Steve McKayb68dd222015-04-20 17:18:15 -0700260
Steve McKay6035b3c2015-12-04 11:19:09 -0800261 switch (item.getItemId()) {
262 case android.R.id.home:
263 onBackPressed();
264 return true;
265
266 case R.id.menu_create_dir:
267 showCreateDirectoryDialog();
268 return true;
269
270 case R.id.menu_search:
Steve McKayf2eb8d92016-03-08 14:01:47 -0800271 // SearchViewManager listens for this directly.
Steve McKay6035b3c2015-12-04 11:19:09 -0800272 return false;
273
274 case R.id.menu_sort_name:
275 setUserSortOrder(State.SORT_ORDER_DISPLAY_NAME);
276 return true;
277
278 case R.id.menu_sort_date:
279 setUserSortOrder(State.SORT_ORDER_LAST_MODIFIED);
280 return true;
Aga Wronska94e53e42016-04-07 13:09:58 -0700281
Steve McKay6035b3c2015-12-04 11:19:09 -0800282 case R.id.menu_sort_size:
283 setUserSortOrder(State.SORT_ORDER_SIZE);
284 return true;
285
286 case R.id.menu_grid:
Steve McKay7776aa52016-01-25 19:00:22 -0800287 setViewMode(State.MODE_GRID);
Steve McKay6035b3c2015-12-04 11:19:09 -0800288 return true;
289
290 case R.id.menu_list:
Steve McKay7776aa52016-01-25 19:00:22 -0800291 setViewMode(State.MODE_LIST);
Steve McKay6035b3c2015-12-04 11:19:09 -0800292 return true;
293
294 case R.id.menu_paste_from_clipboard:
Steve McKayd4800812016-02-02 11:41:03 -0800295 DirectoryFragment dir = getDirectoryFragment();
296 if (dir != null) {
297 dir.pasteFromClipboard();
298 }
Ben Kwa65f393a2016-02-17 10:48:57 -0800299 return true;
Steve McKay6035b3c2015-12-04 11:19:09 -0800300
Aga Wronskaceb990e2016-03-30 18:07:59 -0700301 case R.id.menu_advanced:
302 setDisplayAdvancedDevices(!mState.showAdvanced);
303 return true;
304
Steve McKay6035b3c2015-12-04 11:19:09 -0800305 case R.id.menu_file_size:
306 setDisplayFileSize(!LocalPreferences.getDisplayFileSize(this));
307 return true;
308
309 case R.id.menu_settings:
Aga Wronska94e53e42016-04-07 13:09:58 -0700310 Metrics.logUserAction(this, Metrics.USER_ACTION_SETTINGS);
311
Steve McKay6035b3c2015-12-04 11:19:09 -0800312 final RootInfo root = getCurrentRoot();
313 final Intent intent = new Intent(DocumentsContract.ACTION_DOCUMENT_ROOT_SETTINGS);
314 intent.setDataAndType(root.getUri(), DocumentsContract.Root.MIME_TYPE_ITEM);
315 startActivity(intent);
316 return true;
317
318 default:
319 return super.onOptionsItemSelected(item);
320 }
Steve McKayb68dd222015-04-20 17:18:15 -0700321 }
322
Steve McKayd4800812016-02-02 11:41:03 -0800323 final @Nullable DirectoryFragment getDirectoryFragment() {
324 return DirectoryFragment.get(getFragmentManager());
325 }
326
Steve McKayceeb3f72015-05-19 16:10:25 -0700327 void showCreateDirectoryDialog() {
Aga Wronska94e53e42016-04-07 13:09:58 -0700328 Metrics.logUserAction(this, Metrics.USER_ACTION_CREATE_DIR);
329
Steve McKayceeb3f72015-05-19 16:10:25 -0700330 CreateDirectoryFragment.show(getFragmentManager());
331 }
332
Steve McKay5e1acc92016-02-19 12:57:05 -0800333 void onDirectoryCreated(DocumentInfo doc) {
334 // By default we do nothing, just let the new directory appear.
335 // DocumentsActivity auto-opens directories after creating them
336 // As that is more attuned to the "picker" use cases it supports.
337 }
338
Steve McKayceeb3f72015-05-19 16:10:25 -0700339 /**
340 * Returns true if a directory can be created in the current location.
341 * @return
342 */
343 boolean canCreateDirectory() {
344 final RootInfo root = getCurrentRoot();
345 final DocumentInfo cwd = getCurrentDirectory();
346 return cwd != null
347 && cwd.isCreateSupported()
348 && !mSearchManager.isSearching()
Steve McKaye9809272015-10-01 11:39:24 -0700349 && !root.isRecents()
Steve McKayceeb3f72015-05-19 16:10:25 -0700350 && !root.isDownloads();
351 }
352
Tomasz Mikolajewski734936a2015-11-25 13:01:18 +0900353 void openContainerDocument(DocumentInfo doc) {
Steve McKay0af8afd2016-02-25 13:34:03 -0800354 assert(doc.isContainer());
355
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900356 notifyDirectoryNavigated(doc.derivedUri);
357
Daichi Hirono2806beb2016-01-07 15:29:12 +0900358 mState.pushDocument(doc);
Tomasz Mikolajewski2b5170b2016-02-03 15:49:58 +0900359 // Show an opening animation only if pressing "back" would get us back to the
360 // previous directory. Especially after opening a root document, pressing
361 // back, wouldn't go to the previous root, but close the activity.
362 final int anim = (mState.hasLocationChanged() && mState.stack.size() > 1)
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800363 ? AnimationView.ANIM_ENTER : AnimationView.ANIM_NONE;
Tomasz Mikolajewski2b5170b2016-02-03 15:49:58 +0900364 refreshCurrentRootAndDirectory(anim);
Steve McKay351a7492015-08-04 10:11:01 -0700365 }
366
Steve McKayb68dd222015-04-20 17:18:15 -0700367 /**
Aga Wronska8788dad2016-01-15 17:30:15 -0800368 * Refreshes the content of the director and the menu/action bar.
369 * The current directory name and selection will get updated.
Steve McKayb68dd222015-04-20 17:18:15 -0700370 * @param anim
371 */
Steve McKay18d01e82016-02-03 11:15:57 -0800372 @Override
373 public final void refreshCurrentRootAndDirectory(int anim) {
Aga Wronska8788dad2016-01-15 17:30:15 -0800374 mSearchManager.cancelSearch();
375
Aga Wronska8788dad2016-01-15 17:30:15 -0800376 refreshDirectory(anim);
Steve McKayb68dd222015-04-20 17:18:15 -0700377
378 final RootsFragment roots = RootsFragment.get(getFragmentManager());
379 if (roots != null) {
380 roots.onCurrentRootChanged();
381 }
382
Steve McKay18d01e82016-02-03 11:15:57 -0800383 mNavigator.update();
Aga Wronska8788dad2016-01-15 17:30:15 -0800384 invalidateOptionsMenu();
385 }
386
Steve McKayfd8425a2016-02-23 14:34:50 -0800387 final void loadRoot(final Uri uri) {
388 new LoadRootTask(this, uri).executeOnExecutor(
389 ProviderExecutor.forAuthority(uri.getAuthority()));
390 }
391
Aga Wronska8788dad2016-01-15 17:30:15 -0800392 /**
393 * Called when search results changed.
394 * Refreshes the content of the directory. It doesn't refresh elements on the action bar.
395 * e.g. The current directory name displayed on the action bar won't get updated.
396 */
397 @Override
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800398 public void onSearchChanged(@Nullable String query) {
399 // We should not get here if root is not searchable
Steve McKay0af8afd2016-02-25 13:34:03 -0800400 assert(canSearchRoot());
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800401 reloadSearch(query);
Aga Wronska8e21daa2016-03-24 18:22:09 -0700402 }
403
404 @Override
405 public void onSearchFinished() {
406 // Restores menu icons state
Steve McKayf2eb8d92016-03-08 14:01:47 -0800407 invalidateOptionsMenu();
Aga Wronska8788dad2016-01-15 17:30:15 -0800408 }
409
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800410 private void reloadSearch(String query) {
411 FragmentManager fm = getFragmentManager();
412 RootInfo root = getCurrentRoot();
413 DocumentInfo cwd = getCurrentDirectory();
414
415 DirectoryFragment.reloadSearch(fm, root, cwd, query);
Steve McKayb68dd222015-04-20 17:18:15 -0700416 }
417
Ben Kwa0bcdec32015-05-29 15:40:31 -0700418 final List<String> getExcludedAuthorities() {
419 List<String> authorities = new ArrayList<>();
420 if (getIntent().getBooleanExtra(DocumentsContract.EXTRA_EXCLUDE_SELF, false)) {
421 // Exclude roots provided by the calling package.
422 String packageName = getCallingPackageMaybeExtra();
423 try {
424 PackageInfo pkgInfo = getPackageManager().getPackageInfo(packageName,
425 PackageManager.GET_PROVIDERS);
426 for (ProviderInfo provider: pkgInfo.providers) {
427 authorities.add(provider.authority);
428 }
429 } catch (PackageManager.NameNotFoundException e) {
430 Log.e(mTag, "Calling package name does not resolve: " + packageName);
431 }
432 }
433 return authorities;
434 }
435
Aga Wronska654e25c2016-01-29 11:41:41 -0800436 boolean canSearchRoot() {
437 final RootInfo root = getCurrentRoot();
438 return (root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0;
439 }
440
Steve McKayb68dd222015-04-20 17:18:15 -0700441 final String getCallingPackageMaybeExtra() {
Ben Kwa0bcdec32015-05-29 15:40:31 -0700442 String callingPackage = getCallingPackage();
443 // System apps can set the calling package name using an extra.
444 try {
445 ApplicationInfo info = getPackageManager().getApplicationInfo(callingPackage, 0);
446 if (info.isSystemApp() || info.isUpdatedSystemApp()) {
447 final String extra = getIntent().getStringExtra(DocumentsContract.EXTRA_PACKAGE_NAME);
448 if (extra != null) {
449 callingPackage = extra;
450 }
451 }
452 } finally {
453 return callingPackage;
454 }
Steve McKayb68dd222015-04-20 17:18:15 -0700455 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700456
457 public static BaseActivity get(Fragment fragment) {
458 return (BaseActivity) fragment.getActivity();
459 }
460
Ben Kwa0574b182015-09-08 07:31:19 -0700461 public State getDisplayState() {
462 return mState;
463 }
464
Aga Wronska64ae1f42016-03-22 14:18:43 -0700465 /*
466 * Get the default directory to be presented after starting the activity.
467 * Method can be overridden if the change of the behavior of the the child activity is needed.
468 */
469 public Uri getDefaultRoot() {
Steve McKay17f7e582016-04-04 15:26:48 -0700470 return Shared.shouldShowDocumentsRoot(this, getIntent())
471 ? DocumentsContract.buildHomeUri()
472 : DocumentsContract.buildRootUri(
473 "com.android.providers.downloads.documents", "downloads");
474 }
Aga Wronskaceb990e2016-03-30 18:07:59 -0700475
Aga Wronska94e53e42016-04-07 13:09:58 -0700476 /**
477 * Set internal storage visible based on explicit user action.
478 */
Aga Wronskaceb990e2016-03-30 18:07:59 -0700479 void setDisplayAdvancedDevices(boolean display) {
Aga Wronska94e53e42016-04-07 13:09:58 -0700480 Metrics.logUserAction(this,
481 display ? Metrics.USER_ACTION_SHOW_ADVANCED : Metrics.USER_ACTION_HIDE_ADVANCED);
482
Aga Wronskaceb990e2016-03-30 18:07:59 -0700483 mState.showAdvanced = display;
484 RootsFragment.get(getFragmentManager()).onDisplayStateChanged();
485 invalidateOptionsMenu();
Aga Wronska64ae1f42016-03-22 14:18:43 -0700486 }
487
Aga Wronska94e53e42016-04-07 13:09:58 -0700488 /**
489 * Set file size visible based on explicit user action.
490 */
Steve McKayb68dd222015-04-20 17:18:15 -0700491 void setDisplayFileSize(boolean display) {
Aga Wronska94e53e42016-04-07 13:09:58 -0700492 Metrics.logUserAction(this,
493 display ? Metrics.USER_ACTION_SHOW_SIZE : Metrics.USER_ACTION_HIDE_SIZE);
494
Steve McKayb68dd222015-04-20 17:18:15 -0700495 LocalPreferences.setDisplayFileSize(this, display);
Steve McKay4d0255f2015-09-25 16:02:56 -0700496 mState.showSize = display;
Steve McKayd4800812016-02-02 11:41:03 -0800497 DirectoryFragment dir = getDirectoryFragment();
498 if (dir != null) {
499 dir.onDisplayStateChanged();
500 }
Steve McKayb68dd222015-04-20 17:18:15 -0700501 invalidateOptionsMenu();
502 }
503
Steve McKayb68dd222015-04-20 17:18:15 -0700504 /**
505 * Set state sort order based on explicit user action.
506 */
507 void setUserSortOrder(int sortOrder) {
Aga Wronska94e53e42016-04-07 13:09:58 -0700508 switch(sortOrder) {
509 case State.SORT_ORDER_DISPLAY_NAME:
510 Metrics.logUserAction(this, Metrics.USER_ACTION_SORT_NAME);
511 break;
512 case State.SORT_ORDER_LAST_MODIFIED:
513 Metrics.logUserAction(this, Metrics.USER_ACTION_SORT_DATE);
514 break;
515 case State.SORT_ORDER_SIZE:
516 Metrics.logUserAction(this, Metrics.USER_ACTION_SORT_SIZE);
517 break;
518 }
519
Steve McKay4d0255f2015-09-25 16:02:56 -0700520 mState.userSortOrder = sortOrder;
Steve McKayd4800812016-02-02 11:41:03 -0800521 DirectoryFragment dir = getDirectoryFragment();
522 if (dir != null) {
523 dir.onSortOrderChanged();
Ben Kwa65f393a2016-02-17 10:48:57 -0800524 }
Steve McKayb68dd222015-04-20 17:18:15 -0700525 }
526
527 /**
Steve McKay7776aa52016-01-25 19:00:22 -0800528 * Set mode based on explicit user action.
Steve McKayb68dd222015-04-20 17:18:15 -0700529 */
Steve McKay7776aa52016-01-25 19:00:22 -0800530 void setViewMode(@ViewMode int mode) {
Aga Wronska94e53e42016-04-07 13:09:58 -0700531 if (mode == State.MODE_GRID) {
532 Metrics.logUserAction(this, Metrics.USER_ACTION_GRID);
533 } else if (mode == State.MODE_LIST) {
534 Metrics.logUserAction(this, Metrics.USER_ACTION_LIST);
535 }
536
Steve McKay30aabb82016-02-17 11:00:55 -0800537 LocalPreferences.setViewMode(this, getCurrentRoot(), mode);
Steve McKay7776aa52016-01-25 19:00:22 -0800538 mState.derivedMode = mode;
539
540 // view icon needs to be updated, but we *could* do it
541 // in onOptionsItemSelected, and not do the full invalidation
542 // But! That's a larger refactoring we'll save for another day.
543 invalidateOptionsMenu();
Steve McKayd4800812016-02-02 11:41:03 -0800544 DirectoryFragment dir = getDirectoryFragment();
545 if (dir != null) {
546 dir.onViewModeChanged();
Ben Kwa65f393a2016-02-17 10:48:57 -0800547 }
Steve McKayb68dd222015-04-20 17:18:15 -0700548 }
549
Aga Wronska3c237182016-01-20 16:32:33 -0800550 public void setPending(boolean pending) {
Steve McKayb68dd222015-04-20 17:18:15 -0700551 final SaveFragment save = SaveFragment.get(getFragmentManager());
552 if (save != null) {
553 save.setPending(pending);
554 }
555 }
556
557 @Override
558 protected void onSaveInstanceState(Bundle state) {
559 super.onSaveInstanceState(state);
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800560 state.putParcelable(Shared.EXTRA_STATE, mState);
561 mSearchManager.onSaveInstanceState(state);
Steve McKayb68dd222015-04-20 17:18:15 -0700562 }
563
564 @Override
565 protected void onRestoreInstanceState(Bundle state) {
566 super.onRestoreInstanceState(state);
567 }
568
Steve McKay18d01e82016-02-03 11:15:57 -0800569 @Override
570 public boolean isSearchExpanded() {
571 return mSearchManager.isExpanded();
572 }
573
574 @Override
Steve McKayf68210e2015-11-03 15:23:16 -0800575 public RootInfo getCurrentRoot() {
Steve McKay4d0255f2015-09-25 16:02:56 -0700576 if (mState.stack.root != null) {
577 return mState.stack.root;
Steve McKayb68dd222015-04-20 17:18:15 -0700578 } else {
579 return mRoots.getRecentsRoot();
580 }
581 }
582
583 public DocumentInfo getCurrentDirectory() {
Steve McKay4d0255f2015-09-25 16:02:56 -0700584 return mState.stack.peek();
Steve McKayb68dd222015-04-20 17:18:15 -0700585 }
586
Steve McKay459bc2b2015-09-16 15:07:31 -0700587 public Executor getExecutorForCurrentDirectory() {
Steve McKayb68dd222015-04-20 17:18:15 -0700588 final DocumentInfo cwd = getCurrentDirectory();
589 if (cwd != null && cwd.authority != null) {
590 return ProviderExecutor.forAuthority(cwd.authority);
591 } else {
592 return AsyncTask.THREAD_POOL_EXECUTOR;
593 }
594 }
595
Steve McKay0fbfc652015-08-20 16:48:49 -0700596 @Override
597 public void onBackPressed() {
598 // While action bar is expanded, the state stack UI is hidden.
599 if (mSearchManager.cancelSearch()) {
600 return;
601 }
602
Steve McKayd4800812016-02-02 11:41:03 -0800603 DirectoryFragment dir = getDirectoryFragment();
604 if (dir != null && dir.onBackPressed()) {
Steve McKay86c05762016-01-28 15:30:10 -0800605 return;
606 }
607
Daichi Hirono2806beb2016-01-07 15:29:12 +0900608 if (!mState.hasLocationChanged()) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700609 super.onBackPressed();
610 return;
611 }
612
Steve McKayfd8425a2016-02-23 14:34:50 -0800613 if (onBeforePopDir() || popDir()) {
Steve McKay95c79f52016-02-04 19:40:45 -0800614 return;
Steve McKay0fbfc652015-08-20 16:48:49 -0700615 }
Steve McKay95c79f52016-02-04 19:40:45 -0800616
617 super.onBackPressed();
Steve McKay0fbfc652015-08-20 16:48:49 -0700618 }
619
Steve McKayfd8425a2016-02-23 14:34:50 -0800620 boolean onBeforePopDir() {
621 // Files app overrides this with some fancy logic.
622 return false;
623 }
624
Steve McKayb68dd222015-04-20 17:18:15 -0700625 public void onStackPicked(DocumentStack stack) {
626 try {
627 // Update the restored stack to ensure we have freshest data
628 stack.updateDocuments(getContentResolver());
Daichi Hirono2806beb2016-01-07 15:29:12 +0900629 mState.setStack(stack);
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800630 refreshCurrentRootAndDirectory(AnimationView.ANIM_SIDE);
Steve McKayb68dd222015-04-20 17:18:15 -0700631
632 } catch (FileNotFoundException e) {
633 Log.w(mTag, "Failed to restore stack: " + e);
634 }
635 }
636
Ben Kwa74e5d412016-02-10 07:46:35 -0800637 /**
638 * Declare a global key handler to route key events when there isn't a specific focus view. This
639 * covers the scenario where a user opens DocumentsUI and just starts typing.
640 *
641 * @param keyCode
642 * @param event
643 * @return
644 */
645 @CallSuper
646 @Override
647 public boolean onKeyDown(int keyCode, KeyEvent event) {
648 if (Events.isNavigationKeyCode(keyCode)) {
649 // Forward all unclaimed navigation keystrokes to the DirectoryFragment. This causes any
650 // stray navigation keystrokes focus the content pane, which is probably what the user
651 // is trying to do.
652 DirectoryFragment df = DirectoryFragment.get(getFragmentManager());
653 if (df != null) {
654 df.requestFocus();
655 return true;
656 }
657 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
Ben Kwa65f393a2016-02-17 10:48:57 -0800658 // Tab toggles focus on the navigation drawer.
Ben Kwa74e5d412016-02-10 07:46:35 -0800659 toggleNavDrawerFocus();
660 return true;
Ben Kwa65f393a2016-02-17 10:48:57 -0800661 } else if (keyCode == KeyEvent.KEYCODE_DEL) {
662 popDir();
663 return true;
Ben Kwa74e5d412016-02-10 07:46:35 -0800664 }
665 return super.onKeyDown(keyCode, event);
666 }
667
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900668 public void addEventListener(EventListener listener) {
669 mEventListeners.add(listener);
670 }
671
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900672 public void removeEventListener(EventListener listener) {
673 mEventListeners.remove(listener);
674 }
675
676 public void notifyDirectoryLoaded(Uri uri) {
677 for (EventListener listener : mEventListeners) {
678 listener.onDirectoryLoaded(uri);
679 }
680 }
681
682 void notifyDirectoryNavigated(Uri uri) {
683 for (EventListener listener : mEventListeners) {
684 listener.onDirectoryNavigated(uri);
685 }
686 }
687
Ben Kwa74e5d412016-02-10 07:46:35 -0800688 /**
689 * Toggles focus between the navigation drawer and the directory listing. If the drawer isn't
690 * locked, open/close it as appropriate.
691 */
692 void toggleNavDrawerFocus() {
693 if (mNavDrawerHasFocus) {
694 mDrawer.setOpen(false);
695 DirectoryFragment df = DirectoryFragment.get(getFragmentManager());
696 if (df != null) {
697 df.requestFocus();
698 }
699 } else {
700 mDrawer.setOpen(true);
701 RootsFragment rf = RootsFragment.get(getFragmentManager());
702 if (rf != null) {
703 rf.requestFocus();
704 }
705 }
706 mNavDrawerHasFocus = !mNavDrawerHasFocus;
707 }
708
Tomasz Mikolajewski5a1e8792016-01-27 17:36:51 +0900709 DocumentInfo getRootDocumentBlocking(RootInfo root) {
Daichi Hirono2806beb2016-01-07 15:29:12 +0900710 try {
711 final Uri uri = DocumentsContract.buildDocumentUri(
712 root.authority, root.documentId);
713 return DocumentInfo.fromUri(getContentResolver(), uri);
714 } catch (FileNotFoundException e) {
715 Log.w(mTag, "Failed to find root", e);
716 return null;
717 }
718 }
719
Ben Kwa65f393a2016-02-17 10:48:57 -0800720 /**
721 * Pops the top entry off the directory stack, and returns the user to the previous directory.
722 * If the directory stack only contains one item, this method does nothing.
723 *
724 * @return Whether the stack was popped.
725 */
726 private boolean popDir() {
727 if (mState.stack.size() > 1) {
728 mState.stack.pop();
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800729 refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE);
Ben Kwa65f393a2016-02-17 10:48:57 -0800730 return true;
731 }
732 return false;
733 }
734
Daichi Hirono320a08f2016-03-25 19:04:39 +0900735 /**
736 * Closes the activity when it's idle.
737 */
738 private void addListenerForLaunchCompletion() {
739 addEventListener(new EventListener() {
740 @Override
741 public void onDirectoryNavigated(Uri uri) {
742 }
743
744 @Override
745 public void onDirectoryLoaded(Uri uri) {
746 removeEventListener(this);
747 getMainLooper().getQueue().addIdleHandler(new IdleHandler() {
748 @Override
749 public boolean queueIdle() {
750 // If startup benchmark is requested by a whitelisted testing package, then
751 // close the activity once idle, and notify the testing activity.
752 if (getIntent().getBooleanExtra(EXTRA_BENCHMARK, false) &&
753 BENCHMARK_TESTING_PACKAGE.equals(getCallingPackage())) {
754 setResult(RESULT_OK);
755 finish();
756 }
757
758 Metrics.logStartupMs(
759 BaseActivity.this, (int) (new Date().getTime() - mStartTime));
760
761 // Remove the idle handler.
762 return false;
763 }
764 });
765 new Handler().post(new Runnable() {
766 @Override public void run() {
767 }
768 });
769 }
770 });
771 }
772
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800773 private static final class PickRootTask extends PairedTask<BaseActivity, Void, DocumentInfo> {
Steve McKayb68dd222015-04-20 17:18:15 -0700774 private RootInfo mRoot;
775
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800776 public PickRootTask(BaseActivity activity, RootInfo root) {
777 super(activity);
Steve McKayb68dd222015-04-20 17:18:15 -0700778 mRoot = root;
779 }
780
781 @Override
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800782 protected DocumentInfo run(Void... params) {
783 return mOwner.getRootDocumentBlocking(mRoot);
Steve McKayb68dd222015-04-20 17:18:15 -0700784 }
785
786 @Override
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800787 protected void finish(DocumentInfo result) {
788 if (result != null) {
789 mOwner.openContainerDocument(result);
Steve McKayb68dd222015-04-20 17:18:15 -0700790 }
791 }
792 }
793
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800794 private static final class HandleRootsChangedTask
795 extends PairedTask<BaseActivity, RootInfo, RootInfo> {
Tomasz Mikolajewskic6a7faf2016-04-15 11:27:53 +0900796 DocumentInfo mDefaultRootDocument;
Daichi Hirono2806beb2016-01-07 15:29:12 +0900797
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800798 public HandleRootsChangedTask(BaseActivity activity) {
799 super(activity);
800 }
801
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900802 @Override
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800803 protected RootInfo run(RootInfo... roots) {
Steve McKay0af8afd2016-02-25 13:34:03 -0800804 assert(roots.length == 1);
805
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900806 final RootInfo currentRoot = roots[0];
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800807 final Collection<RootInfo> cachedRoots = mOwner.mRoots.getRootsBlocking();
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900808 for (final RootInfo root : cachedRoots) {
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900809 if (root.getUri().equals(currentRoot.getUri())) {
810 // We don't need to change the current root as the current root was not removed.
811 return null;
812 }
813 }
Tomasz Mikolajewskic6a7faf2016-04-15 11:27:53 +0900814
815 // Choose the default root.
816 final RootInfo defaultRoot = mOwner.mRoots.getDefaultRootBlocking(mOwner.mState);
817 assert(defaultRoot != null);
818 if (!defaultRoot.isRecents()) {
819 mDefaultRootDocument = mOwner.getRootDocumentBlocking(defaultRoot);
820 }
821 return defaultRoot;
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900822 }
823
824 @Override
Tomasz Mikolajewskic6a7faf2016-04-15 11:27:53 +0900825 protected void finish(RootInfo defaultRoot) {
826 if (defaultRoot == null) {
827 return;
828 }
829
830 // Clear entire backstack and start in new root.
831 mOwner.mState.onRootChanged(defaultRoot);
832 mOwner.mSearchManager.update(defaultRoot);
833
834 if (defaultRoot.isRecents()) {
835 mOwner.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
836 } else {
837 mOwner.openContainerDocument(mDefaultRootDocument);
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900838 }
839 }
840 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700841}