1 /*
2  * Copyright (C) 2014 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 
17 package com.android.systemui.recents;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.RemoteException;
22 import android.util.Log;
23 
24 import com.android.systemui.dagger.SysUISingleton;
25 import com.android.systemui.plugins.ActivityStarter;
26 import com.android.systemui.shared.recents.IOverviewProxy;
27 import com.android.systemui.statusbar.policy.KeyguardStateController;
28 
29 import javax.inject.Inject;
30 
31 /**
32  * An implementation of the Recents interface which proxies to the OverviewProxyService.
33  */
34 @SysUISingleton
35 public class OverviewProxyRecentsImpl implements RecentsImplementation {
36 
37     private final static String TAG = "OverviewProxyRecentsImpl";
38     private Handler mHandler;
39     private final OverviewProxyService mOverviewProxyService;
40     private final ActivityStarter mActivityStarter;
41     private final KeyguardStateController mKeyguardStateController;
42 
43     @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
44     @Inject
OverviewProxyRecentsImpl( OverviewProxyService overviewProxyService, ActivityStarter activityStarter, KeyguardStateController keyguardStateController)45     public OverviewProxyRecentsImpl(
46             OverviewProxyService overviewProxyService,
47             ActivityStarter activityStarter,
48             KeyguardStateController keyguardStateController) {
49         mOverviewProxyService = overviewProxyService;
50         mActivityStarter = activityStarter;
51         mKeyguardStateController = keyguardStateController;
52     }
53 
54     @Override
onStart(Context context)55     public void onStart(Context context) {
56         mHandler = new Handler();
57     }
58 
59     @Override
showRecentApps(boolean triggeredFromAltTab)60     public void showRecentApps(boolean triggeredFromAltTab) {
61         IOverviewProxy overviewProxy = mOverviewProxyService.getProxy();
62         if (overviewProxy != null) {
63             try {
64                 overviewProxy.onOverviewShown(triggeredFromAltTab);
65             } catch (RemoteException e) {
66                 Log.e(TAG, "Failed to send overview show event to launcher.", e);
67             }
68         }
69     }
70 
71     @Override
hideRecentApps(boolean triggeredFromAltTab, boolean triggeredFromHomeKey)72     public void hideRecentApps(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
73         IOverviewProxy overviewProxy = mOverviewProxyService.getProxy();
74         if (overviewProxy != null) {
75             try {
76                 overviewProxy.onOverviewHidden(triggeredFromAltTab, triggeredFromHomeKey);
77             } catch (RemoteException e) {
78                 Log.e(TAG, "Failed to send overview hide event to launcher.", e);
79             }
80         }
81     }
82 
83     @Override
toggleRecentApps()84     public void toggleRecentApps() {
85         // If connected to launcher service, let it handle the toggle logic
86         IOverviewProxy overviewProxy = mOverviewProxyService.getProxy();
87         if (overviewProxy != null) {
88             final Runnable toggleRecents = () -> {
89                 try {
90                     if (mOverviewProxyService.getProxy() != null) {
91                         mOverviewProxyService.getProxy().onOverviewToggle();
92                         mOverviewProxyService.notifyToggleRecentApps();
93                     }
94                 } catch (RemoteException e) {
95                     Log.e(TAG, "Cannot send toggle recents through proxy service.", e);
96                 }
97             };
98             // Preload only if device for current user is unlocked
99             if (mKeyguardStateController.isShowing()) {
100                 mActivityStarter.executeRunnableDismissingKeyguard(
101                         () -> mHandler.post(toggleRecents), null, true /* dismissShade */,
102                         false /* afterKeyguardGone */,
103                         true /* deferred */);
104             } else {
105                 toggleRecents.run();
106             }
107         }
108     }
109 }
110