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