blob: d4354b4cbc094cae8e59a7b5856a75e9fc9a1ac6 [file] [log] [blame]
Garfield Tan63bf8132016-10-11 11:00:49 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
19import android.app.Activity;
Garfield Tanbe1e0792017-02-24 17:14:53 -080020import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Garfield Tan63bf8132016-10-11 11:00:49 -070024import android.net.Uri;
Zemiao Zhuc85758f2020-03-03 17:02:21 -080025
KOUSHIK PANUGANTI6ca7acc2018-04-17 16:00:10 -070026import androidx.localbroadcastmanager.content.LocalBroadcastManager;
Garfield Tan63bf8132016-10-11 11:00:49 -070027
28import com.android.documentsui.AbstractActionHandler.CommonAddons;
29import com.android.documentsui.base.DocumentInfo;
30import com.android.documentsui.base.PairedTask;
31import com.android.documentsui.base.RootInfo;
32import com.android.documentsui.base.State;
33import com.android.documentsui.dirlist.AnimationView;
Steve McKay3a268232016-10-19 11:15:47 -070034import com.android.documentsui.queries.SearchViewManager;
Jon Mann9bd40992017-03-24 12:34:34 -070035import com.android.documentsui.roots.ProvidersAccess;
Garfield Tan63bf8132016-10-11 11:00:49 -070036
37import java.util.Collection;
38
39/**
40 * Monitors roots change and refresh the page when necessary.
41 */
42final class RootsMonitor<T extends Activity & CommonAddons> {
43
Garfield Tanbe1e0792017-02-24 17:14:53 -080044 private final LocalBroadcastManager mManager;
45 private final BroadcastReceiver mReceiver;
Garfield Tan63bf8132016-10-11 11:00:49 -070046
47 RootsMonitor(
48 final T activity,
49 final ActionHandler actions,
Jon Mann9bd40992017-03-24 12:34:34 -070050 final ProvidersAccess providers,
Garfield Tan63bf8132016-10-11 11:00:49 -070051 final DocumentsAccess docs,
52 final State state,
Garfield Tan80b52b32017-04-25 15:50:52 -070053 final SearchViewManager searchMgr,
54 final Runnable actionModeFinisher) {
Garfield Tanbe1e0792017-02-24 17:14:53 -080055 mManager = LocalBroadcastManager.getInstance(activity);
Garfield Tan63bf8132016-10-11 11:00:49 -070056
Garfield Tanbe1e0792017-02-24 17:14:53 -080057 mReceiver = new BroadcastReceiver() {
Garfield Tan63bf8132016-10-11 11:00:49 -070058 @Override
Garfield Tanbe1e0792017-02-24 17:14:53 -080059 public void onReceive(Context context, Intent intent) {
Steve McKay2d19a692017-08-22 09:05:08 -070060 new HandleRootsChangedTask<>(
Garfield Tan63bf8132016-10-11 11:00:49 -070061 activity,
62 actions,
Jon Mann9bd40992017-03-24 12:34:34 -070063 providers,
Garfield Tan63bf8132016-10-11 11:00:49 -070064 docs,
65 state,
Garfield Tan80b52b32017-04-25 15:50:52 -070066 searchMgr,
67 actionModeFinisher).execute(activity.getCurrentRoot());
Garfield Tan63bf8132016-10-11 11:00:49 -070068 }
69 };
70 }
71
72 void start() {
Jon Mann9bd40992017-03-24 12:34:34 -070073 mManager.registerReceiver(mReceiver, new IntentFilter(ProvidersAccess.BROADCAST_ACTION));
Garfield Tan63bf8132016-10-11 11:00:49 -070074 }
75
76 void stop() {
Garfield Tanbe1e0792017-02-24 17:14:53 -080077 mManager.unregisterReceiver(mReceiver);
Garfield Tan63bf8132016-10-11 11:00:49 -070078 }
79
80 private static class HandleRootsChangedTask<T extends Activity & CommonAddons>
81 extends PairedTask<T, RootInfo, RootInfo> {
82 private final ActionHandler mActions;
Jon Mann9bd40992017-03-24 12:34:34 -070083 private final ProvidersAccess mProviders;
Garfield Tan63bf8132016-10-11 11:00:49 -070084 private final DocumentsAccess mDocs;
85 private final State mState;
86 private final SearchViewManager mSearchMgr;
Garfield Tan80b52b32017-04-25 15:50:52 -070087 private final Runnable mActionModeFinisher;
Garfield Tan63bf8132016-10-11 11:00:49 -070088
89 private RootInfo mCurrentRoot;
90 private DocumentInfo mDefaultRootDocument;
91
92 private HandleRootsChangedTask(
93 T activity,
94 ActionHandler actions,
Jon Mann9bd40992017-03-24 12:34:34 -070095 ProvidersAccess providers,
Garfield Tan63bf8132016-10-11 11:00:49 -070096 DocumentsAccess docs,
97 State state,
Garfield Tan80b52b32017-04-25 15:50:52 -070098 SearchViewManager searchMgr,
99 Runnable actionModeFinisher) {
Garfield Tan63bf8132016-10-11 11:00:49 -0700100 super(activity);
101 mActions = actions;
Jon Mann9bd40992017-03-24 12:34:34 -0700102 mProviders = providers;
Garfield Tan63bf8132016-10-11 11:00:49 -0700103 mDocs = docs;
104 mState = state;
105 mSearchMgr = searchMgr;
Garfield Tan80b52b32017-04-25 15:50:52 -0700106 mActionModeFinisher = actionModeFinisher;
Garfield Tan63bf8132016-10-11 11:00:49 -0700107 }
108
109 @Override
110 protected RootInfo run(RootInfo... roots) {
111 assert (roots.length == 1);
112 mCurrentRoot = roots[0];
Jon Mann9bd40992017-03-24 12:34:34 -0700113 final Collection<RootInfo> cachedRoots = mProviders.getRootsBlocking();
Garfield Tan63bf8132016-10-11 11:00:49 -0700114 for (final RootInfo root : cachedRoots) {
115 if (root.getUri().equals(mCurrentRoot.getUri())) {
116 // We don't need to change the current root as the current root was not removed.
117 return null;
118 }
119 }
120
121 // Choose the default root.
Jon Mann9bd40992017-03-24 12:34:34 -0700122 final RootInfo defaultRoot = mProviders.getDefaultRootBlocking(mState);
Garfield Tan63bf8132016-10-11 11:00:49 -0700123 assert (defaultRoot != null);
124 if (!defaultRoot.isRecents()) {
125 mDefaultRootDocument = mDocs.getRootDocument(defaultRoot);
126 }
127 return defaultRoot;
128 }
129
130 @Override
131 protected void finish(RootInfo defaultRoot) {
132 if (defaultRoot == null) {
133 return;
134 }
135
136 // If the activity has been launched for the specific root and it is removed, finish the
137 // activity.
138 final Uri uri = mOwner.getIntent().getData();
139 if (uri != null && uri.equals(mCurrentRoot.getUri())) {
Zemiao Zhuc85758f2020-03-03 17:02:21 -0800140 mOwner.finishAndRemoveTask();
Garfield Tan63bf8132016-10-11 11:00:49 -0700141 return;
142 }
143
Garfield Tan80b52b32017-04-25 15:50:52 -0700144 // Clean action mode before changing root.
145 mActionModeFinisher.run();
146
Garfield Tan63bf8132016-10-11 11:00:49 -0700147 // Clear entire backstack and start in new root.
Garfield Tan2a837422016-10-19 11:50:45 -0700148 mState.stack.changeRoot(defaultRoot);
Tomasz Mikolajewski9e047852017-02-13 14:05:17 +0900149 mSearchMgr.update(mState.stack);
Garfield Tan63bf8132016-10-11 11:00:49 -0700150
151 if (defaultRoot.isRecents()) {
152 mOwner.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
153 } else {
154 mActions.openContainerDocument(mDefaultRootDocument);
155 }
156 }
157 }
158}