Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1 | /** |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 2 | * Copyright (C) 2015 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 | package com.android.server.vr; |
| 17 | |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 18 | import static android.view.Display.INVALID_DISPLAY; |
| 19 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 20 | import android.Manifest; |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 21 | import android.app.ActivityManagerInternal; |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 23 | import android.app.AppOpsManager; |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 24 | import android.app.Vr2dDisplayProperties; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 25 | import android.app.NotificationManager; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 26 | import android.annotation.NonNull; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 27 | import android.content.ComponentName; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.content.pm.ApplicationInfo; |
| 31 | import android.content.pm.PackageManager; |
| 32 | import android.content.pm.PackageManager.NameNotFoundException; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 33 | import android.hardware.display.DisplayManager; |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 34 | import android.os.Binder; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 35 | import android.os.Handler; |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 36 | import android.os.IBinder; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 37 | import android.os.IInterface; |
| 38 | import android.os.Looper; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 39 | import android.os.Message; |
| 40 | import android.os.RemoteCallbackList; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 41 | import android.os.RemoteException; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 42 | import android.os.UserHandle; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 43 | import android.provider.Settings; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 44 | import android.service.notification.NotificationListenerService; |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 45 | import android.service.vr.IPersistentVrStateCallbacks; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 46 | import android.service.vr.IVrListener; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 47 | import android.service.vr.IVrManager; |
| 48 | import android.service.vr.IVrStateCallbacks; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 49 | import android.service.vr.VrListenerService; |
Ruben Brunk | 1f4f26b | 2016-08-02 14:21:36 -0700 | [diff] [blame] | 50 | import android.text.TextUtils; |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 51 | import android.util.ArrayMap; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 52 | import android.util.ArraySet; |
| 53 | import android.util.Slog; |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 54 | import android.util.SparseArray; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 55 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 56 | import com.android.internal.R; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 57 | import com.android.internal.util.DumpUtils; |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 58 | import com.android.server.LocalServices; |
Craig Donner | 9906259 | 2016-04-14 21:45:19 -0700 | [diff] [blame] | 59 | import com.android.server.SystemConfig; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 60 | import com.android.server.SystemService; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 61 | import com.android.server.utils.ManagedApplicationService.PendingEvent; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 62 | import com.android.server.vr.EnabledComponentsObserver.EnabledComponentChangeListener; |
| 63 | import com.android.server.utils.ManagedApplicationService; |
| 64 | import com.android.server.utils.ManagedApplicationService.BinderChecker; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 65 | |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 66 | import java.io.FileDescriptor; |
| 67 | import java.io.PrintWriter; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 68 | import java.lang.StringBuilder; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 69 | import java.text.SimpleDateFormat; |
| 70 | import java.util.ArrayDeque; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 71 | import java.util.ArrayList; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 72 | import java.util.Collection; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 73 | import java.util.Date; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 74 | import java.util.Objects; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 77 | * Service tracking whether VR mode is active, and notifying listening services of state changes. |
| 78 | * <p/> |
| 79 | * Services running in system server may modify the state of VrManagerService via the interface in |
| 80 | * VrManagerInternal, and may register to receive callbacks when the system VR mode changes via the |
| 81 | * interface given in VrStateListener. |
| 82 | * <p/> |
| 83 | * Device vendors may choose to receive VR state changes by implementing the VR mode HAL, e.g.: |
| 84 | * hardware/libhardware/modules/vr |
| 85 | * <p/> |
| 86 | * In general applications may enable or disable VR mode by calling |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 87 | * {@link android.app.Activity#setVrModeEnabled)}. An application may also implement a service to |
| 88 | * be run while in VR mode by implementing {@link android.service.vr.VrListenerService}. |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 89 | * |
Elliot Waite | 54de774 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 90 | * @see android.service.vr.VrListenerService |
| 91 | * @see com.android.server.vr.VrManagerInternal |
| 92 | * @see com.android.server.vr.VrStateListener |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 93 | * |
| 94 | * @hide |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 95 | */ |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 96 | public class VrManagerService extends SystemService implements EnabledComponentChangeListener{ |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 97 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 98 | public static final String TAG = "VrManagerService"; |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 99 | static final boolean DBG = false; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 100 | |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 101 | private static final int PENDING_STATE_DELAY_MS = 300; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 102 | private static final int EVENT_LOG_SIZE = 32; |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 103 | private static final int INVALID_APPOPS_MODE = -1; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 104 | /** Null set of sleep sleep flags. */ |
| 105 | private static final int FLAG_NONE = 0; |
| 106 | /** Flag set when the device is not sleeping. */ |
| 107 | private static final int FLAG_AWAKE = 1; |
| 108 | /** Flag set when the screen has been turned on. */ |
| 109 | private static final int FLAG_SCREEN_ON = 2; |
| 110 | /** Flag indicating that all system sleep flags have been set.*/ |
| 111 | private static final int FLAG_ALL = FLAG_AWAKE | FLAG_SCREEN_ON; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 112 | |
Ruben Brunk | baa4b55 | 2016-02-02 16:27:37 -0800 | [diff] [blame] | 113 | private static native void initializeNative(); |
| 114 | private static native void setVrModeNative(boolean enabled); |
| 115 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 116 | private final Object mLock = new Object(); |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 117 | |
| 118 | private final IBinder mOverlayToken = new Binder(); |
| 119 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 120 | // State protected by mLock |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 121 | private boolean mVrModeAllowed; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 122 | private boolean mVrModeEnabled; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 123 | private boolean mPersistentVrModeEnabled; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 124 | private EnabledComponentsObserver mComponentObserver; |
| 125 | private ManagedApplicationService mCurrentVrService; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 126 | private ComponentName mDefaultVrService; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 127 | private Context mContext; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 128 | private ComponentName mCurrentVrModeComponent; |
| 129 | private int mCurrentVrModeUser; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 130 | private boolean mWasDefaultGranted; |
| 131 | private boolean mGuard; |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 132 | private final RemoteCallbackList<IVrStateCallbacks> mVrStateRemoteCallbacks = |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 133 | new RemoteCallbackList<>(); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 134 | private final RemoteCallbackList<IPersistentVrStateCallbacks> |
| 135 | mPersistentVrStateRemoteCallbacks = new RemoteCallbackList<>(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 136 | private int mPreviousCoarseLocationMode = INVALID_APPOPS_MODE; |
| 137 | private int mPreviousManageOverlayMode = INVALID_APPOPS_MODE; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 138 | private VrState mPendingState; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 139 | private final ArrayDeque<VrState> mLoggingDeque = new ArrayDeque<>(EVENT_LOG_SIZE); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 140 | private final NotificationAccessManager mNotifAccessManager = new NotificationAccessManager(); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 141 | /** Tracks the state of the screen and keyguard UI.*/ |
Mark Urbanus | 72729f9 | 2017-02-17 12:00:54 -0800 | [diff] [blame] | 142 | private int mSystemSleepFlags = FLAG_AWAKE; |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 143 | private Vr2dDisplay mVr2dDisplay; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 144 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 145 | private static final int MSG_VR_STATE_CHANGE = 0; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 146 | private static final int MSG_PENDING_VR_STATE_CHANGE = 1; |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 147 | private static final int MSG_PERSISTENT_VR_MODE_STATE_CHANGE = 2; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 148 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Set whether VR mode may be enabled. |
| 151 | * <p/> |
| 152 | * If VR mode is not allowed to be enabled, calls to set VR mode will be cached. When VR mode |
| 153 | * is again allowed to be enabled, the most recent cached state will be applied. |
| 154 | * |
| 155 | * @param allowed {@code true} if calling any of the setVrMode methods may cause the device to |
| 156 | * enter VR mode. |
| 157 | */ |
| 158 | private void setVrModeAllowedLocked(boolean allowed) { |
| 159 | if (mVrModeAllowed != allowed) { |
| 160 | mVrModeAllowed = allowed; |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 161 | if (DBG) Slog.d(TAG, "VR mode is " + ((allowed) ? "allowed" : "disallowed")); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 162 | if (mVrModeAllowed) { |
| 163 | consumeAndApplyPendingStateLocked(); |
| 164 | } else { |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 165 | // Disable persistent mode when VR mode isn't allowed, allows an escape hatch to |
| 166 | // exit persistent VR mode when screen is turned off. |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 167 | setPersistentModeAndNotifyListenersLocked(false); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 168 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 169 | // Set pending state to current state. |
| 170 | mPendingState = (mVrModeEnabled && mCurrentVrService != null) |
| 171 | ? new VrState(mVrModeEnabled, mCurrentVrService.getComponent(), |
| 172 | mCurrentVrService.getUserId(), mCurrentVrModeComponent) |
| 173 | : null; |
| 174 | |
| 175 | // Unbind current VR service and do necessary callbacks. |
| 176 | updateCurrentVrServiceLocked(false, null, 0, null); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private void setSleepState(boolean isAsleep) { |
| 182 | synchronized(mLock) { |
| 183 | |
| 184 | if (!isAsleep) { |
| 185 | mSystemSleepFlags |= FLAG_AWAKE; |
| 186 | } else { |
| 187 | mSystemSleepFlags &= ~FLAG_AWAKE; |
| 188 | } |
| 189 | |
| 190 | setVrModeAllowedLocked(mSystemSleepFlags == FLAG_ALL); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private void setScreenOn(boolean isScreenOn) { |
| 195 | synchronized(mLock) { |
| 196 | if (isScreenOn) { |
| 197 | mSystemSleepFlags |= FLAG_SCREEN_ON; |
| 198 | } else { |
| 199 | mSystemSleepFlags &= ~FLAG_SCREEN_ON; |
| 200 | } |
| 201 | setVrModeAllowedLocked(mSystemSleepFlags == FLAG_ALL); |
| 202 | } |
| 203 | } |
| 204 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 205 | private final Handler mHandler = new Handler() { |
| 206 | @Override |
| 207 | public void handleMessage(Message msg) { |
| 208 | switch(msg.what) { |
| 209 | case MSG_VR_STATE_CHANGE : { |
| 210 | boolean state = (msg.arg1 == 1); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 211 | int i = mVrStateRemoteCallbacks.beginBroadcast(); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 212 | while (i > 0) { |
| 213 | i--; |
| 214 | try { |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 215 | mVrStateRemoteCallbacks.getBroadcastItem(i).onVrStateChanged(state); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 216 | } catch (RemoteException e) { |
| 217 | // Noop |
| 218 | } |
| 219 | } |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 220 | mVrStateRemoteCallbacks.finishBroadcast(); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 221 | } break; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 222 | case MSG_PENDING_VR_STATE_CHANGE : { |
| 223 | synchronized(mLock) { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 224 | if (mVrModeAllowed) { |
| 225 | VrManagerService.this.consumeAndApplyPendingStateLocked(); |
| 226 | } |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 227 | } |
| 228 | } break; |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 229 | case MSG_PERSISTENT_VR_MODE_STATE_CHANGE : { |
| 230 | boolean state = (msg.arg1 == 1); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 231 | int i = mPersistentVrStateRemoteCallbacks.beginBroadcast(); |
| 232 | while (i > 0) { |
| 233 | i--; |
| 234 | try { |
| 235 | mPersistentVrStateRemoteCallbacks.getBroadcastItem(i) |
| 236 | .onPersistentVrStateChanged(state); |
| 237 | } catch (RemoteException e) { |
| 238 | // Noop |
| 239 | } |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 240 | } |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 241 | mPersistentVrStateRemoteCallbacks.finishBroadcast(); |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 242 | } break; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 243 | default : |
| 244 | throw new IllegalStateException("Unknown message type: " + msg.what); |
| 245 | } |
| 246 | } |
| 247 | }; |
| 248 | |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 249 | private static class VrState { |
| 250 | final boolean enabled; |
| 251 | final int userId; |
| 252 | final ComponentName targetPackageName; |
| 253 | final ComponentName callingPackage; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 254 | final long timestamp; |
| 255 | final boolean defaultPermissionsGranted; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 256 | |
| 257 | VrState(boolean enabled, ComponentName targetPackageName, int userId, |
| 258 | ComponentName callingPackage) { |
| 259 | this.enabled = enabled; |
| 260 | this.userId = userId; |
| 261 | this.targetPackageName = targetPackageName; |
| 262 | this.callingPackage = callingPackage; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 263 | this.defaultPermissionsGranted = false; |
| 264 | this.timestamp = System.currentTimeMillis(); |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 265 | } |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 266 | |
| 267 | VrState(boolean enabled, ComponentName targetPackageName, int userId, |
| 268 | ComponentName callingPackage, boolean defaultPermissionsGranted) { |
| 269 | this.enabled = enabled; |
| 270 | this.userId = userId; |
| 271 | this.targetPackageName = targetPackageName; |
| 272 | this.callingPackage = callingPackage; |
| 273 | this.defaultPermissionsGranted = defaultPermissionsGranted; |
| 274 | this.timestamp = System.currentTimeMillis(); |
| 275 | } |
| 276 | } |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 277 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 278 | private static final BinderChecker sBinderChecker = new BinderChecker() { |
| 279 | @Override |
| 280 | public IInterface asInterface(IBinder binder) { |
| 281 | return IVrListener.Stub.asInterface(binder); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public boolean checkType(IInterface service) { |
| 286 | return service instanceof IVrListener; |
| 287 | } |
| 288 | }; |
| 289 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 290 | private final class NotificationAccessManager { |
| 291 | private final SparseArray<ArraySet<String>> mAllowedPackages = new SparseArray<>(); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 292 | private final ArrayMap<String, Integer> mNotificationAccessPackageToUserId = |
| 293 | new ArrayMap<>(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 294 | |
| 295 | public void update(Collection<String> packageNames) { |
| 296 | int currentUserId = ActivityManager.getCurrentUser(); |
| 297 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 298 | ArraySet<String> allowed = mAllowedPackages.get(currentUserId); |
| 299 | if (allowed == null) { |
| 300 | allowed = new ArraySet<>(); |
| 301 | } |
| 302 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 303 | // Make sure we revoke notification access for listeners in other users |
| 304 | final int listenerCount = mNotificationAccessPackageToUserId.size(); |
| 305 | for (int i = listenerCount - 1; i >= 0; i--) { |
| 306 | final int grantUserId = mNotificationAccessPackageToUserId.valueAt(i); |
| 307 | if (grantUserId != currentUserId) { |
| 308 | String packageName = mNotificationAccessPackageToUserId.keyAt(i); |
| 309 | revokeNotificationListenerAccess(packageName, grantUserId); |
| 310 | revokeNotificationPolicyAccess(packageName); |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 311 | revokeCoarseLocationPermissionIfNeeded(packageName, grantUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 312 | mNotificationAccessPackageToUserId.removeAt(i); |
| 313 | } |
| 314 | } |
| 315 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 316 | for (String pkg : allowed) { |
| 317 | if (!packageNames.contains(pkg)) { |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 318 | revokeNotificationListenerAccess(pkg, currentUserId); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 319 | revokeNotificationPolicyAccess(pkg); |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 320 | revokeCoarseLocationPermissionIfNeeded(pkg, currentUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 321 | mNotificationAccessPackageToUserId.remove(pkg); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | for (String pkg : packageNames) { |
| 325 | if (!allowed.contains(pkg)) { |
| 326 | grantNotificationPolicyAccess(pkg); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 327 | grantNotificationListenerAccess(pkg, currentUserId); |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 328 | grantCoarseLocationPermissionIfNeeded(pkg, currentUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 329 | mNotificationAccessPackageToUserId.put(pkg, currentUserId); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | allowed.clear(); |
| 334 | allowed.addAll(packageNames); |
| 335 | mAllowedPackages.put(currentUserId, allowed); |
| 336 | } |
| 337 | } |
| 338 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 339 | /** |
| 340 | * Called when a user, package, or setting changes that could affect whether or not the |
| 341 | * currently bound VrListenerService is changed. |
| 342 | */ |
| 343 | @Override |
| 344 | public void onEnabledComponentChanged() { |
| 345 | synchronized (mLock) { |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 346 | int currentUser = ActivityManager.getCurrentUser(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 347 | // Update listeners |
| 348 | ArraySet<ComponentName> enabledListeners = mComponentObserver.getEnabled(currentUser); |
| 349 | |
| 350 | ArraySet<String> enabledPackages = new ArraySet<>(); |
| 351 | for (ComponentName n : enabledListeners) { |
| 352 | String pkg = n.getPackageName(); |
| 353 | if (isDefaultAllowed(pkg)) { |
| 354 | enabledPackages.add(n.getPackageName()); |
| 355 | } |
| 356 | } |
| 357 | mNotifAccessManager.update(enabledPackages); |
| 358 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 359 | if (!mVrModeAllowed) { |
| 360 | return; // Don't do anything, we shouldn't be in VR mode. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 363 | // If there is a pending state change, we'd better deal with that first |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 364 | consumeAndApplyPendingStateLocked(false); |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 365 | |
| 366 | if (mCurrentVrService == null) { |
| 367 | return; // No active services |
| 368 | } |
| 369 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 370 | // There is an active service, update it if needed |
| 371 | updateCurrentVrServiceLocked(mVrModeEnabled, mCurrentVrService.getComponent(), |
Alex Sakhartchouk | 6b1251f | 2017-05-17 12:34:53 -0400 | [diff] [blame] | 372 | mCurrentVrService.getUserId(), mCurrentVrModeComponent); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 376 | private final IVrManager mVrManager = new IVrManager.Stub() { |
| 377 | |
| 378 | @Override |
| 379 | public void registerListener(IVrStateCallbacks cb) { |
| 380 | enforceCallerPermission(Manifest.permission.ACCESS_VR_MANAGER); |
| 381 | if (cb == null) { |
| 382 | throw new IllegalArgumentException("Callback binder object is null."); |
| 383 | } |
| 384 | |
| 385 | VrManagerService.this.addStateCallback(cb); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void unregisterListener(IVrStateCallbacks cb) { |
| 390 | enforceCallerPermission(Manifest.permission.ACCESS_VR_MANAGER); |
| 391 | if (cb == null) { |
| 392 | throw new IllegalArgumentException("Callback binder object is null."); |
| 393 | } |
| 394 | |
| 395 | VrManagerService.this.removeStateCallback(cb); |
| 396 | } |
| 397 | |
| 398 | @Override |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 399 | public void registerPersistentVrStateListener(IPersistentVrStateCallbacks cb) { |
| 400 | enforceCallerPermission(Manifest.permission.ACCESS_VR_MANAGER); |
| 401 | if (cb == null) { |
| 402 | throw new IllegalArgumentException("Callback binder object is null."); |
| 403 | } |
| 404 | |
| 405 | VrManagerService.this.addPersistentStateCallback(cb); |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void unregisterPersistentVrStateListener(IPersistentVrStateCallbacks cb) { |
| 410 | enforceCallerPermission(Manifest.permission.ACCESS_VR_MANAGER); |
| 411 | if (cb == null) { |
| 412 | throw new IllegalArgumentException("Callback binder object is null."); |
| 413 | } |
| 414 | |
| 415 | VrManagerService.this.removePersistentStateCallback(cb); |
| 416 | } |
| 417 | |
| 418 | @Override |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 419 | public boolean getVrModeState() { |
| 420 | return VrManagerService.this.getVrMode(); |
| 421 | } |
| 422 | |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 423 | @Override |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 424 | public void setPersistentVrModeEnabled(boolean enabled) { |
Craig Donner | 8deb67c | 2017-02-07 18:10:32 -0800 | [diff] [blame] | 425 | enforceCallerPermission(Manifest.permission.RESTRICTED_VR_ACCESS); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 426 | VrManagerService.this.setPersistentVrModeEnabled(enabled); |
| 427 | } |
| 428 | |
| 429 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 430 | public void setVr2dDisplayProperties( |
| 431 | Vr2dDisplayProperties vr2dDisplayProp) { |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 432 | enforceCallerPermission(Manifest.permission.RESTRICTED_VR_ACCESS); |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 433 | VrManagerService.this.setVr2dDisplayProperties(vr2dDisplayProp); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 437 | public int getVr2dDisplayId() { |
| 438 | return VrManagerService.this.getVr2dDisplayId(); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 442 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 443 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 444 | |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 445 | pw.println("********* Dump of VrManagerService *********"); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 446 | pw.println("VR mode is currently: " + ((mVrModeAllowed) ? "allowed" : "disallowed")); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 447 | pw.println("Persistent VR mode is currently: " + |
| 448 | ((mPersistentVrModeEnabled) ? "enabled" : "disabled")); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 449 | pw.println("Previous state transitions:\n"); |
| 450 | String tab = " "; |
| 451 | dumpStateTransitions(pw); |
| 452 | pw.println("\n\nRemote Callbacks:"); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 453 | int i=mVrStateRemoteCallbacks.beginBroadcast(); // create the broadcast item array |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 454 | while(i-->0) { |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 455 | pw.print(tab); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 456 | pw.print(mVrStateRemoteCallbacks.getBroadcastItem(i)); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 457 | if (i>0) pw.println(","); |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 458 | } |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 459 | mVrStateRemoteCallbacks.finishBroadcast(); |
| 460 | pw.println("\n\nPersistent Vr State Remote Callbacks:"); |
| 461 | i=mPersistentVrStateRemoteCallbacks.beginBroadcast(); |
| 462 | while(i-->0) { |
| 463 | pw.print(tab); |
| 464 | pw.print(mPersistentVrStateRemoteCallbacks.getBroadcastItem(i)); |
| 465 | if (i>0) pw.println(","); |
| 466 | } |
| 467 | mPersistentVrStateRemoteCallbacks.finishBroadcast(); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 468 | pw.println("\n"); |
| 469 | pw.println("Installed VrListenerService components:"); |
| 470 | int userId = mCurrentVrModeUser; |
| 471 | ArraySet<ComponentName> installed = mComponentObserver.getInstalled(userId); |
| 472 | if (installed == null || installed.size() == 0) { |
| 473 | pw.println("None"); |
| 474 | } else { |
| 475 | for (ComponentName n : installed) { |
| 476 | pw.print(tab); |
| 477 | pw.println(n.flattenToString()); |
| 478 | } |
| 479 | } |
| 480 | pw.println("Enabled VrListenerService components:"); |
| 481 | ArraySet<ComponentName> enabled = mComponentObserver.getEnabled(userId); |
| 482 | if (enabled == null || enabled.size() == 0) { |
| 483 | pw.println("None"); |
| 484 | } else { |
| 485 | for (ComponentName n : enabled) { |
| 486 | pw.print(tab); |
| 487 | pw.println(n.flattenToString()); |
| 488 | } |
| 489 | } |
| 490 | pw.println("\n"); |
| 491 | pw.println("********* End of VrManagerService Dump *********"); |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 492 | } |
| 493 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | private void enforceCallerPermission(String permission) { |
| 497 | if (mContext.checkCallingOrSelfPermission(permission) |
| 498 | != PackageManager.PERMISSION_GRANTED) { |
| 499 | throw new SecurityException("Caller does not hold the permission " + permission); |
| 500 | } |
| 501 | } |
| 502 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 503 | /** |
| 504 | * Implementation of VrManagerInternal. Callable only from system services. |
| 505 | */ |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 506 | private final class LocalService extends VrManagerInternal { |
| 507 | @Override |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 508 | public void setVrMode(boolean enabled, ComponentName packageName, int userId, |
| 509 | ComponentName callingPackage) { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 510 | VrManagerService.this.setVrMode(enabled, packageName, userId, callingPackage); |
Ruben Brunk | af9f004 | 2016-06-15 17:08:35 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | @Override |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 514 | public void onSleepStateChanged(boolean isAsleep) { |
| 515 | VrManagerService.this.setSleepState(isAsleep); |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public void onScreenStateChanged(boolean isScreenOn) { |
| 520 | VrManagerService.this.setScreenOn(isScreenOn); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | @Override |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 524 | public boolean isCurrentVrListener(String packageName, int userId) { |
| 525 | return VrManagerService.this.isCurrentVrListener(packageName, userId); |
| 526 | } |
| 527 | |
| 528 | @Override |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 529 | public int hasVrPackage(ComponentName packageName, int userId) { |
| 530 | return VrManagerService.this.hasVrPackage(packageName, userId); |
| 531 | } |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 532 | |
| 533 | @Override |
| 534 | public void setPersistentVrModeEnabled(boolean enabled) { |
| 535 | VrManagerService.this.setPersistentVrModeEnabled(enabled); |
| 536 | } |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 537 | |
| 538 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 539 | public void setVr2dDisplayProperties( |
| 540 | Vr2dDisplayProperties compatDisplayProp) { |
| 541 | VrManagerService.this.setVr2dDisplayProperties(compatDisplayProp); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 545 | public int getVr2dDisplayId() { |
| 546 | return VrManagerService.this.getVr2dDisplayId(); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | @Override |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 550 | public void addPersistentVrModeStateListener(IPersistentVrStateCallbacks listener) { |
| 551 | VrManagerService.this.addPersistentStateCallback(listener); |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 552 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | public VrManagerService(Context context) { |
| 556 | super(context); |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void onStart() { |
Ruben Brunk | baa4b55 | 2016-02-02 16:27:37 -0800 | [diff] [blame] | 561 | synchronized(mLock) { |
| 562 | initializeNative(); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 563 | mContext = getContext(); |
Ruben Brunk | baa4b55 | 2016-02-02 16:27:37 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 566 | publishLocalService(VrManagerInternal.class, new LocalService()); |
Craig Donner | 8deb67c | 2017-02-07 18:10:32 -0800 | [diff] [blame] | 567 | publishBinderService(Context.VR_SERVICE, mVrManager.asBinder()); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 570 | @Override |
| 571 | public void onBootPhase(int phase) { |
Ruben Brunk | 8507004 | 2016-03-21 12:10:38 -0700 | [diff] [blame] | 572 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 573 | synchronized (mLock) { |
| 574 | Looper looper = Looper.getMainLooper(); |
| 575 | Handler handler = new Handler(looper); |
| 576 | ArrayList<EnabledComponentChangeListener> listeners = new ArrayList<>(); |
| 577 | listeners.add(this); |
| 578 | mComponentObserver = EnabledComponentsObserver.build(mContext, handler, |
| 579 | Settings.Secure.ENABLED_VR_LISTENERS, looper, |
| 580 | android.Manifest.permission.BIND_VR_LISTENER_SERVICE, |
| 581 | VrListenerService.SERVICE_INTERFACE, mLock, listeners); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 582 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 583 | mComponentObserver.rebuildAll(); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 584 | } |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 585 | |
| 586 | //TODO: something more robust than picking the first one |
| 587 | ArraySet<ComponentName> defaultVrComponents = |
| 588 | SystemConfig.getInstance().getDefaultVrComponents(); |
| 589 | if (defaultVrComponents.size() > 0) { |
| 590 | mDefaultVrService = defaultVrComponents.valueAt(0); |
| 591 | } else { |
| 592 | Slog.i(TAG, "No default vr listener service found."); |
| 593 | } |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 594 | |
| 595 | DisplayManager dm = |
| 596 | (DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE); |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 597 | ActivityManagerInternal ami = LocalServices.getService(ActivityManagerInternal.class); |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 598 | mVr2dDisplay = new Vr2dDisplay(dm, ami, mVrManager); |
| 599 | mVr2dDisplay.init(getContext()); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 600 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 601 | synchronized (mLock) { |
| 602 | mVrModeAllowed = true; |
| 603 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 607 | @Override |
| 608 | public void onStartUser(int userHandle) { |
| 609 | synchronized (mLock) { |
| 610 | mComponentObserver.onUsersChanged(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public void onSwitchUser(int userHandle) { |
| 616 | synchronized (mLock) { |
| 617 | mComponentObserver.onUsersChanged(); |
| 618 | } |
| 619 | |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public void onStopUser(int userHandle) { |
| 624 | synchronized (mLock) { |
| 625 | mComponentObserver.onUsersChanged(); |
| 626 | } |
| 627 | |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public void onCleanupUser(int userHandle) { |
| 632 | synchronized (mLock) { |
| 633 | mComponentObserver.onUsersChanged(); |
| 634 | } |
| 635 | } |
| 636 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 637 | private void updateOverlayStateLocked(String exemptedPackage, int newUserId, int oldUserId) { |
| 638 | AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class); |
| 639 | |
| 640 | // If user changed drop restrictions for the old user. |
| 641 | if (oldUserId != newUserId) { |
| 642 | appOpsManager.setUserRestrictionForUser(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, |
| 643 | false, mOverlayToken, null, oldUserId); |
| 644 | } |
| 645 | |
| 646 | // Apply the restrictions for the current user based on vr state |
| 647 | String[] exemptions = (exemptedPackage == null) ? new String[0] : |
| 648 | new String[] { exemptedPackage }; |
| 649 | |
| 650 | appOpsManager.setUserRestrictionForUser(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, |
Ruben Brunk | 2781b41 | 2017-04-24 12:07:17 -0700 | [diff] [blame] | 651 | mVrModeEnabled, mOverlayToken, exemptions, newUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | private void updateDependentAppOpsLocked(String newVrServicePackage, int newUserId, |
| 655 | String oldVrServicePackage, int oldUserId) { |
| 656 | // If VR state changed and we also have a VR service change. |
| 657 | if (Objects.equals(newVrServicePackage, oldVrServicePackage)) { |
| 658 | return; |
| 659 | } |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 660 | final long identity = Binder.clearCallingIdentity(); |
| 661 | try { |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 662 | // Set overlay exception state based on VR enabled and current service |
| 663 | updateOverlayStateLocked(newVrServicePackage, newUserId, oldUserId); |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 664 | } finally { |
| 665 | Binder.restoreCallingIdentity(identity); |
| 666 | } |
| 667 | } |
| 668 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 669 | /** |
| 670 | * Send VR mode changes (if the mode state has changed), and update the bound/unbound state of |
| 671 | * the currently selected VR listener service. If the component selected for the VR listener |
| 672 | * service has changed, unbind the previous listener and bind the new listener (if enabled). |
| 673 | * <p/> |
| 674 | * Note: Must be called while holding {@code mLock}. |
| 675 | * |
| 676 | * @param enabled new state for VR mode. |
| 677 | * @param component new component to be bound as a VR listener. |
| 678 | * @param userId user owning the component to be bound. |
Alex Sakhartchouk | 6b1251f | 2017-05-17 12:34:53 -0400 | [diff] [blame] | 679 | * @param calling the component currently using VR mode. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 680 | * |
| 681 | * @return {@code true} if the component/user combination specified is valid. |
| 682 | */ |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 683 | private boolean updateCurrentVrServiceLocked(boolean enabled, @NonNull ComponentName component, |
| 684 | int userId, ComponentName calling) { |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 685 | |
| 686 | boolean sendUpdatedCaller = false; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 687 | final long identity = Binder.clearCallingIdentity(); |
| 688 | try { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 689 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 690 | boolean validUserComponent = (mComponentObserver.isValid(component, userId) == |
| 691 | EnabledComponentsObserver.NO_ERROR); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 692 | boolean goingIntoVrMode = validUserComponent && enabled; |
| 693 | if (!mVrModeEnabled && !goingIntoVrMode) { |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 694 | return validUserComponent; // Disabled -> Disabled transition does nothing. |
| 695 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 696 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 697 | String oldVrServicePackage = mCurrentVrService != null |
| 698 | ? mCurrentVrService.getComponent().getPackageName() : null; |
| 699 | final int oldUserId = mCurrentVrModeUser; |
| 700 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 701 | // Notify system services and VR HAL of mode change. |
| 702 | changeVrModeLocked(goingIntoVrMode); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 703 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 704 | boolean nothingChanged = false; |
| 705 | if (!goingIntoVrMode) { |
| 706 | // Not going into VR mode, unbind whatever is running |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 707 | if (mCurrentVrService != null) { |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 708 | Slog.i(TAG, "Leaving VR mode, disconnecting " |
| 709 | + mCurrentVrService.getComponent() + " for user " |
| 710 | + mCurrentVrService.getUserId()); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 711 | mCurrentVrService.disconnect(); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 712 | mCurrentVrService = null; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 713 | } else { |
| 714 | nothingChanged = true; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 715 | } |
| 716 | } else { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 717 | // Going into VR mode |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 718 | if (mCurrentVrService != null) { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 719 | // Unbind any running service that doesn't match the latest component/user |
| 720 | // selection. |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 721 | if (mCurrentVrService.disconnectIfNotMatching(component, userId)) { |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 722 | Slog.i(TAG, "VR mode component changed to " + component |
| 723 | + ", disconnecting " + mCurrentVrService.getComponent() |
| 724 | + " for user " + mCurrentVrService.getUserId()); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 725 | createAndConnectService(component, userId); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 726 | sendUpdatedCaller = true; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 727 | } else { |
| 728 | nothingChanged = true; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 729 | } |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 730 | // The service with the correct component/user is already bound, do nothing. |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 731 | } else { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 732 | // Nothing was previously running, bind a new service for the latest |
| 733 | // component/user selection. |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 734 | createAndConnectService(component, userId); |
| 735 | sendUpdatedCaller = true; |
| 736 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Ruben Brunk | 666505e | 2017-05-04 16:54:46 -0700 | [diff] [blame] | 739 | if ((calling != null || mPersistentVrModeEnabled) |
| 740 | && !Objects.equals(calling, mCurrentVrModeComponent)) { |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 741 | sendUpdatedCaller = true; |
| 742 | } |
Ruben Brunk | 7196a6b | 2017-03-22 15:43:24 -0700 | [diff] [blame] | 743 | mCurrentVrModeComponent = calling; |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 744 | |
| 745 | if (mCurrentVrModeUser != userId) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 746 | mCurrentVrModeUser = userId; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 747 | sendUpdatedCaller = true; |
| 748 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 749 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 750 | String newVrServicePackage = mCurrentVrService != null |
| 751 | ? mCurrentVrService.getComponent().getPackageName() : null; |
| 752 | final int newUserId = mCurrentVrModeUser; |
| 753 | |
| 754 | // Update AppOps settings that change state when entering/exiting VR mode, or changing |
| 755 | // the current VrListenerService. |
| 756 | updateDependentAppOpsLocked(newVrServicePackage, newUserId, |
| 757 | oldVrServicePackage, oldUserId); |
| 758 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 759 | if (mCurrentVrService != null && sendUpdatedCaller) { |
| 760 | final ComponentName c = mCurrentVrModeComponent; |
| 761 | mCurrentVrService.sendEvent(new PendingEvent() { |
| 762 | @Override |
| 763 | public void runEvent(IInterface service) throws RemoteException { |
| 764 | IVrListener l = (IVrListener) service; |
| 765 | l.focusedActivityChanged(c); |
| 766 | } |
| 767 | }); |
| 768 | } |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 769 | |
| 770 | if (!nothingChanged) { |
| 771 | logStateLocked(); |
| 772 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 773 | |
| 774 | return validUserComponent; |
| 775 | } finally { |
| 776 | Binder.restoreCallingIdentity(identity); |
| 777 | } |
| 778 | } |
| 779 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 780 | private boolean isDefaultAllowed(String packageName) { |
Craig Donner | 0568f28 | 2016-04-11 13:02:44 -0700 | [diff] [blame] | 781 | PackageManager pm = mContext.getPackageManager(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 782 | |
| 783 | ApplicationInfo info = null; |
| 784 | try { |
| 785 | info = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); |
| 786 | } catch (NameNotFoundException e) { |
| 787 | } |
| 788 | |
| 789 | if (info == null || !(info.isSystemApp() || info.isUpdatedSystemApp())) { |
| 790 | return false; |
| 791 | } |
| 792 | return true; |
| 793 | } |
| 794 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 795 | private void grantNotificationPolicyAccess(String pkg) { |
| 796 | NotificationManager nm = mContext.getSystemService(NotificationManager.class); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 797 | nm.setNotificationPolicyAccessGranted(pkg, true); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | private void revokeNotificationPolicyAccess(String pkg) { |
| 801 | NotificationManager nm = mContext.getSystemService(NotificationManager.class); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 802 | // Remove any DND zen rules possibly created by the package. |
| 803 | nm.removeAutomaticZenRules(pkg); |
| 804 | // Remove Notification Policy Access. |
| 805 | nm.setNotificationPolicyAccessGranted(pkg, false); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 808 | private void grantNotificationListenerAccess(String pkg, int userId) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 809 | PackageManager pm = mContext.getPackageManager(); |
| 810 | ArraySet<ComponentName> possibleServices = EnabledComponentsObserver.loadComponentNames(pm, |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 811 | userId, NotificationListenerService.SERVICE_INTERFACE, |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 812 | android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE); |
| 813 | ContentResolver resolver = mContext.getContentResolver(); |
| 814 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 815 | ArraySet<String> current = getNotificationListeners(resolver, userId); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 816 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 817 | for (ComponentName c : possibleServices) { |
| 818 | String flatName = c.flattenToString(); |
| 819 | if (Objects.equals(c.getPackageName(), pkg) |
| 820 | && !current.contains(flatName)) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 821 | current.add(flatName); |
| 822 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 825 | if (current.size() > 0) { |
| 826 | String flatSettings = formatSettings(current); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 827 | Settings.Secure.putStringForUser(resolver, |
| 828 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, |
| 829 | flatSettings, userId); |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 830 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 833 | private void revokeNotificationListenerAccess(String pkg, int userId) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 834 | ContentResolver resolver = mContext.getContentResolver(); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 835 | |
| 836 | ArraySet<String> current = getNotificationListeners(resolver, userId); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 837 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 838 | ArrayList<String> toRemove = new ArrayList<>(); |
| 839 | |
| 840 | for (String c : current) { |
| 841 | ComponentName component = ComponentName.unflattenFromString(c); |
Ruben Brunk | 1f4f26b | 2016-08-02 14:21:36 -0700 | [diff] [blame] | 842 | if (component != null && component.getPackageName().equals(pkg)) { |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 843 | toRemove.add(c); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | current.removeAll(toRemove); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 848 | |
| 849 | String flatSettings = formatSettings(current); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 850 | Settings.Secure.putStringForUser(resolver, |
| 851 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, |
| 852 | flatSettings, userId); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 855 | private void grantCoarseLocationPermissionIfNeeded(String pkg, int userId) { |
| 856 | // Don't clobber the user if permission set in current state explicitly |
| 857 | if (!isPermissionUserUpdated(Manifest.permission.ACCESS_COARSE_LOCATION, pkg, userId)) { |
Ruben Brunk | 12ab5e1 | 2016-11-10 15:27:30 -0800 | [diff] [blame] | 858 | try { |
| 859 | mContext.getPackageManager().grantRuntimePermission(pkg, |
| 860 | Manifest.permission.ACCESS_COARSE_LOCATION, new UserHandle(userId)); |
| 861 | } catch (IllegalArgumentException e) { |
| 862 | // Package was removed during update. |
| 863 | Slog.w(TAG, "Could not grant coarse location permission, package " + pkg |
| 864 | + " was removed."); |
| 865 | } |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
| 869 | private void revokeCoarseLocationPermissionIfNeeded(String pkg, int userId) { |
| 870 | // Don't clobber the user if permission set in current state explicitly |
| 871 | if (!isPermissionUserUpdated(Manifest.permission.ACCESS_COARSE_LOCATION, pkg, userId)) { |
Ruben Brunk | 12ab5e1 | 2016-11-10 15:27:30 -0800 | [diff] [blame] | 872 | try { |
| 873 | mContext.getPackageManager().revokeRuntimePermission(pkg, |
| 874 | Manifest.permission.ACCESS_COARSE_LOCATION, new UserHandle(userId)); |
| 875 | } catch (IllegalArgumentException e) { |
| 876 | // Package was removed during update. |
| 877 | Slog.w(TAG, "Could not revoke coarse location permission, package " + pkg |
| 878 | + " was removed."); |
| 879 | } |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 883 | private boolean isPermissionUserUpdated(String permission, String pkg, int userId) { |
| 884 | final int flags = mContext.getPackageManager().getPermissionFlags( |
| 885 | permission, pkg, new UserHandle(userId)); |
| 886 | return (flags & (PackageManager.FLAG_PERMISSION_USER_SET |
| 887 | | PackageManager.FLAG_PERMISSION_USER_FIXED)) != 0; |
| 888 | } |
| 889 | |
| 890 | private ArraySet<String> getNotificationListeners(ContentResolver resolver, int userId) { |
| 891 | String flat = Settings.Secure.getStringForUser(resolver, |
| 892 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, userId); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 893 | |
| 894 | ArraySet<String> current = new ArraySet<>(); |
| 895 | if (flat != null) { |
| 896 | String[] allowed = flat.split(":"); |
| 897 | for (String s : allowed) { |
Ruben Brunk | 1f4f26b | 2016-08-02 14:21:36 -0700 | [diff] [blame] | 898 | if (!TextUtils.isEmpty(s)) { |
| 899 | current.add(s); |
| 900 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | return current; |
| 904 | } |
| 905 | |
| 906 | private static String formatSettings(Collection<String> c) { |
| 907 | if (c == null || c.isEmpty()) { |
| 908 | return ""; |
| 909 | } |
| 910 | |
| 911 | StringBuilder b = new StringBuilder(); |
| 912 | boolean start = true; |
| 913 | for (String s : c) { |
| 914 | if ("".equals(s)) { |
| 915 | continue; |
| 916 | } |
| 917 | if (!start) { |
| 918 | b.append(':'); |
| 919 | } |
| 920 | b.append(s); |
| 921 | start = false; |
| 922 | } |
| 923 | return b.toString(); |
| 924 | } |
| 925 | |
| 926 | |
| 927 | |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 928 | private void createAndConnectService(@NonNull ComponentName component, int userId) { |
| 929 | mCurrentVrService = VrManagerService.create(mContext, component, userId); |
| 930 | mCurrentVrService.connect(); |
| 931 | Slog.i(TAG, "Connecting " + component + " for user " + userId); |
| 932 | } |
| 933 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 934 | /** |
| 935 | * Send VR mode change callbacks to HAL and system services if mode has actually changed. |
| 936 | * <p/> |
| 937 | * Note: Must be called while holding {@code mLock}. |
| 938 | * |
| 939 | * @param enabled new state of the VR mode. |
| 940 | */ |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 941 | private void changeVrModeLocked(boolean enabled) { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 942 | if (mVrModeEnabled != enabled) { |
| 943 | mVrModeEnabled = enabled; |
| 944 | |
| 945 | // Log mode change event. |
| 946 | Slog.i(TAG, "VR mode " + ((mVrModeEnabled) ? "enabled" : "disabled")); |
| 947 | setVrModeNative(mVrModeEnabled); |
| 948 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 949 | onVrModeChangedLocked(); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * Notify system services of VR mode change. |
| 955 | * <p/> |
| 956 | * Note: Must be called while holding {@code mLock}. |
| 957 | */ |
| 958 | private void onVrModeChangedLocked() { |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 959 | mHandler.sendMessage(mHandler.obtainMessage(MSG_VR_STATE_CHANGE, |
| 960 | (mVrModeEnabled) ? 1 : 0, 0)); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Helper function for making ManagedApplicationService instances. |
| 965 | */ |
| 966 | private static ManagedApplicationService create(@NonNull Context context, |
| 967 | @NonNull ComponentName component, int userId) { |
| 968 | return ManagedApplicationService.build(context, component, userId, |
| 969 | R.string.vr_listener_binding_label, Settings.ACTION_VR_LISTENER_SETTINGS, |
| 970 | sBinderChecker); |
| 971 | } |
| 972 | |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 973 | /** |
| 974 | * Apply the pending VR state. If no state is pending, disconnect any currently bound |
| 975 | * VR listener service. |
| 976 | */ |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 977 | private void consumeAndApplyPendingStateLocked() { |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 978 | consumeAndApplyPendingStateLocked(true); |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * Apply the pending VR state. |
| 983 | * |
| 984 | * @param disconnectIfNoPendingState if {@code true}, then any currently bound VR listener |
| 985 | * service will be disconnected if no state is pending. If this is {@code false} then the |
| 986 | * nothing will be changed when there is no pending state. |
| 987 | */ |
| 988 | private void consumeAndApplyPendingStateLocked(boolean disconnectIfNoPendingState) { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 989 | if (mPendingState != null) { |
| 990 | updateCurrentVrServiceLocked(mPendingState.enabled, |
| 991 | mPendingState.targetPackageName, mPendingState.userId, |
| 992 | mPendingState.callingPackage); |
| 993 | mPendingState = null; |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 994 | } else if (disconnectIfNoPendingState) { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 995 | updateCurrentVrServiceLocked(false, null, 0, null); |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 999 | private void logStateLocked() { |
| 1000 | ComponentName currentBoundService = (mCurrentVrService == null) ? null : |
| 1001 | mCurrentVrService.getComponent(); |
| 1002 | VrState current = new VrState(mVrModeEnabled, currentBoundService, mCurrentVrModeUser, |
| 1003 | mCurrentVrModeComponent, mWasDefaultGranted); |
| 1004 | if (mLoggingDeque.size() == EVENT_LOG_SIZE) { |
| 1005 | mLoggingDeque.removeFirst(); |
| 1006 | } |
| 1007 | mLoggingDeque.add(current); |
| 1008 | } |
| 1009 | |
| 1010 | private void dumpStateTransitions(PrintWriter pw) { |
| 1011 | SimpleDateFormat d = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); |
| 1012 | String tab = " "; |
| 1013 | if (mLoggingDeque.size() == 0) { |
| 1014 | pw.print(tab); |
| 1015 | pw.println("None"); |
| 1016 | } |
| 1017 | for (VrState state : mLoggingDeque) { |
| 1018 | pw.print(d.format(new Date(state.timestamp))); |
| 1019 | pw.print(tab); |
| 1020 | pw.print("State changed to:"); |
| 1021 | pw.print(tab); |
| 1022 | pw.println((state.enabled) ? "ENABLED" : "DISABLED"); |
| 1023 | if (state.enabled) { |
| 1024 | pw.print(tab); |
| 1025 | pw.print("User="); |
| 1026 | pw.println(state.userId); |
| 1027 | pw.print(tab); |
| 1028 | pw.print("Current VR Activity="); |
| 1029 | pw.println((state.callingPackage == null) ? |
| 1030 | "None" : state.callingPackage.flattenToString()); |
| 1031 | pw.print(tab); |
| 1032 | pw.print("Bound VrListenerService="); |
| 1033 | pw.println((state.targetPackageName == null) ? |
| 1034 | "None" : state.targetPackageName.flattenToString()); |
| 1035 | if (state.defaultPermissionsGranted) { |
| 1036 | pw.print(tab); |
| 1037 | pw.println("Default permissions granted to the bound VrListenerService."); |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1043 | /* |
| 1044 | * Implementation of VrManagerInternal calls. These are callable from system services. |
| 1045 | */ |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1046 | private void setVrMode(boolean enabled, @NonNull ComponentName targetPackageName, |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 1047 | int userId, @NonNull ComponentName callingPackage) { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1048 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1049 | synchronized (mLock) { |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1050 | VrState pending; |
| 1051 | ComponentName targetListener; |
| 1052 | ComponentName foregroundVrComponent; |
| 1053 | |
| 1054 | // If the device is in persistent VR mode, then calls to disable VR mode are ignored, |
| 1055 | // and the system default VR listener is used. |
| 1056 | boolean targetEnabledState = enabled || mPersistentVrModeEnabled; |
| 1057 | if (!enabled && mPersistentVrModeEnabled) { |
| 1058 | targetListener = mDefaultVrService; |
| 1059 | |
| 1060 | // Current foreground component isn't a VR one (in 2D app case) |
| 1061 | foregroundVrComponent = null; |
| 1062 | } else { |
| 1063 | targetListener = targetPackageName; |
| 1064 | foregroundVrComponent = callingPackage; |
| 1065 | } |
| 1066 | pending = new VrState( |
| 1067 | targetEnabledState, targetListener, userId, foregroundVrComponent); |
| 1068 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 1069 | if (!mVrModeAllowed) { |
| 1070 | // We're not allowed to be in VR mode. Make this state pending. This will be |
| 1071 | // applied the next time we are allowed to enter VR mode unless it is superseded by |
| 1072 | // another call. |
| 1073 | mPendingState = pending; |
| 1074 | return; |
| 1075 | } |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1076 | |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1077 | if (!targetEnabledState && mCurrentVrService != null) { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1078 | // If we're transitioning out of VR mode, delay briefly to avoid expensive HAL calls |
| 1079 | // and service bind/unbind in case we are immediately switching to another VR app. |
| 1080 | if (mPendingState == null) { |
| 1081 | mHandler.sendEmptyMessageDelayed(MSG_PENDING_VR_STATE_CHANGE, |
| 1082 | PENDING_STATE_DELAY_MS); |
| 1083 | } |
| 1084 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 1085 | mPendingState = pending; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1086 | return; |
| 1087 | } else { |
| 1088 | mHandler.removeMessages(MSG_PENDING_VR_STATE_CHANGE); |
| 1089 | mPendingState = null; |
| 1090 | } |
| 1091 | |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1092 | updateCurrentVrServiceLocked( |
| 1093 | targetEnabledState, targetListener, userId, foregroundVrComponent); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | private void setPersistentVrModeEnabled(boolean enabled) { |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 1098 | synchronized(mLock) { |
| 1099 | setPersistentModeAndNotifyListenersLocked(enabled); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1100 | // Disabling persistent mode when not showing a VR should disable the overall vr mode. |
| 1101 | if (!enabled && mCurrentVrModeComponent == null) { |
| 1102 | setVrMode(false, null, 0, null); |
| 1103 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1107 | public void setVr2dDisplayProperties( |
| 1108 | Vr2dDisplayProperties compatDisplayProp) { |
| 1109 | if (mVr2dDisplay != null) { |
| 1110 | mVr2dDisplay.setVirtualDisplayProperties(compatDisplayProp); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 1111 | return; |
| 1112 | } |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1113 | Slog.w(TAG, "Vr2dDisplay is null!"); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1116 | private int getVr2dDisplayId() { |
| 1117 | if (mVr2dDisplay != null) { |
| 1118 | return mVr2dDisplay.getVirtualDisplayId(); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 1119 | } |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1120 | Slog.w(TAG, "Vr2dDisplay is null!"); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 1121 | return INVALID_DISPLAY; |
| 1122 | } |
| 1123 | |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 1124 | private void setPersistentModeAndNotifyListenersLocked(boolean enabled) { |
| 1125 | if (mPersistentVrModeEnabled == enabled) { |
| 1126 | return; |
| 1127 | } |
| 1128 | mPersistentVrModeEnabled = enabled; |
| 1129 | |
| 1130 | mHandler.sendMessage(mHandler.obtainMessage(MSG_PERSISTENT_VR_MODE_STATE_CHANGE, |
| 1131 | (mPersistentVrModeEnabled) ? 1 : 0, 0)); |
| 1132 | } |
| 1133 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1134 | private int hasVrPackage(@NonNull ComponentName targetPackageName, int userId) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1135 | synchronized (mLock) { |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1136 | return mComponentObserver.isValid(targetPackageName, userId); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 1140 | private boolean isCurrentVrListener(String packageName, int userId) { |
| 1141 | synchronized (mLock) { |
| 1142 | if (mCurrentVrService == null) { |
| 1143 | return false; |
| 1144 | } |
| 1145 | return mCurrentVrService.getComponent().getPackageName().equals(packageName) && |
| 1146 | userId == mCurrentVrService.getUserId(); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1150 | /* |
| 1151 | * Implementation of IVrManager calls. |
| 1152 | */ |
| 1153 | |
| 1154 | private void addStateCallback(IVrStateCallbacks cb) { |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 1155 | mVrStateRemoteCallbacks.register(cb); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1158 | private void removeStateCallback(IVrStateCallbacks cb) { |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 1159 | mVrStateRemoteCallbacks.unregister(cb); |
| 1160 | } |
| 1161 | |
| 1162 | private void addPersistentStateCallback(IPersistentVrStateCallbacks cb) { |
| 1163 | mPersistentVrStateRemoteCallbacks.register(cb); |
| 1164 | } |
| 1165 | |
| 1166 | private void removePersistentStateCallback(IPersistentVrStateCallbacks cb) { |
| 1167 | mPersistentVrStateRemoteCallbacks.unregister(cb); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1170 | private boolean getVrMode() { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1171 | synchronized (mLock) { |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1172 | return mVrModeEnabled; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1175 | } |