blob: 69f0cef6d39eef8e6afaeead2306d98234d36abb [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;
104 private long mLastPowerDownWhileNonInteractive = 0;
105
106
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700107 public GestureLauncherService(Context context) {
108 super(context);
109 mContext = context;
110 }
111
112 public void onStart() {
Adrian Roos5941c982015-09-03 15:59:49 -0700113 LocalServices.addService(GestureLauncherService.class, this);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700114 }
115
116 public void onBootPhase(int phase) {
117 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
118 Resources resources = mContext.getResources();
119 if (!isGestureLauncherEnabled(resources)) {
120 if (DBG) Slog.d(TAG, "Gesture launcher is disabled in system properties.");
121 return;
122 }
123
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700124 PowerManager powerManager = (PowerManager) mContext.getSystemService(
125 Context.POWER_SERVICE);
Selim Cinek372d1bd2015-08-14 13:19:37 -0700126 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700127 "GestureLauncherService");
Jason Monk5a5e7d62015-08-04 11:20:25 -0400128 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700129 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400130
131 mUserId = ActivityManager.getCurrentUser();
132 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
Adrian Roos5941c982015-09-03 15:59:49 -0700133 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400134 }
135 }
136
Adrian Roos5941c982015-09-03 15:59:49 -0700137 private void registerContentObservers() {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400138 mContext.getContentResolver().registerContentObserver(
139 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
140 false, mSettingObserver, mUserId);
Adrian Roos5941c982015-09-03 15:59:49 -0700141 mContext.getContentResolver().registerContentObserver(
142 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
143 false, mSettingObserver, mUserId);
Jason Monk5a5e7d62015-08-04 11:20:25 -0400144 }
145
146 private void updateCameraRegistered() {
147 Resources resources = mContext.getResources();
148 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
149 registerCameraLaunchGesture(resources);
150 } else {
151 unregisterCameraLaunchGesture();
152 }
153 }
154
Adrian Roos5941c982015-09-03 15:59:49 -0700155 private void updateCameraDoubleTapPowerEnabled() {
156 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
157 synchronized (this) {
158 mCameraDoubleTapPowerEnabled = enabled;
159 }
160 }
161
Jason Monk5a5e7d62015-08-04 11:20:25 -0400162 private void unregisterCameraLaunchGesture() {
163 if (mRegistered) {
164 mRegistered = false;
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700165 mCameraGestureOnTimeMs = 0L;
166 mCameraGestureLastEventTime = 0L;
167 mCameraGestureSensor1LastOnTimeMs = 0;
168 mCameraGestureSensor2LastOnTimeMs = 0;
169 mCameraLaunchLastEventExtra = 0;
170
Jason Monk5a5e7d62015-08-04 11:20:25 -0400171 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
172 Context.SENSOR_SERVICE);
173 sensorManager.unregisterListener(mGestureListener);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700174 }
175 }
176
177 /**
178 * Registers for the camera launch gesture.
179 */
180 private void registerCameraLaunchGesture(Resources resources) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400181 if (mRegistered) {
182 return;
183 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700184 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
185 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700186 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
187 Context.SENSOR_SERVICE);
188 int cameraLaunchGestureId = resources.getInteger(
189 com.android.internal.R.integer.config_cameraLaunchGestureSensorType);
190 if (cameraLaunchGestureId != -1) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400191 mRegistered = false;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700192 String sensorName = resources.getString(
193 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
194 mCameraLaunchSensor = sensorManager.getDefaultSensor(
195 cameraLaunchGestureId,
196 true /*wakeUp*/);
197
198 // Compare the camera gesture string type to that in the resource file to make
199 // sure we are registering the correct sensor. This is redundant check, it
200 // makes the code more robust.
201 if (mCameraLaunchSensor != null) {
202 if (sensorName.equals(mCameraLaunchSensor.getStringType())) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400203 mRegistered = sensorManager.registerListener(mGestureListener,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700204 mCameraLaunchSensor, 0);
205 } else {
206 String message = String.format("Wrong configuration. Sensor type and sensor "
207 + "string type don't match: %s in resources, %s in the sensor.",
208 sensorName, mCameraLaunchSensor.getStringType());
209 throw new RuntimeException(message);
210 }
211 }
Jason Monk5a5e7d62015-08-04 11:20:25 -0400212 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700213 } else {
214 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
215 }
216 }
217
Jason Monk5a5e7d62015-08-04 11:20:25 -0400218 public static boolean isCameraLaunchSettingEnabled(Context context, int userId) {
219 return isCameraLaunchEnabled(context.getResources())
220 && (Settings.Secure.getIntForUser(context.getContentResolver(),
221 Settings.Secure.CAMERA_GESTURE_DISABLED, 0, userId) == 0);
222 }
223
Adrian Roos5941c982015-09-03 15:59:49 -0700224 public static boolean isCameraDoubleTapPowerSettingEnabled(Context context, int userId) {
225 return isCameraDoubleTapPowerEnabled(context.getResources())
226 && (Settings.Secure.getIntForUser(context.getContentResolver(),
227 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0, userId) == 0);
228 }
229
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700230 /**
231 * Whether to enable the camera launch gesture.
232 */
233 public static boolean isCameraLaunchEnabled(Resources resources) {
234 boolean configSet = resources.getInteger(
235 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
236 return configSet &&
237 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
238 }
239
Adrian Roos5941c982015-09-03 15:59:49 -0700240 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
241 return resources.getBoolean(
242 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
243 }
244
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700245 /**
246 * Whether GestureLauncherService should be enabled according to system properties.
247 */
248 public static boolean isGestureLauncherEnabled(Resources resources) {
Adrian Roos5941c982015-09-03 15:59:49 -0700249 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources);
250 }
251
252 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive) {
253 boolean launched = false;
254 synchronized (this) {
255 if (!mCameraDoubleTapPowerEnabled) {
256 mLastPowerDownWhileNonInteractive = 0;
257 return false;
258 }
259 if (event.getEventTime() - mLastPowerDownWhileNonInteractive
260 < CAMERA_POWER_DOUBLE_TAP_TIME_MS) {
261 launched = true;
262 }
263 mLastPowerDownWhileNonInteractive = interactive ? 0 : event.getEventTime();
264 }
265 if (launched) {
266 Slog.i(TAG, "Power button double tap gesture detected, launching camera.");
267 launched = handleCameraLaunchGesture(false /* useWakelock */,
268 MetricsLogger.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE);
269 }
270 return launched;
271 }
272
273 /**
274 * @return true if camera was launched, false otherwise.
275 */
276 private boolean handleCameraLaunchGesture(boolean useWakelock, int logCategory) {
277 boolean userSetupComplete = Settings.Secure.getInt(mContext.getContentResolver(),
278 Settings.Secure.USER_SETUP_COMPLETE, 0) != 0;
279 if (!userSetupComplete) {
280 if (DBG) Slog.d(TAG, String.format(
281 "userSetupComplete = %s, ignoring camera launch gesture.",
282 userSetupComplete));
283 return false;
284 }
285 if (DBG) Slog.d(TAG, String.format(
286 "userSetupComplete = %s, performing camera launch gesture.",
287 userSetupComplete));
288
289 if (useWakelock) {
290 // Make sure we don't sleep too early
291 mWakeLock.acquire(500L);
292 }
293 StatusBarManagerInternal service = LocalServices.getService(
294 StatusBarManagerInternal.class);
295 service.onCameraLaunchGestureDetected();
296 MetricsLogger.action(mContext, logCategory);
297 return true;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700298 }
299
Jason Monk5a5e7d62015-08-04 11:20:25 -0400300 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
301 @Override
302 public void onReceive(Context context, Intent intent) {
303 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
304 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
305 mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
Adrian Roos5941c982015-09-03 15:59:49 -0700306 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400307 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700308 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400309 }
310 }
311 };
312
313 private final ContentObserver mSettingObserver = new ContentObserver(new Handler()) {
314 public void onChange(boolean selfChange, android.net.Uri uri, int userId) {
315 if (userId == mUserId) {
316 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700317 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400318 }
319 }
320 };
321
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700322 private final class GestureEventListener implements SensorEventListener {
323 @Override
324 public void onSensorChanged(SensorEvent event) {
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700325 if (!mRegistered) {
326 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
327 return;
328 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700329 if (event.sensor == mCameraLaunchSensor) {
Adrian Roos5941c982015-09-03 15:59:49 -0700330 if (DBG) {
331 float[] values = event.values;
332 Slog.d(TAG, String.format("Received a camera launch event: " +
333 "values=[%.4f, %.4f, %.4f].", values[0], values[1], values[2]));
334 }
335 if (handleCameraLaunchGesture(true /* useWakelock */,
336 MetricsLogger.ACTION_WIGGLE_CAMERA_GESTURE)) {
337 trackCameraLaunchEvent(event);
338 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700339 return;
340 }
341 }
342
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700343 @Override
344 public void onAccuracyChanged(Sensor sensor, int accuracy) {
345 // Ignored.
346 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700347
348 private void trackCameraLaunchEvent(SensorEvent event) {
349 long now = SystemClock.elapsedRealtime();
350 long totalDuration = now - mCameraGestureOnTimeMs;
351 // values[0]: ratio between total time duration when accel is turned on and time
352 // duration since camera launch gesture is subscribed.
353 // values[1]: ratio between total time duration when gyro is turned on and time duration
354 // since camera launch gesture is subscribed.
355 // values[2]: extra information
356 float[] values = event.values;
357
358 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
359 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
360 int extra = (int) values[2];
361
362 // We only log the difference in the event log to make aggregation easier.
363 long gestureOnTimeDiff = now - mCameraGestureLastEventTime;
364 long sensor1OnTimeDiff = sensor1OnTime - mCameraGestureSensor1LastOnTimeMs;
365 long sensor2OnTimeDiff = sensor2OnTime - mCameraGestureSensor2LastOnTimeMs;
366 int extraDiff = extra - mCameraLaunchLastEventExtra;
367
368 // Gating against negative time difference. This doesn't usually happen, but it may
369 // happen because of numeric errors.
370 if (gestureOnTimeDiff < 0 || sensor1OnTimeDiff < 0 || sensor2OnTimeDiff < 0) {
371 if (DBG) Slog.d(TAG, "Skipped event logging because negative numbers.");
372 return;
373 }
374
375 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
376 "sensor2OnTime: %d, extra: %d",
377 gestureOnTimeDiff,
378 sensor1OnTimeDiff,
379 sensor2OnTimeDiff,
380 extraDiff));
381 EventLogTags.writeCameraGestureTriggered(
382 gestureOnTimeDiff,
383 sensor1OnTimeDiff,
384 sensor2OnTimeDiff,
385 extraDiff);
386
387 mCameraGestureLastEventTime = now;
388 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
389 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
390 mCameraLaunchLastEventExtra = extra;
391 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700392 }
393}