Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 23 | import android.content.IntentFilter; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 25 | import android.database.ContentObserver; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 26 | import android.hardware.Sensor; |
| 27 | import android.hardware.SensorEvent; |
| 28 | import android.hardware.SensorEventListener; |
| 29 | import android.hardware.SensorManager; |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 30 | import android.os.Handler; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 31 | import android.os.PowerManager; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 32 | import android.os.PowerManager.WakeLock; |
Zhentao Sun | 575b6fd | 2015-08-25 21:26:45 -0700 | [diff] [blame] | 33 | import android.os.SystemClock; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 34 | import android.os.SystemProperties; |
Kevin Gabayan | 024713c | 2015-07-31 12:01:43 -0700 | [diff] [blame] | 35 | import android.provider.Settings; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 36 | import android.util.Slog; |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 37 | import android.view.KeyEvent; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 38 | |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 39 | import com.android.internal.logging.MetricsLogger; |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 40 | import com.android.server.statusbar.StatusBarManagerInternal; |
| 41 | |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 42 | /** |
| 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 Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 49 | public class GestureLauncherService extends SystemService { |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 50 | private static final boolean DBG = false; |
| 51 | private static final String TAG = "GestureLauncherService"; |
| 52 | |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 53 | /** |
| 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 Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 59 | /** The listener that receives the gesture event. */ |
| 60 | private final GestureEventListener mGestureListener = new GestureEventListener(); |
| 61 | |
| 62 | private Sensor mCameraLaunchSensor; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 63 | private Context mContext; |
| 64 | |
| 65 | /** The wake lock held when a gesture is detected. */ |
| 66 | private WakeLock mWakeLock; |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 67 | private boolean mRegistered; |
| 68 | private int mUserId; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 69 | |
Zhentao Sun | 575b6fd | 2015-08-25 21:26:45 -0700 | [diff] [blame] | 70 | // 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 Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 100 | /** |
| 101 | * Whether camera double tap power button gesture is currently enabled; |
| 102 | */ |
| 103 | private boolean mCameraDoubleTapPowerEnabled; |
| 104 | private long mLastPowerDownWhileNonInteractive = 0; |
| 105 | |
| 106 | |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 107 | public GestureLauncherService(Context context) { |
| 108 | super(context); |
| 109 | mContext = context; |
| 110 | } |
| 111 | |
| 112 | public void onStart() { |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 113 | LocalServices.addService(GestureLauncherService.class, this); |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 114 | } |
| 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 Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 124 | PowerManager powerManager = (PowerManager) mContext.getSystemService( |
| 125 | Context.POWER_SERVICE); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 126 | mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 127 | "GestureLauncherService"); |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 128 | updateCameraRegistered(); |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 129 | updateCameraDoubleTapPowerEnabled(); |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 130 | |
| 131 | mUserId = ActivityManager.getCurrentUser(); |
| 132 | mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED)); |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 133 | registerContentObservers(); |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 137 | private void registerContentObservers() { |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 138 | mContext.getContentResolver().registerContentObserver( |
| 139 | Settings.Secure.getUriFor(Settings.Secure.CAMERA_GESTURE_DISABLED), |
| 140 | false, mSettingObserver, mUserId); |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 141 | mContext.getContentResolver().registerContentObserver( |
| 142 | Settings.Secure.getUriFor(Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED), |
| 143 | false, mSettingObserver, mUserId); |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 144 | } |
| 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 Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 155 | private void updateCameraDoubleTapPowerEnabled() { |
| 156 | boolean enabled = isCameraDoubleTapPowerSettingEnabled(mContext, mUserId); |
| 157 | synchronized (this) { |
| 158 | mCameraDoubleTapPowerEnabled = enabled; |
| 159 | } |
| 160 | } |
| 161 | |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 162 | private void unregisterCameraLaunchGesture() { |
| 163 | if (mRegistered) { |
| 164 | mRegistered = false; |
Zhentao Sun | 575b6fd | 2015-08-25 21:26:45 -0700 | [diff] [blame] | 165 | mCameraGestureOnTimeMs = 0L; |
| 166 | mCameraGestureLastEventTime = 0L; |
| 167 | mCameraGestureSensor1LastOnTimeMs = 0; |
| 168 | mCameraGestureSensor2LastOnTimeMs = 0; |
| 169 | mCameraLaunchLastEventExtra = 0; |
| 170 | |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 171 | SensorManager sensorManager = (SensorManager) mContext.getSystemService( |
| 172 | Context.SENSOR_SERVICE); |
| 173 | sensorManager.unregisterListener(mGestureListener); |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Registers for the camera launch gesture. |
| 179 | */ |
| 180 | private void registerCameraLaunchGesture(Resources resources) { |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 181 | if (mRegistered) { |
| 182 | return; |
| 183 | } |
Zhentao Sun | 575b6fd | 2015-08-25 21:26:45 -0700 | [diff] [blame] | 184 | mCameraGestureOnTimeMs = SystemClock.elapsedRealtime(); |
| 185 | mCameraGestureLastEventTime = mCameraGestureOnTimeMs; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 186 | 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 Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 191 | mRegistered = false; |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 192 | 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 Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 203 | mRegistered = sensorManager.registerListener(mGestureListener, |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 204 | 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 Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 212 | if (DBG) Slog.d(TAG, "Camera launch sensor registered: " + mRegistered); |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 213 | } else { |
| 214 | if (DBG) Slog.d(TAG, "Camera launch sensor is not specified."); |
| 215 | } |
| 216 | } |
| 217 | |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 218 | 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 Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 224 | 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 Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 230 | /** |
| 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 Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 240 | public static boolean isCameraDoubleTapPowerEnabled(Resources resources) { |
| 241 | return resources.getBoolean( |
| 242 | com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled); |
| 243 | } |
| 244 | |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Whether GestureLauncherService should be enabled according to system properties. |
| 247 | */ |
| 248 | public static boolean isGestureLauncherEnabled(Resources resources) { |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 249 | 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 Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 300 | 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 Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 306 | registerContentObservers(); |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 307 | updateCameraRegistered(); |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 308 | updateCameraDoubleTapPowerEnabled(); |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 309 | } |
| 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 Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 317 | updateCameraDoubleTapPowerEnabled(); |
Jason Monk | 5a5e7d6 | 2015-08-04 11:20:25 -0400 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | }; |
| 321 | |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 322 | private final class GestureEventListener implements SensorEventListener { |
| 323 | @Override |
| 324 | public void onSensorChanged(SensorEvent event) { |
Zhentao Sun | 575b6fd | 2015-08-25 21:26:45 -0700 | [diff] [blame] | 325 | if (!mRegistered) { |
| 326 | if (DBG) Slog.d(TAG, "Ignoring gesture event because it's unregistered."); |
| 327 | return; |
| 328 | } |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 329 | if (event.sensor == mCameraLaunchSensor) { |
Adrian Roos | 5941c98 | 2015-09-03 15:59:49 -0700 | [diff] [blame^] | 330 | 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 Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | } |
| 342 | |
Zhentao Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 343 | @Override |
| 344 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 345 | // Ignored. |
| 346 | } |
Zhentao Sun | 575b6fd | 2015-08-25 21:26:45 -0700 | [diff] [blame] | 347 | |
| 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 Sun | c6cd1f9 | 2015-07-21 17:43:53 -0700 | [diff] [blame] | 392 | } |
| 393 | } |