blob: 7c85001c91757d9f24186b06aca140fb3aa32723 [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;
Jason Monk5a5e7d62015-08-04 11:20:25 -040020import android.content.BroadcastReceiver;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070021import android.content.Context;
22import android.content.Intent;
Jason Monk5a5e7d62015-08-04 11:20:25 -040023import android.content.IntentFilter;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070024import android.content.res.Resources;
Jason Monk5a5e7d62015-08-04 11:20:25 -040025import android.database.ContentObserver;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070026import android.hardware.Sensor;
27import android.hardware.SensorEvent;
28import android.hardware.SensorEventListener;
29import android.hardware.SensorManager;
Jason Monk5a5e7d62015-08-04 11:20:25 -040030import android.os.Handler;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070031import android.os.PowerManager;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070032import android.os.PowerManager.WakeLock;
Zhentao Sun575b6fd2015-08-25 21:26:45 -070033import android.os.SystemClock;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070034import android.os.SystemProperties;
Kevin Gabayan024713c2015-07-31 12:01:43 -070035import android.provider.Settings;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070036import android.util.Slog;
Adrian Roos5941c982015-09-03 15:59:49 -070037import android.view.KeyEvent;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070038
Adrian Roos5941c982015-09-03 15:59:49 -070039import com.android.internal.logging.MetricsLogger;
Selim Cinek372d1bd2015-08-14 13:19:37 -070040import com.android.server.statusbar.StatusBarManagerInternal;
41
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070042/**
43 * The service that listens for gestures detected in sensor firmware and starts the intent
44 * accordingly.
45 * <p>For now, only camera launch gesture is supported, and in the future, more gestures can be
46 * added.</p>
47 * @hide
48 */
Adrian Roos5941c982015-09-03 15:59:49 -070049public class GestureLauncherService extends SystemService {
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070050 private static final boolean DBG = false;
51 private static final String TAG = "GestureLauncherService";
52
Adrian Roos5941c982015-09-03 15:59:49 -070053 /**
54 * Time in milliseconds in which the power button must be pressed twice so it will be considered
55 * as a camera launch.
56 */
57 private static final long CAMERA_POWER_DOUBLE_TAP_TIME_MS = 300;
58
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070059 /** The listener that receives the gesture event. */
60 private final GestureEventListener mGestureListener = new GestureEventListener();
61
62 private Sensor mCameraLaunchSensor;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070063 private Context mContext;
64
65 /** The wake lock held when a gesture is detected. */
66 private WakeLock mWakeLock;
Jason Monk5a5e7d62015-08-04 11:20:25 -040067 private boolean mRegistered;
68 private int mUserId;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070069
Zhentao Sun575b6fd2015-08-25 21:26:45 -070070 // Below are fields used for event logging only.
71 /** Elapsed real time when the camera gesture is turned on. */
72 private long mCameraGestureOnTimeMs = 0L;
73
74 /** Elapsed real time when the last camera gesture was detected. */
75 private long mCameraGestureLastEventTime = 0L;
76
77 /**
78 * How long the sensor 1 has been turned on since camera launch sensor was
79 * subscribed to and when the last camera launch gesture was detected.
80 * <p>Sensor 1 is the main sensor used to detect camera launch gesture.</p>
81 */
82 private long mCameraGestureSensor1LastOnTimeMs = 0L;
83
84 /**
85 * If applicable, how long the sensor 2 has been turned on since camera
86 * launch sensor was subscribed to and when the last camera launch
87 * gesture was detected.
88 * <p>Sensor 2 is the secondary sensor used to detect camera launch gesture.
89 * This is optional and if only sensor 1 is used for detect camera launch
90 * gesture, this value would always be 0.</p>
91 */
92 private long mCameraGestureSensor2LastOnTimeMs = 0L;
93
94 /**
95 * Extra information about the event when the last camera launch gesture
96 * was detected.
97 */
98 private int mCameraLaunchLastEventExtra = 0;
99
Adrian Roos5941c982015-09-03 15:59:49 -0700100 /**
101 * Whether camera double tap power button gesture is currently enabled;
102 */
103 private boolean mCameraDoubleTapPowerEnabled;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700104 private long mLastPowerDown;
Adrian Roos5941c982015-09-03 15:59:49 -0700105
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700106 public GestureLauncherService(Context context) {
107 super(context);
108 mContext = context;
109 }
110
111 public void onStart() {
Adrian Roos5941c982015-09-03 15:59:49 -0700112 LocalServices.addService(GestureLauncherService.class, this);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700113 }
114
115 public void onBootPhase(int phase) {
116 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
117 Resources resources = mContext.getResources();
118 if (!isGestureLauncherEnabled(resources)) {
119 if (DBG) Slog.d(TAG, "Gesture launcher is disabled in system properties.");
120 return;
121 }
122
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700123 PowerManager powerManager = (PowerManager) mContext.getSystemService(
124 Context.POWER_SERVICE);
Selim Cinek372d1bd2015-08-14 13:19:37 -0700125 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700126 "GestureLauncherService");
Jason Monk5a5e7d62015-08-04 11:20:25 -0400127 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700128 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400129
130 mUserId = ActivityManager.getCurrentUser();
131 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
Adrian Roos5941c982015-09-03 15:59:49 -0700132 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400133 }
134 }
135
Adrian Roos5941c982015-09-03 15:59:49 -0700136 private void registerContentObservers() {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400137 mContext.getContentResolver().registerContentObserver(
138 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
139 false, mSettingObserver, mUserId);
Adrian Roos5941c982015-09-03 15:59:49 -0700140 mContext.getContentResolver().registerContentObserver(
141 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
142 false, mSettingObserver, mUserId);
Jason Monk5a5e7d62015-08-04 11:20:25 -0400143 }
144
145 private void updateCameraRegistered() {
146 Resources resources = mContext.getResources();
147 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
148 registerCameraLaunchGesture(resources);
149 } else {
150 unregisterCameraLaunchGesture();
151 }
152 }
153
Adrian Roos5941c982015-09-03 15:59:49 -0700154 private void updateCameraDoubleTapPowerEnabled() {
155 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
156 synchronized (this) {
157 mCameraDoubleTapPowerEnabled = enabled;
158 }
159 }
160
Jason Monk5a5e7d62015-08-04 11:20:25 -0400161 private void unregisterCameraLaunchGesture() {
162 if (mRegistered) {
163 mRegistered = false;
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700164 mCameraGestureOnTimeMs = 0L;
165 mCameraGestureLastEventTime = 0L;
166 mCameraGestureSensor1LastOnTimeMs = 0;
167 mCameraGestureSensor2LastOnTimeMs = 0;
168 mCameraLaunchLastEventExtra = 0;
169
Jason Monk5a5e7d62015-08-04 11:20:25 -0400170 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
171 Context.SENSOR_SERVICE);
172 sensorManager.unregisterListener(mGestureListener);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700173 }
174 }
175
176 /**
177 * Registers for the camera launch gesture.
178 */
179 private void registerCameraLaunchGesture(Resources resources) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400180 if (mRegistered) {
181 return;
182 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700183 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
184 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700185 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
186 Context.SENSOR_SERVICE);
187 int cameraLaunchGestureId = resources.getInteger(
188 com.android.internal.R.integer.config_cameraLaunchGestureSensorType);
189 if (cameraLaunchGestureId != -1) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400190 mRegistered = false;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700191 String sensorName = resources.getString(
192 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
193 mCameraLaunchSensor = sensorManager.getDefaultSensor(
194 cameraLaunchGestureId,
195 true /*wakeUp*/);
196
197 // Compare the camera gesture string type to that in the resource file to make
198 // sure we are registering the correct sensor. This is redundant check, it
199 // makes the code more robust.
200 if (mCameraLaunchSensor != null) {
201 if (sensorName.equals(mCameraLaunchSensor.getStringType())) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400202 mRegistered = sensorManager.registerListener(mGestureListener,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700203 mCameraLaunchSensor, 0);
204 } else {
205 String message = String.format("Wrong configuration. Sensor type and sensor "
206 + "string type don't match: %s in resources, %s in the sensor.",
207 sensorName, mCameraLaunchSensor.getStringType());
208 throw new RuntimeException(message);
209 }
210 }
Jason Monk5a5e7d62015-08-04 11:20:25 -0400211 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700212 } else {
213 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
214 }
215 }
216
Jason Monk5a5e7d62015-08-04 11:20:25 -0400217 public static boolean isCameraLaunchSettingEnabled(Context context, int userId) {
218 return isCameraLaunchEnabled(context.getResources())
219 && (Settings.Secure.getIntForUser(context.getContentResolver(),
220 Settings.Secure.CAMERA_GESTURE_DISABLED, 0, userId) == 0);
221 }
222
Adrian Roos5941c982015-09-03 15:59:49 -0700223 public static boolean isCameraDoubleTapPowerSettingEnabled(Context context, int userId) {
224 return isCameraDoubleTapPowerEnabled(context.getResources())
225 && (Settings.Secure.getIntForUser(context.getContentResolver(),
226 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0, userId) == 0);
227 }
228
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700229 /**
230 * Whether to enable the camera launch gesture.
231 */
232 public static boolean isCameraLaunchEnabled(Resources resources) {
233 boolean configSet = resources.getInteger(
234 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
235 return configSet &&
236 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
237 }
238
Adrian Roos5941c982015-09-03 15:59:49 -0700239 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
240 return resources.getBoolean(
241 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
242 }
243
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700244 /**
245 * Whether GestureLauncherService should be enabled according to system properties.
246 */
247 public static boolean isGestureLauncherEnabled(Resources resources) {
Adrian Roos5941c982015-09-03 15:59:49 -0700248 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources);
249 }
250
251 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive) {
252 boolean launched = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700253 boolean intercept = false;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700254 long doubleTapInterval;
Adrian Roos5941c982015-09-03 15:59:49 -0700255 synchronized (this) {
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700256 doubleTapInterval = event.getEventTime() - mLastPowerDown;
257 if (mCameraDoubleTapPowerEnabled
258 && doubleTapInterval < CAMERA_POWER_DOUBLE_TAP_TIME_MS) {
Adrian Roos5941c982015-09-03 15:59:49 -0700259 launched = true;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700260 intercept = interactive;
Adrian Roos5941c982015-09-03 15:59:49 -0700261 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700262 mLastPowerDown = event.getEventTime();
Adrian Roos5941c982015-09-03 15:59:49 -0700263 }
264 if (launched) {
265 Slog.i(TAG, "Power button double tap gesture detected, launching camera.");
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700266 launched = handleCameraLaunchGesture(false /* useWakelock */);
267 if (launched) {
268 MetricsLogger.action(mContext, MetricsLogger.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
269 (int) doubleTapInterval);
270 }
Adrian Roos5941c982015-09-03 15:59:49 -0700271 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700272 MetricsLogger.histogram(mContext, "power_double_tap_interval", (int) doubleTapInterval);
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700273 return intercept && launched;
Adrian Roos5941c982015-09-03 15:59:49 -0700274 }
275
276 /**
277 * @return true if camera was launched, false otherwise.
278 */
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700279 private boolean handleCameraLaunchGesture(boolean useWakelock) {
Adrian Roos5941c982015-09-03 15:59:49 -0700280 boolean userSetupComplete = Settings.Secure.getInt(mContext.getContentResolver(),
281 Settings.Secure.USER_SETUP_COMPLETE, 0) != 0;
282 if (!userSetupComplete) {
283 if (DBG) Slog.d(TAG, String.format(
284 "userSetupComplete = %s, ignoring camera launch gesture.",
285 userSetupComplete));
286 return false;
287 }
288 if (DBG) Slog.d(TAG, String.format(
289 "userSetupComplete = %s, performing camera launch gesture.",
290 userSetupComplete));
291
292 if (useWakelock) {
293 // Make sure we don't sleep too early
294 mWakeLock.acquire(500L);
295 }
296 StatusBarManagerInternal service = LocalServices.getService(
297 StatusBarManagerInternal.class);
298 service.onCameraLaunchGestureDetected();
Adrian Roos5941c982015-09-03 15:59:49 -0700299 return true;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700300 }
301
Jason Monk5a5e7d62015-08-04 11:20:25 -0400302 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
303 @Override
304 public void onReceive(Context context, Intent intent) {
305 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
306 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
307 mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
Adrian Roos5941c982015-09-03 15:59:49 -0700308 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400309 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700310 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400311 }
312 }
313 };
314
315 private final ContentObserver mSettingObserver = new ContentObserver(new Handler()) {
316 public void onChange(boolean selfChange, android.net.Uri uri, int userId) {
317 if (userId == mUserId) {
318 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700319 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400320 }
321 }
322 };
323
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700324 private final class GestureEventListener implements SensorEventListener {
325 @Override
326 public void onSensorChanged(SensorEvent event) {
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700327 if (!mRegistered) {
328 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
329 return;
330 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700331 if (event.sensor == mCameraLaunchSensor) {
Adrian Roos5941c982015-09-03 15:59:49 -0700332 if (DBG) {
333 float[] values = event.values;
334 Slog.d(TAG, String.format("Received a camera launch event: " +
335 "values=[%.4f, %.4f, %.4f].", values[0], values[1], values[2]));
336 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700337 if (handleCameraLaunchGesture(true /* useWakelock */)) {
338 MetricsLogger.action(mContext, MetricsLogger.ACTION_WIGGLE_CAMERA_GESTURE);
Adrian Roos5941c982015-09-03 15:59:49 -0700339 trackCameraLaunchEvent(event);
340 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700341 return;
342 }
343 }
344
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700345 @Override
346 public void onAccuracyChanged(Sensor sensor, int accuracy) {
347 // Ignored.
348 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700349
350 private void trackCameraLaunchEvent(SensorEvent event) {
351 long now = SystemClock.elapsedRealtime();
352 long totalDuration = now - mCameraGestureOnTimeMs;
353 // values[0]: ratio between total time duration when accel is turned on and time
354 // duration since camera launch gesture is subscribed.
355 // values[1]: ratio between total time duration when gyro is turned on and time duration
356 // since camera launch gesture is subscribed.
357 // values[2]: extra information
358 float[] values = event.values;
359
360 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
361 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
362 int extra = (int) values[2];
363
364 // We only log the difference in the event log to make aggregation easier.
365 long gestureOnTimeDiff = now - mCameraGestureLastEventTime;
366 long sensor1OnTimeDiff = sensor1OnTime - mCameraGestureSensor1LastOnTimeMs;
367 long sensor2OnTimeDiff = sensor2OnTime - mCameraGestureSensor2LastOnTimeMs;
368 int extraDiff = extra - mCameraLaunchLastEventExtra;
369
370 // Gating against negative time difference. This doesn't usually happen, but it may
371 // happen because of numeric errors.
372 if (gestureOnTimeDiff < 0 || sensor1OnTimeDiff < 0 || sensor2OnTimeDiff < 0) {
373 if (DBG) Slog.d(TAG, "Skipped event logging because negative numbers.");
374 return;
375 }
376
377 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
378 "sensor2OnTime: %d, extra: %d",
379 gestureOnTimeDiff,
380 sensor1OnTimeDiff,
381 sensor2OnTimeDiff,
382 extraDiff));
383 EventLogTags.writeCameraGestureTriggered(
384 gestureOnTimeDiff,
385 sensor1OnTimeDiff,
386 sensor2OnTimeDiff,
387 extraDiff);
388
389 mCameraGestureLastEventTime = now;
390 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
391 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
392 mCameraLaunchLastEventExtra = extra;
393 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700394 }
395}