1 /*
2  * Copyright (C) 2020 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.navigationbar;
18 
19 import static android.content.Intent.ACTION_OVERLAY_CHANGED;
20 
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.om.IOverlayManager;
26 import android.content.pm.PackageManager;
27 import android.content.res.ApkAssets;
28 import android.os.PatternMatcher;
29 import android.os.RemoteException;
30 import android.os.ServiceManager;
31 import android.os.UserHandle;
32 import android.provider.Settings;
33 import android.provider.Settings.Secure;
34 import android.util.Log;
35 
36 import com.android.systemui.Dumpable;
37 import com.android.systemui.dagger.SysUISingleton;
38 import com.android.systemui.dagger.qualifiers.UiBackground;
39 import com.android.systemui.dump.DumpManager;
40 import com.android.systemui.shared.system.ActivityManagerWrapper;
41 import com.android.systemui.statusbar.policy.ConfigurationController;
42 import com.android.systemui.statusbar.policy.DeviceProvisionedController;
43 
44 import java.io.FileDescriptor;
45 import java.io.PrintWriter;
46 import java.util.ArrayList;
47 import java.util.concurrent.Executor;
48 
49 import javax.inject.Inject;
50 
51 /**
52  * Controller for tracking the current navigation bar mode.
53  */
54 @SysUISingleton
55 public class NavigationModeController implements Dumpable {
56 
57     private static final String TAG = NavigationModeController.class.getSimpleName();
58     private static final boolean DEBUG = true;
59 
60     public interface ModeChangedListener {
onNavigationModeChanged(int mode)61         void onNavigationModeChanged(int mode);
62     }
63 
64     private final Context mContext;
65     private Context mCurrentUserContext;
66     private final IOverlayManager mOverlayManager;
67     private final Executor mUiBgExecutor;
68 
69     private ArrayList<ModeChangedListener> mListeners = new ArrayList<>();
70 
71     private final DeviceProvisionedController.DeviceProvisionedListener mDeviceProvisionedCallback =
72             new DeviceProvisionedController.DeviceProvisionedListener() {
73                 @Override
74                 public void onUserSwitched() {
75                     if (DEBUG) {
76                         Log.d(TAG, "onUserSwitched: "
77                                 + ActivityManagerWrapper.getInstance().getCurrentUserId());
78                     }
79 
80                     // Update the nav mode for the current user
81                     updateCurrentInteractionMode(true /* notify */);
82                 }
83             };
84 
85     // The primary user SysUI process doesn't get AppInfo changes from overlay package changes for
86     // the secondary user (b/158613864), so we need to update the interaction mode here as well
87     // as a fallback if we don't receive the configuration change
88     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
89         @Override
90         public void onReceive(Context context, Intent intent) {
91             if (DEBUG) {
92                 Log.d(TAG, "ACTION_OVERLAY_CHANGED");
93             }
94             updateCurrentInteractionMode(true /* notify */);
95         }
96     };
97 
98 
99     @Inject
NavigationModeController(Context context, DeviceProvisionedController deviceProvisionedController, ConfigurationController configurationController, @UiBackground Executor uiBgExecutor, DumpManager dumpManager)100     public NavigationModeController(Context context,
101             DeviceProvisionedController deviceProvisionedController,
102             ConfigurationController configurationController,
103             @UiBackground Executor uiBgExecutor,
104             DumpManager dumpManager) {
105         mContext = context;
106         mCurrentUserContext = context;
107         mOverlayManager = IOverlayManager.Stub.asInterface(
108                 ServiceManager.getService(Context.OVERLAY_SERVICE));
109         mUiBgExecutor = uiBgExecutor;
110         dumpManager.registerDumpable(getClass().getSimpleName(), this);
111 
112         deviceProvisionedController.addCallback(mDeviceProvisionedCallback);
113 
114         IntentFilter overlayFilter = new IntentFilter(ACTION_OVERLAY_CHANGED);
115         overlayFilter.addDataScheme("package");
116         overlayFilter.addDataSchemeSpecificPart("android", PatternMatcher.PATTERN_LITERAL);
117         mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, overlayFilter, null, null);
118 
119         configurationController.addCallback(new ConfigurationController.ConfigurationListener() {
120             @Override
121             public void onThemeChanged() {
122                 if (DEBUG) {
123                     Log.d(TAG, "onOverlayChanged");
124                 }
125                 updateCurrentInteractionMode(true /* notify */);
126             }
127         });
128 
129         updateCurrentInteractionMode(false /* notify */);
130     }
131 
updateCurrentInteractionMode(boolean notify)132     public void updateCurrentInteractionMode(boolean notify) {
133         mCurrentUserContext = getCurrentUserContext();
134         int mode = getCurrentInteractionMode(mCurrentUserContext);
135         mUiBgExecutor.execute(() ->
136             Settings.Secure.putString(mCurrentUserContext.getContentResolver(),
137                     Secure.NAVIGATION_MODE, String.valueOf(mode)));
138         if (DEBUG) {
139             Log.d(TAG, "updateCurrentInteractionMode: mode=" + mode);
140             dumpAssetPaths(mCurrentUserContext);
141         }
142 
143         if (notify) {
144             for (int i = 0; i < mListeners.size(); i++) {
145                 mListeners.get(i).onNavigationModeChanged(mode);
146             }
147         }
148     }
149 
addListener(ModeChangedListener listener)150     public int addListener(ModeChangedListener listener) {
151         mListeners.add(listener);
152         return getCurrentInteractionMode(mCurrentUserContext);
153     }
154 
removeListener(ModeChangedListener listener)155     public void removeListener(ModeChangedListener listener) {
156         mListeners.remove(listener);
157     }
158 
getCurrentInteractionMode(Context context)159     private int getCurrentInteractionMode(Context context) {
160         int mode = context.getResources().getInteger(
161                 com.android.internal.R.integer.config_navBarInteractionMode);
162         if (DEBUG) {
163             Log.d(TAG, "getCurrentInteractionMode: mode=" + mode
164                     + " contextUser=" + context.getUserId());
165         }
166         return mode;
167     }
168 
getCurrentUserContext()169     public Context getCurrentUserContext() {
170         int userId = ActivityManagerWrapper.getInstance().getCurrentUserId();
171         if (DEBUG) {
172             Log.d(TAG, "getCurrentUserContext: contextUser=" + mContext.getUserId()
173                     + " currentUser=" + userId);
174         }
175         if (mContext.getUserId() == userId) {
176             return mContext;
177         }
178         try {
179             return mContext.createPackageContextAsUser(mContext.getPackageName(),
180                     0 /* flags */, UserHandle.of(userId));
181         } catch (PackageManager.NameNotFoundException e) {
182             // Never happens for the sysui package
183             Log.e(TAG, "Failed to create package context", e);
184             return null;
185         }
186     }
187 
188     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)189     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
190         pw.println("NavigationModeController:");
191         pw.println("  mode=" + getCurrentInteractionMode(mCurrentUserContext));
192         String defaultOverlays = "";
193         try {
194             defaultOverlays = String.join(", ", mOverlayManager.getDefaultOverlayPackages());
195         } catch (RemoteException e) {
196             defaultOverlays = "failed_to_fetch";
197         }
198         pw.println("  defaultOverlays=" + defaultOverlays);
199         dumpAssetPaths(mCurrentUserContext);
200     }
201 
dumpAssetPaths(Context context)202     private void dumpAssetPaths(Context context) {
203         Log.d(TAG, "  contextUser=" + mCurrentUserContext.getUserId());
204         Log.d(TAG, "  assetPaths=");
205         ApkAssets[] assets = context.getResources().getAssets().getApkAssets();
206         for (ApkAssets a : assets) {
207             Log.d(TAG, "    " + a.getDebugName());
208         }
209     }
210 }
211