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