summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Daichi Hirono <hirono@google.com> 2016-01-07 01:18:34 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2016-01-07 01:18:34 +0000
commit1830c2aa55c1565f8075dad39f7aafcbc2a63f6a (patch)
treef632ad4078c1bdd7ca30f4ca8acc7e82d42e2918
parent873b382cfa20ad177d41b275c314330e30191ab7 (diff)
parent60e9a075579a3f5c7713bff6015b4021e33410a4 (diff)
Merge "Leave from the current root when the root is removed."
-rw-r--r--packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java63
-rw-r--r--packages/DocumentsUI/src/com/android/documentsui/RootsCache.java16
2 files changed, 75 insertions, 4 deletions
diff --git a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java
index 9c0a04ca751c..a2416674f5de 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java
@@ -39,7 +39,6 @@ import android.provider.DocumentsContract.Root;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.util.Log;
-import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@@ -60,6 +59,7 @@ import com.android.documentsui.model.DocumentInfo;
import com.android.documentsui.model.DocumentStack;
import com.android.documentsui.model.DurableUtils;
import com.android.documentsui.model.RootInfo;
+import com.android.internal.util.Preconditions;
import libcore.io.IoUtils;
@@ -111,6 +111,13 @@ public abstract class BaseActivity extends Activity {
setContentView(mLayoutId);
mRoots = DocumentsApplication.getRootsCache(this);
+ mRoots.setOnCacheUpdateListener(
+ new RootsCache.OnCacheUpdateListener() {
+ @Override
+ public void onCacheUpdate() {
+ new HandleRootsChangedTask().execute(getCurrentRoot());
+ }
+ });
mDirectoryContainer = (DirectoryContainerView) findViewById(R.id.container_directory);
mSearchManager = new SearchManager();
@@ -203,7 +210,25 @@ public abstract class BaseActivity extends Activity {
if (mRoots.isRecentsRoot(root)) {
onCurrentDirectoryChanged(ANIM_SIDE);
} else {
- new PickRootTask(root).executeOnExecutor(getExecutorForCurrentDirectory());
+ new PickRootTask(root, true).executeOnExecutor(getExecutorForCurrentDirectory());
+ }
+ }
+
+ void setRoot(RootInfo root) {
+ // Clear entire backstack and start in new root
+ mState.stack.root = root;
+ mState.stack.clear();
+ mState.stackTouched = false;
+
+ mSearchManager.update(root);
+
+ // Recents is always in memory, so we just load it directly.
+ // Otherwise we delegate loading data from disk to a task
+ // to ensure a responsive ui.
+ if (mRoots.isRecentsRoot(root)) {
+ onCurrentDirectoryChanged(ANIM_SIDE);
+ } else {
+ new PickRootTask(root, false).executeOnExecutor(getExecutorForCurrentDirectory());
}
}
@@ -483,9 +508,11 @@ public abstract class BaseActivity extends Activity {
final class PickRootTask extends AsyncTask<Void, Void, DocumentInfo> {
private RootInfo mRoot;
+ private boolean mTouched;
- public PickRootTask(RootInfo root) {
+ public PickRootTask(RootInfo root, boolean touched) {
mRoot = root;
+ mTouched = touched;
}
@Override
@@ -504,7 +531,7 @@ public abstract class BaseActivity extends Activity {
protected void onPostExecute(DocumentInfo result) {
if (result != null) {
mState.stack.push(result);
- mState.stackTouched = true;
+ mState.stackTouched = mTouched;
onCurrentDirectoryChanged(ANIM_SIDE);
}
}
@@ -591,6 +618,34 @@ public abstract class BaseActivity extends Activity {
}
}
+ final class HandleRootsChangedTask extends AsyncTask<RootInfo, Void, RootInfo> {
+ @Override
+ protected RootInfo doInBackground(RootInfo... roots) {
+ Preconditions.checkArgument(roots.length == 1);
+ final RootInfo currentRoot = roots[0];
+ final Collection<RootInfo> cachedRoots = mRoots.getRootsBlocking();
+ RootInfo homeRoot = null;
+ for (final RootInfo root : cachedRoots) {
+ if (root.isHome()) {
+ homeRoot = root;
+ }
+ if (root.getUri().equals(currentRoot.getUri())) {
+ // We don't need to change the current root as the current root was not removed.
+ return null;
+ }
+ }
+ Preconditions.checkNotNull(homeRoot);
+ return homeRoot;
+ }
+
+ @Override
+ protected void onPostExecute(RootInfo result) {
+ if (result != null) {
+ setRoot(result);
+ }
+ }
+ }
+
final class ItemSelectedListener implements OnItemSelectedListener {
boolean mIgnoreNextNavigation;
diff --git a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java
index 72ee6cbab5fd..21e756623bdd 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java
@@ -63,6 +63,7 @@ public class RootsCache {
private final Context mContext;
private final ContentObserver mObserver;
+ private OnCacheUpdateListener mCacheUpdateListener;
private final RootInfo mRecentsRoot = new RootInfo();
@@ -94,6 +95,10 @@ public class RootsCache {
}
}
+ static interface OnCacheUpdateListener {
+ void onCacheUpdate();
+ }
+
/**
* Gather roots from all known storage providers.
*/
@@ -209,6 +214,13 @@ public class RootsCache {
return null;
}
+ @Override
+ protected void onPostExecute(Void result) {
+ if (mCacheUpdateListener != null) {
+ mCacheUpdateListener.onCacheUpdate();
+ }
+ }
+
private void handleDocumentsProvider(ProviderInfo info) {
// Ignore stopped packages for now; we might query them
// later during UI interaction.
@@ -348,6 +360,10 @@ public class RootsCache {
}
}
+ public void setOnCacheUpdateListener(OnCacheUpdateListener cacheUpdateListener) {
+ mCacheUpdateListener = cacheUpdateListener;
+ }
+
@VisibleForTesting
static List<RootInfo> getMatchingRoots(Collection<RootInfo> roots, State state) {
final List<RootInfo> matching = new ArrayList<>();