1 /*
2  * Copyright (C) 2021 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.launcher3.taskbar;
17 
18 import static com.android.launcher3.taskbar.TaskbarStashController.FLAG_IN_APP;
19 import static com.android.launcher3.taskbar.TaskbarStashController.FLAG_IN_STASHED_LAUNCHER_STATE;
20 import static com.android.launcher3.taskbar.TaskbarStashController.TASKBAR_STASH_DURATION;
21 
22 import android.animation.Animator;
23 
24 import com.android.launcher3.statemanager.StateManager;
25 import com.android.quickstep.RecentsActivity;
26 import com.android.quickstep.fallback.RecentsState;
27 import com.android.quickstep.views.RecentsView;
28 
29 /**
30  * A data source which integrates with the fallback RecentsActivity instance (for 3P launchers).
31  */
32 public class FallbackTaskbarUIController extends TaskbarUIController {
33 
34     private final RecentsActivity mRecentsActivity;
35 
36     private final StateManager.StateListener<RecentsState> mStateListener =
37             new StateManager.StateListener<RecentsState>() {
38                 @Override
39                 public void onStateTransitionStart(RecentsState toState) {
40                     animateToRecentsState(toState);
41 
42                     // Handle tapping on live tile.
43                     RecentsView recentsView = mRecentsActivity.getOverviewPanel();
44                     recentsView.setTaskLaunchListener(toState == RecentsState.DEFAULT
45                             ? (() -> animateToRecentsState(RecentsState.BACKGROUND_APP)) : null);
46                 }
47             };
48 
FallbackTaskbarUIController(RecentsActivity recentsActivity)49     public FallbackTaskbarUIController(RecentsActivity recentsActivity) {
50         mRecentsActivity = recentsActivity;
51     }
52 
53     @Override
init(TaskbarControllers taskbarControllers)54     protected void init(TaskbarControllers taskbarControllers) {
55         super.init(taskbarControllers);
56 
57         mRecentsActivity.setTaskbarUIController(this);
58         mRecentsActivity.getStateManager().addStateListener(mStateListener);
59     }
60 
61     @Override
onDestroy()62     protected void onDestroy() {
63         super.onDestroy();
64         mRecentsActivity.setTaskbarUIController(null);
65         mRecentsActivity.getStateManager().removeStateListener(mStateListener);
66     }
67 
68     /**
69      * Creates an animation to animate the taskbar for the given state (but does not start it).
70      * Currently this animation just force stashes the taskbar in Overview.
71      */
createAnimToRecentsState(RecentsState toState, long duration)72     public Animator createAnimToRecentsState(RecentsState toState, long duration) {
73         boolean forceStashed = toState.hasOverviewActions();
74         TaskbarStashController controller = mControllers.taskbarStashController;
75         // Set both FLAG_IN_STASHED_LAUNCHER_STATE and FLAG_IN_APP to ensure the state is respected.
76         // For all other states, just use the current stashed-in-app setting (e.g. if long clicked).
77         controller.updateStateForFlag(FLAG_IN_STASHED_LAUNCHER_STATE, forceStashed);
78         controller.updateStateForFlag(FLAG_IN_APP, !forceStashed);
79         return controller.applyStateWithoutStart(duration);
80     }
81 
animateToRecentsState(RecentsState toState)82     private void animateToRecentsState(RecentsState toState) {
83         Animator anim = createAnimToRecentsState(toState, TASKBAR_STASH_DURATION);
84         if (anim != null) {
85             anim.start();
86         }
87     }
88 }
89