blob: 5a9048898d02031e7052d23ae1410b7b3d2093a0 [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;
Adrian Roos5941c982015-09-03 15:59:49 -070061
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070062 /** The listener that receives the gesture event. */
63 private final GestureEventListener mGestureListener = new GestureEventListener();
64
65 private Sensor mCameraLaunchSensor;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070066 private Context mContext;
67
68 /** The wake lock held when a gesture is detected. */
69 private WakeLock mWakeLock;
Jason Monk5a5e7d62015-08-04 11:20:25 -040070 private boolean mRegistered;
71 private int mUserId;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070072
Zhentao Sun575b6fd2015-08-25 21:26:45 -070073 // Below are fields used for event logging only.
74 /** Elapsed real time when the camera gesture is turned on. */
75 private long mCameraGestureOnTimeMs = 0L;
76
77 /** Elapsed real time when the last camera gesture was detected. */
78 private long mCameraGestureLastEventTime = 0L;
79
80 /**
81 * How long the sensor 1 has been turned on since camera launch sensor was
82 * subscribed to and when the last camera launch gesture was detected.
83 * <p>Sensor 1 is the main sensor used to detect camera launch gesture.</p>
84 */
85 private long mCameraGestureSensor1LastOnTimeMs = 0L;
86
87 /**
88 * If applicable, how long the sensor 2 has been turned on since camera
89 * launch sensor was subscribed to and when the last camera launch
90 * gesture was detected.
91 * <p>Sensor 2 is the secondary sensor used to detect camera launch gesture.
92 * This is optional and if only sensor 1 is used for detect camera launch
93 * gesture, this value would always be 0.</p>
94 */
95 private long mCameraGestureSensor2LastOnTimeMs = 0L;
96
97 /**
98 * Extra information about the event when the last camera launch gesture
99 * was detected.
100 */
101 private int mCameraLaunchLastEventExtra = 0;
102
Adrian Roos5941c982015-09-03 15:59:49 -0700103 /**
104 * Whether camera double tap power button gesture is currently enabled;
105 */
106 private boolean mCameraDoubleTapPowerEnabled;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700107 private long mLastPowerDown;
Adrian Roos5941c982015-09-03 15:59:49 -0700108
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700109 public GestureLauncherService(Context context) {
110 super(context);
111 mContext = context;
112 }
113
114 public void onStart() {
Adrian Roos5941c982015-09-03 15:59:49 -0700115 LocalServices.addService(GestureLauncherService.class, this);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700116 }
117
118 public void onBootPhase(int phase) {
119 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
120 Resources resources = mContext.getResources();
121 if (!isGestureLauncherEnabled(resources)) {
122 if (DBG) Slog.d(TAG, "Gesture launcher is disabled in system properties.");
123 return;
124 }
125
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700126 PowerManager powerManager = (PowerManager) mContext.getSystemService(
127 Context.POWER_SERVICE);
Selim Cinek372d1bd2015-08-14 13:19:37 -0700128 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700129 "GestureLauncherService");
Jason Monk5a5e7d62015-08-04 11:20:25 -0400130 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700131 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400132
133 mUserId = ActivityManager.getCurrentUser();
134 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
Adrian Roos5941c982015-09-03 15:59:49 -0700135 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400136 }
137 }
138
Adrian Roos5941c982015-09-03 15:59:49 -0700139 private void registerContentObservers() {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400140 mContext.getContentResolver().registerContentObserver(
141 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
142 false, mSettingObserver, mUserId);
Adrian Roos5941c982015-09-03 15:59:49 -0700143 mContext.getContentResolver().registerContentObserver(
144 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
145 false, mSettingObserver, mUserId);
Jason Monk5a5e7d62015-08-04 11:20:25 -0400146 }
147
148 private void updateCameraRegistered() {
149 Resources resources = mContext.getResources();
150 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
151 registerCameraLaunchGesture(resources);
152 } else {
153 unregisterCameraLaunchGesture();
154 }
155 }
156
Adrian Roos5941c982015-09-03 15:59:49 -0700157 private void updateCameraDoubleTapPowerEnabled() {
158 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
159 synchronized (this) {
160 mCameraDoubleTapPowerEnabled = enabled;
161 }
162 }
163
Jason Monk5a5e7d62015-08-04 11:20:25 -0400164 private void unregisterCameraLaunchGesture() {
165 if (mRegistered) {
166 mRegistered = false;
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700167 mCameraGestureOnTimeMs = 0L;
168 mCameraGestureLastEventTime = 0L;
169 mCameraGestureSensor1LastOnTimeMs = 0;
170 mCameraGestureSensor2LastOnTimeMs = 0;
171 mCameraLaunchLastEventExtra = 0;
172
Jason Monk5a5e7d62015-08-04 11:20:25 -0400173 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
174 Context.SENSOR_SERVICE);
175 sensorManager.unregisterListener(mGestureListener);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700176 }
177 }
178
179 /**
180 * Registers for the camera launch gesture.
181 */
182 private void registerCameraLaunchGesture(Resources resources) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400183 if (mRegistered) {
184 return;
185 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700186 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
187 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700188 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
189 Context.SENSOR_SERVICE);
190 int cameraLaunchGestureId = resources.getInteger(
191 com.android.internal.R.integer.config_cameraLaunchGestureSensorType);
192 if (cameraLaunchGestureId != -1) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400193 mRegistered = false;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700194 String sensorName = resources.getString(
195 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
196 mCameraLaunchSensor = sensorManager.getDefaultSensor(
197 cameraLaunchGestureId,
198 true /*wakeUp*/);
199
200 // Compare the camera gesture string type to that in the resource file to make
201 // sure we are registering the correct sensor. This is redundant check, it
202 // makes the code more robust.
203 if (mCameraLaunchSensor != null) {
204 if (sensorName.equals(mCameraLaunchSensor.getStringType())) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400205 mRegistered = sensorManager.registerListener(mGestureListener,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700206 mCameraLaunchSensor, 0);
207 } else {
208 String message = String.format("Wrong configuration. Sensor type and sensor "
209 + "string type don't match: %s in resources, %s in the sensor.",
210 sensorName, mCameraLaunchSensor.getStringType());
211 throw new RuntimeException(message);
212 }
213 }
Jason Monk5a5e7d62015-08-04 11:20:25 -0400214 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700215 } else {
216 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
217 }
218 }
219
Jason Monk5a5e7d62015-08-04 11:20:25 -0400220 public static boolean isCameraLaunchSettingEnabled(Context context, int userId) {
221 return isCameraLaunchEnabled(context.getResources())
222 && (Settings.Secure.getIntForUser(context.getContentResolver(),
223 Settings.Secure.CAMERA_GESTURE_DISABLED, 0, userId) == 0);
224 }
225
Adrian Roos5941c982015-09-03 15:59:49 -0700226 public static boolean isCameraDoubleTapPowerSettingEnabled(Context context, int userId) {
227 return isCameraDoubleTapPowerEnabled(context.getResources())
228 && (Settings.Secure.getIntForUser(context.getContentResolver(),
229 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0, userId) == 0);
230 }
231
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700232 /**
233 * Whether to enable the camera launch gesture.
234 */
235 public static boolean isCameraLaunchEnabled(Resources resources) {
236 boolean configSet = resources.getInteger(
237 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
238 return configSet &&
239 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
240 }
241
Adrian Roos5941c982015-09-03 15:59:49 -0700242 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
243 return resources.getBoolean(
244 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
245 }
246
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700247 /**
248 * Whether GestureLauncherService should be enabled according to system properties.
249 */
250 public static boolean isGestureLauncherEnabled(Resources resources) {
Adrian Roos5941c982015-09-03 15:59:49 -0700251 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources);
252 }
253
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700254 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive,
255 MutableBoolean outLaunched) {
Adrian Roos5941c982015-09-03 15:59:49 -0700256 boolean launched = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700257 boolean intercept = false;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700258 long doubleTapInterval;
Adrian Roos5941c982015-09-03 15:59:49 -0700259 synchronized (this) {
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700260 doubleTapInterval = event.getEventTime() - mLastPowerDown;
261 if (mCameraDoubleTapPowerEnabled
Jorim Jaggi4e9817e2016-04-11 18:30:57 -0700262 && doubleTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS) {
Adrian Roos5941c982015-09-03 15:59:49 -0700263 launched = true;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700264 intercept = interactive;
Adrian Roos5941c982015-09-03 15:59:49 -0700265 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700266 mLastPowerDown = event.getEventTime();
Adrian Roos5941c982015-09-03 15:59:49 -0700267 }
268 if (launched) {
Jorim Jaggid3f84d42015-10-01 14:13:04 -0700269 Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
270 + doubleTapInterval + "ms");
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700271 launched = handleCameraLaunchGesture(false /* useWakelock */,
272 StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700273 if (launched) {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500274 MetricsLogger.action(mContext, MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700275 (int) doubleTapInterval);
276 }
Adrian Roos5941c982015-09-03 15:59:49 -0700277 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700278 MetricsLogger.histogram(mContext, "power_double_tap_interval", (int) doubleTapInterval);
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700279 outLaunched.value = launched;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700280 return intercept && launched;
Adrian Roos5941c982015-09-03 15:59:49 -0700281 }
282
283 /**
284 * @return true if camera was launched, false otherwise.
285 */
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700286 private boolean handleCameraLaunchGesture(boolean useWakelock, int source) {
Adrian Roos5941c982015-09-03 15:59:49 -0700287 boolean userSetupComplete = Settings.Secure.getInt(mContext.getContentResolver(),
288 Settings.Secure.USER_SETUP_COMPLETE, 0) != 0;
289 if (!userSetupComplete) {
290 if (DBG) Slog.d(TAG, String.format(
291 "userSetupComplete = %s, ignoring camera launch gesture.",
292 userSetupComplete));
293 return false;
294 }
295 if (DBG) Slog.d(TAG, String.format(
296 "userSetupComplete = %s, performing camera launch gesture.",
297 userSetupComplete));
298
299 if (useWakelock) {
300 // Make sure we don't sleep too early
301 mWakeLock.acquire(500L);
302 }
303 StatusBarManagerInternal service = LocalServices.getService(
304 StatusBarManagerInternal.class);
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700305 service.onCameraLaunchGestureDetected(source);
Adrian Roos5941c982015-09-03 15:59:49 -0700306 return true;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700307 }
308
Jason Monk5a5e7d62015-08-04 11:20:25 -0400309 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
310 @Override
311 public void onReceive(Context context, Intent intent) {
312 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
313 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
314 mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
Adrian Roos5941c982015-09-03 15:59:49 -0700315 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400316 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700317 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400318 }
319 }
320 };
321
322 private final ContentObserver mSettingObserver = new ContentObserver(new Handler()) {
323 public void onChange(boolean selfChange, android.net.Uri uri, int userId) {
324 if (userId == mUserId) {
325 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700326 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400327 }
328 }
329 };
330
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700331 private final class GestureEventListener implements SensorEventListener {
332 @Override
333 public void onSensorChanged(SensorEvent event) {
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700334 if (!mRegistered) {
335 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
336 return;
337 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700338 if (event.sensor == mCameraLaunchSensor) {
Adrian Roos5941c982015-09-03 15:59:49 -0700339 if (DBG) {
340 float[] values = event.values;
341 Slog.d(TAG, String.format("Received a camera launch event: " +
342 "values=[%.4f, %.4f, %.4f].", values[0], values[1], values[2]));
343 }
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700344 if (handleCameraLaunchGesture(true /* useWakelock */,
345 StatusBarManager.CAMERA_LAUNCH_SOURCE_WIGGLE)) {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500346 MetricsLogger.action(mContext, MetricsEvent.ACTION_WIGGLE_CAMERA_GESTURE);
Adrian Roos5941c982015-09-03 15:59:49 -0700347 trackCameraLaunchEvent(event);
348 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700349 return;
350 }
351 }
352
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700353 @Override
354 public void onAccuracyChanged(Sensor sensor, int accuracy) {
355 // Ignored.
356 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700357
358 private void trackCameraLaunchEvent(SensorEvent event) {
359 long now = SystemClock.elapsedRealtime();
360 long totalDuration = now - mCameraGestureOnTimeMs;
361 // values[0]: ratio between total time duration when accel is turned on and time
362 // duration since camera launch gesture is subscribed.
363 // values[1]: ratio between total time duration when gyro is turned on and time duration
364 // since camera launch gesture is subscribed.
365 // values[2]: extra information
366 float[] values = event.values;
367
368 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
369 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
370 int extra = (int) values[2];
371
372 // We only log the difference in the event log to make aggregation easier.
373 long gestureOnTimeDiff = now - mCameraGestureLastEventTime;
374 long sensor1OnTimeDiff = sensor1OnTime - mCameraGestureSensor1LastOnTimeMs;
375 long sensor2OnTimeDiff = sensor2OnTime - mCameraGestureSensor2LastOnTimeMs;
376 int extraDiff = extra - mCameraLaunchLastEventExtra;
377
378 // Gating against negative time difference. This doesn't usually happen, but it may
379 // happen because of numeric errors.
380 if (gestureOnTimeDiff < 0 || sensor1OnTimeDiff < 0 || sensor2OnTimeDiff < 0) {
381 if (DBG) Slog.d(TAG, "Skipped event logging because negative numbers.");
382 return;
383 }
384
385 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
386 "sensor2OnTime: %d, extra: %d",
387 gestureOnTimeDiff,
388 sensor1OnTimeDiff,
389 sensor2OnTimeDiff,
390 extraDiff));
391 EventLogTags.writeCameraGestureTriggered(
392 gestureOnTimeDiff,
393 sensor1OnTimeDiff,
394 sensor2OnTimeDiff,
395 extraDiff);
396
397 mCameraGestureLastEventTime = now;
398 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
399 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
400 mCameraLaunchLastEventExtra = extra;
401 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700402 }
403}