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.annotation.Nullable;
20 import android.app.trust.TrustManager;
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.RemoteException;
24 import android.util.Log;
25 
26 import com.android.systemui.Dependency;
27 import com.android.systemui.dagger.SysUISingleton;
28 import com.android.systemui.shared.recents.IOverviewProxy;
29 import com.android.systemui.statusbar.phone.StatusBar;
30 
31 import java.util.Optional;
32 
33 import javax.inject.Inject;
34 
35 import dagger.Lazy;
36 
37 /**
38  * An implementation of the Recents interface which proxies to the OverviewProxyService.
39  */
40 @SysUISingleton
41 public class OverviewProxyRecentsImpl implements RecentsImplementation {
42 
43     private final static String TAG = "OverviewProxyRecentsImpl";
44     @Nullable
45     private final Lazy<Optional<StatusBar>> mStatusBarOptionalLazy;
46 
47     private Context mContext;
48     private Handler mHandler;
49     private TrustManager mTrustManager;
50     private OverviewProxyService mOverviewProxyService;
51 
52     @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
53     @Inject
OverviewProxyRecentsImpl(Lazy<Optional<StatusBar>> statusBarOptionalLazy)54     public OverviewProxyRecentsImpl(Lazy<Optional<StatusBar>> statusBarOptionalLazy) {
55         mStatusBarOptionalLazy = statusBarOptionalLazy;
56     }
57 
58     @Override
onStart(Context context)59     public void onStart(Context context) {
60         mContext = context;
61         mHandler = new Handler();
62         mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
63         mOverviewProxyService = Dependency.get(OverviewProxyService.class);
64     }
65 
66     @Override
showRecentApps(boolean triggeredFromAltTab)67     public void showRecentApps(boolean triggeredFromAltTab) {
68         IOverviewProxy overviewProxy = mOverviewProxyService.getProxy();
69         if (overviewProxy != null) {
70             try {
71                 overviewProxy.onOverviewShown(triggeredFromAltTab);
72                 return;
73             } catch (RemoteException e) {
74                 Log.e(TAG, "Failed to send overview show event to launcher.", e);
75             }
76         } else {
77             // Do nothing
78         }
79     }
80 
81     @Override
hideRecentApps(boolean triggeredFromAltTab, boolean triggeredFromHomeKey)82     public void hideRecentApps(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
83         IOverviewProxy overviewProxy = mOverviewProxyService.getProxy();
84         if (overviewProxy != null) {
85             try {
86                 overviewProxy.onOverviewHidden(triggeredFromAltTab, triggeredFromHomeKey);
87                 return;
88             } catch (RemoteException e) {
89                 Log.e(TAG, "Failed to send overview hide event to launcher.", e);
90             }
91         } else {
92             // Do nothing
93         }
94     }
95 
96     @Override
toggleRecentApps()97     public void toggleRecentApps() {
98         // If connected to launcher service, let it handle the toggle logic
99         IOverviewProxy overviewProxy = mOverviewProxyService.getProxy();
100         if (overviewProxy != null) {
101             final Runnable toggleRecents = () -> {
102                 try {
103                     if (mOverviewProxyService.getProxy() != null) {
104                         mOverviewProxyService.getProxy().onOverviewToggle();
105                         mOverviewProxyService.notifyToggleRecentApps();
106                     }
107                 } catch (RemoteException e) {
108                     Log.e(TAG, "Cannot send toggle recents through proxy service.", e);
109                 }
110             };
111             // Preload only if device for current user is unlocked
112             final Optional<StatusBar> statusBarOptional = mStatusBarOptionalLazy.get();
113             if (statusBarOptional.map(StatusBar::isKeyguardShowing).orElse(false)) {
114                 statusBarOptional.get().executeRunnableDismissingKeyguard(() -> {
115                         // Flush trustmanager before checking device locked per user
116                         mTrustManager.reportKeyguardShowingChanged();
117                         mHandler.post(toggleRecents);
118                     }, null,  true /* dismissShade */, false /* afterKeyguardGone */,
119                     true /* deferred */);
120             } else {
121                 toggleRecents.run();
122             }
123             return;
124         } else {
125             // Do nothing
126         }
127     }
128 }
129