blob: aa986489e1c2e48655386529bd8a63f8cbeeafdf [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;
Jorim Jaggi40aa8812015-09-23 12:59:22 -070020import android.app.StatusBarManager;
Jason Monk5a5e7d62015-08-04 11:20:25 -040021import android.content.BroadcastReceiver;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070022import android.content.Context;
23import android.content.Intent;
Jason Monk5a5e7d62015-08-04 11:20:25 -040024import android.content.IntentFilter;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070025import android.content.res.Resources;
Jason Monk5a5e7d62015-08-04 11:20:25 -040026import android.database.ContentObserver;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070027import android.hardware.Sensor;
28import android.hardware.SensorEvent;
29import android.hardware.SensorEventListener;
30import android.hardware.SensorManager;
Jason Monk5a5e7d62015-08-04 11:20:25 -040031import android.os.Handler;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070032import android.os.PowerManager;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070033import android.os.PowerManager.WakeLock;
Zhentao Sun575b6fd2015-08-25 21:26:45 -070034import android.os.SystemClock;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070035import android.os.SystemProperties;
Kevin Gabayan024713c2015-07-31 12:01:43 -070036import android.provider.Settings;
Jorim Jaggif1cdf952016-04-05 21:41:06 -070037import android.util.MutableBoolean;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070038import android.util.Slog;
Adrian Roos5941c982015-09-03 15:59:49 -070039import android.view.KeyEvent;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070040
Adrian Roos5941c982015-09-03 15:59:49 -070041import com.android.internal.logging.MetricsLogger;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050042import com.android.internal.logging.MetricsProto.MetricsEvent;
Selim Cinek372d1bd2015-08-14 13:19:37 -070043import com.android.server.statusbar.StatusBarManagerInternal;
44
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070045/**
46 * The service that listens for gestures detected in sensor firmware and starts the intent
47 * accordingly.
48 * <p>For now, only camera launch gesture is supported, and in the future, more gestures can be
49 * added.</p>
50 * @hide
51 */
Adrian Roos5941c982015-09-03 15:59:49 -070052public class GestureLauncherService extends SystemService {
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070053 private static final boolean DBG = false;
54 private static final String TAG = "GestureLauncherService";
55
Adrian Roos5941c982015-09-03 15:59:49 -070056 /**
57 * Time in milliseconds in which the power button must be pressed twice so it will be considered
58 * as a camera launch.
59 */
Jorim Jaggid3f84d42015-10-01 14:13:04 -070060 private static final long CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS = 300;
61 private static final long CAMERA_POWER_DOUBLE_TAP_MIN_TIME_MS = 120;
Adrian Roos5941c982015-09-03 15:59:49 -070062
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070063 /** The listener that receives the gesture event. */
64 private final GestureEventListener mGestureListener = new GestureEventListener();
65
66 private Sensor mCameraLaunchSensor;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070067 private Context mContext;
68
69 /** The wake lock held when a gesture is detected. */
70 private WakeLock mWakeLock;
Jason Monk5a5e7d62015-08-04 11:20:25 -040071 private boolean mRegistered;
72 private int mUserId;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070073
Zhentao Sun575b6fd2015-08-25 21:26:45 -070074 // Below are fields used for event logging only.
75 /** Elapsed real time when the camera gesture is turned on. */
76 private long mCameraGestureOnTimeMs = 0L;
77
78 /** Elapsed real time when the last camera gesture was detected. */
79 private long mCameraGestureLastEventTime = 0L;
80
81 /**
82 * How long the sensor 1 has been turned on since camera launch sensor was
83 * subscribed to and when the last camera launch gesture was detected.
84 * <p>Sensor 1 is the main sensor used to detect camera launch gesture.</p>
85 */
86 private long mCameraGestureSensor1LastOnTimeMs = 0L;
87
88 /**
89 * If applicable, how long the sensor 2 has been turned on since camera
90 * launch sensor was subscribed to and when the last camera launch
91 * gesture was detected.
92 * <p>Sensor 2 is the secondary sensor used to detect camera launch gesture.
93 * This is optional and if only sensor 1 is used for detect camera launch
94 * gesture, this value would always be 0.</p>
95 */
96 private long mCameraGestureSensor2LastOnTimeMs = 0L;
97
98 /**
99 * Extra information about the event when the last camera launch gesture
100 * was detected.
101 */
102 private int mCameraLaunchLastEventExtra = 0;
103
Adrian Roos5941c982015-09-03 15:59:49 -0700104 /**
105 * Whether camera double tap power button gesture is currently enabled;
106 */
107 private boolean mCameraDoubleTapPowerEnabled;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700108 private long mLastPowerDown;
Adrian Roos5941c982015-09-03 15:59:49 -0700109
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700110 public GestureLauncherService(Context context) {
111 super(context);
112 mContext = context;
113 }
114
115 public void onStart() {
Adrian Roos5941c982015-09-03 15:59:49 -0700116 LocalServices.addService(GestureLauncherService.class, this);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700117 }
118
119 public void onBootPhase(int phase) {
120 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
121 Resources resources = mContext.getResources();
122 if (!isGestureLauncherEnabled(resources)) {
123 if (DBG) Slog.d(TAG, "Gesture launcher is disabled in system properties.");
124 return;
125 }
126
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700127 PowerManager powerManager = (PowerManager) mContext.getSystemService(
128 Context.POWER_SERVICE);
Selim Cinek372d1bd2015-08-14 13:19:37 -0700129 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700130 "GestureLauncherService");
Jason Monk5a5e7d62015-08-04 11:20:25 -0400131 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700132 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400133
134 mUserId = ActivityManager.getCurrentUser();
135 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
Adrian Roos5941c982015-09-03 15:59:49 -0700136 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400137 }
138 }
139
Adrian Roos5941c982015-09-03 15:59:49 -0700140 private void registerContentObservers() {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400141 mContext.getContentResolver().registerContentObserver(
142 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
143 false, mSettingObserver, mUserId);
Adrian Roos5941c982015-09-03 15:59:49 -0700144 mContext.getContentResolver().registerContentObserver(
145 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
146 false, mSettingObserver, mUserId);
Jason Monk5a5e7d62015-08-04 11:20:25 -0400147 }
148
149 private void updateCameraRegistered() {
150 Resources resources = mContext.getResources();
151 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
152 registerCameraLaunchGesture(resources);
153 } else {
154 unregisterCameraLaunchGesture();
155 }
156 }
157
Adrian Roos5941c982015-09-03 15:59:49 -0700158 private void updateCameraDoubleTapPowerEnabled() {
159 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
160 synchronized (this) {
161 mCameraDoubleTapPowerEnabled = enabled;
162 }
163 }
164
Jason Monk5a5e7d62015-08-04 11:20:25 -0400165 private void unregisterCameraLaunchGesture() {
166 if (mRegistered) {
167 mRegistered = false;
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700168 mCameraGestureOnTimeMs = 0L;
169 mCameraGestureLastEventTime = 0L;
170 mCameraGestureSensor1LastOnTimeMs = 0;
171 mCameraGestureSensor2LastOnTimeMs = 0;
172 mCameraLaunchLastEventExtra = 0;
173
Jason Monk5a5e7d62015-08-04 11:20:25 -0400174 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
175 Context.SENSOR_SERVICE);
176 sensorManager.unregisterListener(mGestureListener);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700177 }
178 }
179
180 /**
181 * Registers for the camera launch gesture.
182 */
183 private void registerCameraLaunchGesture(Resources resources) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400184 if (mRegistered) {
185 return;
186 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700187 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
188 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700189 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
190 Context.SENSOR_SERVICE);
191 int cameraLaunchGestureId = resources.getInteger(
192 com.android.internal.R.integer.config_cameraLaunchGestureSensorType);
193 if (cameraLaunchGestureId != -1) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400194 mRegistered = false;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700195 String sensorName = resources.getString(
196 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
197 mCameraLaunchSensor = sensorManager.getDefaultSensor(
198 cameraLaunchGestureId,
199 true /*wakeUp*/);
200
201 // Compare the camera gesture string type to that in the resource file to make
202 // sure we are registering the correct sensor. This is redundant check, it
203 // makes the code more robust.
204 if (mCameraLaunchSensor != null) {
205 if (sensorName.equals(mCameraLaunchSensor.getStringType())) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400206 mRegistered = sensorManager.registerListener(mGestureListener,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700207 mCameraLaunchSensor, 0);
208 } else {
209 String message = String.format("Wrong configuration. Sensor type and sensor "
210 + "string type don't match: %s in resources, %s in the sensor.",
211 sensorName, mCameraLaunchSensor.getStringType());
212 throw new RuntimeException(message);
213 }
214 }
Jason Monk5a5e7d62015-08-04 11:20:25 -0400215 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700216 } else {
217 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
218 }
219 }
220
Jason Monk5a5e7d62015-08-04 11:20:25 -0400221 public static boolean isCameraLaunchSettingEnabled(Context context, int userId) {
222 return isCameraLaunchEnabled(context.getResources())
223 && (Settings.Secure.getIntForUser(context.getContentResolver(),
224 Settings.Secure.CAMERA_GESTURE_DISABLED, 0, userId) == 0);
225 }
226
Adrian Roos5941c982015-09-03 15:59:49 -0700227 public static boolean isCameraDoubleTapPowerSettingEnabled(Context context, int userId) {
228 return isCameraDoubleTapPowerEnabled(context.getResources())
229 && (Settings.Secure.getIntForUser(context.getContentResolver(),
230 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0, userId) == 0);
231 }
232
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700233 /**
234 * Whether to enable the camera launch gesture.
235 */
236 public static boolean isCameraLaunchEnabled(Resources resources) {
237 boolean configSet = resources.getInteger(
238 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
239 return configSet &&
240 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
241 }
242
Adrian Roos5941c982015-09-03 15:59:49 -0700243 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
244 return resources.getBoolean(
245 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
246 }
247
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700248 /**
249 * Whether GestureLauncherService should be enabled according to system properties.
250 */
251 public static boolean isGestureLauncherEnabled(Resources resources) {
Adrian Roos5941c982015-09-03 15:59:49 -0700252 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources);
253 }
254
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700255 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive,
256 MutableBoolean outLaunched) {
Adrian Roos5941c982015-09-03 15:59:49 -0700257 boolean launched = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700258 boolean intercept = false;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700259 long doubleTapInterval;
Adrian Roos5941c982015-09-03 15:59:49 -0700260 synchronized (this) {
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700261 doubleTapInterval = event.getEventTime() - mLastPowerDown;
262 if (mCameraDoubleTapPowerEnabled
Jorim Jaggid3f84d42015-10-01 14:13:04 -0700263 && doubleTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS
264 && doubleTapInterval > CAMERA_POWER_DOUBLE_TAP_MIN_TIME_MS) {
Adrian Roos5941c982015-09-03 15:59:49 -0700265 launched = true;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700266 intercept = interactive;
Adrian Roos5941c982015-09-03 15:59:49 -0700267 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700268 mLastPowerDown = event.getEventTime();
Adrian Roos5941c982015-09-03 15:59:49 -0700269 }
270 if (launched) {
Jorim Jaggid3f84d42015-10-01 14:13:04 -0700271 Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
272 + doubleTapInterval + "ms");
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700273 launched = handleCameraLaunchGesture(false /* useWakelock */,
274 StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700275 if (launched) {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500276 MetricsLogger.action(mContext, MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700277 (int) doubleTapInterval);
278 }
Adrian Roos5941c982015-09-03 15:59:49 -0700279 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700280 MetricsLogger.histogram(mContext, "power_double_tap_interval", (int) doubleTapInterval);
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700281 outLaunched.value = launched;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700282 return intercept && launched;
Adrian Roos5941c982015-09-03 15:59:49 -0700283 }
284
285 /**
286 * @return true if camera was launched, false otherwise.
287 */
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700288 private boolean handleCameraLaunchGesture(boolean useWakelock, int source) {
Adrian Roos5941c982015-09-03 15:59:49 -0700289 boolean userSetupComplete = Settings.Secure.getInt(mContext.getContentResolver(),
290 Settings.Secure.USER_SETUP_COMPLETE, 0) != 0;
291 if (!userSetupComplete) {
292 if (DBG) Slog.d(TAG, String.format(
293 "userSetupComplete = %s, ignoring camera launch gesture.",
294 userSetupComplete));
295 return false;
296 }
297 if (DBG) Slog.d(TAG, String.format(
298 "userSetupComplete = %s, performing camera launch gesture.",
299 userSetupComplete));
300
301 if (useWakelock) {
302 // Make sure we don't sleep too early
303 mWakeLock.acquire(500L);
304 }
305 StatusBarManagerInternal service = LocalServices.getService(
306 StatusBarManagerInternal.class);
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700307 service.onCameraLaunchGestureDetected(source);
Adrian Roos5941c982015-09-03 15:59:49 -0700308 return true;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700309 }
310
Jason Monk5a5e7d62015-08-04 11:20:25 -0400311 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
312 @Override
313 public void onReceive(Context context, Intent intent) {
314 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
315 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
316 mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
Adrian Roos5941c982015-09-03 15:59:49 -0700317 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400318 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700319 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400320 }
321 }
322 };
323
324 private final ContentObserver mSettingObserver = new ContentObserver(new Handler()) {
325 public void onChange(boolean selfChange, android.net.Uri uri, int userId) {
326 if (userId == mUserId) {
327 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700328 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400329 }
330 }
331 };
332
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700333 private final class GestureEventListener implements SensorEventListener {
334 @Override
335 public void onSensorChanged(SensorEvent event) {
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700336 if (!mRegistered) {
337 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
338 return;
339 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700340 if (event.sensor == mCameraLaunchSensor) {
Adrian Roos5941c982015-09-03 15:59:49 -0700341 if (DBG) {
342 float[] values = event.values;
343 Slog.d(TAG, String.format("Received a camera launch event: " +
344 "values=[%.4f, %.4f, %.4f].", values[0], values[1], values[2]));
345 }
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700346 if (handleCameraLaunchGesture(true /* useWakelock */,
347 StatusBarManager.CAMERA_LAUNCH_SOURCE_WIGGLE)) {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500348 MetricsLogger.action(mContext, MetricsEvent.ACTION_WIGGLE_CAMERA_GESTURE);
Adrian Roos5941c982015-09-03 15:59:49 -0700349 trackCameraLaunchEvent(event);
350 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700351 return;
352 }
353 }
354
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700355 @Override
356 public void onAccuracyChanged(Sensor sensor, int accuracy) {
357 // Ignored.
358 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700359
360 private void trackCameraLaunchEvent(SensorEvent event) {
361 long now = SystemClock.elapsedRealtime();
362 long totalDuration = now - mCameraGestureOnTimeMs;
363 // values[0]: ratio between total time duration when accel is turned on and time
364 // duration since camera launch gesture is subscribed.
365 // values[1]: ratio between total time duration when gyro is turned on and time duration
366 // since camera launch gesture is subscribed.
367 // values[2]: extra information
368 float[] values = event.values;
369
370 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
371 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
372 int extra = (int) values[2];
373
374 // We only log the difference in the event log to make aggregation easier.
375 long gestureOnTimeDiff = now - mCameraGestureLastEventTime;
376 long sensor1OnTimeDiff = sensor1OnTime - mCameraGestureSensor1LastOnTimeMs;
377 long sensor2OnTimeDiff = sensor2OnTime - mCameraGestureSensor2LastOnTimeMs;
378 int extraDiff = extra - mCameraLaunchLastEventExtra;
379
380 // Gating against negative time difference. This doesn't usually happen, but it may
381 // happen because of numeric errors.
382 if (gestureOnTimeDiff < 0 || sensor1OnTimeDiff < 0 || sensor2OnTimeDiff < 0) {
383 if (DBG) Slog.d(TAG, "Skipped event logging because negative numbers.");
384 return;
385 }
386
387 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
388 "sensor2OnTime: %d, extra: %d",
389 gestureOnTimeDiff,
390 sensor1OnTimeDiff,
391 sensor2OnTimeDiff,
392 extraDiff));
393 EventLogTags.writeCameraGestureTriggered(
394 gestureOnTimeDiff,
395 sensor1OnTimeDiff,
396 sensor2OnTimeDiff,
397 extraDiff);
398
399 mCameraGestureLastEventTime = now;
400 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
401 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
402 mCameraLaunchLastEventExtra = extra;
403 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700404 }
405}