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.Dependency;
23 import com.android.systemui.SysUiServiceProvider;
24 import com.android.systemui.SystemUI;
25 import com.android.systemui.plugins.GlobalActions;
26 import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
27 import com.android.systemui.statusbar.CommandQueue;
28 import com.android.systemui.statusbar.CommandQueue.Callbacks;
29 import com.android.systemui.statusbar.policy.ExtensionController;
30 import com.android.systemui.statusbar.policy.ExtensionController.Extension;
31 
32 public class GlobalActionsComponent extends SystemUI implements Callbacks, GlobalActionsManager {
33 
34     private GlobalActions mPlugin;
35     private Extension<GlobalActions> mExtension;
36     private IStatusBarService mBarService;
37 
38     @Override
start()39     public void start() {
40         mBarService = IStatusBarService.Stub.asInterface(
41                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
42         mExtension = Dependency.get(ExtensionController.class).newExtension(GlobalActions.class)
43                 .withPlugin(GlobalActions.class)
44                 .withDefault(() -> new GlobalActionsImpl(mContext))
45                 .withCallback(this::onExtensionCallback)
46                 .build();
47         mPlugin = mExtension.get();
48         SysUiServiceProvider.getComponent(mContext, CommandQueue.class).addCallbacks(this);
49     }
50 
onExtensionCallback(GlobalActions newPlugin)51     private void onExtensionCallback(GlobalActions newPlugin) {
52         if (mPlugin != null) {
53             mPlugin.destroy();
54         }
55         mPlugin = newPlugin;
56     }
57 
58     @Override
handleShowShutdownUi(boolean isReboot, String reason)59     public void handleShowShutdownUi(boolean isReboot, String reason) {
60         mExtension.get().showShutdownUi(isReboot, reason);
61     }
62 
63     @Override
handleShowGlobalActionsMenu()64     public void handleShowGlobalActionsMenu() {
65         mExtension.get().showGlobalActions(this);
66     }
67 
68     @Override
onGlobalActionsShown()69     public void onGlobalActionsShown() {
70         try {
71             mBarService.onGlobalActionsShown();
72         } catch (RemoteException e) {
73         }
74     }
75 
76     @Override
onGlobalActionsHidden()77     public void onGlobalActionsHidden() {
78         try {
79             mBarService.onGlobalActionsHidden();
80         } catch (RemoteException e) {
81         }
82     }
83 
84     @Override
shutdown()85     public void shutdown() {
86         try {
87             mBarService.shutdown();
88         } catch (RemoteException e) {
89         }
90     }
91 
92     @Override
reboot(boolean safeMode)93     public void reboot(boolean safeMode) {
94         try {
95             mBarService.reboot(safeMode);
96         } catch (RemoteException e) {
97         }
98     }
99 }
100