From 99900a0e586dc474df52a2f9dda1379170d7e669 Mon Sep 17 00:00:00 2001 From: Daichi Hirono Date: Tue, 19 Apr 2016 17:56:13 +0900 Subject: [PATCH] Terminate DocumentsUI opened for the specific root. If USB is opened in DocumentsUI from storage settings and format it from context menu, a user expects it goes back to storage settings after formatting the storage. However, previous we show Downloads as a result of redirect from the previous USB storage. The CL changes the logic for callback so that it finishes the activity if DocumentsUI is opened for the specific root and the root was removed while a user stays at the root. Fixes: 28246076 Change-Id: I5548152fc27fd13bd9b75b3083bcfbdd9f93509e --- .../src/com/android/documentsui/BaseActivity.java | 34 +++++++++++++--------- .../src/com/android/documentsui/RootsCache.java | 16 ---------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java index 44d7136258ae..50273afeceb6 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java +++ b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java @@ -32,6 +32,7 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ProviderInfo; +import android.database.ContentObserver; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; @@ -46,7 +47,6 @@ import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; -import android.view.WindowManager; import android.widget.Spinner; import com.android.documentsui.SearchViewManager.SearchManagerListener; @@ -78,6 +78,12 @@ public abstract class BaseActivity extends Activity List mEventListeners = new ArrayList<>(); private final String mTag; + private final ContentObserver mRootsCacheObserver = new ContentObserver(new Handler()) { + @Override + public void onChange(boolean selfChange) { + new HandleRootsChangedTask(BaseActivity.this).execute(getCurrentRoot()); + } + }; @LayoutRes private int mLayoutId; @@ -118,14 +124,8 @@ public abstract class BaseActivity extends Activity mRoots = DocumentsApplication.getRootsCache(this); - mRoots.setOnCacheUpdateListener( - new RootsCache.OnCacheUpdateListener() { - @Override - public void onCacheUpdate() { - new HandleRootsChangedTask(BaseActivity.this) - .execute(getCurrentRoot()); - } - }); + getContentResolver().registerContentObserver( + RootsCache.sNotificationUri, false, mRootsCacheObserver); mSearchManager = new SearchViewManager(this, icicle); @@ -190,7 +190,7 @@ public abstract class BaseActivity extends Activity @Override protected void onDestroy() { - mRoots.setOnCacheUpdateListener(null); + getContentResolver().unregisterContentObserver(mRootsCacheObserver); super.onDestroy(); } @@ -793,6 +793,7 @@ public abstract class BaseActivity extends Activity private static final class HandleRootsChangedTask extends PairedTask { + RootInfo mCurrentRoot; DocumentInfo mDefaultRootDocument; public HandleRootsChangedTask(BaseActivity activity) { @@ -802,11 +803,10 @@ public abstract class BaseActivity extends Activity @Override protected RootInfo run(RootInfo... roots) { assert(roots.length == 1); - - final RootInfo currentRoot = roots[0]; + mCurrentRoot = roots[0]; final Collection cachedRoots = mOwner.mRoots.getRootsBlocking(); for (final RootInfo root : cachedRoots) { - if (root.getUri().equals(currentRoot.getUri())) { + if (root.getUri().equals(mCurrentRoot.getUri())) { // We don't need to change the current root as the current root was not removed. return null; } @@ -827,6 +827,14 @@ public abstract class BaseActivity extends Activity return; } + // If the activity has been launched for the specific root and it is removed, finish the + // activity. + final Uri uri = mOwner.getIntent().getData(); + if (uri != null && uri.equals(mCurrentRoot.getUri())) { + mOwner.finish(); + return; + } + // Clear entire backstack and start in new root. mOwner.mState.onRootChanged(defaultRoot); mOwner.mSearchManager.update(defaultRoot); diff --git a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java index 3a04d9db6173..1eee0e72122b 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java +++ b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java @@ -67,7 +67,6 @@ public class RootsCache { private final Context mContext; private final ContentObserver mObserver; - private OnCacheUpdateListener mCacheUpdateListener; private final RootInfo mRecentsRoot; @@ -115,10 +114,6 @@ public class RootsCache { } } - static interface OnCacheUpdateListener { - void onCacheUpdate(); - } - /** * Gather roots from all known storage providers. */ @@ -276,13 +271,6 @@ 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. @@ -441,10 +429,6 @@ public class RootsCache { } } - public void setOnCacheUpdateListener(OnCacheUpdateListener cacheUpdateListener) { - mCacheUpdateListener = cacheUpdateListener; - } - /** * Returns the default root for the specified state. */ -- 2.11.0