blob: 2aa039064410e1b47f112318372f6a0af4bd1f39 [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;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070037import android.util.Slog;
Adrian Roos5941c982015-09-03 15:59:49 -070038import android.view.KeyEvent;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070039
Adrian Roos5941c982015-09-03 15:59:49 -070040import com.android.internal.logging.MetricsLogger;
Selim Cinek372d1bd2015-08-14 13:19:37 -070041import com.android.server.statusbar.StatusBarManagerInternal;
42
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070043/**
44 * The service that listens for gestures detected in sensor firmware and starts the intent
45 * accordingly.
46 * <p>For now, only camera launch gesture is supported, and in the future, more gestures can be
47 * added.</p>
48 * @hide
49 */
Adrian Roos5941c982015-09-03 15:59:49 -070050public class GestureLauncherService extends SystemService {
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070051 private static final boolean DBG = false;
52 private static final String TAG = "GestureLauncherService";
53
Adrian Roos5941c982015-09-03 15:59:49 -070054 /**
55 * Time in milliseconds in which the power button must be pressed twice so it will be considered
56 * as a camera launch.
57 */
Jorim Jaggid3f84d42015-10-01 14:13:04 -070058 private static final long CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS = 300;
59 private static final long CAMERA_POWER_DOUBLE_TAP_MIN_TIME_MS = 120;
Adrian Roos5941c982015-09-03 15:59:49 -070060
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070061 /** The listener that receives the gesture event. */
62 private final GestureEventListener mGestureListener = new GestureEventListener();
63
64 private Sensor mCameraLaunchSensor;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070065 private Context mContext;
66
67 /** The wake lock held when a gesture is detected. */
68 private WakeLock mWakeLock;
Jason Monk5a5e7d62015-08-04 11:20:25 -040069 private boolean mRegistered;
70 private int mUserId;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070071
Zhentao Sun575b6fd2015-08-25 21:26:45 -070072 // Below are fields used for event logging only.
73 /** Elapsed real time when the camera gesture is turned on. */
74 private long mCameraGestureOnTimeMs = 0L;
75
76 /** Elapsed real time when the last camera gesture was detected. */
77 private long mCameraGestureLastEventTime = 0L;
78
79 /**
80 * How long the sensor 1 has been turned on since camera launch sensor was
81 * subscribed to and when the last camera launch gesture was detected.
82 * <p>Sensor 1 is the main sensor used to detect camera launch gesture.</p>
83 */
84 private long mCameraGestureSensor1LastOnTimeMs = 0L;
85
86 /**
87 * If applicable, how long the sensor 2 has been turned on since camera
88 * launch sensor was subscribed to and when the last camera launch
89 * gesture was detected.
90 * <p>Sensor 2 is the secondary sensor used to detect camera launch gesture.
91 * This is optional and if only sensor 1 is used for detect camera launch
92 * gesture, this value would always be 0.</p>
93 */
94 private long mCameraGestureSensor2LastOnTimeMs = 0L;
95
96 /**
97 * Extra information about the event when the last camera launch gesture
98 * was detected.
99 */
100 private int mCameraLaunchLastEventExtra = 0;
101
Adrian Roos5941c982015-09-03 15:59:49 -0700102 /**
103 * Whether camera double tap power button gesture is currently enabled;
104 */
105 private boolean mCameraDoubleTapPowerEnabled;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700106 private long mLastPowerDown;
Adrian Roos5941c982015-09-03 15:59:49 -0700107
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700108 public GestureLauncherService(Context context) {
109 super(context);
110 mContext = context;
111 }
112
113 public void onStart() {
Adrian Roos5941c982015-09-03 15:59:49 -0700114 LocalServices.addService(GestureLauncherService.class, this);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700115 }
116
117 public void onBootPhase(int phase) {
118 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
119 Resources resources = mContext.getResources();
120 if (!isGestureLauncherEnabled(resources)) {
121 if (DBG) Slog.d(TAG, "Gesture launcher is disabled in system properties.");
122 return;
123 }
124
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700125 PowerManager powerManager = (PowerManager) mContext.getSystemService(
126 Context.POWER_SERVICE);
Selim Cinek372d1bd2015-08-14 13:19:37 -0700127 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700128 "GestureLauncherService");
Jason Monk5a5e7d62015-08-04 11:20:25 -0400129 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700130 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400131
132 mUserId = ActivityManager.getCurrentUser();
133 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
Adrian Roos5941c982015-09-03 15:59:49 -0700134 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400135 }
136 }
137
Adrian Roos5941c982015-09-03 15:59:49 -0700138 private void registerContentObservers() {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400139 mContext.getContentResolver().registerContentObserver(
140 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
141 false, mSettingObserver, mUserId);
Adrian Roos5941c982015-09-03 15:59:49 -0700142 mContext.getContentResolver().registerContentObserver(
143 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
144 false, mSettingObserver, mUserId);
Jason Monk5a5e7d62015-08-04 11:20:25 -0400145 }
146
147 private void updateCameraRegistered() {
148 Resources resources = mContext.getResources();
149 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
150 registerCameraLaunchGesture(resources);
151 } else {
152 unregisterCameraLaunchGesture();
153 }
154 }
155
Adrian Roos5941c982015-09-03 15:59:49 -0700156 private void updateCameraDoubleTapPowerEnabled() {
157 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
158 synchronized (this) {
159 mCameraDoubleTapPowerEnabled = enabled;
160 }
161 }
162
Jason Monk5a5e7d62015-08-04 11:20:25 -0400163 private void unregisterCameraLaunchGesture() {
164 if (mRegistered) {
165 mRegistered = false;
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700166 mCameraGestureOnTimeMs = 0L;
167 mCameraGestureLastEventTime = 0L;
168 mCameraGestureSensor1LastOnTimeMs = 0;
169 mCameraGestureSensor2LastOnTimeMs = 0;
170 mCameraLaunchLastEventExtra = 0;
171
Jason Monk5a5e7d62015-08-04 11:20:25 -0400172 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
173 Context.SENSOR_SERVICE);
174 sensorManager.unregisterListener(mGestureListener);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700175 }
176 }
177
178 /**
179 * Registers for the camera launch gesture.
180 */
181 private void registerCameraLaunchGesture(Resources resources) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400182 if (mRegistered) {
183 return;
184 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700185 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
186 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700187 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
188 Context.SENSOR_SERVICE);
189 int cameraLaunchGestureId = resources.getInteger(
190 com.android.internal.R.integer.config_cameraLaunchGestureSensorType);
191 if (cameraLaunchGestureId != -1) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400192 mRegistered = false;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700193 String sensorName = resources.getString(
194 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
195 mCameraLaunchSensor = sensorManager.getDefaultSensor(
196 cameraLaunchGestureId,
197 true /*wakeUp*/);
198
199 // Compare the camera gesture string type to that in the resource file to make
200 // sure we are registering the correct sensor. This is redundant check, it
201 // makes the code more robust.
202 if (mCameraLaunchSensor != null) {
203 if (sensorName.equals(mCameraLaunchSensor.getStringType())) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400204 mRegistered = sensorManager.registerListener(mGestureListener,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700205 mCameraLaunchSensor, 0);
206 } else {
207 String message = String.format("Wrong configuration. Sensor type and sensor "
208 + "string type don't match: %s in resources, %s in the sensor.",
209 sensorName, mCameraLaunchSensor.getStringType());
210 throw new RuntimeException(message);
211 }
212 }
Jason Monk5a5e7d62015-08-04 11:20:25 -0400213 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700214 } else {
215 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
216 }
217 }
218
Jason Monk5a5e7d62015-08-04 11:20:25 -0400219 public static boolean isCameraLaunchSettingEnabled(Context context, int userId) {
220 return isCameraLaunchEnabled(context.getResources())
221 && (Settings.Secure.getIntForUser(context.getContentResolver(),
222 Settings.Secure.CAMERA_GESTURE_DISABLED, 0, userId) == 0);
223 }
224
Adrian Roos5941c982015-09-03 15:59:49 -0700225 public static boolean isCameraDoubleTapPowerSettingEnabled(Context context, int userId) {
226 return isCameraDoubleTapPowerEnabled(context.getResources())
227 && (Settings.Secure.getIntForUser(context.getContentResolver(),
228 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0, userId) == 0);
229 }
230
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700231 /**
232 * Whether to enable the camera launch gesture.
233 */
234 public static boolean isCameraLaunchEnabled(Resources resources) {
235 boolean configSet = resources.getInteger(
236 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
237 return configSet &&
238 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
239 }
240
Adrian Roos5941c982015-09-03 15:59:49 -0700241 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
242 return resources.getBoolean(
243 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
244 }
245
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700246 /**
247 * Whether GestureLauncherService should be enabled according to system properties.
248 */
249 public static boolean isGestureLauncherEnabled(Resources resources) {
Adrian Roos5941c982015-09-03 15:59:49 -0700250 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources);
251 }
252
253 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive) {
254 boolean launched = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700255 boolean intercept = false;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700256 long doubleTapInterval;
Adrian Roos5941c982015-09-03 15:59:49 -0700257 synchronized (this) {
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700258 doubleTapInterval = event.getEventTime() - mLastPowerDown;
259 if (mCameraDoubleTapPowerEnabled
Jorim Jaggid3f84d42015-10-01 14:13:04 -0700260 && doubleTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS
261 && doubleTapInterval > CAMERA_POWER_DOUBLE_TAP_MIN_TIME_MS) {
Adrian Roos5941c982015-09-03 15:59:49 -0700262 launched = true;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700263 intercept = interactive;
Adrian Roos5941c982015-09-03 15:59:49 -0700264 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700265 mLastPowerDown = event.getEventTime();
Adrian Roos5941c982015-09-03 15:59:49 -0700266 }
267 if (launched) {
Jorim Jaggid3f84d42015-10-01 14:13:04 -0700268 Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
269 + doubleTapInterval + "ms");
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700270 launched = handleCameraLaunchGesture(false /* useWakelock */,
271 StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700272 if (launched) {
273 MetricsLogger.action(mContext, MetricsLogger.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
274 (int) doubleTapInterval);
275 }
Adrian Roos5941c982015-09-03 15:59:49 -0700276 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700277 MetricsLogger.histogram(mContext, "power_double_tap_interval", (int) doubleTapInterval);
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700278 return intercept && launched;
Adrian Roos5941c982015-09-03 15:59:49 -0700279 }
280
281 /**
282 * @return true if camera was launched, false otherwise.
283 */
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700284 private boolean handleCameraLaunchGesture(boolean useWakelock, int source) {
Adrian Roos5941c982015-09-03 15:59:49 -0700285 boolean userSetupComplete = Settings.Secure.getInt(mContext.getContentResolver(),
286 Settings.Secure.USER_SETUP_COMPLETE, 0) != 0;
287 if (!userSetupComplete) {
288 if (DBG) Slog.d(TAG, String.format(
289 "userSetupComplete = %s, ignoring camera launch gesture.",
290 userSetupComplete));
291 return false;
292 }
293 if (DBG) Slog.d(TAG, String.format(
294 "userSetupComplete = %s, performing camera launch gesture.",
295 userSetupComplete));
296
297 if (useWakelock) {
298 // Make sure we don't sleep too early
299 mWakeLock.acquire(500L);
300 }
301 StatusBarManagerInternal service = LocalServices.getService(
302 StatusBarManagerInternal.class);
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700303 service.onCameraLaunchGestureDetected(source);
Adrian Roos5941c982015-09-03 15:59:49 -0700304 return true;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700305 }
306
Jason Monk5a5e7d62015-08-04 11:20:25 -0400307 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
308 @Override
309 public void onReceive(Context context, Intent intent) {
310 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
311 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
312 mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
Adrian Roos5941c982015-09-03 15:59:49 -0700313 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400314 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700315 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400316 }
317 }
318 };
319
320 private final ContentObserver mSettingObserver = new ContentObserver(new Handler()) {
321 public void onChange(boolean selfChange, android.net.Uri uri, int userId) {
322 if (userId == mUserId) {
323 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700324 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400325 }
326 }
327 };
328
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700329 private final class GestureEventListener implements SensorEventListener {
330 @Override
331 public void onSensorChanged(SensorEvent event) {
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700332 if (!mRegistered) {
333 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
334 return;
335 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700336 if (event.sensor == mCameraLaunchSensor) {
Adrian Roos5941c982015-09-03 15:59:49 -0700337 if (DBG) {
338 float[] values = event.values;
339 Slog.d(TAG, String.format("Received a camera launch event: " +
340 "values=[%.4f, %.4f, %.4f].", values[0], values[1], values[2]));
341 }
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700342 if (handleCameraLaunchGesture(true /* useWakelock */,
343 StatusBarManager.CAMERA_LAUNCH_SOURCE_WIGGLE)) {
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700344 MetricsLogger.action(mContext, MetricsLogger.ACTION_WIGGLE_CAMERA_GESTURE);
Adrian Roos5941c982015-09-03 15:59:49 -0700345 trackCameraLaunchEvent(event);
346 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700347 return;
348 }
349 }
350
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700351 @Override
352 public void onAccuracyChanged(Sensor sensor, int accuracy) {
353 // Ignored.
354 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700355
356 private void trackCameraLaunchEvent(SensorEvent event) {
357 long now = SystemClock.elapsedRealtime();
358 long totalDuration = now - mCameraGestureOnTimeMs;
359 // values[0]: ratio between total time duration when accel is turned on and time
360 // duration since camera launch gesture is subscribed.
361 // values[1]: ratio between total time duration when gyro is turned on and time duration
362 // since camera launch gesture is subscribed.
363 // values[2]: extra information
364 float[] values = event.values;
365
366 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
367 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
368 int extra = (int) values[2];
369
370 // We only log the difference in the event log to make aggregation easier.
371 long gestureOnTimeDiff = now - mCameraGestureLastEventTime;
372 long sensor1OnTimeDiff = sensor1OnTime - mCameraGestureSensor1LastOnTimeMs;
373 long sensor2OnTimeDiff = sensor2OnTime - mCameraGestureSensor2LastOnTimeMs;
374 int extraDiff = extra - mCameraLaunchLastEventExtra;
375
376 // Gating against negative time difference. This doesn't usually happen, but it may
377 // happen because of numeric errors.
378 if (gestureOnTimeDiff < 0 || sensor1OnTimeDiff < 0 || sensor2OnTimeDiff < 0) {
379 if (DBG) Slog.d(TAG, "Skipped event logging because negative numbers.");
380 return;
381 }
382
383 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
384 "sensor2OnTime: %d, extra: %d",
385 gestureOnTimeDiff,
386 sensor1OnTimeDiff,
387 sensor2OnTimeDiff,
388 extraDiff));
389 EventLogTags.writeCameraGestureTriggered(
390 gestureOnTimeDiff,
391 sensor1OnTimeDiff,
392 sensor2OnTimeDiff,
393 extraDiff);
394
395 mCameraGestureLastEventTime = now;
396 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
397 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
398 mCameraLaunchLastEventExtra = extra;
399 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700400 }
401}