blob: 1480e2581716a26de21fd2f832576e591d430309 [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 McKayd9caa6a2016-09-15 16:36:45 -070019import static com.android.documentsui.base.Shared.DEBUG;
20import static com.android.documentsui.base.Shared.EXTRA_BENCHMARK;
Steve McKayd9caa6a2016-09-15 16:36:45 -070021import static com.android.documentsui.base.State.MODE_GRID;
Steve McKayb68dd222015-04-20 17:18:15 -070022
Steve McKayd0a2a2c2015-03-25 14:35:33 -070023import android.app.Activity;
24import android.app.Fragment;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080025import android.app.FragmentManager;
Steve McKayb68dd222015-04-20 17:18:15 -070026import android.content.Intent;
Ben Kwa0bcdec32015-05-29 15:40:31 -070027import android.content.pm.PackageInfo;
28import android.content.pm.PackageManager;
29import android.content.pm.ProviderInfo;
Steve McKayb68dd222015-04-20 17:18:15 -070030import android.net.Uri;
31import android.os.AsyncTask;
32import android.os.Bundle;
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +090033import android.os.MessageQueue.IdleHandler;
Steve McKayb68dd222015-04-20 17:18:15 -070034import android.provider.DocumentsContract;
35import android.provider.DocumentsContract.Root;
Steve McKayd4800812016-02-02 11:41:03 -080036import android.support.annotation.CallSuper;
Steve McKay0fbfc652015-08-20 16:48:49 -070037import android.support.annotation.LayoutRes;
Steve McKayfefcd702015-08-20 16:19:38 +000038import android.support.annotation.Nullable;
Steve McKayd9caa6a2016-09-15 16:36:45 -070039import android.support.annotation.VisibleForTesting;
Steve McKayb68dd222015-04-20 17:18:15 -070040import android.util.Log;
Steve McKayb68dd222015-04-20 17:18:15 -070041import android.view.Menu;
42import android.view.MenuItem;
Garfield, Tan171e6f52016-07-29 14:44:58 -070043import android.view.View;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070044
Steve McKayc8889af2016-09-23 11:22:41 -070045import com.android.documentsui.AbstractActionHandler.CommonAddons;
Steve McKay92ae43d2016-11-08 12:06:58 -080046import com.android.documentsui.Injector.Injected;
Ben Linb8c54e72016-06-10 12:13:27 -070047import com.android.documentsui.NavigationViewManager.Breadcrumb;
Steve McKayd0805062016-09-15 14:30:38 -070048import com.android.documentsui.base.DocumentInfo;
Steve McKayd0805062016-09-15 14:30:38 -070049import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070050import com.android.documentsui.base.Shared;
51import com.android.documentsui.base.State;
52import com.android.documentsui.base.State.ViewMode;
Steve McKayfb4fd2f2016-03-11 10:49:32 -080053import com.android.documentsui.dirlist.AnimationView;
Steve McKayf68210e2015-11-03 15:23:16 -080054import com.android.documentsui.dirlist.DirectoryFragment;
Steve McKaye8b547f2016-12-19 14:42:55 -080055import com.android.documentsui.prefs.LocalPreferences;
Chen Su7b6831b2016-12-22 16:21:32 +000056import com.android.documentsui.prefs.PreferencesMonitor;
Steve McKay6a0a5682016-11-30 12:09:43 -080057import com.android.documentsui.queries.DebugCommandProcessor;
Steve McKay3a268232016-10-19 11:15:47 -070058import com.android.documentsui.queries.SearchViewManager;
59import com.android.documentsui.queries.SearchViewManager.SearchManagerListener;
Steve McKaydef48682016-10-03 09:07:38 -070060import com.android.documentsui.roots.GetRootDocumentTask;
Steve McKayd9caa6a2016-09-15 16:36:45 -070061import com.android.documentsui.roots.RootsCache;
Steve McKay5b0a2c12016-10-07 11:22:31 -070062import com.android.documentsui.selection.Selection;
Steve McKayd0805062016-09-15 14:30:38 -070063import com.android.documentsui.sidebar.RootsFragment;
Garfield, Tan171e6f52016-07-29 14:44:58 -070064import com.android.documentsui.sorting.SortController;
65import com.android.documentsui.sorting.SortModel;
Steve McKay64ac2512015-05-12 12:49:58 -070066
Steve McKay64ac2512015-05-12 12:49:58 -070067import java.util.ArrayList;
Daichi Hirono320a08f2016-03-25 19:04:39 +090068import java.util.Date;
Steve McKay64ac2512015-05-12 12:49:58 -070069import java.util.List;
70import java.util.concurrent.Executor;
71
Steve McKay92ae43d2016-11-08 12:06:58 -080072public abstract class BaseActivity
73 extends Activity implements CommonAddons, NavigationViewManager.Environment {
Garfield, Tana5588b62016-07-13 09:23:04 -070074
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +090075 private static final String BENCHMARK_TESTING_PACKAGE = "com.android.documentsui.appperftests";
76
Steve McKay16e0c1f2016-09-15 12:41:13 -070077 protected SearchViewManager mSearchManager;
Steve McKayd9caa6a2016-09-15 16:36:45 -070078 protected State mState;
Steve McKay16e0c1f2016-09-15 12:41:13 -070079
Steve McKay92ae43d2016-11-08 12:06:58 -080080 @Injected
81 protected Injector<?> mInjector;
82
Steve McKay16e0c1f2016-09-15 12:41:13 -070083 protected @Nullable RetainedState mRetainedState;
84 protected RootsCache mRoots;
Garfield Tan63bf8132016-10-11 11:00:49 -070085 protected DocumentsAccess mDocs;
Steve McKay16e0c1f2016-09-15 12:41:13 -070086 protected DrawerController mDrawer;
Steve McKay92ae43d2016-11-08 12:06:58 -080087
Steve McKay16e0c1f2016-09-15 12:41:13 -070088 protected NavigationViewManager mNavigator;
Steve McKay16e0c1f2016-09-15 12:41:13 -070089 protected SortController mSortController;
Steve McKay0269fb62015-04-22 15:55:34 -070090
Steve McKay5b0a2c12016-10-07 11:22:31 -070091 private final List<EventListener> mEventListeners = new ArrayList<>();
Steve McKayf2c8b0d2015-09-23 15:44:24 -070092 private final String mTag;
Steve McKay95c79f52016-02-04 19:40:45 -080093
Steve McKay0fbfc652015-08-20 16:48:49 -070094 @LayoutRes
95 private int mLayoutId;
Steve McKayb68dd222015-04-20 17:18:15 -070096
Steve McKay92ae43d2016-11-08 12:06:58 -080097 private RootsMonitor<BaseActivity> mRootsMonitor;
Garfield Tan63bf8132016-10-11 11:00:49 -070098
Daichi Hirono320a08f2016-03-25 19:04:39 +090099 private long mStartTime;
Ben Kwa74e5d412016-02-10 07:46:35 -0800100
Chen Su7b6831b2016-12-22 16:21:32 +0000101 private PreferencesMonitor mPreferencesMonitor;
102
Steve McKay6525a192016-10-18 14:28:00 -0700103 public BaseActivity(@LayoutRes int layoutId, String tag) {
104 mLayoutId = layoutId;
105 mTag = tag;
106 }
107
Steve McKayc8889af2016-09-23 11:22:41 -0700108 protected abstract void onTaskFinished(Uri... uris);
109 protected abstract void refreshDirectory(int anim);
110 /** Allows sub-classes to include information in a newly created State instance. */
111 protected abstract void includeState(State initialState);
112 protected abstract void onDirectoryCreated(DocumentInfo doc);
113
Steve McKay92ae43d2016-11-08 12:06:58 -0800114 public abstract Injector<?> getInjector();
Garfield Tan7d75f7b2016-09-20 16:33:24 -0700115
Steve McKay18d01e82016-02-03 11:15:57 -0800116 @CallSuper
Steve McKayb68dd222015-04-20 17:18:15 -0700117 @Override
118 public void onCreate(Bundle icicle) {
Daichi Hirono320a08f2016-03-25 19:04:39 +0900119 // Record the time when onCreate is invoked for metric.
120 mStartTime = new Date().getTime();
121
Steve McKayb68dd222015-04-20 17:18:15 -0700122 super.onCreate(icicle);
Steve McKay0fbfc652015-08-20 16:48:49 -0700123
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +0900124 final Intent intent = getIntent();
125
Daichi Hirono320a08f2016-03-25 19:04:39 +0900126 addListenerForLaunchCompletion();
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +0900127
Steve McKay18d01e82016-02-03 11:15:57 -0800128 setContentView(mLayoutId);
129
Steve McKay92ae43d2016-11-08 12:06:58 -0800130 mInjector = getInjector();
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800131 mState = getState(icicle);
Steve McKay92ae43d2016-11-08 12:06:58 -0800132 mDrawer = DrawerController.create(this, mInjector.config);
Tomasz Mikolajewskib8373c22016-03-15 17:41:31 +0900133 Metrics.logActivityLaunch(this, mState, intent);
Ben Kwa1c0a3892016-01-26 11:50:03 -0800134
Steve McKay2bab2f82016-06-03 09:23:39 -0700135 // we're really interested in retainining state in our very complex
136 // DirectoryFragment. So we do a little code yoga to extend
137 // support to that fragment.
138 mRetainedState = (RetainedState) getLastNonConfigurationInstance();
Steve McKayb68dd222015-04-20 17:18:15 -0700139 mRoots = DocumentsApplication.getRootsCache(this);
Garfield Tan63bf8132016-10-11 11:00:49 -0700140 mDocs = DocumentsAccess.create(this);
Steve McKay18d01e82016-02-03 11:15:57 -0800141
Steve McKay18d01e82016-02-03 11:15:57 -0800142 DocumentsToolbar toolbar = (DocumentsToolbar) findViewById(R.id.toolbar);
143 setActionBar(toolbar);
Steve McKay85ec0d62016-06-24 15:05:08 -0700144
145 Breadcrumb breadcrumb =
146 Shared.findView(this, R.id.dropdown_breadcrumb, R.id.horizontal_breadcrumb);
147 assert(breadcrumb != null);
148
Ben Linb8c54e72016-06-10 12:13:27 -0700149 mNavigator = new NavigationViewManager(mDrawer, toolbar, mState, this, breadcrumb);
Ben Lin21c54442016-09-21 14:20:12 -0700150 SearchManagerListener searchListener = new SearchManagerListener() {
151 /**
152 * Called when search results changed. Refreshes the content of the directory. It
153 * doesn't refresh elements on the action bar. e.g. The current directory name displayed
154 * on the action bar won't get updated.
155 */
156 @Override
157 public void onSearchChanged(@Nullable String query) {
Ben Lin21c54442016-09-21 14:20:12 -0700158 reloadSearch(query);
159 }
Steve McKay18d01e82016-02-03 11:15:57 -0800160
Ben Lin21c54442016-09-21 14:20:12 -0700161 @Override
162 public void onSearchFinished() {
163 // Restores menu icons state
164 invalidateOptionsMenu();
165 }
166
167 @Override
168 public void onSearchViewChanged(boolean opened) {
Ben Lin21c54442016-09-21 14:20:12 -0700169 mNavigator.update();
170 }
171 };
Steve McKay6a0a5682016-11-30 12:09:43 -0800172
173 // "Commands" are meta input for controlling system behavior.
174 // We piggy back on search input as it is the only text input
175 // area in the app. But the functionality is independent
176 // of "regular" search query processing.
177 DebugCommandProcessor dbgCommands = new DebugCommandProcessor();
178 dbgCommands.add(new DebugCommandProcessor.DumpRootsCacheHandler(this));
179 mSearchManager = new SearchViewManager(searchListener, dbgCommands, icicle);
Steve McKay9de0da62016-08-25 15:18:23 -0700180 mSortController = SortController.create(this, mState.derivedMode, mState.sortModel);
181
Chen Su7b6831b2016-12-22 16:21:32 +0000182 mPreferencesMonitor = new PreferencesMonitor(getApplicationContext());
183
Steve McKay0fbfc652015-08-20 16:48:49 -0700184 // Base classes must update result in their onCreate.
185 setResult(Activity.RESULT_CANCELED);
Steve McKay0269fb62015-04-22 15:55:34 -0700186 }
187
188 @Override
Garfield Tan63bf8132016-10-11 11:00:49 -0700189 protected void onPostCreate(Bundle savedInstanceState) {
190 super.onPostCreate(savedInstanceState);
191
192 mRootsMonitor = new RootsMonitor<>(
193 this,
Steve McKay92ae43d2016-11-08 12:06:58 -0800194 mInjector.actions,
Garfield Tan63bf8132016-10-11 11:00:49 -0700195 mRoots,
196 mDocs,
197 mState,
198 mSearchManager);
199 mRootsMonitor.start();
200 }
201
202 @Override
Steve McKay0269fb62015-04-22 15:55:34 -0700203 public boolean onCreateOptionsMenu(Menu menu) {
204 boolean showMenu = super.onCreateOptionsMenu(menu);
205
206 getMenuInflater().inflate(R.menu.activity, menu);
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800207 mNavigator.update();
Aga Wronska8e21daa2016-03-24 18:22:09 -0700208 boolean fullBarSearch = getResources().getBoolean(R.bool.full_bar_search_view);
209 mSearchManager.install((DocumentsToolbar) findViewById(R.id.toolbar), fullBarSearch);
Steve McKay0269fb62015-04-22 15:55:34 -0700210
211 return showMenu;
Steve McKayb68dd222015-04-20 17:18:15 -0700212 }
213
Steve McKay7bd32e12015-04-30 16:12:59 -0700214 @Override
Chen Su7b6831b2016-12-22 16:21:32 +0000215 protected void onResume() {
216 super.onResume();
217 mPreferencesMonitor.start();
218 }
219
220 @Override
221 protected void onPause() {
222 super.onPause();
223 mPreferencesMonitor.stop();
224 }
225
226 @Override
Steve McKayd4800812016-02-02 11:41:03 -0800227 @CallSuper
Steve McKay7bd32e12015-04-30 16:12:59 -0700228 public boolean onPrepareOptionsMenu(Menu menu) {
Steve McKaye9809272015-10-01 11:39:24 -0700229 super.onPrepareOptionsMenu(menu);
Tomasz Mikolajewski9e047852017-02-13 14:05:17 +0900230 mSearchManager.showMenu(mState.stack);
Steve McKaye9809272015-10-01 11:39:24 -0700231 return true;
Steve McKay7bd32e12015-04-30 16:12:59 -0700232 }
233
Daichi Hironoe28c3c82016-01-13 13:19:02 +0900234 @Override
235 protected void onDestroy() {
Garfield Tan63bf8132016-10-11 11:00:49 -0700236 mRootsMonitor.stop();
Daichi Hironoe28c3c82016-01-13 13:19:02 +0900237 super.onDestroy();
238 }
239
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800240 private State getState(@Nullable Bundle icicle) {
241 if (icicle != null) {
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800242 State state = icicle.<State>getParcelable(Shared.EXTRA_STATE);
Steve McKayc7dc0cf2016-02-04 12:15:22 -0800243 if (DEBUG) Log.d(mTag, "Recovered existing state object: " + state);
244 return state;
245 }
246
Ben Kwa0574b182015-09-08 07:31:19 -0700247 State state = new State();
248
249 final Intent intent = getIntent();
Ben Kwa0574b182015-09-08 07:31:19 -0700250
Garfield, Tan171e6f52016-07-29 14:44:58 -0700251 state.sortModel = SortModel.createModel();
Ben Kwa0574b182015-09-08 07:31:19 -0700252 state.localOnly = intent.getBooleanExtra(Intent.EXTRA_LOCAL_ONLY, false);
Steve McKay459bc2b2015-09-16 15:07:31 -0700253 state.initAcceptMimes(intent);
Ben Kwa0574b182015-09-08 07:31:19 -0700254 state.excludedAuthorities = getExcludedAuthorities();
255
Aga Wronskaceb990e2016-03-30 18:07:59 -0700256 includeState(state);
257
Steve McKay92ae43d2016-11-08 12:06:58 -0800258 state.showAdvanced = Shared.mustShowDeviceRoot(intent)
259 || mInjector.prefs.getShowDeviceRoot();
Steve McKay91226ee2016-06-20 11:46:56 -0700260
Steve McKay6525a192016-10-18 14:28:00 -0700261 // Only show the toggle if advanced isn't forced enabled.
Steve McKay01996bc2016-10-21 13:38:49 -0700262 state.showDeviceStorageOption = !Shared.mustShowDeviceRoot(intent);
Aga Wronskaceb990e2016-03-30 18:07:59 -0700263
264 if (DEBUG) Log.d(mTag, "Created new state object: " + state);
265
Ben Kwa0574b182015-09-08 07:31:19 -0700266 return state;
267 }
268
Garfield Tan208945c2016-10-04 14:36:38 -0700269 @Override
Steve McKaycb9eb422016-02-09 16:17:24 -0800270 public void setRootsDrawerOpen(boolean open) {
271 mNavigator.revealRootsDrawer(open);
272 }
Steve McKayb68dd222015-04-20 17:18:15 -0700273
Steve McKayc8889af2016-09-23 11:22:41 -0700274 @Override
Steve McKayd0805062016-09-15 14:30:38 -0700275 public void onRootPicked(RootInfo root) {
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800276 // Clicking on the current root removes search
277 mSearchManager.cancelSearch();
278
Aga Wronskafa421722016-02-08 12:00:38 -0800279 // Skip refreshing if root nor directory didn't change
280 if (root.equals(getCurrentRoot()) && mState.stack.size() == 1) {
Aga Wronskafd26e8d2016-02-02 18:01:58 -0800281 return;
282 }
283
Ben Lin3b7a99d2017-01-27 15:01:57 -0800284 mInjector.actionModeController.finishActionMode();
Steve McKay7776aa52016-01-25 19:00:22 -0800285 mState.derivedMode = LocalPreferences.getViewMode(this, root, MODE_GRID);
Steve McKay743bc1b2016-08-26 16:10:39 -0700286 mSortController.onViewModeChanged(mState.derivedMode);
Steve McKay7776aa52016-01-25 19:00:22 -0800287
Garfield, Tan171e6f52016-07-29 14:44:58 -0700288 // Set summary header's visibility. Only recents and downloads root may have summary in
289 // their docs.
290 mState.sortModel.setDimensionVisibility(
291 SortModel.SORT_DIMENSION_ID_SUMMARY,
292 root.isRecents() || root.isDownloads() ? View.VISIBLE : View.INVISIBLE);
293
Steve McKayb68dd222015-04-20 17:18:15 -0700294 // Clear entire backstack and start in new root
Garfield Tan2a837422016-10-19 11:50:45 -0700295 mState.stack.changeRoot(root);
Steve McKay0269fb62015-04-22 15:55:34 -0700296
297 // Recents is always in memory, so we just load it directly.
298 // Otherwise we delegate loading data from disk to a task
299 // to ensure a responsive ui.
300 if (mRoots.isRecentsRoot(root)) {
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800301 refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
Steve McKay0269fb62015-04-22 15:55:34 -0700302 } else {
Garfield Tan7d75f7b2016-09-20 16:33:24 -0700303 new GetRootDocumentTask(
304 root,
305 this,
Steve McKay92ae43d2016-11-08 12:06:58 -0800306 mInjector.actions::openContainerDocument)
Garfield Tan7d75f7b2016-09-20 16:33:24 -0700307 .executeOnExecutor(getExecutorForCurrentDirectory());
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900308 }
309 }
310
Steve McKayb68dd222015-04-20 17:18:15 -0700311 @Override
312 public boolean onOptionsItemSelected(MenuItem item) {
Steve McKayb68dd222015-04-20 17:18:15 -0700313
Steve McKay6035b3c2015-12-04 11:19:09 -0800314 switch (item.getItemId()) {
315 case android.R.id.home:
316 onBackPressed();
317 return true;
318
319 case R.id.menu_create_dir:
320 showCreateDirectoryDialog();
321 return true;
322
323 case R.id.menu_search:
Steve McKayf2eb8d92016-03-08 14:01:47 -0800324 // SearchViewManager listens for this directly.
Steve McKay6035b3c2015-12-04 11:19:09 -0800325 return false;
326
Steve McKay6035b3c2015-12-04 11:19:09 -0800327 case R.id.menu_grid:
Steve McKay7776aa52016-01-25 19:00:22 -0800328 setViewMode(State.MODE_GRID);
Steve McKay6035b3c2015-12-04 11:19:09 -0800329 return true;
330
331 case R.id.menu_list:
Steve McKay7776aa52016-01-25 19:00:22 -0800332 setViewMode(State.MODE_LIST);
Steve McKay6035b3c2015-12-04 11:19:09 -0800333 return true;
334
Aga Wronskaceb990e2016-03-30 18:07:59 -0700335 case R.id.menu_advanced:
336 setDisplayAdvancedDevices(!mState.showAdvanced);
337 return true;
338
Jon Mann49be24c2017-02-13 17:29:20 -0800339 case R.id.menu_select_all:
340 DirectoryFragment dir = getDirectoryFragment();
341 if (dir != null) {
342 dir.selectAllFiles();
343 }
344 return true;
345
Steve McKay6035b3c2015-12-04 11:19:09 -0800346 default:
347 return super.onOptionsItemSelected(item);
348 }
Steve McKayb68dd222015-04-20 17:18:15 -0700349 }
350
Steve McKay16e0c1f2016-09-15 12:41:13 -0700351 protected final @Nullable DirectoryFragment getDirectoryFragment() {
Steve McKayd4800812016-02-02 11:41:03 -0800352 return DirectoryFragment.get(getFragmentManager());
353 }
354
Steve McKay16e0c1f2016-09-15 12:41:13 -0700355 protected void showCreateDirectoryDialog() {
Aga Wronska94e53e42016-04-07 13:09:58 -0700356 Metrics.logUserAction(this, Metrics.USER_ACTION_CREATE_DIR);
357
Steve McKayceeb3f72015-05-19 16:10:25 -0700358 CreateDirectoryFragment.show(getFragmentManager());
359 }
360
361 /**
362 * Returns true if a directory can be created in the current location.
363 * @return
364 */
Steve McKay16e0c1f2016-09-15 12:41:13 -0700365 protected boolean canCreateDirectory() {
Steve McKayceeb3f72015-05-19 16:10:25 -0700366 final RootInfo root = getCurrentRoot();
367 final DocumentInfo cwd = getCurrentDirectory();
368 return cwd != null
369 && cwd.isCreateSupported()
370 && !mSearchManager.isSearching()
Ben Lind04aec72017-02-24 16:48:25 -0800371 && !root.isRecents();
Steve McKayceeb3f72015-05-19 16:10:25 -0700372 }
373
Garfield Tan16868832016-09-26 10:01:45 -0700374 // TODO: make navigator listen to state
375 @Override
376 public final void updateNavigator() {
377 mNavigator.update();
378 }
379
Steve McKayb68dd222015-04-20 17:18:15 -0700380 /**
Aga Wronska8788dad2016-01-15 17:30:15 -0800381 * Refreshes the content of the director and the menu/action bar.
382 * The current directory name and selection will get updated.
Steve McKayb68dd222015-04-20 17:18:15 -0700383 * @param anim
384 */
Steve McKay18d01e82016-02-03 11:15:57 -0800385 @Override
386 public final void refreshCurrentRootAndDirectory(int anim) {
Garfield Tan40c85052017-02-02 12:44:35 -0800387 // The following call will crash if it's called before onCreateOptionMenu() is called in
388 // which we install menu item to search view manager, and there is a search query we need to
389 // restore. This happens when we're still initializing our UI so we shouldn't cancel the
390 // search which will be restored later in onCreateOptionMenu(). Try finding a way to guard
391 // refreshCurrentRootAndDirectory() from being called while we're restoring the state of UI
392 // from the saved state passed in onCreate().
Aga Wronska8788dad2016-01-15 17:30:15 -0800393 mSearchManager.cancelSearch();
394
Aga Wronska8788dad2016-01-15 17:30:15 -0800395 refreshDirectory(anim);
Steve McKayb68dd222015-04-20 17:18:15 -0700396
397 final RootsFragment roots = RootsFragment.get(getFragmentManager());
398 if (roots != null) {
399 roots.onCurrentRootChanged();
400 }
401
Steve McKay18d01e82016-02-03 11:15:57 -0800402 mNavigator.update();
Ben Lin37408d02016-10-21 11:30:02 -0700403 // Causes talkback to announce the activity's new title
404 if (mState.stack.isRecents()) {
405 setTitle(mRoots.getRecentsRoot().title);
406 } else {
407 setTitle(mState.stack.getTitle());
408 }
Aga Wronska8788dad2016-01-15 17:30:15 -0800409 invalidateOptionsMenu();
410 }
411
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800412 private void reloadSearch(String query) {
413 FragmentManager fm = getFragmentManager();
414 RootInfo root = getCurrentRoot();
415 DocumentInfo cwd = getCurrentDirectory();
416
417 DirectoryFragment.reloadSearch(fm, root, cwd, query);
Steve McKayb68dd222015-04-20 17:18:15 -0700418 }
419
Steve McKay16e0c1f2016-09-15 12:41:13 -0700420 private final List<String> getExcludedAuthorities() {
Ben Kwa0bcdec32015-05-29 15:40:31 -0700421 List<String> authorities = new ArrayList<>();
422 if (getIntent().getBooleanExtra(DocumentsContract.EXTRA_EXCLUDE_SELF, false)) {
423 // Exclude roots provided by the calling package.
Steve McKay988d8a32016-09-27 09:41:17 -0700424 String packageName = Shared.getCallingPackageName(this);
Ben Kwa0bcdec32015-05-29 15:40:31 -0700425 try {
426 PackageInfo pkgInfo = getPackageManager().getPackageInfo(packageName,
427 PackageManager.GET_PROVIDERS);
428 for (ProviderInfo provider: pkgInfo.providers) {
429 authorities.add(provider.authority);
430 }
431 } catch (PackageManager.NameNotFoundException e) {
432 Log.e(mTag, "Calling package name does not resolve: " + packageName);
433 }
434 }
435 return authorities;
436 }
437
Steve McKay92ae43d2016-11-08 12:06:58 -0800438 public static BaseActivity get(Fragment fragment) {
439 return (BaseActivity) fragment.getActivity();
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700440 }
441
Ben Kwa0574b182015-09-08 07:31:19 -0700442 public State getDisplayState() {
443 return mState;
444 }
445
Ben Lin1c456292016-10-07 16:43:18 -0700446 public DragShadowBuilder getShadowBuilder() {
447 throw new UnsupportedOperationException(
448 "Drag and drop not supported, can't get shadow builder");
449 }
450
Aga Wronska94e53e42016-04-07 13:09:58 -0700451 /**
452 * Set internal storage visible based on explicit user action.
453 */
Aga Wronskaceb990e2016-03-30 18:07:59 -0700454 void setDisplayAdvancedDevices(boolean display) {
Aga Wronska94e53e42016-04-07 13:09:58 -0700455 Metrics.logUserAction(this,
456 display ? Metrics.USER_ACTION_SHOW_ADVANCED : Metrics.USER_ACTION_HIDE_ADVANCED);
457
Steve McKay92ae43d2016-11-08 12:06:58 -0800458 mInjector.prefs.setShowDeviceRoot(display);
Aga Wronskaceb990e2016-03-30 18:07:59 -0700459 mState.showAdvanced = display;
460 RootsFragment.get(getFragmentManager()).onDisplayStateChanged();
461 invalidateOptionsMenu();
Aga Wronska64ae1f42016-03-22 14:18:43 -0700462 }
463
Aga Wronska94e53e42016-04-07 13:09:58 -0700464 /**
Steve McKay7776aa52016-01-25 19:00:22 -0800465 * Set mode based on explicit user action.
Steve McKayb68dd222015-04-20 17:18:15 -0700466 */
Steve McKay7776aa52016-01-25 19:00:22 -0800467 void setViewMode(@ViewMode int mode) {
Aga Wronska94e53e42016-04-07 13:09:58 -0700468 if (mode == State.MODE_GRID) {
469 Metrics.logUserAction(this, Metrics.USER_ACTION_GRID);
470 } else if (mode == State.MODE_LIST) {
471 Metrics.logUserAction(this, Metrics.USER_ACTION_LIST);
472 }
473
Steve McKay30aabb82016-02-17 11:00:55 -0800474 LocalPreferences.setViewMode(this, getCurrentRoot(), mode);
Steve McKay7776aa52016-01-25 19:00:22 -0800475 mState.derivedMode = mode;
476
477 // view icon needs to be updated, but we *could* do it
478 // in onOptionsItemSelected, and not do the full invalidation
479 // But! That's a larger refactoring we'll save for another day.
480 invalidateOptionsMenu();
Steve McKayd4800812016-02-02 11:41:03 -0800481 DirectoryFragment dir = getDirectoryFragment();
482 if (dir != null) {
483 dir.onViewModeChanged();
Ben Kwa65f393a2016-02-17 10:48:57 -0800484 }
Garfield, Tan171e6f52016-07-29 14:44:58 -0700485
486 mSortController.onViewModeChanged(mode);
Steve McKayb68dd222015-04-20 17:18:15 -0700487 }
488
Aga Wronska3c237182016-01-20 16:32:33 -0800489 public void setPending(boolean pending) {
Steve McKay16e0c1f2016-09-15 12:41:13 -0700490 // TODO: Isolate this behavior to PickActivity.
Steve McKayb68dd222015-04-20 17:18:15 -0700491 }
492
493 @Override
494 protected void onSaveInstanceState(Bundle state) {
495 super.onSaveInstanceState(state);
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800496 state.putParcelable(Shared.EXTRA_STATE, mState);
497 mSearchManager.onSaveInstanceState(state);
Steve McKayb68dd222015-04-20 17:18:15 -0700498 }
499
500 @Override
501 protected void onRestoreInstanceState(Bundle state) {
502 super.onRestoreInstanceState(state);
503 }
504
Steve McKay2bab2f82016-06-03 09:23:39 -0700505 /**
506 * Delegate ths call to the current fragment so it can save selection.
507 * Feel free to expand on this with other useful state.
508 */
509 @Override
510 public RetainedState onRetainNonConfigurationInstance() {
511 RetainedState retained = new RetainedState();
512 DirectoryFragment fragment = DirectoryFragment.get(getFragmentManager());
513 if (fragment != null) {
514 fragment.retainState(retained);
515 }
516 return retained;
517 }
518
519 public @Nullable RetainedState getRetainedState() {
520 return mRetainedState;
521 }
522
Steve McKay18d01e82016-02-03 11:15:57 -0800523 @Override
524 public boolean isSearchExpanded() {
525 return mSearchManager.isExpanded();
526 }
527
528 @Override
Steve McKayf68210e2015-11-03 15:23:16 -0800529 public RootInfo getCurrentRoot() {
Garfield Tan2a837422016-10-19 11:50:45 -0700530 RootInfo root = mState.stack.getRoot();
531 if (root != null) {
532 return root;
Steve McKayb68dd222015-04-20 17:18:15 -0700533 } else {
534 return mRoots.getRecentsRoot();
535 }
536 }
537
Steve McKayeed2f4e2016-10-03 20:30:52 -0700538 @Override
Steve McKayb68dd222015-04-20 17:18:15 -0700539 public DocumentInfo getCurrentDirectory() {
Steve McKay4d0255f2015-09-25 16:02:56 -0700540 return mState.stack.peek();
Steve McKayb68dd222015-04-20 17:18:15 -0700541 }
542
Steve McKay459bc2b2015-09-16 15:07:31 -0700543 public Executor getExecutorForCurrentDirectory() {
Steve McKayb68dd222015-04-20 17:18:15 -0700544 final DocumentInfo cwd = getCurrentDirectory();
545 if (cwd != null && cwd.authority != null) {
546 return ProviderExecutor.forAuthority(cwd.authority);
547 } else {
548 return AsyncTask.THREAD_POOL_EXECUTOR;
549 }
550 }
551
Steve McKay0fbfc652015-08-20 16:48:49 -0700552 @Override
553 public void onBackPressed() {
554 // While action bar is expanded, the state stack UI is hidden.
555 if (mSearchManager.cancelSearch()) {
556 return;
557 }
558
Steve McKayd4800812016-02-02 11:41:03 -0800559 DirectoryFragment dir = getDirectoryFragment();
560 if (dir != null && dir.onBackPressed()) {
Steve McKay86c05762016-01-28 15:30:10 -0800561 return;
562 }
563
Ben Linec243362016-09-21 16:58:58 -0700564 if (popDir()) {
Steve McKay95c79f52016-02-04 19:40:45 -0800565 return;
Steve McKay0fbfc652015-08-20 16:48:49 -0700566 }
Steve McKay95c79f52016-02-04 19:40:45 -0800567
568 super.onBackPressed();
Steve McKay0fbfc652015-08-20 16:48:49 -0700569 }
570
Garfield Tan63bf8132016-10-11 11:00:49 -0700571 @VisibleForTesting
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900572 public void addEventListener(EventListener listener) {
573 mEventListeners.add(listener);
574 }
575
Garfield Tan63bf8132016-10-11 11:00:49 -0700576 @VisibleForTesting
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900577 public void removeEventListener(EventListener listener) {
578 mEventListeners.remove(listener);
579 }
580
Garfield Tan63bf8132016-10-11 11:00:49 -0700581 @VisibleForTesting
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900582 public void notifyDirectoryLoaded(Uri uri) {
583 for (EventListener listener : mEventListeners) {
584 listener.onDirectoryLoaded(uri);
585 }
586 }
587
Garfield Tan63bf8132016-10-11 11:00:49 -0700588 @VisibleForTesting
589 @Override
590 public void notifyDirectoryNavigated(Uri uri) {
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900591 for (EventListener listener : mEventListeners) {
592 listener.onDirectoryNavigated(uri);
593 }
594 }
595
Ben Kwa74e5d412016-02-10 07:46:35 -0800596 /**
Ben Kwa65f393a2016-02-17 10:48:57 -0800597 * Pops the top entry off the directory stack, and returns the user to the previous directory.
598 * If the directory stack only contains one item, this method does nothing.
599 *
600 * @return Whether the stack was popped.
601 */
Steve McKay04718262016-11-08 11:01:35 -0800602 protected boolean popDir() {
Ben Kwa65f393a2016-02-17 10:48:57 -0800603 if (mState.stack.size() > 1) {
604 mState.stack.pop();
Steve McKayfb4fd2f2016-03-11 10:49:32 -0800605 refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE);
Ben Kwa65f393a2016-02-17 10:48:57 -0800606 return true;
607 }
608 return false;
609 }
610
Steve McKay92ae43d2016-11-08 12:06:58 -0800611 protected boolean focusSidebar() {
Steve McKay04718262016-11-08 11:01:35 -0800612 RootsFragment rf = RootsFragment.get(getFragmentManager());
613 assert (rf != null);
614 return rf.requestFocus();
615 }
616
Daichi Hirono320a08f2016-03-25 19:04:39 +0900617 /**
618 * Closes the activity when it's idle.
619 */
620 private void addListenerForLaunchCompletion() {
621 addEventListener(new EventListener() {
622 @Override
623 public void onDirectoryNavigated(Uri uri) {
624 }
625
626 @Override
627 public void onDirectoryLoaded(Uri uri) {
628 removeEventListener(this);
629 getMainLooper().getQueue().addIdleHandler(new IdleHandler() {
630 @Override
631 public boolean queueIdle() {
632 // If startup benchmark is requested by a whitelisted testing package, then
633 // close the activity once idle, and notify the testing activity.
634 if (getIntent().getBooleanExtra(EXTRA_BENCHMARK, false) &&
635 BENCHMARK_TESTING_PACKAGE.equals(getCallingPackage())) {
636 setResult(RESULT_OK);
637 finish();
638 }
639
640 Metrics.logStartupMs(
641 BaseActivity.this, (int) (new Date().getTime() - mStartTime));
642
643 // Remove the idle handler.
644 return false;
645 }
646 });
Daichi Hirono320a08f2016-03-25 19:04:39 +0900647 }
648 });
649 }
650
Steve McKayb03a59c2016-10-19 15:59:53 -0700651 public static final class RetainedState {
Steve McKayd9caa6a2016-09-15 16:36:45 -0700652 public @Nullable Selection selection;
653
654 public boolean hasSelection() {
655 return selection != null;
656 }
657 }
658
659 @VisibleForTesting
660 protected interface EventListener {
661 /**
662 * @param uri Uri navigated to. If recents, then null.
663 */
664 void onDirectoryNavigated(@Nullable Uri uri);
665
666 /**
667 * @param uri Uri of the loaded directory. If recents, then null.
668 */
669 void onDirectoryLoaded(@Nullable Uri uri);
670 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700671}