blob: 2e2ddbad2bdb7897da5003458580cae7e91ce8e0 [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;
Amith Yamasanif16c3be2016-07-15 15:33:03 -070036import android.os.UserHandle;
Kevin Gabayan024713c2015-07-31 12:01:43 -070037import android.provider.Settings;
Jorim Jaggif1cdf952016-04-05 21:41:06 -070038import android.util.MutableBoolean;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070039import android.util.Slog;
Adrian Roos5941c982015-09-03 15:59:49 -070040import android.view.KeyEvent;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070041
Juan Langacf62e62017-04-18 16:35:39 -070042import com.android.internal.annotations.VisibleForTesting;
Adrian Roos5941c982015-09-03 15:59:49 -070043import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010044import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Selim Cinek372d1bd2015-08-14 13:19:37 -070045import com.android.server.statusbar.StatusBarManagerInternal;
46
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070047/**
48 * The service that listens for gestures detected in sensor firmware and starts the intent
49 * accordingly.
50 * <p>For now, only camera launch gesture is supported, and in the future, more gestures can be
51 * added.</p>
52 * @hide
53 */
Adrian Roos5941c982015-09-03 15:59:49 -070054public class GestureLauncherService extends SystemService {
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070055 private static final boolean DBG = false;
56 private static final String TAG = "GestureLauncherService";
57
Adrian Roos5941c982015-09-03 15:59:49 -070058 /**
59 * Time in milliseconds in which the power button must be pressed twice so it will be considered
60 * as a camera launch.
61 */
Juan Langacf62e62017-04-18 16:35:39 -070062 @VisibleForTesting static final long CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS = 300;
Adrian Roos5941c982015-09-03 15:59:49 -070063
Juan Lang0ba22372017-04-05 13:03:04 -070064 /**
65 * Interval in milliseconds in which the power button must be depressed in succession to be
66 * considered part of an extended sequence of taps. Note that this is a looser threshold than
67 * the camera launch gesture, because the purpose of this threshold is to measure the
68 * frequency of consecutive taps, for evaluation for future gestures.
69 */
70 @VisibleForTesting static final long POWER_SHORT_TAP_SEQUENCE_MAX_INTERVAL_MS = 500;
71
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070072 /** The listener that receives the gesture event. */
73 private final GestureEventListener mGestureListener = new GestureEventListener();
74
75 private Sensor mCameraLaunchSensor;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070076 private Context mContext;
Juan Lang36f46242017-04-19 11:58:28 -070077 private final MetricsLogger mMetricsLogger;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070078
79 /** The wake lock held when a gesture is detected. */
80 private WakeLock mWakeLock;
Jason Monk5a5e7d62015-08-04 11:20:25 -040081 private boolean mRegistered;
82 private int mUserId;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -070083
Zhentao Sun575b6fd2015-08-25 21:26:45 -070084 // Below are fields used for event logging only.
85 /** Elapsed real time when the camera gesture is turned on. */
86 private long mCameraGestureOnTimeMs = 0L;
87
88 /** Elapsed real time when the last camera gesture was detected. */
89 private long mCameraGestureLastEventTime = 0L;
90
91 /**
92 * How long the sensor 1 has been turned on since camera launch sensor was
93 * subscribed to and when the last camera launch gesture was detected.
94 * <p>Sensor 1 is the main sensor used to detect camera launch gesture.</p>
95 */
96 private long mCameraGestureSensor1LastOnTimeMs = 0L;
97
98 /**
99 * If applicable, how long the sensor 2 has been turned on since camera
100 * launch sensor was subscribed to and when the last camera launch
101 * gesture was detected.
102 * <p>Sensor 2 is the secondary sensor used to detect camera launch gesture.
103 * This is optional and if only sensor 1 is used for detect camera launch
104 * gesture, this value would always be 0.</p>
105 */
106 private long mCameraGestureSensor2LastOnTimeMs = 0L;
107
108 /**
109 * Extra information about the event when the last camera launch gesture
110 * was detected.
111 */
112 private int mCameraLaunchLastEventExtra = 0;
113
Adrian Roos5941c982015-09-03 15:59:49 -0700114 /**
115 * Whether camera double tap power button gesture is currently enabled;
116 */
117 private boolean mCameraDoubleTapPowerEnabled;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700118 private long mLastPowerDown;
Juan Lang0ba22372017-04-05 13:03:04 -0700119 private int mPowerButtonConsecutiveTaps;
Adrian Roos5941c982015-09-03 15:59:49 -0700120
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700121 public GestureLauncherService(Context context) {
Juan Lang36f46242017-04-19 11:58:28 -0700122 this(context, new MetricsLogger());
123 }
124
125 @VisibleForTesting
126 GestureLauncherService(Context context, MetricsLogger metricsLogger) {
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700127 super(context);
128 mContext = context;
Juan Lang36f46242017-04-19 11:58:28 -0700129 mMetricsLogger = metricsLogger;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700130 }
131
132 public void onStart() {
Adrian Roos5941c982015-09-03 15:59:49 -0700133 LocalServices.addService(GestureLauncherService.class, this);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700134 }
135
136 public void onBootPhase(int phase) {
137 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
138 Resources resources = mContext.getResources();
139 if (!isGestureLauncherEnabled(resources)) {
140 if (DBG) Slog.d(TAG, "Gesture launcher is disabled in system properties.");
141 return;
142 }
143
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700144 PowerManager powerManager = (PowerManager) mContext.getSystemService(
145 Context.POWER_SERVICE);
Selim Cinek372d1bd2015-08-14 13:19:37 -0700146 mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700147 "GestureLauncherService");
Jason Monk5a5e7d62015-08-04 11:20:25 -0400148 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700149 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400150
151 mUserId = ActivityManager.getCurrentUser();
152 mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
Adrian Roos5941c982015-09-03 15:59:49 -0700153 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400154 }
155 }
156
Adrian Roos5941c982015-09-03 15:59:49 -0700157 private void registerContentObservers() {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400158 mContext.getContentResolver().registerContentObserver(
159 Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED),
160 false, mSettingObserver, mUserId);
Adrian Roos5941c982015-09-03 15:59:49 -0700161 mContext.getContentResolver().registerContentObserver(
162 Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED),
163 false, mSettingObserver, mUserId);
Jason Monk5a5e7d62015-08-04 11:20:25 -0400164 }
165
166 private void updateCameraRegistered() {
167 Resources resources = mContext.getResources();
168 if (isCameraLaunchSettingEnabled(mContext, mUserId)) {
169 registerCameraLaunchGesture(resources);
170 } else {
171 unregisterCameraLaunchGesture();
172 }
173 }
174
Juan Langacf62e62017-04-18 16:35:39 -0700175 @VisibleForTesting
176 void updateCameraDoubleTapPowerEnabled() {
Adrian Roos5941c982015-09-03 15:59:49 -0700177 boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId);
178 synchronized (this) {
179 mCameraDoubleTapPowerEnabled = enabled;
180 }
181 }
182
Jason Monk5a5e7d62015-08-04 11:20:25 -0400183 private void unregisterCameraLaunchGesture() {
184 if (mRegistered) {
185 mRegistered = false;
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700186 mCameraGestureOnTimeMs = 0L;
187 mCameraGestureLastEventTime = 0L;
188 mCameraGestureSensor1LastOnTimeMs = 0;
189 mCameraGestureSensor2LastOnTimeMs = 0;
190 mCameraLaunchLastEventExtra = 0;
191
Jason Monk5a5e7d62015-08-04 11:20:25 -0400192 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
193 Context.SENSOR_SERVICE);
194 sensorManager.unregisterListener(mGestureListener);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700195 }
196 }
197
198 /**
199 * Registers for the camera launch gesture.
200 */
201 private void registerCameraLaunchGesture(Resources resources) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400202 if (mRegistered) {
203 return;
204 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700205 mCameraGestureOnTimeMs = SystemClock.elapsedRealtime();
206 mCameraGestureLastEventTime = mCameraGestureOnTimeMs;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700207 SensorManager sensorManager = (SensorManager) mContext.getSystemService(
208 Context.SENSOR_SERVICE);
209 int cameraLaunchGestureId = resources.getInteger(
210 com.android.internal.R.integer.config_cameraLaunchGestureSensorType);
211 if (cameraLaunchGestureId != -1) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400212 mRegistered = false;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700213 String sensorName = resources.getString(
214 com.android.internal.R.string.config_cameraLaunchGestureSensorStringType);
215 mCameraLaunchSensor = sensorManager.getDefaultSensor(
216 cameraLaunchGestureId,
217 true /*wakeUp*/);
218
219 // Compare the camera gesture string type to that in the resource file to make
220 // sure we are registering the correct sensor. This is redundant check, it
221 // makes the code more robust.
222 if (mCameraLaunchSensor != null) {
223 if (sensorName.equals(mCameraLaunchSensor.getStringType())) {
Jason Monk5a5e7d62015-08-04 11:20:25 -0400224 mRegistered = sensorManager.registerListener(mGestureListener,
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700225 mCameraLaunchSensor, 0);
226 } else {
227 String message = String.format("Wrong configuration. Sensor type and sensor "
228 + "string type don't match: %s in resources, %s in the sensor.",
229 sensorName, mCameraLaunchSensor.getStringType());
230 throw new RuntimeException(message);
231 }
232 }
Jason Monk5a5e7d62015-08-04 11:20:25 -0400233 if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered);
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700234 } else {
235 if (DBG) Slog.d(TAG, "Camera launch sensor is not specified.");
236 }
237 }
238
Jason Monk5a5e7d62015-08-04 11:20:25 -0400239 public static boolean isCameraLaunchSettingEnabled(Context context, int userId) {
240 return isCameraLaunchEnabled(context.getResources())
241 && (Settings.Secure.getIntForUser(context.getContentResolver(),
242 Settings.Secure.CAMERA_GESTURE_DISABLED, 0, userId) == 0);
243 }
244
Adrian Roos5941c982015-09-03 15:59:49 -0700245 public static boolean isCameraDoubleTapPowerSettingEnabled(Context context, int userId) {
246 return isCameraDoubleTapPowerEnabled(context.getResources())
247 && (Settings.Secure.getIntForUser(context.getContentResolver(),
248 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0, userId) == 0);
249 }
250
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700251 /**
252 * Whether to enable the camera launch gesture.
253 */
254 public static boolean isCameraLaunchEnabled(Resources resources) {
255 boolean configSet = resources.getInteger(
256 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
257 return configSet &&
258 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
259 }
260
Adrian Roos5941c982015-09-03 15:59:49 -0700261 public static boolean isCameraDoubleTapPowerEnabled(Resources resources) {
262 return resources.getBoolean(
263 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
264 }
265
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700266 /**
267 * Whether GestureLauncherService should be enabled according to system properties.
268 */
269 public static boolean isGestureLauncherEnabled(Resources resources) {
Adrian Roos5941c982015-09-03 15:59:49 -0700270 return isCameraLaunchEnabled(resources) || isCameraDoubleTapPowerEnabled(resources);
271 }
272
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700273 public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive,
274 MutableBoolean outLaunched) {
Adrian Roos5941c982015-09-03 15:59:49 -0700275 boolean launched = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700276 boolean intercept = false;
Juan Lang0ba22372017-04-05 13:03:04 -0700277 long powerTapInterval;
Adrian Roos5941c982015-09-03 15:59:49 -0700278 synchronized (this) {
Juan Lang0ba22372017-04-05 13:03:04 -0700279 powerTapInterval = event.getEventTime() - mLastPowerDown;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700280 if (mCameraDoubleTapPowerEnabled
Juan Lang0ba22372017-04-05 13:03:04 -0700281 && powerTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS) {
Adrian Roos5941c982015-09-03 15:59:49 -0700282 launched = true;
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700283 intercept = interactive;
Juan Lang0ba22372017-04-05 13:03:04 -0700284 mPowerButtonConsecutiveTaps++;
285 } else if (powerTapInterval < POWER_SHORT_TAP_SEQUENCE_MAX_INTERVAL_MS) {
286 mPowerButtonConsecutiveTaps++;
287 } else {
288 mPowerButtonConsecutiveTaps = 1;
Adrian Roos5941c982015-09-03 15:59:49 -0700289 }
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700290 mLastPowerDown = event.getEventTime();
Adrian Roos5941c982015-09-03 15:59:49 -0700291 }
Juan Lang0ba22372017-04-05 13:03:04 -0700292 if (DBG && mPowerButtonConsecutiveTaps > 1) {
293 Slog.i(TAG, Long.valueOf(mPowerButtonConsecutiveTaps) +
294 " consecutive power button taps detected");
295 }
Adrian Roos5941c982015-09-03 15:59:49 -0700296 if (launched) {
Jorim Jaggid3f84d42015-10-01 14:13:04 -0700297 Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
Juan Lang0ba22372017-04-05 13:03:04 -0700298 + powerTapInterval + "ms");
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700299 launched = handleCameraLaunchGesture(false /* useWakelock */,
300 StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700301 if (launched) {
Juan Lang36f46242017-04-19 11:58:28 -0700302 mMetricsLogger.action(MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
Juan Lang0ba22372017-04-05 13:03:04 -0700303 (int) powerTapInterval);
Jorim Jaggi1b68e8b2015-09-22 14:44:14 -0700304 }
Adrian Roos5941c982015-09-03 15:59:49 -0700305 }
Juan Lang0ba22372017-04-05 13:03:04 -0700306 mMetricsLogger.histogram("power_consecutive_short_tap_count", mPowerButtonConsecutiveTaps);
307 mMetricsLogger.histogram("power_double_tap_interval", (int) powerTapInterval);
Jorim Jaggif1cdf952016-04-05 21:41:06 -0700308 outLaunched.value = launched;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700309 return intercept && launched;
Adrian Roos5941c982015-09-03 15:59:49 -0700310 }
311
312 /**
313 * @return true if camera was launched, false otherwise.
314 */
Juan Langacf62e62017-04-18 16:35:39 -0700315 @VisibleForTesting
316 boolean handleCameraLaunchGesture(boolean useWakelock, int source) {
Amith Yamasanif16c3be2016-07-15 15:33:03 -0700317 boolean userSetupComplete = Settings.Secure.getIntForUser(mContext.getContentResolver(),
318 Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
Adrian Roos5941c982015-09-03 15:59:49 -0700319 if (!userSetupComplete) {
320 if (DBG) Slog.d(TAG, String.format(
321 "userSetupComplete = %s, ignoring camera launch gesture.",
322 userSetupComplete));
323 return false;
324 }
325 if (DBG) Slog.d(TAG, String.format(
326 "userSetupComplete = %s, performing camera launch gesture.",
327 userSetupComplete));
328
329 if (useWakelock) {
330 // Make sure we don't sleep too early
331 mWakeLock.acquire(500L);
332 }
333 StatusBarManagerInternal service = LocalServices.getService(
334 StatusBarManagerInternal.class);
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700335 service.onCameraLaunchGestureDetected(source);
Adrian Roos5941c982015-09-03 15:59:49 -0700336 return true;
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700337 }
338
Jason Monk5a5e7d62015-08-04 11:20:25 -0400339 private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
340 @Override
341 public void onReceive(Context context, Intent intent) {
342 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
343 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
344 mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
Adrian Roos5941c982015-09-03 15:59:49 -0700345 registerContentObservers();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400346 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700347 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400348 }
349 }
350 };
351
352 private final ContentObserver mSettingObserver = new ContentObserver(new Handler()) {
353 public void onChange(boolean selfChange, android.net.Uri uri, int userId) {
354 if (userId == mUserId) {
355 updateCameraRegistered();
Adrian Roos5941c982015-09-03 15:59:49 -0700356 updateCameraDoubleTapPowerEnabled();
Jason Monk5a5e7d62015-08-04 11:20:25 -0400357 }
358 }
359 };
360
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700361 private final class GestureEventListener implements SensorEventListener {
362 @Override
363 public void onSensorChanged(SensorEvent event) {
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700364 if (!mRegistered) {
365 if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered.");
366 return;
367 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700368 if (event.sensor == mCameraLaunchSensor) {
Adrian Roos5941c982015-09-03 15:59:49 -0700369 if (DBG) {
370 float[] values = event.values;
371 Slog.d(TAG, String.format("Received a camera launch event: " +
372 "values=[%.4f, %.4f, %.4f].", values[0], values[1], values[2]));
373 }
Jorim Jaggi40aa8812015-09-23 12:59:22 -0700374 if (handleCameraLaunchGesture(true /* useWakelock */,
375 StatusBarManager.CAMERA_LAUNCH_SOURCE_WIGGLE)) {
Juan Lang36f46242017-04-19 11:58:28 -0700376 mMetricsLogger.action(MetricsEvent.ACTION_WIGGLE_CAMERA_GESTURE);
Adrian Roos5941c982015-09-03 15:59:49 -0700377 trackCameraLaunchEvent(event);
378 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700379 return;
380 }
381 }
382
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700383 @Override
384 public void onAccuracyChanged(Sensor sensor, int accuracy) {
385 // Ignored.
386 }
Zhentao Sun575b6fd2015-08-25 21:26:45 -0700387
388 private void trackCameraLaunchEvent(SensorEvent event) {
389 long now = SystemClock.elapsedRealtime();
390 long totalDuration = now - mCameraGestureOnTimeMs;
391 // values[0]: ratio between total time duration when accel is turned on and time
392 // duration since camera launch gesture is subscribed.
393 // values[1]: ratio between total time duration when gyro is turned on and time duration
394 // since camera launch gesture is subscribed.
395 // values[2]: extra information
396 float[] values = event.values;
397
398 long sensor1OnTime = (long) (totalDuration * (double) values[0]);
399 long sensor2OnTime = (long) (totalDuration * (double) values[1]);
400 int extra = (int) values[2];
401
402 // We only log the difference in the event log to make aggregation easier.
403 long gestureOnTimeDiff = now - mCameraGestureLastEventTime;
404 long sensor1OnTimeDiff = sensor1OnTime - mCameraGestureSensor1LastOnTimeMs;
405 long sensor2OnTimeDiff = sensor2OnTime - mCameraGestureSensor2LastOnTimeMs;
406 int extraDiff = extra - mCameraLaunchLastEventExtra;
407
408 // Gating against negative time difference. This doesn't usually happen, but it may
409 // happen because of numeric errors.
410 if (gestureOnTimeDiff < 0 || sensor1OnTimeDiff < 0 || sensor2OnTimeDiff < 0) {
411 if (DBG) Slog.d(TAG, "Skipped event logging because negative numbers.");
412 return;
413 }
414
415 if (DBG) Slog.d(TAG, String.format("totalDuration: %d, sensor1OnTime: %s, " +
416 "sensor2OnTime: %d, extra: %d",
417 gestureOnTimeDiff,
418 sensor1OnTimeDiff,
419 sensor2OnTimeDiff,
420 extraDiff));
421 EventLogTags.writeCameraGestureTriggered(
422 gestureOnTimeDiff,
423 sensor1OnTimeDiff,
424 sensor2OnTimeDiff,
425 extraDiff);
426
427 mCameraGestureLastEventTime = now;
428 mCameraGestureSensor1LastOnTimeMs = sensor1OnTime;
429 mCameraGestureSensor2LastOnTimeMs = sensor2OnTime;
430 mCameraLaunchLastEventExtra = extra;
431 }
Zhentao Sunc6cd1f92015-07-21 17:43:53 -0700432 }
433}