public void onDocumentPicked(DocumentInfo doc) { final FragmentManager fm = getFragmentManager(); if (doc.isDirectory()) { mState.stack.push(doc); mState.stackTouched = true; onCurrentDirectoryChanged(ANIM_DOWN); } else if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) { // Explicit file picked, return new ExistingFinishTask(doc.derivedUri).executeOnExecutor(getCurrentExecutor()); } else if (mState.action == ACTION_CREATE) { // Replace selected file SaveFragment.get(fm).setReplaceTarget(doc); } else if (mState.action == ACTION_MANAGE) { // First try managing the document; we expect manager to filter // based on authority, so we don't grant. final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT); manage.setData(doc.derivedUri); try { startActivity(manage); } catch (ActivityNotFoundException ex) { // Fall back to viewing final Intent view = new Intent(Intent.ACTION_VIEW); view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); view.setData(doc.derivedUri); try { startActivity(view); } catch (ActivityNotFoundException ex2) { /// M: Show toast with enhance way. showToast(R.string.toast_no_application); } } } }
private void onCurrentDirectoryChanged(int anim) { final FragmentManager fm = getFragmentManager(); final RootInfo root = getCurrentRoot(); final DocumentInfo cwd = getCurrentDirectory(); mDirectoryContainer.setDrawDisappearingFirst(anim == ANIM_DOWN); if (cwd == null) { // No directory means recents if (mState.action == ACTION_CREATE || mState.action == ACTION_OPEN_TREE) { RecentsCreateFragment.show(fm); } else { DirectoryFragment.showRecentsOpen(fm, anim); /// M: When state has stored, we need use store state to init fragment(this may happen when // activity /// re-create, in this case we need use the userMode store in saveInstance, and only when // activity /// first created we need follow the below design. {@ if (!mState.restored) { // Start recents in grid when requesting visual things final boolean visualMimes = MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, mState.acceptMimes); mState.userMode = visualMimes ? MODE_GRID : MODE_LIST; mState.derivedMode = mState.userMode; } /// @} } } else { if (mState.currentSearch != null) { // Ongoing search DirectoryFragment.showSearch(fm, root, mState.currentSearch, anim); } else { // Normal boring directory DirectoryFragment.showNormal(fm, root, cwd, anim); } } // Forget any replacement target if (mState.action == ACTION_CREATE) { final SaveFragment save = SaveFragment.get(fm); if (save != null) { save.setReplaceTarget(null); } } if (mState.action == ACTION_OPEN_TREE) { final PickFragment pick = PickFragment.get(fm); if (pick != null) { final CharSequence displayName = (mState.stack.size() <= 1) ? root.title : cwd.displayName; pick.setPickTarget(cwd, displayName); } } final RootsFragment roots = RootsFragment.get(fm); if (roots != null) { roots.onCurrentRootChanged(); } updateActionBar(); invalidateOptionsMenu(); dumpStack(); }
public void setPending(boolean pending) { final SaveFragment save = SaveFragment.get(getFragmentManager()); if (save != null) { save.setPending(pending); } }
@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); final FragmentManager fm = getFragmentManager(); final RootInfo root = getCurrentRoot(); final DocumentInfo cwd = getCurrentDirectory(); final MenuItem createDir = menu.findItem(R.id.menu_create_dir); final MenuItem search = menu.findItem(R.id.menu_search); final MenuItem sort = menu.findItem(R.id.menu_sort); final MenuItem sortSize = menu.findItem(R.id.menu_sort_size); final MenuItem grid = menu.findItem(R.id.menu_grid); final MenuItem list = menu.findItem(R.id.menu_list); final MenuItem advanced = menu.findItem(R.id.menu_advanced); final MenuItem fileSize = menu.findItem(R.id.menu_file_size); sort.setVisible(cwd != null); grid.setVisible(mState.derivedMode != MODE_GRID); list.setVisible(mState.derivedMode != MODE_LIST); if (mState.currentSearch != null) { // Search uses backend ranking; no sorting sort.setVisible(false); search.expandActionView(); mSearchView.setIconified(false); mSearchView.clearFocus(); mSearchView.setQuery(mState.currentSearch, false); } else { mIgnoreNextClose = true; mSearchView.setIconified(true); mSearchView.clearFocus(); mIgnoreNextCollapse = true; search.collapseActionView(); } // Only sort by size when visible sortSize.setVisible(mState.showSize); final boolean searchVisible; if (mState.action == ACTION_CREATE || mState.action == ACTION_OPEN_TREE) { createDir.setVisible(cwd != null && cwd.isCreateSupported()); searchVisible = false; // No display options in recent directories if (cwd == null) { grid.setVisible(false); list.setVisible(false); } if (mState.action == ACTION_CREATE) { /// M: add to avoid seldom NullPointerException SaveFragment saveFragment = SaveFragment.get(fm); if (null != saveFragment) { saveFragment.setSaveEnabled(cwd != null && cwd.isCreateSupported()); } else { Log.e(TAG, "onPrepareOptionsMenu, SaveFragment is null"); } } } else { createDir.setVisible(false); searchVisible = root != null && ((root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0); } // TODO: close any search in-progress when hiding search.setVisible(searchVisible); advanced.setTitle( LocalPreferences.getDisplayAdvancedDevices(this) ? R.string.menu_advanced_hide : R.string.menu_advanced_show); fileSize.setTitle( LocalPreferences.getDisplayFileSize(this) ? R.string.menu_file_size_hide : R.string.menu_file_size_show); /// M: If in force show advance mode, need hide advance menu advanced.setVisible(mState.action != ACTION_MANAGE && !mState.forceAdvanced); /// M: If cmd == null and mState.action == ACTION_CREATE, it means show /// RecentsCreateFragment, we need hide file size menu to avoid exception fileSize.setVisible( mState.action != ACTION_MANAGE && !(cwd == null && mState.action == ACTION_CREATE)); return true; }
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); mRoots = DocumentsApplication.getRootsCache(this); setResult(Activity.RESULT_CANCELED); setContentView(R.layout.activity); final Context context = this; final Resources res = getResources(); mShowAsDialog = res.getBoolean(R.bool.show_as_dialog); if (mShowAsDialog) { // Strongly define our horizontal dimension; we leave vertical as // WRAP_CONTENT so that system resizes us when IME is showing. final WindowManager.LayoutParams a = getWindow().getAttributes(); final Point size = new Point(); getWindowManager().getDefaultDisplay().getSize(size); a.width = (int) res.getFraction(R.dimen.dialog_width, size.x, size.x); getWindow().setAttributes(a); } else { // Non-dialog means we have a drawer mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, R.drawable.ic_hamburger, R.string.drawer_open, R.string.drawer_close); mDrawerLayout.setDrawerListener(mDrawerListener); mRootsDrawer = findViewById(R.id.drawer_roots); } mDirectoryContainer = (DirectoryContainerView) findViewById(R.id.container_directory); if (icicle != null) { mState = icicle.getParcelable(EXTRA_STATE); } else { buildDefaultState(); } mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setTitleTextAppearance( context, android.R.style.TextAppearance_DeviceDefault_Widget_ActionBar_Title); /// M: add log to print intent info Intent intent = getIntent(); Log.d( TAG, "onCreate: intent = " + intent + ", extra = " + (intent != null ? intent.getExtras() : null)); mToolbarStack = (Spinner) findViewById(R.id.stack); mToolbarStack.setOnItemSelectedListener(mStackListener); mRootsToolbar = (Toolbar) findViewById(R.id.roots_toolbar); if (mRootsToolbar != null) { mRootsToolbar.setTitleTextAppearance( context, android.R.style.TextAppearance_DeviceDefault_Widget_ActionBar_Title); } setActionBar(mToolbar); // Hide roots when we're managing a specific root if (mState.action == ACTION_MANAGE) { if (mShowAsDialog) { findViewById(R.id.container_roots).setVisibility(View.GONE); } else { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); } } if (mState.action == ACTION_CREATE) { final String mimeType = getIntent().getType(); final String title = getIntent().getStringExtra(Intent.EXTRA_TITLE); SaveFragment.show(getFragmentManager(), mimeType, title); } else if (mState.action == ACTION_OPEN_TREE) { PickFragment.show(getFragmentManager()); } if (mState.action == ACTION_GET_CONTENT) { final Intent moreApps = new Intent(getIntent()); moreApps.setComponent(null); moreApps.setPackage(null); RootsFragment.show(getFragmentManager(), moreApps); } else if (mState.action == ACTION_OPEN || mState.action == ACTION_CREATE || mState.action == ACTION_OPEN_TREE) { RootsFragment.show(getFragmentManager(), null); } if (!mState.restored) { /// M: If show ad ACTION_MANAGE, hide default title to avoid show to user setTitle(null); if (mState.action == ACTION_MANAGE) { final Uri rootUri = getIntent().getData(); new RestoreRootTask(rootUri).executeOnExecutor(getCurrentExecutor()); } else { new RestoreStackTask().execute(); } } else { onCurrentDirectoryChanged(ANIM_NONE); } /// M: update action bar when roots are reloaded @{ mRoots.addCallback(mRootUpdatedListener); }