• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.quickstep;
17 
18 import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
19 import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
20 
21 import static com.android.launcher3.QuickstepAppTransitionManagerImpl.RECENTS_LAUNCH_DURATION;
22 import static com.android.launcher3.QuickstepAppTransitionManagerImpl.STATUS_BAR_TRANSITION_DURATION;
23 import static com.android.launcher3.QuickstepAppTransitionManagerImpl.STATUS_BAR_TRANSITION_PRE_DELAY;
24 import static com.android.launcher3.testing.TestProtocol.OVERVIEW_STATE_ORDINAL;
25 import static com.android.quickstep.TaskUtils.taskIsATargetWithMode;
26 import static com.android.quickstep.TaskViewUtils.createRecentsWindowAnimator;
27 import static com.android.systemui.shared.system.RemoteAnimationTargetCompat.MODE_CLOSING;
28 
29 import android.animation.Animator;
30 import android.animation.AnimatorListenerAdapter;
31 import android.animation.AnimatorSet;
32 import android.app.ActivityOptions;
33 import android.content.Intent;
34 import android.content.res.Configuration;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.os.Looper;
38 import android.view.View;
39 
40 import com.android.launcher3.AbstractFloatingView;
41 import com.android.launcher3.DeviceProfile;
42 import com.android.launcher3.InvariantDeviceProfile;
43 import com.android.launcher3.LauncherAnimationRunner;
44 import com.android.launcher3.R;
45 import com.android.launcher3.anim.Interpolators;
46 import com.android.launcher3.anim.PendingAnimation;
47 import com.android.launcher3.compat.AccessibilityManagerCompat;
48 import com.android.launcher3.statemanager.StateManager;
49 import com.android.launcher3.statemanager.StateManager.AtomicAnimationFactory;
50 import com.android.launcher3.statemanager.StateManager.StateHandler;
51 import com.android.launcher3.statemanager.StatefulActivity;
52 import com.android.launcher3.util.ActivityTracker;
53 import com.android.launcher3.util.SystemUiController;
54 import com.android.launcher3.util.Themes;
55 import com.android.launcher3.views.BaseDragLayer;
56 import com.android.quickstep.fallback.FallbackRecentsStateController;
57 import com.android.quickstep.fallback.FallbackRecentsView;
58 import com.android.quickstep.fallback.RecentsDragLayer;
59 import com.android.quickstep.fallback.RecentsState;
60 import com.android.quickstep.util.RecentsAtomicAnimationFactory;
61 import com.android.quickstep.views.OverviewActionsView;
62 import com.android.quickstep.views.TaskView;
63 import com.android.systemui.shared.system.ActivityOptionsCompat;
64 import com.android.systemui.shared.system.RemoteAnimationAdapterCompat;
65 import com.android.systemui.shared.system.RemoteAnimationRunnerCompat;
66 import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
67 
68 import java.io.FileDescriptor;
69 import java.io.PrintWriter;
70 
71 /**
72  * A recents activity that shows the recently launched tasks as swipable task cards.
73  * See {@link com.android.quickstep.views.RecentsView}.
74  */
75 public final class RecentsActivity extends StatefulActivity<RecentsState> {
76 
77     public static final ActivityTracker<RecentsActivity> ACTIVITY_TRACKER =
78             new ActivityTracker<>();
79 
80     private Handler mUiHandler = new Handler(Looper.getMainLooper());
81 
82     private RecentsDragLayer mDragLayer;
83     private FallbackRecentsView mFallbackRecentsView;
84     private OverviewActionsView mActionsView;
85 
86     private Configuration mOldConfig;
87 
88     private StateManager<RecentsState> mStateManager;
89 
90     /**
91      * Init drag layer and overview panel views.
92      */
setupViews()93     protected void setupViews() {
94         inflateRootView(R.layout.fallback_recents_activity);
95         setContentView(getRootView());
96         mDragLayer = findViewById(R.id.drag_layer);
97         mFallbackRecentsView = findViewById(R.id.overview_panel);
98         mActionsView = findViewById(R.id.overview_actions_view);
99 
100         mDragLayer.recreateControllers();
101         mFallbackRecentsView.init(mActionsView);
102     }
103 
104     @Override
onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig)105     public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
106         onHandleConfigChanged();
107         super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
108     }
109 
110     @Override
onNewIntent(Intent intent)111     protected void onNewIntent(Intent intent) {
112         super.onNewIntent(intent);
113         ACTIVITY_TRACKER.handleNewIntent(this, intent);
114     }
115 
116     /**
117          * Logic for when device configuration changes (rotation, screen size change, multi-window,
118          * etc.)
119          */
onHandleConfigChanged()120     protected void onHandleConfigChanged() {
121         mUserEventDispatcher = null;
122         initDeviceProfile();
123 
124         AbstractFloatingView.closeOpenViews(this, true,
125                 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
126         dispatchDeviceProfileChanged();
127 
128         reapplyUi();
129         mDragLayer.recreateControllers();
130     }
131 
132     /**
133      * Generate the device profile to use in this activity.
134      * @return device profile
135      */
createDeviceProfile()136     protected DeviceProfile createDeviceProfile() {
137         DeviceProfile dp = InvariantDeviceProfile.INSTANCE.get(this).getDeviceProfile(this);
138 
139         // In case we are reusing IDP, create a copy so that we don't conflict with Launcher
140         // activity.
141         return (mDragLayer != null) && isInMultiWindowMode()
142                 ? dp.getMultiWindowProfile(this, getMultiWindowDisplaySize())
143                 : dp.copy(this);
144     }
145 
146     @Override
getDragLayer()147     public BaseDragLayer getDragLayer() {
148         return mDragLayer;
149     }
150 
151     @Override
getOverviewPanel()152     public <T extends View> T getOverviewPanel() {
153         return (T) mFallbackRecentsView;
154     }
155 
getActionsView()156     public OverviewActionsView getActionsView() {
157         return mActionsView;
158     }
159 
160     @Override
returnToHomescreen()161     public void returnToHomescreen() {
162         super.returnToHomescreen();
163         // TODO(b/137318995) This should go home, but doing so removes freeform windows
164     }
165 
166     @Override
getActivityLaunchOptions(final View v)167     public ActivityOptions getActivityLaunchOptions(final View v) {
168         if (!(v instanceof TaskView)) {
169             return null;
170         }
171 
172         final TaskView taskView = (TaskView) v;
173         RemoteAnimationRunnerCompat runner = new LauncherAnimationRunner(mUiHandler,
174                 true /* startAtFrontOfQueue */) {
175 
176             @Override
177             public void onCreateAnimation(RemoteAnimationTargetCompat[] appTargets,
178                     RemoteAnimationTargetCompat[] wallpaperTargets, AnimationResult result) {
179                 AnimatorSet anim = composeRecentsLaunchAnimator(taskView, appTargets,
180                         wallpaperTargets);
181                 anim.addListener(resetStateListener());
182                 result.setAnimation(anim, RecentsActivity.this);
183             }
184         };
185         return ActivityOptionsCompat.makeRemoteAnimation(new RemoteAnimationAdapterCompat(
186                 runner, RECENTS_LAUNCH_DURATION,
187                 RECENTS_LAUNCH_DURATION - STATUS_BAR_TRANSITION_DURATION
188                         - STATUS_BAR_TRANSITION_PRE_DELAY));
189     }
190 
191     /**
192      * Composes the animations for a launch from the recents list if possible.
193      */
composeRecentsLaunchAnimator(TaskView taskView, RemoteAnimationTargetCompat[] appTargets, RemoteAnimationTargetCompat[] wallpaperTargets)194     private AnimatorSet composeRecentsLaunchAnimator(TaskView taskView,
195             RemoteAnimationTargetCompat[] appTargets,
196             RemoteAnimationTargetCompat[] wallpaperTargets) {
197         AnimatorSet target = new AnimatorSet();
198         boolean activityClosing = taskIsATargetWithMode(appTargets, getTaskId(), MODE_CLOSING);
199         PendingAnimation pa = new PendingAnimation(RECENTS_LAUNCH_DURATION);
200         createRecentsWindowAnimator(taskView, !activityClosing, appTargets,
201                 wallpaperTargets, null /* depthController */, pa);
202         target.play(pa.buildAnim());
203 
204         // Found a visible recents task that matches the opening app, lets launch the app from there
205         if (activityClosing) {
206             Animator adjacentAnimation = mFallbackRecentsView
207                     .createAdjacentPageAnimForTaskLaunch(taskView);
208             adjacentAnimation.setInterpolator(Interpolators.TOUCH_RESPONSE_INTERPOLATOR);
209             adjacentAnimation.setDuration(RECENTS_LAUNCH_DURATION);
210             adjacentAnimation.addListener(resetStateListener());
211             target.play(adjacentAnimation);
212         }
213         return target;
214     }
215 
216     @Override
onStart()217     protected void onStart() {
218         // Set the alpha to 1 before calling super, as it may get set back to 0 due to
219         // onActivityStart callback.
220         mFallbackRecentsView.setContentAlpha(1);
221         super.onStart();
222     }
223 
224     @Override
onStop()225     protected void onStop() {
226         super.onStop();
227 
228         // Workaround for b/78520668, explicitly trim memory once UI is hidden
229         onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
230     }
231 
232     @Override
onResume()233     protected void onResume() {
234         super.onResume();
235         AccessibilityManagerCompat.sendStateEventToTest(getBaseContext(), OVERVIEW_STATE_ORDINAL);
236     }
237 
238     @Override
onCreate(Bundle savedInstanceState)239     protected void onCreate(Bundle savedInstanceState) {
240         super.onCreate(savedInstanceState);
241 
242         mStateManager = new StateManager<>(this, RecentsState.DEFAULT);
243 
244         mOldConfig = new Configuration(getResources().getConfiguration());
245         initDeviceProfile();
246         setupViews();
247 
248         getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
249                 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
250         ACTIVITY_TRACKER.handleCreate(this);
251     }
252 
253     @Override
onConfigurationChanged(Configuration newConfig)254     public void onConfigurationChanged(Configuration newConfig) {
255         int diff = newConfig.diff(mOldConfig);
256         if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
257             onHandleConfigChanged();
258         }
259         mOldConfig.setTo(newConfig);
260         super.onConfigurationChanged(newConfig);
261     }
262 
263     /**
264      * Initialize/update the device profile.
265      */
initDeviceProfile()266     private void initDeviceProfile() {
267         mDeviceProfile = createDeviceProfile();
268         onDeviceProfileInitiated();
269     }
270 
271     @Override
onEnterAnimationComplete()272     public void onEnterAnimationComplete() {
273         super.onEnterAnimationComplete();
274         // After the transition to home, enable the high-res thumbnail loader if it wasn't enabled
275         // as a part of quickstep, so that high-res thumbnails can load the next time we enter
276         // overview
277         RecentsModel.INSTANCE.get(this).getThumbnailCache()
278                 .getHighResLoadingState().setVisible(true);
279     }
280 
281     @Override
onTrimMemory(int level)282     public void onTrimMemory(int level) {
283         super.onTrimMemory(level);
284         RecentsModel.INSTANCE.get(this).onTrimMemory(level);
285     }
286 
287     @Override
onDestroy()288     protected void onDestroy() {
289         super.onDestroy();
290         ACTIVITY_TRACKER.onActivityDestroyed(this);
291     }
292 
293     @Override
onBackPressed()294     public void onBackPressed() {
295         // TODO: Launch the task we came from
296         startHome();
297     }
298 
startHome()299     public void startHome() {
300         startActivity(new Intent(Intent.ACTION_MAIN)
301                 .addCategory(Intent.CATEGORY_HOME)
302                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
303     }
304 
305     @Override
createStateHandlers()306     protected StateHandler<RecentsState>[] createStateHandlers() {
307         return new StateHandler[] { new FallbackRecentsStateController(this) };
308     }
309 
310     @Override
getStateManager()311     public StateManager<RecentsState> getStateManager() {
312         return mStateManager;
313     }
314 
315     @Override
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)316     public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
317         super.dump(prefix, fd, writer, args);
318         writer.println(prefix + "Misc:");
319         dumpMisc(prefix + "\t", writer);
320     }
321 
322     @Override
createAtomicAnimationFactory()323     public AtomicAnimationFactory<RecentsState> createAtomicAnimationFactory() {
324         return new RecentsAtomicAnimationFactory<>(this, 0);
325     }
326 
resetStateListener()327     private AnimatorListenerAdapter resetStateListener() {
328         return new AnimatorListenerAdapter() {
329             @Override
330             public void onAnimationEnd(Animator animation) {
331                 mFallbackRecentsView.resetTaskVisuals();
332                 mStateManager.reapplyState();
333             }
334         };
335     }
336 }
337