2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
19 import android.app.ActivityManager;
20 import android.app.StatusBarManager;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.res.Resources;
26 import android.database.ContentObserver;
27 import android.hardware.Sensor;
28 import android.hardware.SensorEvent;
29 import android.hardware.SensorEventListener;
30 import android.hardware.SensorManager;
31 import android.os.Handler;
32 import android.os.PowerManager;
33 import android.os.PowerManager.WakeLock;
34 import android.os.SystemClock;
35 import android.os.SystemProperties;
36 import android.provider.Settings;
37 import android.util.MutableBoolean;
38 import android.util.Slog;
39 import android.view.KeyEvent;
41 import com.android.internal.logging.MetricsLogger;
42 import com.android.internal.logging.MetricsProto.MetricsEvent;
43 import com.android.server.statusbar.StatusBarManagerInternal;
46 * The service that listens for gestures detected in sensor firmware and starts the intent
48 * <p>For now, only camera launch gesture is supported, and in the future, more gestures can be
52 public class GestureLauncherService extends SystemService {
53 private static final boolean DBG = false;
54 private static final String TAG = "GestureLauncherService";
57 * Time in milliseconds in which the power button must be pressed twice so it will be considered
60 private static final long CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS = 300;
62 /** The listener that receives the gesture event. */
63 private final GestureEventListener mGestureListener = new GestureEventListener();
65 private Sensor mCameraLaunchSensor;
66 private Context mContext;
68 /** The wake lock held when a gesture is detected. */
69 private WakeLock mWakeLock;
70 private boolean mRegistered;
73 // Below are fields used for event logging only.
74 /** Elapsed real time when the camera gesture is turned on. */
75 private long mCameraGestureOnTimeMs = 0L;
77 /** Elapsed real time when the last camera gesture was detected. */
78 private long mCameraGestureLastEventTime = 0L;
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>
85 private long mCameraGestureSensor1LastOnTimeMs = 0L;
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>
95 private long mCameraGestureSensor2LastOnTimeMs = 0L;
98 * Extra information about the event when the last camera launch gesture
101 private int mCameraLaunchLastEventExtra = 0;
104 * Whether camera double tap power button gesture is currently enabled;
106 private boolean mCameraDoubleTapPowerEnabled;
107 private long mLastPowerDown;
109 public GestureLauncherService(Context context) {
114 public void onStart() {
115 LocalServices.addService(GestureLauncherService.class, this);
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.");
126 PowerManager powerManager = (PowerManager) mContext.getSystemService(
127 Context.POWER_SERVICE);
128 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
129 "GestureLauncherService");
130 updateCameraRegistered();
131 updateCameraDoubleTapPowerEnabled();
133 mUserId = ActivityManager.getCurrentUser();
134 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
135 registerContentObservers();
139 private void registerContentObservers() {
140 mContext.getContentResolver().registerContentObserver(
141 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
142 false, mSettingObserver, mUserId);
143 mContext.getContentResolver().registerContentObserver(
144 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
145 false, mSettingObserver, mUserId);
148 private void updateCameraRegistered() {
149 Resources resources = mContext.getResources();
150 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
151 registerCameraLaunchGesture(resources);
153 unregisterCameraLaunchGesture();
157 private void updateCameraDoubleTapPowerEnabled() {
158 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
159 synchronized (this) {
160 mCameraDoubleTapPowerEnabled = enabled;
164 private void unregisterCameraLaunchGesture() {
167 mCameraGestureOnTimeMs = 0L;
168 mCameraGestureLastEventTime = 0L;
169 mCameraGestureSensor1LastOnTimeMs = 0;
170 mCameraGestureSensor2LastOnTimeMs = 0;
171 mCameraLaunchLastEventExtra = 0;
173 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
174 Context.SENSOR_SERVICE);
175 sensorManager.unregisterListener(mGestureListener);
180 * Registers for the camera launch gesture.
182 private void registerCameraLaunchGesture(Resources resources) {
186 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
187 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
188 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) {
194 String sensorName = resources.getString(
195 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
196 mCameraLaunchSensor = sensorManager.getDefaultSensor(
197 cameraLaunchGestureId,
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())) {
205 mRegistered = sensorManager.registerListener(mGestureListener,
206 mCameraLaunchSensor, 0);
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);
214 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered);
216 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
220 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);
226 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);
233 * Whether to enable the camera launch gesture.
235 public static boolean isCameraLaunchEnabled(Resources resources) {
236 boolean configSet = resources.getInteger(
237 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
239 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
242 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
243 return resources.getBoolean(
244 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
248 * Whether GestureLauncherService should be enabled according to system properties.
250 public static boolean isGestureLauncherEnabled(Resources resources) {
251 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources);
254 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive,
255 MutableBoolean outLaunched) {
256 boolean launched = false;
257 boolean intercept = false;
258 long doubleTapInterval;
259 synchronized (this) {
260 doubleTapInterval = event.getEventTime() - mLastPowerDown;
261 if (mCameraDoubleTapPowerEnabled
262 && doubleTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS) {
264 intercept = interactive;
266 mLastPowerDown = event.getEventTime();
269 Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
270 + doubleTapInterval + "ms");
271 launched = handleCameraLaunchGesture(false /* useWakelock */,
272 StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
274 MetricsLogger.action(mContext, MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
275 (int) doubleTapInterval);
278 MetricsLogger.histogram(mContext, "power_double_tap_interval", (int) doubleTapInterval);
279 outLaunched.value = launched;
280 return intercept && launched;
284 * @return true if camera was launched, false otherwise.
286 private boolean handleCameraLaunchGesture(boolean useWakelock, int source) {
287 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.",
295 if (DBG) Slog.d(TAG, String.format(
296 "userSetupComplete = %s, performing camera launch gesture.",
300 // Make sure we don't sleep too early
301 mWakeLock.acquire(500L);
303 StatusBarManagerInternal service = LocalServices.getService(
304 StatusBarManagerInternal.class);
305 service.onCameraLaunchGestureDetected(source);
309 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
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);
315 registerContentObservers();
316 updateCameraRegistered();
317 updateCameraDoubleTapPowerEnabled();
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();
326 updateCameraDoubleTapPowerEnabled();
331 private final class GestureEventListener implements SensorEventListener {
333 public void onSensorChanged(SensorEvent event) {
335 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
338 if (event.sensor == mCameraLaunchSensor) {
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]));
344 if (handleCameraLaunchGesture(true /* useWakelock */,
345 StatusBarManager.CAMERA_LAUNCH_SOURCE_WIGGLE)) {
346 MetricsLogger.action(mContext, MetricsEvent.ACTION_WIGGLE_CAMERA_GESTURE);
347 trackCameraLaunchEvent(event);
354 public void onAccuracyChanged(Sensor sensor, int accuracy) {
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;
368 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
369 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
370 int extra = (int) values[2];
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;
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.");
385 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
386 "sensor2OnTime: %d, extra: %d",
391 EventLogTags.writeCameraGestureTriggered(
397 mCameraGestureLastEventTime = now;
398 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
399 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
400 mCameraLaunchLastEventExtra = extra;