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