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;
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;
List<EventListener> 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;
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);
@Override
protected void onDestroy() {
- mRoots.setOnCacheUpdateListener(null);
+ getContentResolver().unregisterContentObserver(mRootsCacheObserver);
super.onDestroy();
}
private static final class HandleRootsChangedTask
extends PairedTask<BaseActivity, RootInfo, RootInfo> {
+ RootInfo mCurrentRoot;
DocumentInfo mDefaultRootDocument;
public HandleRootsChangedTask(BaseActivity activity) {
@Override
protected RootInfo run(RootInfo... roots) {
assert(roots.length == 1);
-
- final RootInfo currentRoot = roots[0];
+ mCurrentRoot = roots[0];
final Collection<RootInfo> 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;
}
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);
private final Context mContext;
private final ContentObserver mObserver;
- private OnCacheUpdateListener mCacheUpdateListener;
private final RootInfo mRecentsRoot;
}
}
- static interface OnCacheUpdateListener {
- void onCacheUpdate();
- }
-
/**
* Gather roots from all known storage providers.
*/
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.
}
}
- public void setOnCacheUpdateListener(OnCacheUpdateListener cacheUpdateListener) {
- mCacheUpdateListener = cacheUpdateListener;
- }
-
/**
* Returns the default root for the specified state.
*/