1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.globalactions;
16 
17 import android.content.Context;
18 import android.os.RemoteException;
19 import android.os.ServiceManager;
20 
21 import com.android.internal.statusbar.IStatusBarService;
22 import com.android.systemui.CoreStartable;
23 import com.android.systemui.dagger.SysUISingleton;
24 import com.android.systemui.plugins.GlobalActions;
25 import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
26 import com.android.systemui.statusbar.CommandQueue;
27 import com.android.systemui.statusbar.CommandQueue.Callbacks;
28 import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
29 import com.android.systemui.statusbar.policy.ExtensionController;
30 import com.android.systemui.statusbar.policy.ExtensionController.Extension;
31 
32 import javax.inject.Inject;
33 import javax.inject.Provider;
34 
35 /**
36  * Manages power menu plugins and communicates power menu actions to the CentralSurfaces.
37  */
38 @SysUISingleton
39 public class GlobalActionsComponent implements CoreStartable, Callbacks, GlobalActionsManager {
40 
41     private final CommandQueue mCommandQueue;
42     private final ExtensionController mExtensionController;
43     private final Provider<GlobalActions> mGlobalActionsProvider;
44     private GlobalActions mPlugin;
45     private Extension<GlobalActions> mExtension;
46     private IStatusBarService mBarService;
47     private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
48 
49     @Inject
GlobalActionsComponent(CommandQueue commandQueue, ExtensionController extensionController, Provider<GlobalActions> globalActionsProvider, StatusBarKeyguardViewManager statusBarKeyguardViewManager)50     public GlobalActionsComponent(CommandQueue commandQueue,
51             ExtensionController extensionController,
52             Provider<GlobalActions> globalActionsProvider,
53             StatusBarKeyguardViewManager statusBarKeyguardViewManager) {
54         mCommandQueue = commandQueue;
55         mExtensionController = extensionController;
56         mGlobalActionsProvider = globalActionsProvider;
57         mStatusBarKeyguardViewManager = statusBarKeyguardViewManager;
58     }
59 
60     @Override
start()61     public void start() {
62         mBarService = IStatusBarService.Stub.asInterface(
63                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
64         mExtension = mExtensionController.newExtension(GlobalActions.class)
65                 .withPlugin(GlobalActions.class)
66                 .withDefault(mGlobalActionsProvider::get)
67                 .withCallback(this::onExtensionCallback)
68                 .build();
69         mPlugin = mExtension.get();
70         mCommandQueue.addCallback(this);
71     }
72 
onExtensionCallback(GlobalActions newPlugin)73     private void onExtensionCallback(GlobalActions newPlugin) {
74         if (mPlugin != null) {
75             mPlugin.destroy();
76         }
77         mPlugin = newPlugin;
78     }
79 
80     @Override
handleShowShutdownUi(boolean isReboot, String reason)81     public void handleShowShutdownUi(boolean isReboot, String reason) {
82         mExtension.get().showShutdownUi(isReboot, reason);
83     }
84 
85     @Override
handleShowGlobalActionsMenu()86     public void handleShowGlobalActionsMenu() {
87         mStatusBarKeyguardViewManager.setGlobalActionsVisible(true);
88         mExtension.get().showGlobalActions(this);
89     }
90 
91     @Override
onGlobalActionsShown()92     public void onGlobalActionsShown() {
93         try {
94             mBarService.onGlobalActionsShown();
95         } catch (RemoteException e) {
96         }
97     }
98 
99     @Override
onGlobalActionsHidden()100     public void onGlobalActionsHidden() {
101         try {
102             mStatusBarKeyguardViewManager.setGlobalActionsVisible(false);
103             mBarService.onGlobalActionsHidden();
104         } catch (RemoteException e) {
105         }
106     }
107 
108     @Override
shutdown()109     public void shutdown() {
110         try {
111             mBarService.shutdown();
112         } catch (RemoteException e) {
113         }
114     }
115 
116     @Override
reboot(boolean safeMode)117     public void reboot(boolean safeMode) {
118         try {
119             mBarService.reboot(safeMode);
120         } catch (RemoteException e) {
121         }
122     }
123 }
124