blob: 0fed6410b2cd9a9cfaf421b095de104e5db84e57 [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;
Steve McKay7776aa52016-01-25 19:00:22 -080020import static com.android.documentsui.State.MODE_GRID;
Steve McKayf68210e2015-11-03 15:23:16 -080021import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_DOWN;
22import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE;
23import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_SIDE;
24import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_UP;
Steve McKay351a7492015-08-04 10:11:01 -070025import static com.android.internal.util.Preconditions.checkArgument;
Steve McKay7776aa52016-01-25 19:00:22 -080026import static com.android.internal.util.Preconditions.checkState;
Steve McKayb68dd222015-04-20 17:18:15 -070027
Steve McKayd0a2a2c2015-03-25 14:35:33 -070028import android.app.Activity;
29import android.app.Fragment;
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.database.Cursor;
36import android.net.Uri;
37import android.os.AsyncTask;
38import android.os.Bundle;
Steve McKayb68dd222015-04-20 17:18:15 -070039import android.provider.DocumentsContract;
40import android.provider.DocumentsContract.Root;
Steve McKayd4800812016-02-02 11:41:03 -080041import android.support.annotation.CallSuper;
Steve McKay0fbfc652015-08-20 16:48:49 -070042import android.support.annotation.LayoutRes;
Steve McKayfefcd702015-08-20 16:19:38 +000043import android.support.annotation.Nullable;
Steve McKayb68dd222015-04-20 17:18:15 -070044import android.util.Log;
Steve McKayb68dd222015-04-20 17:18:15 -070045import android.view.LayoutInflater;
46import android.view.Menu;
47import android.view.MenuItem;
Steve McKayb68dd222015-04-20 17:18:15 -070048import android.view.View;
49import android.view.ViewGroup;
50import android.widget.AdapterView;
51import android.widget.AdapterView.OnItemSelectedListener;
52import android.widget.BaseAdapter;
53import android.widget.ImageView;
Steve McKayb68dd222015-04-20 17:18:15 -070054import android.widget.TextView;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070055
Steve McKayb68dd222015-04-20 17:18:15 -070056import com.android.documentsui.RecentsProvider.ResumeColumns;
Aga Wronska8788dad2016-01-15 17:30:15 -080057import com.android.documentsui.SearchManager.SearchManagerListener;
Steve McKay7776aa52016-01-25 19:00:22 -080058import com.android.documentsui.State.ViewMode;
Steve McKayf68210e2015-11-03 15:23:16 -080059import com.android.documentsui.dirlist.DirectoryFragment;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070060import com.android.documentsui.model.DocumentInfo;
61import com.android.documentsui.model.DocumentStack;
62import com.android.documentsui.model.DurableUtils;
63import com.android.documentsui.model.RootInfo;
Daichi Hirono3067d0d2015-12-25 11:08:42 +090064import com.android.internal.util.Preconditions;
Steve McKay64ac2512015-05-12 12:49:58 -070065
Steve McKay64ac2512015-05-12 12:49:58 -070066import libcore.io.IoUtils;
67
68import java.io.FileNotFoundException;
69import java.io.IOException;
70import java.util.ArrayList;
71import java.util.Collection;
Steve McKay64ac2512015-05-12 12:49:58 -070072import java.util.List;
73import java.util.concurrent.Executor;
74
Aga Wronska8788dad2016-01-15 17:30:15 -080075public abstract class BaseActivity extends Activity implements SearchManagerListener {
Steve McKayb68dd222015-04-20 17:18:15 -070076
77 static final String EXTRA_STATE = "state";
78
Steve McKay0fbfc652015-08-20 16:48:49 -070079 State mState;
Steve McKayb68dd222015-04-20 17:18:15 -070080 RootsCache mRoots;
Steve McKay0269fb62015-04-22 15:55:34 -070081 SearchManager mSearchManager;
Steve McKay0fbfc652015-08-20 16:48:49 -070082 DrawerController mDrawer;
Steve McKayf2c8b0d2015-09-23 15:44:24 -070083 boolean mProductivityDevice;
Steve McKay0269fb62015-04-22 15:55:34 -070084
Steve McKayf2c8b0d2015-09-23 15:44:24 -070085 private final String mTag;
Steve McKay0fbfc652015-08-20 16:48:49 -070086 @LayoutRes
87 private int mLayoutId;
Steve McKayfad3d4a2015-09-22 15:09:21 -070088 private DirectoryContainerView mDirectoryContainer;
Steve McKayb68dd222015-04-20 17:18:15 -070089
Ben Kwab8a5e082015-12-07 13:25:27 -080090 public abstract void onDocumentPicked(DocumentInfo doc, @Nullable SiblingProvider siblings);
Steve McKayd0a2a2c2015-03-25 14:35:33 -070091 public abstract void onDocumentsPicked(List<DocumentInfo> docs);
Steve McKay351a7492015-08-04 10:11:01 -070092
Steve McKayb68dd222015-04-20 17:18:15 -070093 abstract void onTaskFinished(Uri... uris);
Aga Wronska8788dad2016-01-15 17:30:15 -080094 abstract void refreshDirectory(int anim);
Steve McKayb68dd222015-04-20 17:18:15 -070095 abstract void updateActionBar();
96 abstract void saveStackBlocking();
Ben Kwa0574b182015-09-08 07:31:19 -070097 abstract State buildState();
Steve McKayb68dd222015-04-20 17:18:15 -070098
Steve McKay0fbfc652015-08-20 16:48:49 -070099 public BaseActivity(@LayoutRes int layoutId, String tag) {
100 mLayoutId = layoutId;
Steve McKayb68dd222015-04-20 17:18:15 -0700101 mTag = tag;
102 }
103
104 @Override
105 public void onCreate(Bundle icicle) {
106 super.onCreate(icicle);
Steve McKay0fbfc652015-08-20 16:48:49 -0700107
108 mState = (icicle != null)
109 ? icicle.<State>getParcelable(EXTRA_STATE)
Ben Kwa0574b182015-09-08 07:31:19 -0700110 : buildState();
Steve McKay0fbfc652015-08-20 16:48:49 -0700111
Ben Kwa1c0a3892016-01-26 11:50:03 -0800112 Metrics.logActivityLaunch(this, mState, getIntent());
113
Steve McKay7f395012015-08-24 10:34:49 -0700114 setContentView(mLayoutId);
Steve McKay0fbfc652015-08-20 16:48:49 -0700115
Steve McKayb68dd222015-04-20 17:18:15 -0700116 mRoots = DocumentsApplication.getRootsCache(this);
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900117 mRoots.setOnCacheUpdateListener(
118 new RootsCache.OnCacheUpdateListener() {
119 @Override
120 public void onCacheUpdate() {
121 new HandleRootsChangedTask().execute(getCurrentRoot());
122 }
123 });
Steve McKayfad3d4a2015-09-22 15:09:21 -0700124 mDirectoryContainer = (DirectoryContainerView) findViewById(R.id.container_directory);
Aga Wronska8788dad2016-01-15 17:30:15 -0800125 mSearchManager = new SearchManager(this);
Steve McKay0fbfc652015-08-20 16:48:49 -0700126
127 // Base classes must update result in their onCreate.
128 setResult(Activity.RESULT_CANCELED);
Steve McKay0269fb62015-04-22 15:55:34 -0700129 }
130
131 @Override
132 public boolean onCreateOptionsMenu(Menu menu) {
133 boolean showMenu = super.onCreateOptionsMenu(menu);
134
135 getMenuInflater().inflate(R.menu.activity, menu);
Daichi Hironoe577ef02015-06-11 14:29:28 +0900136 mSearchManager.install((DocumentsToolBar) findViewById(R.id.toolbar));
Steve McKay0269fb62015-04-22 15:55:34 -0700137
138 return showMenu;
Steve McKayb68dd222015-04-20 17:18:15 -0700139 }
140
Steve McKay7bd32e12015-04-30 16:12:59 -0700141 @Override
Steve McKayd4800812016-02-02 11:41:03 -0800142 @CallSuper
Steve McKay7bd32e12015-04-30 16:12:59 -0700143 public boolean onPrepareOptionsMenu(Menu menu) {
Steve McKaye9809272015-10-01 11:39:24 -0700144 super.onPrepareOptionsMenu(menu);
Steve McKay7bd32e12015-04-30 16:12:59 -0700145
Steve McKayd4800812016-02-02 11:41:03 -0800146 mSearchManager.showMenu(canSearchRoot());
147
Steve McKaye9809272015-10-01 11:39:24 -0700148 final boolean inRecents = getCurrentDirectory() == null;
Steve McKay7bd32e12015-04-30 16:12:59 -0700149
150 final MenuItem sort = menu.findItem(R.id.menu_sort);
151 final MenuItem sortSize = menu.findItem(R.id.menu_sort_size);
152 final MenuItem grid = menu.findItem(R.id.menu_grid);
153 final MenuItem list = menu.findItem(R.id.menu_list);
Steve McKay7bd32e12015-04-30 16:12:59 -0700154 final MenuItem advanced = menu.findItem(R.id.menu_advanced);
155 final MenuItem fileSize = menu.findItem(R.id.menu_file_size);
156
Steve McKayd4800812016-02-02 11:41:03 -0800157 // Search uses backend ranking; no sorting, recents doesn't support sort.
Steve McKaye9809272015-10-01 11:39:24 -0700158 sort.setVisible(!inRecents && !mSearchManager.isSearching());
Steve McKayd4800812016-02-02 11:41:03 -0800159 sortSize.setVisible(mState.showSize); // Only sort by size when file sizes are visible
160 fileSize.setVisible(!mState.forceSize);
Steve McKaye9809272015-10-01 11:39:24 -0700161
162 // grid/list is effectively a toggle.
163 grid.setVisible(mState.derivedMode != State.MODE_GRID);
164 list.setVisible(mState.derivedMode != State.MODE_LIST);
165
Steve McKaye9809272015-10-01 11:39:24 -0700166 advanced.setVisible(!mState.forceAdvanced);
Steve McKay7bd32e12015-04-30 16:12:59 -0700167 advanced.setTitle(LocalPreferences.getDisplayAdvancedDevices(this)
168 ? R.string.menu_advanced_hide : R.string.menu_advanced_show);
169 fileSize.setTitle(LocalPreferences.getDisplayFileSize(this)
170 ? R.string.menu_file_size_hide : R.string.menu_file_size_show);
171
Steve McKaye9809272015-10-01 11:39:24 -0700172 return true;
Steve McKay7bd32e12015-04-30 16:12:59 -0700173 }
174
Daichi Hironoe28c3c82016-01-13 13:19:02 +0900175 @Override
176 protected void onDestroy() {
177 mRoots.setOnCacheUpdateListener(null);
178 super.onDestroy();
179 }
180
Ben Kwa0574b182015-09-08 07:31:19 -0700181 State buildDefaultState() {
182 State state = new State();
183
184 final Intent intent = getIntent();
Ben Kwa0574b182015-09-08 07:31:19 -0700185
186 state.localOnly = intent.getBooleanExtra(Intent.EXTRA_LOCAL_ONLY, false);
Ben Kwa0574b182015-09-08 07:31:19 -0700187
Steve McKay459bc2b2015-09-16 15:07:31 -0700188 state.forceSize = intent.getBooleanExtra(DocumentsContract.EXTRA_SHOW_FILESIZE, false);
189 state.showSize = state.forceSize || LocalPreferences.getDisplayFileSize(this);
190
191 state.forceAdvanced = intent.getBooleanExtra(DocumentsContract.EXTRA_SHOW_ADVANCED, false);
192 state.showAdvanced = state.forceAdvanced
193 || LocalPreferences.getDisplayAdvancedDevices(this);
194
195 state.initAcceptMimes(intent);
Ben Kwa0574b182015-09-08 07:31:19 -0700196 state.excludedAuthorities = getExcludedAuthorities();
197
198 return state;
199 }
200
Steve McKayb68dd222015-04-20 17:18:15 -0700201 void onStackRestored(boolean restored, boolean external) {}
202
203 void onRootPicked(RootInfo root) {
Steve McKay7776aa52016-01-25 19:00:22 -0800204 mState.derivedMode = LocalPreferences.getViewMode(this, root, MODE_GRID);
205
Steve McKayb68dd222015-04-20 17:18:15 -0700206 // Clear entire backstack and start in new root
Daichi Hirono2806beb2016-01-07 15:29:12 +0900207 mState.onRootChanged(root);
Steve McKay0269fb62015-04-22 15:55:34 -0700208 mSearchManager.update(root);
209
210 // Recents is always in memory, so we just load it directly.
211 // Otherwise we delegate loading data from disk to a task
212 // to ensure a responsive ui.
213 if (mRoots.isRecentsRoot(root)) {
Aga Wronska8788dad2016-01-15 17:30:15 -0800214 refreshCurrentRootAndDirectory(ANIM_SIDE);
Steve McKay0269fb62015-04-22 15:55:34 -0700215 } else {
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900216 new PickRootTask(root, true).executeOnExecutor(getExecutorForCurrentDirectory());
217 }
218 }
219
Steve McKayb68dd222015-04-20 17:18:15 -0700220 void expandMenus(Menu menu) {
221 for (int i = 0; i < menu.size(); i++) {
222 final MenuItem item = menu.getItem(i);
223 switch (item.getItemId()) {
224 case R.id.menu_advanced:
225 case R.id.menu_file_size:
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700226 case R.id.menu_new_window:
Aga Wronska1f7f2392016-01-08 17:29:24 -0800227 case R.id.menu_search:
Steve McKayb68dd222015-04-20 17:18:15 -0700228 break;
229 default:
230 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
231 }
232 }
233 }
234
235 @Override
236 public boolean onOptionsItemSelected(MenuItem item) {
Steve McKayb68dd222015-04-20 17:18:15 -0700237
Steve McKay6035b3c2015-12-04 11:19:09 -0800238 switch (item.getItemId()) {
239 case android.R.id.home:
240 onBackPressed();
241 return true;
242
243 case R.id.menu_create_dir:
244 showCreateDirectoryDialog();
245 return true;
246
247 case R.id.menu_search:
248 return false;
249
250 case R.id.menu_sort_name:
251 setUserSortOrder(State.SORT_ORDER_DISPLAY_NAME);
252 return true;
253
254 case R.id.menu_sort_date:
255 setUserSortOrder(State.SORT_ORDER_LAST_MODIFIED);
256 return true;
257 case R.id.menu_sort_size:
258 setUserSortOrder(State.SORT_ORDER_SIZE);
259 return true;
260
261 case R.id.menu_grid:
Steve McKay7776aa52016-01-25 19:00:22 -0800262 setViewMode(State.MODE_GRID);
Steve McKay6035b3c2015-12-04 11:19:09 -0800263 return true;
264
265 case R.id.menu_list:
Steve McKay7776aa52016-01-25 19:00:22 -0800266 setViewMode(State.MODE_LIST);
Steve McKay6035b3c2015-12-04 11:19:09 -0800267 return true;
268
269 case R.id.menu_paste_from_clipboard:
Steve McKayd4800812016-02-02 11:41:03 -0800270 DirectoryFragment dir = getDirectoryFragment();
271 if (dir != null) {
272 dir.pasteFromClipboard();
273 }
Steve McKay6035b3c2015-12-04 11:19:09 -0800274 return true;
275
276 case R.id.menu_advanced:
277 setDisplayAdvancedDevices(!LocalPreferences.getDisplayAdvancedDevices(this));
278 return true;
279
280 case R.id.menu_file_size:
281 setDisplayFileSize(!LocalPreferences.getDisplayFileSize(this));
282 return true;
283
284 case R.id.menu_settings:
285 final RootInfo root = getCurrentRoot();
286 final Intent intent = new Intent(DocumentsContract.ACTION_DOCUMENT_ROOT_SETTINGS);
287 intent.setDataAndType(root.getUri(), DocumentsContract.Root.MIME_TYPE_ITEM);
288 startActivity(intent);
289 return true;
290
291 default:
292 return super.onOptionsItemSelected(item);
293 }
Steve McKayb68dd222015-04-20 17:18:15 -0700294 }
295
Steve McKayd4800812016-02-02 11:41:03 -0800296 final @Nullable DirectoryFragment getDirectoryFragment() {
297 return DirectoryFragment.get(getFragmentManager());
298 }
299
Steve McKayceeb3f72015-05-19 16:10:25 -0700300 void showCreateDirectoryDialog() {
301 CreateDirectoryFragment.show(getFragmentManager());
302 }
303
304 /**
305 * Returns true if a directory can be created in the current location.
306 * @return
307 */
308 boolean canCreateDirectory() {
309 final RootInfo root = getCurrentRoot();
310 final DocumentInfo cwd = getCurrentDirectory();
311 return cwd != null
312 && cwd.isCreateSupported()
313 && !mSearchManager.isSearching()
Steve McKaye9809272015-10-01 11:39:24 -0700314 && !root.isRecents()
Steve McKayceeb3f72015-05-19 16:10:25 -0700315 && !root.isDownloads();
316 }
317
Steve McKay351a7492015-08-04 10:11:01 -0700318 void onDirectoryCreated(DocumentInfo doc) {
319 checkArgument(doc.isDirectory());
Tomasz Mikolajewski734936a2015-11-25 13:01:18 +0900320 openContainerDocument(doc);
Steve McKay351a7492015-08-04 10:11:01 -0700321 }
322
Tomasz Mikolajewski734936a2015-11-25 13:01:18 +0900323 void openContainerDocument(DocumentInfo doc) {
324 checkArgument(doc.isContainer());
Daichi Hirono2806beb2016-01-07 15:29:12 +0900325 mState.pushDocument(doc);
Aga Wronska8788dad2016-01-15 17:30:15 -0800326 refreshCurrentRootAndDirectory(ANIM_DOWN);
Steve McKay351a7492015-08-04 10:11:01 -0700327 }
328
Steve McKayb68dd222015-04-20 17:18:15 -0700329 /**
Aga Wronska8788dad2016-01-15 17:30:15 -0800330 * Refreshes the content of the director and the menu/action bar.
331 * The current directory name and selection will get updated.
Steve McKayb68dd222015-04-20 17:18:15 -0700332 * @param anim
333 */
Aga Wronska8788dad2016-01-15 17:30:15 -0800334 final void refreshCurrentRootAndDirectory(int anim) {
335 mSearchManager.cancelSearch();
336
Steve McKayfad3d4a2015-09-22 15:09:21 -0700337 mDirectoryContainer.setDrawDisappearingFirst(anim == ANIM_DOWN);
Aga Wronska8788dad2016-01-15 17:30:15 -0800338 refreshDirectory(anim);
Steve McKayb68dd222015-04-20 17:18:15 -0700339
340 final RootsFragment roots = RootsFragment.get(getFragmentManager());
341 if (roots != null) {
342 roots.onCurrentRootChanged();
343 }
344
345 updateActionBar();
Aga Wronska1f7f2392016-01-08 17:29:24 -0800346
Aga Wronska8788dad2016-01-15 17:30:15 -0800347 invalidateOptionsMenu();
348 }
349
350 /**
351 * Called when search results changed.
352 * Refreshes the content of the directory. It doesn't refresh elements on the action bar.
353 * e.g. The current directory name displayed on the action bar won't get updated.
354 */
355 @Override
356 public void onSearchChanged() {
357 mDirectoryContainer.setDrawDisappearingFirst(false);
358 refreshDirectory(ANIM_NONE);
359 }
360
361 /**
362 * Called when search query changed.
363 * Updates the state object.
364 * @param query - New query
365 */
366 @Override
367 public void onSearchQueryChanged(String query) {
368 mState.currentSearch = query;
Steve McKayb68dd222015-04-20 17:18:15 -0700369 }
370
Ben Kwa0bcdec32015-05-29 15:40:31 -0700371 final List<String> getExcludedAuthorities() {
372 List<String> authorities = new ArrayList<>();
373 if (getIntent().getBooleanExtra(DocumentsContract.EXTRA_EXCLUDE_SELF, false)) {
374 // Exclude roots provided by the calling package.
375 String packageName = getCallingPackageMaybeExtra();
376 try {
377 PackageInfo pkgInfo = getPackageManager().getPackageInfo(packageName,
378 PackageManager.GET_PROVIDERS);
379 for (ProviderInfo provider: pkgInfo.providers) {
380 authorities.add(provider.authority);
381 }
382 } catch (PackageManager.NameNotFoundException e) {
383 Log.e(mTag, "Calling package name does not resolve: " + packageName);
384 }
385 }
386 return authorities;
387 }
388
Aga Wronska654e25c2016-01-29 11:41:41 -0800389 boolean canSearchRoot() {
390 final RootInfo root = getCurrentRoot();
391 return (root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0;
392 }
393
Steve McKayb68dd222015-04-20 17:18:15 -0700394 final String getCallingPackageMaybeExtra() {
Ben Kwa0bcdec32015-05-29 15:40:31 -0700395 String callingPackage = getCallingPackage();
396 // System apps can set the calling package name using an extra.
397 try {
398 ApplicationInfo info = getPackageManager().getApplicationInfo(callingPackage, 0);
399 if (info.isSystemApp() || info.isUpdatedSystemApp()) {
400 final String extra = getIntent().getStringExtra(DocumentsContract.EXTRA_PACKAGE_NAME);
401 if (extra != null) {
402 callingPackage = extra;
403 }
404 }
405 } finally {
406 return callingPackage;
407 }
Steve McKayb68dd222015-04-20 17:18:15 -0700408 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700409
410 public static BaseActivity get(Fragment fragment) {
411 return (BaseActivity) fragment.getActivity();
412 }
413
Ben Kwa0574b182015-09-08 07:31:19 -0700414 public State getDisplayState() {
415 return mState;
416 }
417
Steve McKayb68dd222015-04-20 17:18:15 -0700418 void setDisplayAdvancedDevices(boolean display) {
Steve McKayb68dd222015-04-20 17:18:15 -0700419 LocalPreferences.setDisplayAdvancedDevices(this, display);
Steve McKay4d0255f2015-09-25 16:02:56 -0700420 mState.showAdvanced = mState.forceAdvanced | display;
Steve McKayb68dd222015-04-20 17:18:15 -0700421 RootsFragment.get(getFragmentManager()).onDisplayStateChanged();
422 invalidateOptionsMenu();
423 }
424
425 void setDisplayFileSize(boolean display) {
426 LocalPreferences.setDisplayFileSize(this, display);
Steve McKay4d0255f2015-09-25 16:02:56 -0700427 mState.showSize = display;
Steve McKayd4800812016-02-02 11:41:03 -0800428 DirectoryFragment dir = getDirectoryFragment();
429 if (dir != null) {
430 dir.onDisplayStateChanged();
431 }
Steve McKayb68dd222015-04-20 17:18:15 -0700432 invalidateOptionsMenu();
433 }
434
Steve McKayb68dd222015-04-20 17:18:15 -0700435 /**
436 * Set state sort order based on explicit user action.
437 */
438 void setUserSortOrder(int sortOrder) {
Steve McKay4d0255f2015-09-25 16:02:56 -0700439 mState.userSortOrder = sortOrder;
Steve McKayd4800812016-02-02 11:41:03 -0800440 DirectoryFragment dir = getDirectoryFragment();
441 if (dir != null) {
442 dir.onSortOrderChanged();
443 };
Steve McKayb68dd222015-04-20 17:18:15 -0700444 }
445
446 /**
Steve McKay7776aa52016-01-25 19:00:22 -0800447 * Set mode based on explicit user action.
Steve McKayb68dd222015-04-20 17:18:15 -0700448 */
Steve McKay7776aa52016-01-25 19:00:22 -0800449 void setViewMode(@ViewMode int mode) {
450 checkState(mState.stack.root != null);
451 LocalPreferences.setViewMode(this, mState.stack.root, mode);
452 mState.derivedMode = mode;
453
454 // view icon needs to be updated, but we *could* do it
455 // in onOptionsItemSelected, and not do the full invalidation
456 // But! That's a larger refactoring we'll save for another day.
457 invalidateOptionsMenu();
Steve McKayd4800812016-02-02 11:41:03 -0800458 DirectoryFragment dir = getDirectoryFragment();
459 if (dir != null) {
460 dir.onViewModeChanged();
461 };
Steve McKayb68dd222015-04-20 17:18:15 -0700462 }
463
Aga Wronska3c237182016-01-20 16:32:33 -0800464 public void setPending(boolean pending) {
Steve McKayb68dd222015-04-20 17:18:15 -0700465 final SaveFragment save = SaveFragment.get(getFragmentManager());
466 if (save != null) {
467 save.setPending(pending);
468 }
469 }
470
471 @Override
472 protected void onSaveInstanceState(Bundle state) {
473 super.onSaveInstanceState(state);
Steve McKay4d0255f2015-09-25 16:02:56 -0700474 state.putParcelable(EXTRA_STATE, mState);
Steve McKayb68dd222015-04-20 17:18:15 -0700475 }
476
477 @Override
478 protected void onRestoreInstanceState(Bundle state) {
479 super.onRestoreInstanceState(state);
480 }
481
Steve McKayf68210e2015-11-03 15:23:16 -0800482 public RootInfo getCurrentRoot() {
Steve McKay4d0255f2015-09-25 16:02:56 -0700483 if (mState.stack.root != null) {
484 return mState.stack.root;
Steve McKayb68dd222015-04-20 17:18:15 -0700485 } else {
486 return mRoots.getRecentsRoot();
487 }
488 }
489
490 public DocumentInfo getCurrentDirectory() {
Steve McKay4d0255f2015-09-25 16:02:56 -0700491 return mState.stack.peek();
Steve McKayb68dd222015-04-20 17:18:15 -0700492 }
493
Steve McKay459bc2b2015-09-16 15:07:31 -0700494 public Executor getExecutorForCurrentDirectory() {
Steve McKayb68dd222015-04-20 17:18:15 -0700495 final DocumentInfo cwd = getCurrentDirectory();
496 if (cwd != null && cwd.authority != null) {
497 return ProviderExecutor.forAuthority(cwd.authority);
498 } else {
499 return AsyncTask.THREAD_POOL_EXECUTOR;
500 }
501 }
502
Steve McKay0fbfc652015-08-20 16:48:49 -0700503 @Override
504 public void onBackPressed() {
505 // While action bar is expanded, the state stack UI is hidden.
506 if (mSearchManager.cancelSearch()) {
507 return;
508 }
509
Steve McKayd4800812016-02-02 11:41:03 -0800510 DirectoryFragment dir = getDirectoryFragment();
511 if (dir != null && dir.onBackPressed()) {
Steve McKay86c05762016-01-28 15:30:10 -0800512 return;
513 }
514
Daichi Hirono2806beb2016-01-07 15:29:12 +0900515 if (!mState.hasLocationChanged()) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700516 super.onBackPressed();
517 return;
518 }
519
520 final int size = mState.stack.size();
521
522 if (mDrawer.isOpen()) {
523 mDrawer.setOpen(false);
524 } else if (size > 1) {
525 mState.stack.pop();
Aga Wronska8788dad2016-01-15 17:30:15 -0800526 refreshCurrentRootAndDirectory(ANIM_UP);
Steve McKay0fbfc652015-08-20 16:48:49 -0700527 } else {
528 super.onBackPressed();
529 }
530 }
531
Steve McKayb68dd222015-04-20 17:18:15 -0700532 public void onStackPicked(DocumentStack stack) {
533 try {
534 // Update the restored stack to ensure we have freshest data
535 stack.updateDocuments(getContentResolver());
Daichi Hirono2806beb2016-01-07 15:29:12 +0900536 mState.setStack(stack);
Aga Wronska8788dad2016-01-15 17:30:15 -0800537 refreshCurrentRootAndDirectory(ANIM_SIDE);
Steve McKayb68dd222015-04-20 17:18:15 -0700538
539 } catch (FileNotFoundException e) {
540 Log.w(mTag, "Failed to restore stack: " + e);
541 }
542 }
543
Daichi Hirono2806beb2016-01-07 15:29:12 +0900544 private DocumentInfo getRootDocumentBlocking(RootInfo root) {
545 try {
546 final Uri uri = DocumentsContract.buildDocumentUri(
547 root.authority, root.documentId);
548 return DocumentInfo.fromUri(getContentResolver(), uri);
549 } catch (FileNotFoundException e) {
550 Log.w(mTag, "Failed to find root", e);
551 return null;
552 }
553 }
554
Steve McKayb68dd222015-04-20 17:18:15 -0700555 final class PickRootTask extends AsyncTask<Void, Void, DocumentInfo> {
556 private RootInfo mRoot;
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900557 private boolean mTouched;
Steve McKayb68dd222015-04-20 17:18:15 -0700558
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900559 public PickRootTask(RootInfo root, boolean touched) {
Steve McKayb68dd222015-04-20 17:18:15 -0700560 mRoot = root;
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900561 mTouched = touched;
Steve McKayb68dd222015-04-20 17:18:15 -0700562 }
563
564 @Override
565 protected DocumentInfo doInBackground(Void... params) {
Daichi Hirono2806beb2016-01-07 15:29:12 +0900566 return getRootDocumentBlocking(mRoot);
Steve McKayb68dd222015-04-20 17:18:15 -0700567 }
568
569 @Override
570 protected void onPostExecute(DocumentInfo result) {
Daichi Hironoe28c3c82016-01-13 13:19:02 +0900571 if (result != null && !isDestroyed()) {
Daichi Hirono2806beb2016-01-07 15:29:12 +0900572 openContainerDocument(result);
Steve McKayb68dd222015-04-20 17:18:15 -0700573 }
574 }
575 }
576
577 final class RestoreStackTask extends AsyncTask<Void, Void, Void> {
578 private volatile boolean mRestoredStack;
579 private volatile boolean mExternal;
580
581 @Override
582 protected Void doInBackground(Void... params) {
Steve McKay4d0255f2015-09-25 16:02:56 -0700583 if (DEBUG && !mState.stack.isEmpty()) {
584 Log.w(mTag, "Overwriting existing stack.");
585 }
Steve McKayb68dd222015-04-20 17:18:15 -0700586 RootsCache roots = DocumentsApplication.getRootsCache(BaseActivity.this);
587
588 // Restore last stack for calling package
589 final String packageName = getCallingPackageMaybeExtra();
590 final Cursor cursor = getContentResolver()
591 .query(RecentsProvider.buildResume(packageName), null, null, null, null);
592 try {
593 if (cursor.moveToFirst()) {
594 mExternal = cursor.getInt(cursor.getColumnIndex(ResumeColumns.EXTERNAL)) != 0;
595 final byte[] rawStack = cursor.getBlob(
596 cursor.getColumnIndex(ResumeColumns.STACK));
Steve McKay4d0255f2015-09-25 16:02:56 -0700597 DurableUtils.readFromArray(rawStack, mState.stack);
Steve McKayb68dd222015-04-20 17:18:15 -0700598 mRestoredStack = true;
599 }
600 } catch (IOException e) {
601 Log.w(mTag, "Failed to resume: " + e);
602 } finally {
603 IoUtils.closeQuietly(cursor);
604 }
605
606 if (mRestoredStack) {
607 // Update the restored stack to ensure we have freshest data
Steve McKay4d0255f2015-09-25 16:02:56 -0700608 final Collection<RootInfo> matchingRoots = roots.getMatchingRootsBlocking(mState);
Steve McKayb68dd222015-04-20 17:18:15 -0700609 try {
Steve McKay4d0255f2015-09-25 16:02:56 -0700610 mState.stack.updateRoot(matchingRoots);
611 mState.stack.updateDocuments(getContentResolver());
Steve McKayb68dd222015-04-20 17:18:15 -0700612 } catch (FileNotFoundException e) {
613 Log.w(mTag, "Failed to restore stack: " + e);
Steve McKay4d0255f2015-09-25 16:02:56 -0700614 mState.stack.reset();
Steve McKayb68dd222015-04-20 17:18:15 -0700615 mRestoredStack = false;
616 }
617 }
618
619 return null;
620 }
621
622 @Override
623 protected void onPostExecute(Void result) {
624 if (isDestroyed()) return;
Steve McKay4d0255f2015-09-25 16:02:56 -0700625 mState.restored = true;
Aga Wronska8788dad2016-01-15 17:30:15 -0800626 refreshCurrentRootAndDirectory(ANIM_NONE);
Steve McKayb68dd222015-04-20 17:18:15 -0700627 onStackRestored(mRestoredStack, mExternal);
Steve McKayb68dd222015-04-20 17:18:15 -0700628 }
629 }
630
Ben Kwa0574b182015-09-08 07:31:19 -0700631 final class RestoreRootTask extends AsyncTask<Void, Void, RootInfo> {
632 private Uri mRootUri;
633
634 public RestoreRootTask(Uri rootUri) {
635 mRootUri = rootUri;
636 }
637
638 @Override
639 protected RootInfo doInBackground(Void... params) {
640 final String rootId = DocumentsContract.getRootId(mRootUri);
641 return mRoots.getRootOneshot(mRootUri.getAuthority(), rootId);
642 }
643
644 @Override
645 protected void onPostExecute(RootInfo root) {
646 if (isDestroyed()) return;
647 mState.restored = true;
648
649 if (root != null) {
650 onRootPicked(root);
651 } else {
652 Log.w(mTag, "Failed to find root: " + mRootUri);
653 finish();
654 }
655 }
656 }
657
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900658 final class HandleRootsChangedTask extends AsyncTask<RootInfo, Void, RootInfo> {
Daichi Hirono2806beb2016-01-07 15:29:12 +0900659 DocumentInfo mHome;
660
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900661 @Override
662 protected RootInfo doInBackground(RootInfo... roots) {
Daichi Hirono2806beb2016-01-07 15:29:12 +0900663 checkArgument(roots.length == 1);
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900664 final RootInfo currentRoot = roots[0];
665 final Collection<RootInfo> cachedRoots = mRoots.getRootsBlocking();
666 RootInfo homeRoot = null;
667 for (final RootInfo root : cachedRoots) {
668 if (root.isHome()) {
669 homeRoot = root;
670 }
671 if (root.getUri().equals(currentRoot.getUri())) {
672 // We don't need to change the current root as the current root was not removed.
673 return null;
674 }
675 }
676 Preconditions.checkNotNull(homeRoot);
Daichi Hirono2806beb2016-01-07 15:29:12 +0900677 mHome = getRootDocumentBlocking(homeRoot);
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900678 return homeRoot;
679 }
680
681 @Override
Daichi Hirono2806beb2016-01-07 15:29:12 +0900682 protected void onPostExecute(RootInfo homeRoot) {
Daichi Hirono38afb8f2016-01-14 12:45:49 +0900683 if (homeRoot != null && mHome != null && !isDestroyed()) {
Daichi Hirono2806beb2016-01-07 15:29:12 +0900684 // Clear entire backstack and start in new root
685 mState.onRootChanged(homeRoot);
686 mSearchManager.update(homeRoot);
687 openContainerDocument(mHome);
Daichi Hirono3067d0d2015-12-25 11:08:42 +0900688 }
689 }
690 }
691
Steve McKayb68dd222015-04-20 17:18:15 -0700692 final class ItemSelectedListener implements OnItemSelectedListener {
693
694 boolean mIgnoreNextNavigation;
695
696 @Override
697 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
698 if (mIgnoreNextNavigation) {
699 mIgnoreNextNavigation = false;
700 return;
701 }
702
Steve McKay4d0255f2015-09-25 16:02:56 -0700703 while (mState.stack.size() > position + 1) {
Daichi Hirono2806beb2016-01-07 15:29:12 +0900704 mState.popDocument();
Steve McKayb68dd222015-04-20 17:18:15 -0700705 }
Aga Wronska8788dad2016-01-15 17:30:15 -0800706 refreshCurrentRootAndDirectory(ANIM_UP);
Steve McKayb68dd222015-04-20 17:18:15 -0700707 }
708
709 @Override
710 public void onNothingSelected(AdapterView<?> parent) {
711 // Ignored
712 }
713 }
714
715 /**
716 * Class providing toolbar with runtime access to useful activity data.
717 */
718 final class StackAdapter extends BaseAdapter {
719 @Override
720 public int getCount() {
Steve McKay4d0255f2015-09-25 16:02:56 -0700721 return mState.stack.size();
Steve McKayb68dd222015-04-20 17:18:15 -0700722 }
723
724 @Override
725 public DocumentInfo getItem(int position) {
Steve McKay4d0255f2015-09-25 16:02:56 -0700726 return mState.stack.get(mState.stack.size() - position - 1);
Steve McKayb68dd222015-04-20 17:18:15 -0700727 }
728
729 @Override
730 public long getItemId(int position) {
731 return position;
732 }
733
734 @Override
735 public View getView(int position, View convertView, ViewGroup parent) {
736 if (convertView == null) {
737 convertView = LayoutInflater.from(parent.getContext())
738 .inflate(R.layout.item_subdir_title, parent, false);
739 }
740
741 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
742 final DocumentInfo doc = getItem(position);
743
744 if (position == 0) {
745 final RootInfo root = getCurrentRoot();
746 title.setText(root.title);
747 } else {
748 title.setText(doc.displayName);
749 }
750
751 return convertView;
752 }
753
754 @Override
755 public View getDropDownView(int position, View convertView, ViewGroup parent) {
756 if (convertView == null) {
757 convertView = LayoutInflater.from(parent.getContext())
758 .inflate(R.layout.item_subdir, parent, false);
759 }
760
761 final ImageView subdir = (ImageView) convertView.findViewById(R.id.subdir);
762 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
763 final DocumentInfo doc = getItem(position);
764
765 if (position == 0) {
766 final RootInfo root = getCurrentRoot();
767 title.setText(root.title);
768 subdir.setVisibility(View.GONE);
769 } else {
770 title.setText(doc.displayName);
771 subdir.setVisibility(View.VISIBLE);
772 }
773
774 return convertView;
775 }
776 }
777
778 /**
Steve McKay351a7492015-08-04 10:11:01 -0700779 * Interface providing access to current view of documents
780 * even when all documents are not homed to the same parent.
781 */
Ben Kwab8a5e082015-12-07 13:25:27 -0800782 public interface SiblingProvider {
Steve McKay351a7492015-08-04 10:11:01 -0700783 /**
784 * Returns the cursor for the selected document. The cursor can be used to retrieve
785 * details about a document and its siblings.
786 * @return
787 */
788 Cursor getCursor();
789 }
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700790}