blob: 0453a9ea2d06f7c58c41c3932724109965280932 [file] [log] [blame]
Zhentao Sunc6cd1f92015-07-21 17:43:53 -07001/*
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
17package com.android.server;
18
Jason Monk5a5e7d62015-08-04 11:20:25 -040019import android.app.ActivityManager;
Jonathan Solnit4587d102017-05-17 10:28:13 -070020import android.app.KeyguardManager;
Jorim Jaggi40aa8812015-09-23 12:59:22 -070021import android.app.StatusBarManager;
Jason Monk5a5e7d62015-08-04 11:20:25 -040022import android.content.BroadcastReceiver;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070023import android.content.Context;
24import android.content.Intent;
Jason Monk5a5e7d62015-08-04 11:20:25 -040025import android.content.IntentFilter;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070026import android.content.res.Resources;
Jason Monk5a5e7d62015-08-04 11:20:25 -040027import android.database.ContentObserver;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070028import android.hardware.Sensor;
29import android.hardware.SensorEvent;
30import android.hardware.SensorEventListener;
31import android.hardware.SensorManager;
Jason Monk5a5e7d62015-08-04 11:20:25 -040032import android.os.Handler;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070033import android.os.PowerManager;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070034import android.os.PowerManager.WakeLock;
Zhentao Sun575b6fd2015-08-25 21:26:45 -070035import android.os.SystemClock;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070036import android.os.SystemProperties;
Amith Yamasanif16c3be2016-07-15 15:33:03 -070037import android.os.UserHandle;
Kevin Gabayan024713c2015-07-31 12:01:43 -070038import android.provider.Settings;
Jorim Jaggif1cdf952016-04-05 21:41:06 -070039import android.util.MutableBoolean;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070040import android.util.Slog;
Adrian Roos5941c982015-09-03 15:59:49 -070041import android.view.KeyEvent;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070042
Juan Langacf62e62017-04-18 16:35:39 -070043import com.android.internal.annotations.VisibleForTesting;
Adrian Roos5941c982015-09-03 15:59:49 -070044import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010045import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Selim Cinek372d1bd2015-08-14 13:19:37 -070046import com.android.server.statusbar.StatusBarManagerInternal;
47
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070048/**
49 * The service that listens for gestures detected in sensor firmware and starts the intent
50 * accordingly.
51 * <p>For now, only camera launch gesture is supported, and in the future, more gestures can be
52 * added.</p>
53 * @hide
54 */
Adrian Roos5941c982015-09-03 15:59:49 -070055public class GestureLauncherService extends SystemService {
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070056 private static final boolean DBG = false;
57 private static final String TAG = "GestureLauncherService";
58
Adrian Roos5941c982015-09-03 15:59:49 -070059 /**
60 * Time in milliseconds in which the power button must be pressed twice so it will be considered
61 * as a camera launch.
62 */
Juan Langacf62e62017-04-18 16:35:39 -070063 @VisibleForTesting static final long CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS = 300;
Adrian Roos5941c982015-09-03 15:59:49 -070064
Juan Lang0ba22372017-04-05 13:03:04 -070065 /**
66 * Interval in milliseconds in which the power button must be depressed in succession to be
67 * considered part of an extended sequence of taps. Note that this is a looser threshold than
68 * the camera launch gesture, because the purpose of this threshold is to measure the
69 * frequency of consecutive taps, for evaluation for future gestures.
70 */
71 @VisibleForTesting static final long POWER_SHORT_TAP_SEQUENCE_MAX_INTERVAL_MS = 500;
72
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070073 /** The listener that receives the gesture event. */
74 private final GestureEventListener mGestureListener = new GestureEventListener();
Jonathan Solnita4138162017-05-10 21:06:04 -070075 private final CameraLiftTriggerEventListener mCameraLiftTriggerListener =
76 new CameraLiftTriggerEventListener();
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070077
78 private Sensor mCameraLaunchSensor;
Jonathan Solnita4138162017-05-10 21:06:04 -070079 private Sensor mCameraLiftTriggerSensor;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070080 private Context mContext;
Juan Lang36f46242017-04-19 11:58:28 -070081 private final MetricsLogger mMetricsLogger;
Jonathan Solnit4587d102017-05-17 10:28:13 -070082 private PowerManager mPowerManager;
83 private KeyguardManager mKeyguardManager;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070084
85 /** The wake lock held when a gesture is detected. */
86 private WakeLock mWakeLock;
Jonathan Solnita4138162017-05-10 21:06:04 -070087 private boolean mCameraLaunchRegistered;
88 private boolean mCameraLiftRegistered;
Jason Monk5a5e7d62015-08-04 11:20:25 -040089 private int mUserId;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070090
Zhentao Sun575b6fd2015-08-25 21:26:45 -070091 // Below are fields used for event logging only.
92 /** Elapsed real time when the camera gesture is turned on. */
93 private long mCameraGestureOnTimeMs = 0L;
94
95 /** Elapsed real time when the last camera gesture was detected. */
96 private long mCameraGestureLastEventTime = 0L;
97
98 /**
99 * How long the sensor 1 has been turned on since camera launch sensor was
100 * subscribed to and when the last camera launch gesture was detected.
101 * <p>Sensor 1 is the main sensor used to detect camera launch gesture.</p>
102 */
103 private long mCameraGestureSensor1LastOnTimeMs = 0L;
104
105 /**
106 * If applicable, how long the sensor 2 has been turned on since camera
107 * launch sensor was subscribed to and when the last camera launch
108 * gesture was detected.
109 * <p>Sensor 2 is the secondary sensor used to detect camera launch gesture.
110 * This is optional and if only sensor 1 is used for detect camera launch
111 * gesture, this value would always be 0.</p>
112 */
113 private long mCameraGestureSensor2LastOnTimeMs = 0L;
114
115 /**
116 * Extra information about the event when the last camera launch gesture
117 * was detected.
118 */
119 private int mCameraLaunchLastEventExtra = 0;
120
Adrian Roos5941c982015-09-03 15:59:49 -0700121 /**
122 * Whether camera double tap power button gesture is currently enabled;
123 */
124 private boolean mCameraDoubleTapPowerEnabled;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700125 private long mLastPowerDown;
Juan Lang0ba22372017-04-05 13:03:04 -0700126 private int mPowerButtonConsecutiveTaps;
Adrian Roos5941c982015-09-03 15:59:49 -0700127
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700128 public GestureLauncherService(Context context) {
Juan Lang36f46242017-04-19 11:58:28 -0700129 this(context, new MetricsLogger());
130 }
131
132 @VisibleForTesting
133 GestureLauncherService(Context context, MetricsLogger metricsLogger) {
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700134 super(context);
135 mContext = context;
Juan Lang36f46242017-04-19 11:58:28 -0700136 mMetricsLogger = metricsLogger;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700137 }
138
139 public void onStart() {
Adrian Roos5941c982015-09-03 15:59:49 -0700140 LocalServices.addService(GestureLauncherService.class, this);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700141 }
142
143 public void onBootPhase(int phase) {
144 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
145 Resources resources = mContext.getResources();
146 if (!isGestureLauncherEnabled(resources)) {
147 if (DBG) Slog.d(TAG, "Gesture launcher is disabled in system properties.");
148 return;
149 }
150
Jonathan Solnit4587d102017-05-17 10:28:13 -0700151 mKeyguardManager = (KeyguardManager) mContext.getSystemService(
152 Context.KEYGUARD_SERVICE);
153 mPowerManager = (PowerManager) mContext.getSystemService(
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700154 Context.POWER_SERVICE);
Jonathan Solnit4587d102017-05-17 10:28:13 -0700155 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700156 "GestureLauncherService");
Jason Monk5a5e7d62015-08-04 11:20:25 -0400157 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700158 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400159
160 mUserId = ActivityManager.getCurrentUser();
161 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
Adrian Roos5941c982015-09-03 15:59:49 -0700162 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400163 }
164 }
165
Adrian Roos5941c982015-09-03 15:59:49 -0700166 private void registerContentObservers() {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400167 mContext.getContentResolver().registerContentObserver(
168 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
169 false, mSettingObserver, mUserId);
Adrian Roos5941c982015-09-03 15:59:49 -0700170 mContext.getContentResolver().registerContentObserver(
171 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
172 false, mSettingObserver, mUserId);
Jonathan Solnita4138162017-05-10 21:06:04 -0700173 mContext.getContentResolver().registerContentObserver(
174 Settings.Secure.getUriFor(Settings.Secure.CAMERA_LIFT_TRIGGER_ENABLED),
175 false, mSettingObserver, mUserId);
Jason Monk5a5e7d62015-08-04 11:20:25 -0400176 }
177
178 private void updateCameraRegistered() {
179 Resources resources = mContext.getResources();
180 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
181 registerCameraLaunchGesture(resources);
182 } else {
183 unregisterCameraLaunchGesture();
184 }
Jonathan Solnita4138162017-05-10 21:06:04 -0700185
186 if (isCameraLiftTriggerSettingEnabled(mContext, mUserId)) {
187 registerCameraLiftTrigger(resources);
188 } else {
189 unregisterCameraLiftTrigger();
190 }
Jason Monk5a5e7d62015-08-04 11:20:25 -0400191 }
192
Juan Langacf62e62017-04-18 16:35:39 -0700193 @VisibleForTesting
194 void updateCameraDoubleTapPowerEnabled() {
Adrian Roos5941c982015-09-03 15:59:49 -0700195 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
196 synchronized (this) {
197 mCameraDoubleTapPowerEnabled = enabled;
198 }
199 }
200
Jason Monk5a5e7d62015-08-04 11:20:25 -0400201 private void unregisterCameraLaunchGesture() {
Jonathan Solnita4138162017-05-10 21:06:04 -0700202 if (mCameraLaunchRegistered) {
203 mCameraLaunchRegistered = false;
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700204 mCameraGestureOnTimeMs = 0L;
205 mCameraGestureLastEventTime = 0L;
206 mCameraGestureSensor1LastOnTimeMs = 0;
207 mCameraGestureSensor2LastOnTimeMs = 0;
208 mCameraLaunchLastEventExtra = 0;
209
Jason Monk5a5e7d62015-08-04 11:20:25 -0400210 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
211 Context.SENSOR_SERVICE);
212 sensorManager.unregisterListener(mGestureListener);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700213 }
214 }
215
216 /**
217 * Registers for the camera launch gesture.
218 */
219 private void registerCameraLaunchGesture(Resources resources) {
Jonathan Solnita4138162017-05-10 21:06:04 -0700220 if (mCameraLaunchRegistered) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400221 return;
222 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700223 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
224 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700225 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
226 Context.SENSOR_SERVICE);
227 int cameraLaunchGestureId = resources.getInteger(
228 com.android.internal.R.integer.config_cameraLaunchGestureSensorType);
229 if (cameraLaunchGestureId != -1) {
Jonathan Solnita4138162017-05-10 21:06:04 -0700230 mCameraLaunchRegistered = false;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700231 String sensorName = resources.getString(
232 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
233 mCameraLaunchSensor = sensorManager.getDefaultSensor(
234 cameraLaunchGestureId,
235 true /*wakeUp*/);
236
237 // Compare the camera gesture string type to that in the resource file to make
238 // sure we are registering the correct sensor. This is redundant check, it
239 // makes the code more robust.
240 if (mCameraLaunchSensor != null) {
241 if (sensorName.equals(mCameraLaunchSensor.getStringType())) {
Jonathan Solnita4138162017-05-10 21:06:04 -0700242 mCameraLaunchRegistered = sensorManager.registerListener(mGestureListener,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700243 mCameraLaunchSensor, 0);
244 } else {
245 String message = String.format("Wrong configuration. Sensor type and sensor "
246 + "string type don't match: %s in resources, %s in the sensor.",
247 sensorName, mCameraLaunchSensor.getStringType());
248 throw new RuntimeException(message);
249 }
250 }
Jonathan Solnita4138162017-05-10 21:06:04 -0700251 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mCameraLaunchRegistered);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700252 } else {
253 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
254 }
255 }
256
Jonathan Solnita4138162017-05-10 21:06:04 -0700257 private void unregisterCameraLiftTrigger() {
258 if (mCameraLiftRegistered) {
259 mCameraLiftRegistered = false;
260
261 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
262 Context.SENSOR_SERVICE);
263 sensorManager.unregisterListener(mCameraLiftTriggerListener);
264 }
265 }
266
267 /**
268 * Registers for the camera lift trigger.
269 */
270 private void registerCameraLiftTrigger(Resources resources) {
271 if (mCameraLiftRegistered) {
272 return;
273 }
274 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
275 Context.SENSOR_SERVICE);
276 int cameraLiftTriggerId = resources.getInteger(
277 com.android.internal.R.integer.config_cameraLiftTriggerSensorType);
278 if (cameraLiftTriggerId != -1) {
279 mCameraLiftRegistered = false;
280 String sensorName = resources.getString(
281 com.android.internal.R.string.config_cameraLiftTriggerSensorStringType);
282 mCameraLiftTriggerSensor = sensorManager.getDefaultSensor(
283 cameraLiftTriggerId,
284 false /*wakeUp*/);
285
286 // Compare the camera lift trigger string type to that in the resource file to make
287 // sure we are registering the correct sensor. This is redundant check, it
288 // makes the code more robust.
289 if (mCameraLiftTriggerSensor != null) {
290 if (sensorName.equals(mCameraLiftTriggerSensor.getStringType())) {
291 mCameraLiftRegistered = sensorManager.registerListener(mCameraLiftTriggerListener,
292 mCameraLiftTriggerSensor, 0);
293 } else {
294 String message = String.format("Wrong configuration. Sensor type and sensor "
295 + "string type don't match: %s in resources, %s in the sensor.",
296 sensorName, mCameraLiftTriggerSensor.getStringType());
297 throw new RuntimeException(message);
298 }
299 }
300 if (DBG) Slog.d(TAG, "Camera lift trigger sensor registered: " + mCameraLiftRegistered);
301 } else {
302 if (DBG) Slog.d(TAG, "Camera lift trigger sensor is not specified.");
303 }
304 }
305
Jason Monk5a5e7d62015-08-04 11:20:25 -0400306 public static boolean isCameraLaunchSettingEnabled(Context context, int userId) {
307 return isCameraLaunchEnabled(context.getResources())
308 && (Settings.Secure.getIntForUser(context.getContentResolver(),
309 Settings.Secure.CAMERA_GESTURE_DISABLED, 0, userId) == 0);
310 }
311
Adrian Roos5941c982015-09-03 15:59:49 -0700312 public static boolean isCameraDoubleTapPowerSettingEnabled(Context context, int userId) {
313 return isCameraDoubleTapPowerEnabled(context.getResources())
314 && (Settings.Secure.getIntForUser(context.getContentResolver(),
315 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0, userId) == 0);
316 }
317
Jonathan Solnita4138162017-05-10 21:06:04 -0700318 public static boolean isCameraLiftTriggerSettingEnabled(Context context, int userId) {
319 return isCameraLiftTriggerEnabled(context.getResources())
320 && (Settings.Secure.getIntForUser(context.getContentResolver(),
321 Settings.Secure.CAMERA_LIFT_TRIGGER_ENABLED, 0, userId) != 0);
322 }
323
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700324 /**
325 * Whether to enable the camera launch gesture.
326 */
327 public static boolean isCameraLaunchEnabled(Resources resources) {
328 boolean configSet = resources.getInteger(
329 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
330 return configSet &&
331 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
332 }
333
Adrian Roos5941c982015-09-03 15:59:49 -0700334 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
335 return resources.getBoolean(
336 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
337 }
338
Jonathan Solnita4138162017-05-10 21:06:04 -0700339 public static boolean isCameraLiftTriggerEnabled(Resources resources) {
340 boolean configSet = resources.getInteger(
341 com.android.internal.R.integer.config_cameraLiftTriggerSensorType) != -1;
342 return configSet;
343 }
344
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700345 /**
346 * Whether GestureLauncherService should be enabled according to system properties.
347 */
348 public static boolean isGestureLauncherEnabled(Resources resources) {
Jonathan Solnita4138162017-05-10 21:06:04 -0700349 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources) ||
350 isCameraLiftTriggerEnabled(resources);
Adrian Roos5941c982015-09-03 15:59:49 -0700351 }
352
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700353 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive,
354 MutableBoolean outLaunched) {
Adrian Roos5941c982015-09-03 15:59:49 -0700355 boolean launched = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700356 boolean intercept = false;
Juan Lang0ba22372017-04-05 13:03:04 -0700357 long powerTapInterval;
Adrian Roos5941c982015-09-03 15:59:49 -0700358 synchronized (this) {
Juan Lang0ba22372017-04-05 13:03:04 -0700359 powerTapInterval = event.getEventTime() - mLastPowerDown;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700360 if (mCameraDoubleTapPowerEnabled
Juan Lang0ba22372017-04-05 13:03:04 -0700361 && powerTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS) {
Adrian Roos5941c982015-09-03 15:59:49 -0700362 launched = true;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700363 intercept = interactive;
Juan Lang0ba22372017-04-05 13:03:04 -0700364 mPowerButtonConsecutiveTaps++;
365 } else if (powerTapInterval < POWER_SHORT_TAP_SEQUENCE_MAX_INTERVAL_MS) {
366 mPowerButtonConsecutiveTaps++;
367 } else {
368 mPowerButtonConsecutiveTaps = 1;
Adrian Roos5941c982015-09-03 15:59:49 -0700369 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700370 mLastPowerDown = event.getEventTime();
Adrian Roos5941c982015-09-03 15:59:49 -0700371 }
Juan Lang0ba22372017-04-05 13:03:04 -0700372 if (DBG && mPowerButtonConsecutiveTaps > 1) {
373 Slog.i(TAG, Long.valueOf(mPowerButtonConsecutiveTaps) +
374 " consecutive power button taps detected");
375 }
Adrian Roos5941c982015-09-03 15:59:49 -0700376 if (launched) {
Jorim Jaggid3f84d42015-10-01 14:13:04 -0700377 Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
Juan Lang0ba22372017-04-05 13:03:04 -0700378 + powerTapInterval + "ms");
Jonathan Solnita4138162017-05-10 21:06:04 -0700379 launched = handleCameraGesture(false /* useWakelock */,
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700380 StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700381 if (launched) {
Juan Lang36f46242017-04-19 11:58:28 -0700382 mMetricsLogger.action(MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
Juan Lang0ba22372017-04-05 13:03:04 -0700383 (int) powerTapInterval);
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700384 }
Adrian Roos5941c982015-09-03 15:59:49 -0700385 }
Juan Lang0ba22372017-04-05 13:03:04 -0700386 mMetricsLogger.histogram("power_consecutive_short_tap_count", mPowerButtonConsecutiveTaps);
387 mMetricsLogger.histogram("power_double_tap_interval", (int) powerTapInterval);
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700388 outLaunched.value = launched;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700389 return intercept && launched;
Adrian Roos5941c982015-09-03 15:59:49 -0700390 }
391
392 /**
393 * @return true if camera was launched, false otherwise.
394 */
Juan Langacf62e62017-04-18 16:35:39 -0700395 @VisibleForTesting
Jonathan Solnita4138162017-05-10 21:06:04 -0700396 boolean handleCameraGesture(boolean useWakelock, int source) {
Amith Yamasanif16c3be2016-07-15 15:33:03 -0700397 boolean userSetupComplete = Settings.Secure.getIntForUser(mContext.getContentResolver(),
398 Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
Adrian Roos5941c982015-09-03 15:59:49 -0700399 if (!userSetupComplete) {
400 if (DBG) Slog.d(TAG, String.format(
Jonathan Solnita4138162017-05-10 21:06:04 -0700401 "userSetupComplete = %s, ignoring camera gesture.",
Adrian Roos5941c982015-09-03 15:59:49 -0700402 userSetupComplete));
403 return false;
404 }
405 if (DBG) Slog.d(TAG, String.format(
Jonathan Solnita4138162017-05-10 21:06:04 -0700406 "userSetupComplete = %s, performing camera gesture.",
Adrian Roos5941c982015-09-03 15:59:49 -0700407 userSetupComplete));
408
409 if (useWakelock) {
410 // Make sure we don't sleep too early
411 mWakeLock.acquire(500L);
412 }
413 StatusBarManagerInternal service = LocalServices.getService(
414 StatusBarManagerInternal.class);
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700415 service.onCameraLaunchGestureDetected(source);
Adrian Roos5941c982015-09-03 15:59:49 -0700416 return true;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700417 }
418
Jason Monk5a5e7d62015-08-04 11:20:25 -0400419 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
420 @Override
421 public void onReceive(Context context, Intent intent) {
422 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
423 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
424 mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
Adrian Roos5941c982015-09-03 15:59:49 -0700425 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400426 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700427 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400428 }
429 }
430 };
431
432 private final ContentObserver mSettingObserver = new ContentObserver(new Handler()) {
433 public void onChange(boolean selfChange, android.net.Uri uri, int userId) {
434 if (userId == mUserId) {
435 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700436 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400437 }
438 }
439 };
440
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700441 private final class GestureEventListener implements SensorEventListener {
442 @Override
443 public void onSensorChanged(SensorEvent event) {
Jonathan Solnita4138162017-05-10 21:06:04 -0700444 if (!mCameraLaunchRegistered) {
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700445 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
446 return;
447 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700448 if (event.sensor == mCameraLaunchSensor) {
Adrian Roos5941c982015-09-03 15:59:49 -0700449 if (DBG) {
450 float[] values = event.values;
451 Slog.d(TAG, String.format("Received a camera launch event: " +
452 "values=[%.4f, %.4f, %.4f].", values[0], values[1], values[2]));
453 }
Jonathan Solnita4138162017-05-10 21:06:04 -0700454 if (handleCameraGesture(true /* useWakelock */,
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700455 StatusBarManager.CAMERA_LAUNCH_SOURCE_WIGGLE)) {
Juan Lang36f46242017-04-19 11:58:28 -0700456 mMetricsLogger.action(MetricsEvent.ACTION_WIGGLE_CAMERA_GESTURE);
Adrian Roos5941c982015-09-03 15:59:49 -0700457 trackCameraLaunchEvent(event);
458 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700459 return;
460 }
461 }
462
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700463 @Override
464 public void onAccuracyChanged(Sensor sensor, int accuracy) {
465 // Ignored.
466 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700467
468 private void trackCameraLaunchEvent(SensorEvent event) {
469 long now = SystemClock.elapsedRealtime();
470 long totalDuration = now - mCameraGestureOnTimeMs;
471 // values[0]: ratio between total time duration when accel is turned on and time
472 // duration since camera launch gesture is subscribed.
473 // values[1]: ratio between total time duration when gyro is turned on and time duration
474 // since camera launch gesture is subscribed.
475 // values[2]: extra information
476 float[] values = event.values;
477
478 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
479 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
480 int extra = (int) values[2];
481
482 // We only log the difference in the event log to make aggregation easier.
483 long gestureOnTimeDiff = now - mCameraGestureLastEventTime;
484 long sensor1OnTimeDiff = sensor1OnTime - mCameraGestureSensor1LastOnTimeMs;
485 long sensor2OnTimeDiff = sensor2OnTime - mCameraGestureSensor2LastOnTimeMs;
486 int extraDiff = extra - mCameraLaunchLastEventExtra;
487
488 // Gating against negative time difference. This doesn't usually happen, but it may
489 // happen because of numeric errors.
490 if (gestureOnTimeDiff < 0 || sensor1OnTimeDiff < 0 || sensor2OnTimeDiff < 0) {
491 if (DBG) Slog.d(TAG, "Skipped event logging because negative numbers.");
492 return;
493 }
494
495 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
496 "sensor2OnTime: %d, extra: %d",
497 gestureOnTimeDiff,
498 sensor1OnTimeDiff,
499 sensor2OnTimeDiff,
500 extraDiff));
501 EventLogTags.writeCameraGestureTriggered(
502 gestureOnTimeDiff,
503 sensor1OnTimeDiff,
504 sensor2OnTimeDiff,
505 extraDiff);
506
507 mCameraGestureLastEventTime = now;
508 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
509 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
510 mCameraLaunchLastEventExtra = extra;
511 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700512 }
Jonathan Solnita4138162017-05-10 21:06:04 -0700513
514 private final class CameraLiftTriggerEventListener implements SensorEventListener {
515 @Override
516 public void onSensorChanged(SensorEvent event) {
517 if (!mCameraLiftRegistered) {
518 if (DBG) Slog.d(TAG, "Ignoring camera lift event because it's unregistered.");
519 return;
520 }
521 if (event.sensor == mCameraLiftTriggerSensor) {
522 if (DBG) {
523 float[] values = event.values;
524 Slog.d(TAG, String.format("Received a camera lift trigger event: " +
525 "values=[%.4f].", values[0]));
526 }
Jonathan Solnit4587d102017-05-17 10:28:13 -0700527 if (mKeyguardManager.isKeyguardLocked() || !mPowerManager.isInteractive()) {
528 if (handleCameraGesture(true /* useWakelock */,
529 StatusBarManager.CAMERA_LAUNCH_SOURCE_LIFT_TRIGGER)) {
530 MetricsLogger.action(mContext, MetricsEvent.ACTION_CAMERA_LIFT_TRIGGER);
531 }
532 } else if (DBG) {
533 Slog.d(TAG, "Ignoring lift event because device is awake");
Jonathan Solnita4138162017-05-10 21:06:04 -0700534 }
535 return;
536 }
537 }
538
539 @Override
540 public void onAccuracyChanged(Sensor sensor, int accuracy) {
541 // Ignored.
542 }
543 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700544}