| /* |
| * Copyright (C) 2013 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.documentsui; |
| |
| import static com.android.documentsui.State.ACTION_CREATE; |
| import static com.android.documentsui.State.ACTION_GET_CONTENT; |
| import static com.android.documentsui.State.ACTION_OPEN; |
| import static com.android.documentsui.State.ACTION_OPEN_TREE; |
| import static com.android.documentsui.State.ACTION_PICK_COPY_DESTINATION; |
| import static com.android.documentsui.dirlist.DirectoryFragment.ANIM_NONE; |
| |
| import android.app.Activity; |
| import android.app.Fragment; |
| import android.app.FragmentManager; |
| import android.content.ClipData; |
| import android.content.ComponentName; |
| import android.content.ContentProviderClient; |
| import android.content.ContentResolver; |
| import android.content.ContentValues; |
| import android.content.Intent; |
| import android.content.pm.ResolveInfo; |
| import android.content.res.Resources; |
| import android.net.Uri; |
| import android.os.AsyncTask; |
| import android.os.Bundle; |
| import android.os.Parcelable; |
| import android.provider.DocumentsContract; |
| import android.support.design.widget.Snackbar; |
| import android.util.Log; |
| import android.view.Menu; |
| import android.view.MenuItem; |
| import android.view.View; |
| import android.widget.BaseAdapter; |
| import android.widget.Spinner; |
| import android.widget.Toolbar; |
| |
| import com.android.documentsui.RecentsProvider.RecentColumns; |
| import com.android.documentsui.RecentsProvider.ResumeColumns; |
| import com.android.documentsui.dirlist.DirectoryFragment; |
| import com.android.documentsui.model.DocumentInfo; |
| import com.android.documentsui.model.DurableUtils; |
| import com.android.documentsui.model.RootInfo; |
| import com.android.documentsui.services.FileOperationService; |
| |
| import java.util.Arrays; |
| import java.util.List; |
| |
| public class DocumentsActivity extends BaseActivity { |
| private static final int CODE_FORWARD = 42; |
| private static final String TAG = "DocumentsActivity"; |
| |
| private Toolbar mToolbar; |
| private Spinner mToolbarStack; |
| |
| private Toolbar mRootsToolbar; |
| |
| private ItemSelectedListener mStackListener; |
| private BaseAdapter mStackAdapter; |
| |
| public DocumentsActivity() { |
| super(R.layout.docs_activity, TAG); |
| } |
| |
| @Override |
| public void onCreate(Bundle icicle) { |
| super.onCreate(icicle); |
| |
| final Resources res = getResources(); |
| |
| mDrawer = DrawerController.create(this); |
| mToolbar = (Toolbar) findViewById(R.id.toolbar); |
| |
| mStackAdapter = new StackAdapter(); |
| mStackListener = new ItemSelectedListener(); |
| mToolbarStack = (Spinner) findViewById(R.id.stack); |
| mToolbarStack.setOnItemSelectedListener(mStackListener); |
| |
| mRootsToolbar = (Toolbar) findViewById(R.id.roots_toolbar); |
| |
| setActionBar(mToolbar); |
| |
| 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 || |
| mState.action == ACTION_PICK_COPY_DESTINATION) { |
| 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 || |
| mState.action == ACTION_PICK_COPY_DESTINATION) { |
| RootsFragment.show(getFragmentManager(), null); |
| } |
| |
| if (!mState.restored) { |
| // In this case, we set the activity title in AsyncTask.onPostExecute(). To prevent |
| // talkback from reading aloud the default title, we clear it here. |
| setTitle(""); |
| new RestoreStackTask().execute(); |
| } else { |
| onCurrentDirectoryChanged(ANIM_NONE); |
| } |
| } |
| |
| @Override |
| State buildState() { |
| State state = buildDefaultState(); |
| |
| final Intent intent = getIntent(); |
| final String action = intent.getAction(); |
| if (Intent.ACTION_OPEN_DOCUMENT.equals(action)) { |
| state.action = ACTION_OPEN; |
| } else if (Intent.ACTION_CREATE_DOCUMENT.equals(action)) { |
| state.action = ACTION_CREATE; |
| } else if (Intent.ACTION_GET_CONTENT.equals(action)) { |
| state.action = ACTION_GET_CONTENT; |
| } else if (Intent.ACTION_OPEN_DOCUMENT_TREE.equals(action)) { |
| state.action = ACTION_OPEN_TREE; |
| } else if (Shared.ACTION_PICK_COPY_DESTINATION.equals(action)) { |
| state.action = ACTION_PICK_COPY_DESTINATION; |
| } |
| |
| if (state.action == ACTION_OPEN || state.action == ACTION_GET_CONTENT) { |
| state.allowMultiple = intent.getBooleanExtra( |
| Intent.EXTRA_ALLOW_MULTIPLE, false); |
| } |
| |
| if (state.action == ACTION_OPEN || state.action == ACTION_GET_CONTENT |
| || state.action == ACTION_CREATE) { |
| state.openableOnly = intent.hasCategory(Intent.CATEGORY_OPENABLE); |
| } |
| |
| if (state.action == ACTION_PICK_COPY_DESTINATION) { |
| state.directoryCopy = intent.getBooleanExtra( |
| Shared.EXTRA_DIRECTORY_COPY, false); |
| state.transferMode = intent.getIntExtra(FileOperationService.EXTRA_OPERATION, |
| FileOperationService.OPERATION_COPY); |
| } |
| |
| return state; |
| } |
| |
| @Override |
| void onStackRestored(boolean restored, boolean external) { |
| // Show drawer when no stack restored, but only when requesting |
| // non-visual content. However, if we last used an external app, |
| // drawer is always shown. |
| |
| boolean showDrawer = false; |
| if (!restored) { |
| showDrawer = true; |
| } |
| if (MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, mState.acceptMimes)) { |
| showDrawer = false; |
| } |
| if (external && mState.action == ACTION_GET_CONTENT) { |
| showDrawer = true; |
| } |
| |
| if (showDrawer) { |
| setRootsDrawerOpen(true); |
| } |
| } |
| |
| public void onAppPicked(ResolveInfo info) { |
| final Intent intent = new Intent(getIntent()); |
| intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_FORWARD_RESULT); |
| intent.setComponent(new ComponentName( |
| info.activityInfo.applicationInfo.packageName, info.activityInfo.name)); |
| startActivityForResult(intent, CODE_FORWARD); |
| } |
| |
| @Override |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| Log.d(TAG, "onActivityResult() code=" + resultCode); |
| |
| // Only relay back results when not canceled; otherwise stick around to |
| // let the user pick another app/backend. |
| if (requestCode == CODE_FORWARD && resultCode != RESULT_CANCELED) { |
| |
| // Remember that we last picked via external app |
| final String packageName = getCallingPackageMaybeExtra(); |
| final ContentValues values = new ContentValues(); |
| values.put(ResumeColumns.EXTERNAL, 1); |
| getContentResolver().insert(RecentsProvider.buildResume(packageName), values); |
| |
| // Pass back result to original caller |
| setResult(resultCode, data); |
| finish(); |
| } else { |
| super.onActivityResult(requestCode, resultCode, data); |
| } |
| } |
| |
| @Override |
| protected void onPostCreate(Bundle savedInstanceState) { |
| super.onPostCreate(savedInstanceState); |
| mDrawer.syncState(); |
| updateActionBar(); |
| } |
| |
| public void setRootsDrawerOpen(boolean open) { |
| mDrawer.setOpen(open); |
| } |
| |
| @Override |
| public void updateActionBar() { |
| if (mRootsToolbar != null) { |
| final String prompt = getIntent().getStringExtra(DocumentsContract.EXTRA_PROMPT); |
| if (prompt != null) { |
| mRootsToolbar.setTitle(prompt); |
| } else { |
| if (mState.action == ACTION_OPEN || |
| mState.action == ACTION_GET_CONTENT || |
| mState.action == ACTION_OPEN_TREE) { |
| mRootsToolbar.setTitle(R.string.title_open); |
| } else if (mState.action == ACTION_CREATE || |
| mState.action == ACTION_PICK_COPY_DESTINATION) { |
| mRootsToolbar.setTitle(R.string.title_save); |
| } |
| } |
| } |
| |
| if (mDrawer.isUnlocked()) { |
| mToolbar.setNavigationIcon(R.drawable.ic_hamburger); |
| mToolbar.setNavigationContentDescription(R.string.drawer_open); |
| mToolbar.setNavigationOnClickListener( |
| new View.OnClickListener() { |
| @Override |
| public void onClick(View v) { |
| setRootsDrawerOpen(true); |
| } |
| }); |
| } else { |
| mToolbar.setNavigationIcon(null); |
| mToolbar.setNavigationContentDescription(R.string.drawer_open); |
| mToolbar.setNavigationOnClickListener(null); |
| } |
| |
| if (mSearchManager.isExpanded()) { |
| mToolbar.setTitle(null); |
| mToolbarStack.setVisibility(View.GONE); |
| mToolbarStack.setAdapter(null); |
| } else { |
| if (mState.stack.size() <= 1) { |
| mToolbar.setTitle(getCurrentRoot().title); |
| mToolbarStack.setVisibility(View.GONE); |
| mToolbarStack.setAdapter(null); |
| } else { |
| mToolbar.setTitle(null); |
| mToolbarStack.setVisibility(View.VISIBLE); |
| mToolbarStack.setAdapter(mStackAdapter); |
| |
| mStackListener.mIgnoreNextNavigation = true; |
| mToolbarStack.setSelection(mStackAdapter.getCount() - 1); |
| } |
| } |
| } |
| |
| @Override |
| public boolean onCreateOptionsMenu(Menu menu) { |
| boolean showMenu = super.onCreateOptionsMenu(menu); |
| |
| expandMenus(menu); |
| return showMenu; |
| } |
| |
| @Override |
| public boolean onPrepareOptionsMenu(Menu menu) { |
| super.onPrepareOptionsMenu(menu); |
| |
| final DocumentInfo cwd = getCurrentDirectory(); |
| |
| final MenuItem createDir = menu.findItem(R.id.menu_create_dir); |
| final MenuItem grid = menu.findItem(R.id.menu_grid); |
| final MenuItem list = menu.findItem(R.id.menu_list); |
| final MenuItem fileSize = menu.findItem(R.id.menu_file_size); |
| final MenuItem settings = menu.findItem(R.id.menu_settings); |
| |
| boolean recents = cwd == null; |
| boolean picking = mState.action == ACTION_CREATE |
| || mState.action == ACTION_OPEN_TREE |
| || mState.action == ACTION_PICK_COPY_DESTINATION; |
| |
| createDir.setVisible(picking && !recents && cwd.isCreateSupported()); |
| mSearchManager.showMenu(!picking); |
| |
| // No display options in recent directories |
| if (picking && recents) { |
| grid.setVisible(false); |
| list.setVisible(false); |
| } |
| |
| fileSize.setVisible(fileSize.isVisible() && !picking); |
| settings.setVisible(false); |
| |
| if (mState.action == ACTION_CREATE) { |
| final FragmentManager fm = getFragmentManager(); |
| SaveFragment.get(fm).prepareForDirectory(cwd); |
| } |
| |
| Menus.disableHiddenItems(menu); |
| |
| return true; |
| } |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| return mDrawer.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); |
| } |
| |
| @Override |
| void onDirectoryChanged(int anim) { |
| final FragmentManager fm = getFragmentManager(); |
| final RootInfo root = getCurrentRoot(); |
| final DocumentInfo cwd = getCurrentDirectory(); |
| |
| if (cwd == null) { |
| // No directory means recents |
| if (mState.action == ACTION_CREATE || |
| mState.action == ACTION_OPEN_TREE || |
| mState.action == ACTION_PICK_COPY_DESTINATION) { |
| RecentsCreateFragment.show(fm); |
| } else { |
| DirectoryFragment.showRecentsOpen(fm, anim); |
| |
| // Start recents in grid when requesting visual things |
| final boolean visualMimes = MimePredicate.mimeMatches( |
| MimePredicate.VISUAL_MIMES, mState.acceptMimes); |
| mState.userMode = visualMimes ? State.MODE_GRID : State.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 || |
| mState.action == ACTION_PICK_COPY_DESTINATION) { |
| final PickFragment pick = PickFragment.get(fm); |
| if (pick != null) { |
| pick.setPickTarget(mState.action, mState.transferMode, cwd); |
| } |
| } |
| } |
| |
| void onSaveRequested(DocumentInfo replaceTarget) { |
| new ExistingFinishTask(replaceTarget.derivedUri).executeOnExecutor(getExecutorForCurrentDirectory()); |
| } |
| |
| void onSaveRequested(String mimeType, String displayName) { |
| new CreateFinishTask(mimeType, displayName).executeOnExecutor(getExecutorForCurrentDirectory()); |
| } |
| |
| @Override |
| void onRootPicked(RootInfo root) { |
| super.onRootPicked(root); |
| setRootsDrawerOpen(false); |
| } |
| |
| @Override |
| public void onDocumentPicked(DocumentInfo doc, SiblingProvider siblings) { |
| final FragmentManager fm = getFragmentManager(); |
| if (doc.isContainer()) { |
| openContainerDocument(doc); |
| } else if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) { |
| // Explicit file picked, return |
| new ExistingFinishTask(doc.derivedUri).executeOnExecutor(getExecutorForCurrentDirectory()); |
| } else if (mState.action == ACTION_CREATE) { |
| // Replace selected file |
| SaveFragment.get(fm).setReplaceTarget(doc); |
| } |
| } |
| |
| @Override |
| public void onDocumentsPicked(List<DocumentInfo> docs) { |
| if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) { |
| final int size = docs.size(); |
| final Uri[] uris = new Uri[size]; |
| for (int i = 0; i < size; i++) { |
| uris[i] = docs.get(i).derivedUri; |
| } |
| new ExistingFinishTask(uris).executeOnExecutor(getExecutorForCurrentDirectory()); |
| } |
| } |
| |
| public void onPickRequested(DocumentInfo pickTarget) { |
| Uri result; |
| if (mState.action == ACTION_OPEN_TREE) { |
| result = DocumentsContract.buildTreeDocumentUri( |
| pickTarget.authority, pickTarget.documentId); |
| } else if (mState.action == ACTION_PICK_COPY_DESTINATION) { |
| result = pickTarget.derivedUri; |
| } else { |
| // Should not be reached. |
| throw new IllegalStateException("Invalid mState.action."); |
| } |
| new PickFinishTask(result).executeOnExecutor(getExecutorForCurrentDirectory()); |
| } |
| |
| @Override |
| void saveStackBlocking() { |
| final ContentResolver resolver = getContentResolver(); |
| final ContentValues values = new ContentValues(); |
| |
| final byte[] rawStack = DurableUtils.writeToArrayOrNull(mState.stack); |
| if (mState.action == ACTION_CREATE || |
| mState.action == ACTION_OPEN_TREE || |
| mState.action == ACTION_PICK_COPY_DESTINATION) { |
| // Remember stack for last create |
| values.clear(); |
| values.put(RecentColumns.KEY, mState.stack.buildKey()); |
| values.put(RecentColumns.STACK, rawStack); |
| resolver.insert(RecentsProvider.buildRecent(), values); |
| } |
| |
| // Remember location for next app launch |
| final String packageName = getCallingPackageMaybeExtra(); |
| values.clear(); |
| values.put(ResumeColumns.STACK, rawStack); |
| values.put(ResumeColumns.EXTERNAL, 0); |
| resolver.insert(RecentsProvider.buildResume(packageName), values); |
| } |
| |
| @Override |
| void onTaskFinished(Uri... uris) { |
| Log.d(TAG, "onFinished() " + Arrays.toString(uris)); |
| |
| final Intent intent = new Intent(); |
| if (uris.length == 1) { |
| intent.setData(uris[0]); |
| } else if (uris.length > 1) { |
| final ClipData clipData = new ClipData( |
| null, mState.acceptMimes, new ClipData.Item(uris[0])); |
| for (int i = 1; i < uris.length; i++) { |
| clipData.addItem(new ClipData.Item(uris[i])); |
| } |
| intent.setClipData(clipData); |
| } |
| |
| if (mState.action == ACTION_GET_CONTENT) { |
| intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| } else if (mState.action == ACTION_OPEN_TREE) { |
| intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); |
| } else if (mState.action == ACTION_PICK_COPY_DESTINATION) { |
| // Picking a copy destination is only used internally by us, so we |
| // don't need to extend permissions to the caller. |
| intent.putExtra(Shared.EXTRA_STACK, (Parcelable) mState.stack); |
| intent.putExtra(FileOperationService.EXTRA_OPERATION, mState.transferMode); |
| } else { |
| intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
| } |
| |
| setResult(Activity.RESULT_OK, intent); |
| finish(); |
| } |
| |
| public static DocumentsActivity get(Fragment fragment) { |
| return (DocumentsActivity) fragment.getActivity(); |
| } |
| |
| private final class PickFinishTask extends AsyncTask<Void, Void, Void> { |
| private final Uri mUri; |
| |
| public PickFinishTask(Uri uri) { |
| mUri = uri; |
| } |
| |
| @Override |
| protected Void doInBackground(Void... params) { |
| saveStackBlocking(); |
| return null; |
| } |
| |
| @Override |
| protected void onPostExecute(Void result) { |
| onTaskFinished(mUri); |
| } |
| } |
| |
| final class ExistingFinishTask extends AsyncTask<Void, Void, Void> { |
| private final Uri[] mUris; |
| |
| public ExistingFinishTask(Uri... uris) { |
| mUris = uris; |
| } |
| |
| @Override |
| protected Void doInBackground(Void... params) { |
| saveStackBlocking(); |
| return null; |
| } |
| |
| @Override |
| protected void onPostExecute(Void result) { |
| onTaskFinished(mUris); |
| } |
| } |
| |
| /** |
| * Task that creates a new document in the background. |
| */ |
| final class CreateFinishTask extends AsyncTask<Void, Void, Uri> { |
| private final String mMimeType; |
| private final String mDisplayName; |
| |
| public CreateFinishTask(String mimeType, String displayName) { |
| mMimeType = mimeType; |
| mDisplayName = displayName; |
| } |
| |
| @Override |
| protected void onPreExecute() { |
| setPending(true); |
| } |
| |
| @Override |
| protected Uri doInBackground(Void... params) { |
| final ContentResolver resolver = getContentResolver(); |
| final DocumentInfo cwd = getCurrentDirectory(); |
| |
| ContentProviderClient client = null; |
| Uri childUri = null; |
| try { |
| client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| resolver, cwd.derivedUri.getAuthority()); |
| childUri = DocumentsContract.createDocument( |
| client, cwd.derivedUri, mMimeType, mDisplayName); |
| } catch (Exception e) { |
| Log.w(TAG, "Failed to create document", e); |
| } finally { |
| ContentProviderClient.releaseQuietly(client); |
| } |
| |
| if (childUri != null) { |
| saveStackBlocking(); |
| } |
| |
| return childUri; |
| } |
| |
| @Override |
| protected void onPostExecute(Uri result) { |
| if (result != null) { |
| onTaskFinished(result); |
| } else { |
| Snackbars.makeSnackbar( |
| DocumentsActivity.this, R.string.save_error, Snackbar.LENGTH_SHORT).show(); |
| } |
| |
| setPending(false); |
| } |
| } |
| } |