Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -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.systemui.analytics; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.database.ContentObserver; |
| 21 | import android.hardware.Sensor; |
| 22 | import android.hardware.SensorEvent; |
| 23 | import android.hardware.SensorEventListener; |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 24 | import android.os.AsyncTask; |
| 25 | import android.os.Build; |
| 26 | import android.os.Handler; |
| 27 | import android.os.UserHandle; |
| 28 | import android.provider.Settings; |
| 29 | import android.util.Log; |
| 30 | import android.view.MotionEvent; |
| 31 | |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 32 | import java.io.File; |
| 33 | import java.io.FileOutputStream; |
| 34 | import java.io.IOException; |
| 35 | |
| 36 | import static com.android.systemui.statusbar.phone.TouchAnalyticsProto.Session; |
| 37 | import static com.android.systemui.statusbar.phone.TouchAnalyticsProto.Session.PhoneEvent; |
| 38 | |
| 39 | /** |
| 40 | * Tracks touch, sensor and phone events when the lockscreen is on. If the phone is unlocked |
| 41 | * the data containing these events is saved to a file. This data is collected |
| 42 | * to analyze how a human interaction looks like. |
| 43 | * |
| 44 | * A session starts when the screen is turned on. |
| 45 | * A session ends when the screen is turned off or user unlocks the phone. |
| 46 | */ |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 47 | public class DataCollector implements SensorEventListener { |
| 48 | private static final String TAG = "DataCollector"; |
| 49 | private static final String COLLECTOR_ENABLE = "data_collector_enable"; |
| 50 | private static final String COLLECT_BAD_TOUCHES = "data_collector_collect_bad_touches"; |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 51 | |
| 52 | private static final long TIMEOUT_MILLIS = 11000; // 11 seconds. |
| 53 | public static final boolean DEBUG = false; |
| 54 | |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 55 | private final Handler mHandler = new Handler(); |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 56 | private final Context mContext; |
| 57 | |
| 58 | // Err on the side of caution, so logging is not started after a crash even tough the screen |
| 59 | // is off. |
| 60 | private SensorLoggerSession mCurrentSession = null; |
| 61 | |
| 62 | private boolean mEnableCollector = false; |
| 63 | private boolean mTimeoutActive = false; |
| 64 | private boolean mCollectBadTouches = false; |
| 65 | private boolean mCornerSwiping = false; |
| 66 | private boolean mTrackingStarted = false; |
| 67 | |
| 68 | private static DataCollector sInstance = null; |
| 69 | |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 70 | protected final ContentObserver mSettingsObserver = new ContentObserver(mHandler) { |
| 71 | @Override |
| 72 | public void onChange(boolean selfChange) { |
| 73 | updateConfiguration(); |
| 74 | } |
| 75 | }; |
| 76 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 77 | private DataCollector(Context context) { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 78 | mContext = context; |
| 79 | |
| 80 | mContext.getContentResolver().registerContentObserver( |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 81 | Settings.Secure.getUriFor(COLLECTOR_ENABLE), false, |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 82 | mSettingsObserver, |
| 83 | UserHandle.USER_ALL); |
| 84 | |
| 85 | mContext.getContentResolver().registerContentObserver( |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 86 | Settings.Secure.getUriFor(COLLECT_BAD_TOUCHES), false, |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 87 | mSettingsObserver, |
| 88 | UserHandle.USER_ALL); |
| 89 | |
| 90 | updateConfiguration(); |
| 91 | } |
| 92 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 93 | public static DataCollector getInstance(Context context) { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 94 | if (sInstance == null) { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 95 | sInstance = new DataCollector(context); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 96 | } |
| 97 | return sInstance; |
| 98 | } |
| 99 | |
| 100 | private void updateConfiguration() { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 101 | mEnableCollector = Build.IS_DEBUGGABLE && 0 != Settings.Secure.getInt( |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 102 | mContext.getContentResolver(), |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 103 | COLLECTOR_ENABLE, 0); |
| 104 | mCollectBadTouches = mEnableCollector && 0 != Settings.Secure.getInt( |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 105 | mContext.getContentResolver(), |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 106 | COLLECT_BAD_TOUCHES, 0); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private boolean sessionEntrypoint() { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 110 | if (mEnableCollector && mCurrentSession == null) { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 111 | onSessionStart(); |
| 112 | return true; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | private void sessionExitpoint(int result) { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 118 | if (mEnableCollector && mCurrentSession != null) { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 119 | onSessionEnd(result); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private void onSessionStart() { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 124 | mCornerSwiping = false; |
| 125 | mTrackingStarted = false; |
| 126 | mCurrentSession = new SensorLoggerSession(System.currentTimeMillis(), System.nanoTime()); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private void onSessionEnd(int result) { |
| 130 | SensorLoggerSession session = mCurrentSession; |
| 131 | mCurrentSession = null; |
| 132 | |
| 133 | session.end(System.currentTimeMillis(), result); |
| 134 | queueSession(session); |
| 135 | } |
| 136 | |
| 137 | private void queueSession(final SensorLoggerSession currentSession) { |
| 138 | AsyncTask.execute(new Runnable() { |
| 139 | @Override |
| 140 | public void run() { |
| 141 | byte[] b = Session.toByteArray(currentSession.toProto()); |
| 142 | String dir = mContext.getFilesDir().getAbsolutePath(); |
| 143 | if (currentSession.getResult() != Session.SUCCESS) { |
| 144 | if (!mCollectBadTouches) { |
| 145 | return; |
| 146 | } |
| 147 | dir += "/bad_touches"; |
| 148 | } else { |
| 149 | dir += "/good_touches"; |
| 150 | } |
| 151 | |
| 152 | File file = new File(dir); |
| 153 | file.mkdir(); |
| 154 | File touch = new File(file, "trace_" + System.currentTimeMillis()); |
| 155 | |
| 156 | try { |
| 157 | new FileOutputStream(touch).write(b); |
| 158 | } catch (IOException e) { |
| 159 | throw new RuntimeException(e); |
| 160 | } |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public synchronized void onSensorChanged(SensorEvent event) { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 167 | if (mEnableCollector && mCurrentSession != null) { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 168 | mCurrentSession.addSensorEvent(event, System.nanoTime()); |
| 169 | enforceTimeout(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private void enforceTimeout() { |
| 174 | if (mTimeoutActive) { |
| 175 | if (System.currentTimeMillis() - mCurrentSession.getStartTimestampMillis() |
| 176 | > TIMEOUT_MILLIS) { |
| 177 | onSessionEnd(Session.UNKNOWN); |
| 178 | if (DEBUG) { |
| 179 | Log.i(TAG, "Analytics timed out."); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 187 | } |
| 188 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 189 | public boolean isEnabled() { |
| 190 | return mEnableCollector; |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 193 | public void onScreenTurningOn() { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 194 | if (sessionEntrypoint()) { |
| 195 | if (DEBUG) { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 196 | Log.d(TAG, "onScreenTurningOn"); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 197 | } |
| 198 | addEvent(PhoneEvent.ON_SCREEN_ON); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public void onScreenOnFromTouch() { |
| 203 | if (sessionEntrypoint()) { |
| 204 | if (DEBUG) { |
| 205 | Log.d(TAG, "onScreenOnFromTouch"); |
| 206 | } |
| 207 | addEvent(PhoneEvent.ON_SCREEN_ON_FROM_TOUCH); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | public void onScreenOff() { |
| 212 | if (DEBUG) { |
| 213 | Log.d(TAG, "onScreenOff"); |
| 214 | } |
| 215 | addEvent(PhoneEvent.ON_SCREEN_OFF); |
| 216 | sessionExitpoint(Session.FAILURE); |
| 217 | } |
| 218 | |
| 219 | public void onSucccessfulUnlock() { |
| 220 | if (DEBUG) { |
| 221 | Log.d(TAG, "onSuccessfulUnlock"); |
| 222 | } |
| 223 | addEvent(PhoneEvent.ON_SUCCESSFUL_UNLOCK); |
| 224 | sessionExitpoint(Session.SUCCESS); |
| 225 | } |
| 226 | |
| 227 | public void onBouncerShown() { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 228 | if (DEBUG) { |
| 229 | Log.d(TAG, "onBouncerShown"); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 230 | } |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 231 | addEvent(PhoneEvent.ON_BOUNCER_SHOWN); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | public void onBouncerHidden() { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 235 | if (DEBUG) { |
| 236 | Log.d(TAG, "onBouncerHidden"); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 237 | } |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 238 | addEvent(PhoneEvent.ON_BOUNCER_HIDDEN); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | public void onQsDown() { |
| 242 | if (DEBUG) { |
| 243 | Log.d(TAG, "onQsDown"); |
| 244 | } |
| 245 | addEvent(PhoneEvent.ON_QS_DOWN); |
| 246 | } |
| 247 | |
| 248 | public void setQsExpanded(boolean expanded) { |
| 249 | if (DEBUG) { |
| 250 | Log.d(TAG, "setQsExpanded = " + expanded); |
| 251 | } |
| 252 | if (expanded) { |
| 253 | addEvent(PhoneEvent.SET_QS_EXPANDED_TRUE); |
| 254 | } else { |
| 255 | addEvent(PhoneEvent.SET_QS_EXPANDED_FALSE); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | public void onTrackingStarted() { |
| 260 | if (DEBUG) { |
| 261 | Log.d(TAG, "onTrackingStarted"); |
| 262 | } |
| 263 | mTrackingStarted = true; |
| 264 | addEvent(PhoneEvent.ON_TRACKING_STARTED); |
| 265 | } |
| 266 | |
| 267 | public void onTrackingStopped() { |
| 268 | if (mTrackingStarted) { |
| 269 | if (DEBUG) { |
| 270 | Log.d(TAG, "onTrackingStopped"); |
| 271 | } |
| 272 | mTrackingStarted = false; |
| 273 | addEvent(PhoneEvent.ON_TRACKING_STOPPED); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | public void onNotificationActive() { |
| 278 | if (DEBUG) { |
| 279 | Log.d(TAG, "onNotificationActive"); |
| 280 | } |
| 281 | addEvent(PhoneEvent.ON_NOTIFICATION_ACTIVE); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | public void onNotificationDoubleTap() { |
| 286 | if (DEBUG) { |
| 287 | Log.d(TAG, "onNotificationDoubleTap"); |
| 288 | } |
| 289 | addEvent(PhoneEvent.ON_NOTIFICATION_DOUBLE_TAP); |
| 290 | } |
| 291 | |
| 292 | public void setNotificationExpanded() { |
| 293 | if (DEBUG) { |
| 294 | Log.d(TAG, "setNotificationExpanded"); |
| 295 | } |
| 296 | addEvent(PhoneEvent.SET_NOTIFICATION_EXPANDED); |
| 297 | } |
| 298 | |
| 299 | public void onNotificatonStartDraggingDown() { |
| 300 | if (DEBUG) { |
| 301 | Log.d(TAG, "onNotificationStartDraggingDown"); |
| 302 | } |
| 303 | addEvent(PhoneEvent.ON_NOTIFICATION_START_DRAGGING_DOWN); |
| 304 | } |
| 305 | |
| 306 | public void onNotificatonStopDraggingDown() { |
| 307 | if (DEBUG) { |
| 308 | Log.d(TAG, "onNotificationStopDraggingDown"); |
| 309 | } |
| 310 | addEvent(PhoneEvent.ON_NOTIFICATION_STOP_DRAGGING_DOWN); |
| 311 | } |
| 312 | |
| 313 | public void onNotificationDismissed() { |
| 314 | if (DEBUG) { |
| 315 | Log.d(TAG, "onNotificationDismissed"); |
| 316 | } |
| 317 | addEvent(PhoneEvent.ON_NOTIFICATION_DISMISSED); |
| 318 | } |
| 319 | |
| 320 | public void onNotificatonStartDismissing() { |
| 321 | if (DEBUG) { |
| 322 | Log.d(TAG, "onNotificationStartDismissing"); |
| 323 | } |
| 324 | addEvent(PhoneEvent.ON_NOTIFICATION_START_DISMISSING); |
| 325 | } |
| 326 | |
| 327 | public void onNotificatonStopDismissing() { |
| 328 | if (DEBUG) { |
| 329 | Log.d(TAG, "onNotificationStopDismissing"); |
| 330 | } |
| 331 | addEvent(PhoneEvent.ON_NOTIFICATION_STOP_DISMISSING); |
| 332 | } |
| 333 | |
| 334 | public void onCameraOn() { |
| 335 | if (DEBUG) { |
| 336 | Log.d(TAG, "onCameraOn"); |
| 337 | } |
| 338 | addEvent(PhoneEvent.ON_CAMERA_ON); |
| 339 | } |
| 340 | |
| 341 | public void onLeftAffordanceOn() { |
| 342 | if (DEBUG) { |
| 343 | Log.d(TAG, "onLeftAffordanceOn"); |
| 344 | } |
| 345 | addEvent(PhoneEvent.ON_LEFT_AFFORDANCE_ON); |
| 346 | } |
| 347 | |
| 348 | public void onAffordanceSwipingStarted(boolean rightCorner) { |
| 349 | if (DEBUG) { |
| 350 | Log.d(TAG, "onAffordanceSwipingStarted"); |
| 351 | } |
| 352 | mCornerSwiping = true; |
| 353 | if (rightCorner) { |
| 354 | addEvent(PhoneEvent.ON_RIGHT_AFFORDANCE_SWIPING_STARTED); |
| 355 | } else { |
| 356 | addEvent(PhoneEvent.ON_LEFT_AFFORDANCE_SWIPING_STARTED); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | public void onAffordanceSwipingAborted() { |
| 361 | if (mCornerSwiping) { |
| 362 | if (DEBUG) { |
| 363 | Log.d(TAG, "onAffordanceSwipingAborted"); |
| 364 | } |
| 365 | mCornerSwiping = false; |
| 366 | addEvent(PhoneEvent.ON_AFFORDANCE_SWIPING_ABORTED); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | public void onUnlockHintStarted() { |
| 371 | if (DEBUG) { |
| 372 | Log.d(TAG, "onUnlockHintStarted"); |
| 373 | } |
| 374 | addEvent(PhoneEvent.ON_UNLOCK_HINT_STARTED); |
| 375 | } |
| 376 | |
| 377 | public void onCameraHintStarted() { |
| 378 | if (DEBUG) { |
| 379 | Log.d(TAG, "onCameraHintStarted"); |
| 380 | } |
| 381 | addEvent(PhoneEvent.ON_CAMERA_HINT_STARTED); |
| 382 | } |
| 383 | |
| 384 | public void onLeftAffordanceHintStarted() { |
| 385 | if (DEBUG) { |
| 386 | Log.d(TAG, "onLeftAffordanceHintStarted"); |
| 387 | } |
| 388 | addEvent(PhoneEvent.ON_LEFT_AFFORDANCE_HINT_STARTED); |
| 389 | } |
| 390 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 391 | public void onTouchEvent(MotionEvent event, int width, int height) { |
| 392 | if (mCurrentSession != null) { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 393 | if (DEBUG) { |
| 394 | Log.v(TAG, "onTouchEvent(ev.action=" |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 395 | + MotionEvent.actionToString(event.getAction()) + ")"); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 396 | } |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 397 | mCurrentSession.addMotionEvent(event); |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 398 | mCurrentSession.setTouchArea(width, height); |
| 399 | enforceTimeout(); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | private void addEvent(int eventType) { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 404 | if (mEnableCollector && mCurrentSession != null) { |
Blazej Magnowski | 7232332 | 2015-07-24 11:49:40 -0700 | [diff] [blame] | 405 | mCurrentSession.addPhoneEvent(eventType, System.nanoTime()); |
| 406 | } |
| 407 | } |
| 408 | } |