blob: 46e004c5304ac6a49516bbd9c6a88304d27edad1 [file] [log] [blame]
Blazej Magnowski72323322015-07-24 11:49:40 -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.systemui.analytics;
18
Dave Mankoffc88d6222018-10-25 15:31:20 -040019import static com.android.systemui.statusbar.phone.nano.TouchAnalyticsProto.Session;
20import static com.android.systemui.statusbar.phone.nano.TouchAnalyticsProto.Session.PhoneEvent;
21
Blazej Magnowski72323322015-07-24 11:49:40 -070022import android.content.Context;
23import android.database.ContentObserver;
24import android.hardware.Sensor;
25import android.hardware.SensorEvent;
26import android.hardware.SensorEventListener;
Adrian Roos7bb38a92016-07-21 11:44:01 -070027import android.net.Uri;
Blazej Magnowski72323322015-07-24 11:49:40 -070028import android.os.AsyncTask;
29import android.os.Build;
30import android.os.Handler;
Selim Cinekf8c4add2017-06-08 09:54:58 -070031import android.os.Looper;
Blazej Magnowski72323322015-07-24 11:49:40 -070032import android.os.UserHandle;
33import android.provider.Settings;
34import android.util.Log;
35import android.view.MotionEvent;
Adrian Roos7bb38a92016-07-21 11:44:01 -070036import android.widget.Toast;
Blazej Magnowski72323322015-07-24 11:49:40 -070037
Dave Mankoff21beba82018-11-06 16:25:39 -050038import com.android.systemui.Dependency;
39import com.android.systemui.plugins.FalsingPlugin;
40import com.android.systemui.plugins.PluginListener;
41import com.android.systemui.shared.plugins.PluginManager;
42
Blazej Magnowski72323322015-07-24 11:49:40 -070043import java.io.File;
44import java.io.FileOutputStream;
45import java.io.IOException;
46
Blazej Magnowski72323322015-07-24 11:49:40 -070047/**
48 * Tracks touch, sensor and phone events when the lockscreen is on. If the phone is unlocked
49 * the data containing these events is saved to a file. This data is collected
50 * to analyze how a human interaction looks like.
51 *
52 * A session starts when the screen is turned on.
53 * A session ends when the screen is turned off or user unlocks the phone.
54 */
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -070055public class DataCollector implements SensorEventListener {
56 private static final String TAG = "DataCollector";
57 private static final String COLLECTOR_ENABLE = "data_collector_enable";
58 private static final String COLLECT_BAD_TOUCHES = "data_collector_collect_bad_touches";
Adrian Roos7bb38a92016-07-21 11:44:01 -070059 private static final String ALLOW_REJECTED_TOUCH_REPORTS =
60 "data_collector_allow_rejected_touch_reports";
Dave Mankoffc88d6222018-10-25 15:31:20 -040061 private static final String DISABLE_UNLOCKING_FOR_FALSING_COLLECTION =
62 "data_collector_disable_unlocking";
Blazej Magnowski72323322015-07-24 11:49:40 -070063
64 private static final long TIMEOUT_MILLIS = 11000; // 11 seconds.
65 public static final boolean DEBUG = false;
66
Selim Cinekf8c4add2017-06-08 09:54:58 -070067 private final Handler mHandler = new Handler(Looper.getMainLooper());
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -070068 private final Context mContext;
69
70 // Err on the side of caution, so logging is not started after a crash even tough the screen
71 // is off.
72 private SensorLoggerSession mCurrentSession = null;
73
74 private boolean mEnableCollector = false;
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -070075 private boolean mCollectBadTouches = false;
76 private boolean mCornerSwiping = false;
77 private boolean mTrackingStarted = false;
Adrian Roos7bb38a92016-07-21 11:44:01 -070078 private boolean mAllowReportRejectedTouch = false;
Dave Mankoffc88d6222018-10-25 15:31:20 -040079 private boolean mDisableUnlocking = false;
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -070080
81 private static DataCollector sInstance = null;
82
Dave Mankoff21beba82018-11-06 16:25:39 -050083 private FalsingPlugin mFalsingPlugin = null;
84
Blazej Magnowski72323322015-07-24 11:49:40 -070085 protected final ContentObserver mSettingsObserver = new ContentObserver(mHandler) {
86 @Override
87 public void onChange(boolean selfChange) {
88 updateConfiguration();
89 }
90 };
91
Dave Mankoff21beba82018-11-06 16:25:39 -050092 private final PluginListener mPluginListener = new PluginListener<FalsingPlugin>() {
93 public void onPluginConnected(FalsingPlugin plugin, Context context) {
94 mFalsingPlugin = plugin;
95 }
96
97 public void onPluginDisconnected(FalsingPlugin plugin) {
98 mFalsingPlugin = null;
99 }
100 };
101
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700102 private DataCollector(Context context) {
Blazej Magnowski72323322015-07-24 11:49:40 -0700103 mContext = context;
104
105 mContext.getContentResolver().registerContentObserver(
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700106 Settings.Secure.getUriFor(COLLECTOR_ENABLE), false,
Blazej Magnowski72323322015-07-24 11:49:40 -0700107 mSettingsObserver,
108 UserHandle.USER_ALL);
109
110 mContext.getContentResolver().registerContentObserver(
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700111 Settings.Secure.getUriFor(COLLECT_BAD_TOUCHES), false,
Blazej Magnowski72323322015-07-24 11:49:40 -0700112 mSettingsObserver,
113 UserHandle.USER_ALL);
114
Adrian Roos7bb38a92016-07-21 11:44:01 -0700115 mContext.getContentResolver().registerContentObserver(
116 Settings.Secure.getUriFor(ALLOW_REJECTED_TOUCH_REPORTS), false,
117 mSettingsObserver,
118 UserHandle.USER_ALL);
119
Dave Mankoffc88d6222018-10-25 15:31:20 -0400120 mContext.getContentResolver().registerContentObserver(
121 Settings.Secure.getUriFor(DISABLE_UNLOCKING_FOR_FALSING_COLLECTION), false,
122 mSettingsObserver,
123 UserHandle.USER_ALL);
124
Blazej Magnowski72323322015-07-24 11:49:40 -0700125 updateConfiguration();
Dave Mankoff21beba82018-11-06 16:25:39 -0500126
127 Dependency.get(PluginManager.class).addPluginListener(mPluginListener, FalsingPlugin.class);
Blazej Magnowski72323322015-07-24 11:49:40 -0700128 }
129
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700130 public static DataCollector getInstance(Context context) {
Blazej Magnowski72323322015-07-24 11:49:40 -0700131 if (sInstance == null) {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700132 sInstance = new DataCollector(context);
Blazej Magnowski72323322015-07-24 11:49:40 -0700133 }
134 return sInstance;
135 }
136
137 private void updateConfiguration() {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700138 mEnableCollector = Build.IS_DEBUGGABLE && 0 != Settings.Secure.getInt(
Blazej Magnowski72323322015-07-24 11:49:40 -0700139 mContext.getContentResolver(),
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700140 COLLECTOR_ENABLE, 0);
141 mCollectBadTouches = mEnableCollector && 0 != Settings.Secure.getInt(
Blazej Magnowski72323322015-07-24 11:49:40 -0700142 mContext.getContentResolver(),
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700143 COLLECT_BAD_TOUCHES, 0);
Adrian Roos7bb38a92016-07-21 11:44:01 -0700144 mAllowReportRejectedTouch = Build.IS_DEBUGGABLE && 0 != Settings.Secure.getInt(
145 mContext.getContentResolver(),
146 ALLOW_REJECTED_TOUCH_REPORTS, 0);
Dave Mankoffc88d6222018-10-25 15:31:20 -0400147 mDisableUnlocking = mEnableCollector && Build.IS_DEBUGGABLE && 0 != Settings.Secure.getInt(
148 mContext.getContentResolver(),
149 DISABLE_UNLOCKING_FOR_FALSING_COLLECTION, 0);
Blazej Magnowski72323322015-07-24 11:49:40 -0700150 }
151
152 private boolean sessionEntrypoint() {
Adrian Roos7bb38a92016-07-21 11:44:01 -0700153 if (isEnabled() && mCurrentSession == null) {
Blazej Magnowski72323322015-07-24 11:49:40 -0700154 onSessionStart();
155 return true;
156 }
157 return false;
158 }
159
160 private void sessionExitpoint(int result) {
Adrian Roos7bb38a92016-07-21 11:44:01 -0700161 if (mCurrentSession != null) {
Blazej Magnowski72323322015-07-24 11:49:40 -0700162 onSessionEnd(result);
163 }
164 }
165
166 private void onSessionStart() {
Blazej Magnowski72323322015-07-24 11:49:40 -0700167 mCornerSwiping = false;
168 mTrackingStarted = false;
169 mCurrentSession = new SensorLoggerSession(System.currentTimeMillis(), System.nanoTime());
Blazej Magnowski72323322015-07-24 11:49:40 -0700170 }
171
172 private void onSessionEnd(int result) {
173 SensorLoggerSession session = mCurrentSession;
174 mCurrentSession = null;
175
Dave Mankoffc88d6222018-10-25 15:31:20 -0400176 if (mEnableCollector || mDisableUnlocking) {
Adrian Roos7bb38a92016-07-21 11:44:01 -0700177 session.end(System.currentTimeMillis(), result);
178 queueSession(session);
179 }
180 }
181
182 public Uri reportRejectedTouch() {
183 if (mCurrentSession == null) {
184 Toast.makeText(mContext, "Generating rejected touch report failed: session timed out.",
185 Toast.LENGTH_LONG).show();
186 return null;
187 }
188 SensorLoggerSession currentSession = mCurrentSession;
189
190 currentSession.setType(Session.REJECTED_TOUCH_REPORT);
191 currentSession.end(System.currentTimeMillis(), Session.SUCCESS);
192 Session proto = currentSession.toProto();
193
194 byte[] b = Session.toByteArray(proto);
195 File dir = new File(mContext.getExternalCacheDir(), "rejected_touch_reports");
196 dir.mkdir();
197 File touch = new File(dir, "rejected_touch_report_" + System.currentTimeMillis());
198
199 try {
200 new FileOutputStream(touch).write(b);
201 } catch (IOException e) {
202 throw new RuntimeException(e);
203 }
204
205 return Uri.fromFile(touch);
Blazej Magnowski72323322015-07-24 11:49:40 -0700206 }
207
208 private void queueSession(final SensorLoggerSession currentSession) {
209 AsyncTask.execute(new Runnable() {
210 @Override
211 public void run() {
212 byte[] b = Session.toByteArray(currentSession.toProto());
Dave Mankoff21beba82018-11-06 16:25:39 -0500213
214 if (mFalsingPlugin != null) {
215 mFalsingPlugin.dataCollected(currentSession.getResult() == Session.SUCCESS, b);
216 } else {
217 String dir = mContext.getFilesDir().getAbsolutePath();
218 if (currentSession.getResult() != Session.SUCCESS) {
219 if (!mDisableUnlocking && !mCollectBadTouches) {
220 return;
221 }
222 dir += "/bad_touches";
223 } else if (!mDisableUnlocking) {
224 dir += "/good_touches";
Blazej Magnowski72323322015-07-24 11:49:40 -0700225 }
Blazej Magnowski72323322015-07-24 11:49:40 -0700226
Dave Mankoff21beba82018-11-06 16:25:39 -0500227 File file = new File(dir);
228 file.mkdir();
229 File touch = new File(file, "trace_" + System.currentTimeMillis());
230 try {
231 new FileOutputStream(touch).write(b);
232 } catch (IOException e) {
233 throw new RuntimeException(e);
234 }
Blazej Magnowski72323322015-07-24 11:49:40 -0700235 }
236 }
237 });
238 }
239
Blazej Magnowski72323322015-07-24 11:49:40 -0700240 @Override
241 public synchronized void onSensorChanged(SensorEvent event) {
Adrian Roos7bb38a92016-07-21 11:44:01 -0700242 if (isEnabled() && mCurrentSession != null) {
Blazej Magnowski72323322015-07-24 11:49:40 -0700243 mCurrentSession.addSensorEvent(event, System.nanoTime());
Blazej Magnowski72323322015-07-24 11:49:40 -0700244 }
245 }
246
247 @Override
248 public void onAccuracyChanged(Sensor sensor, int accuracy) {
249 }
250
Adrian Roos7bb38a92016-07-21 11:44:01 -0700251 /**
252 * @return true if data is being collected - either for data gathering or creating a
253 * rejected touch report.
254 */
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700255 public boolean isEnabled() {
Dave Mankoffc88d6222018-10-25 15:31:20 -0400256 return mEnableCollector || mAllowReportRejectedTouch || mDisableUnlocking;
Adrian Roos7bb38a92016-07-21 11:44:01 -0700257 }
258
Dave Mankoffc88d6222018-10-25 15:31:20 -0400259 public boolean isUnlockingDisabled() {
260 return mDisableUnlocking;
261 }
Adrian Roos7bb38a92016-07-21 11:44:01 -0700262 /**
263 * @return true if the full data set for data gathering should be collected - including
264 * extensive sensor data, which is is not normally included with rejected touch reports.
265 */
266 public boolean isEnabledFull() {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700267 return mEnableCollector;
Blazej Magnowski72323322015-07-24 11:49:40 -0700268 }
269
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700270 public void onScreenTurningOn() {
Blazej Magnowski72323322015-07-24 11:49:40 -0700271 if (sessionEntrypoint()) {
272 if (DEBUG) {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700273 Log.d(TAG, "onScreenTurningOn");
Blazej Magnowski72323322015-07-24 11:49:40 -0700274 }
275 addEvent(PhoneEvent.ON_SCREEN_ON);
276 }
277 }
278
279 public void onScreenOnFromTouch() {
280 if (sessionEntrypoint()) {
281 if (DEBUG) {
282 Log.d(TAG, "onScreenOnFromTouch");
283 }
284 addEvent(PhoneEvent.ON_SCREEN_ON_FROM_TOUCH);
285 }
286 }
287
288 public void onScreenOff() {
289 if (DEBUG) {
290 Log.d(TAG, "onScreenOff");
291 }
292 addEvent(PhoneEvent.ON_SCREEN_OFF);
293 sessionExitpoint(Session.FAILURE);
294 }
295
296 public void onSucccessfulUnlock() {
297 if (DEBUG) {
298 Log.d(TAG, "onSuccessfulUnlock");
299 }
300 addEvent(PhoneEvent.ON_SUCCESSFUL_UNLOCK);
301 sessionExitpoint(Session.SUCCESS);
302 }
303
304 public void onBouncerShown() {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700305 if (DEBUG) {
306 Log.d(TAG, "onBouncerShown");
Blazej Magnowski72323322015-07-24 11:49:40 -0700307 }
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700308 addEvent(PhoneEvent.ON_BOUNCER_SHOWN);
Blazej Magnowski72323322015-07-24 11:49:40 -0700309 }
310
311 public void onBouncerHidden() {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700312 if (DEBUG) {
313 Log.d(TAG, "onBouncerHidden");
Blazej Magnowski72323322015-07-24 11:49:40 -0700314 }
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700315 addEvent(PhoneEvent.ON_BOUNCER_HIDDEN);
Blazej Magnowski72323322015-07-24 11:49:40 -0700316 }
317
318 public void onQsDown() {
319 if (DEBUG) {
320 Log.d(TAG, "onQsDown");
321 }
322 addEvent(PhoneEvent.ON_QS_DOWN);
323 }
324
325 public void setQsExpanded(boolean expanded) {
326 if (DEBUG) {
327 Log.d(TAG, "setQsExpanded = " + expanded);
328 }
329 if (expanded) {
330 addEvent(PhoneEvent.SET_QS_EXPANDED_TRUE);
331 } else {
332 addEvent(PhoneEvent.SET_QS_EXPANDED_FALSE);
333 }
334 }
335
336 public void onTrackingStarted() {
337 if (DEBUG) {
338 Log.d(TAG, "onTrackingStarted");
339 }
340 mTrackingStarted = true;
341 addEvent(PhoneEvent.ON_TRACKING_STARTED);
342 }
343
344 public void onTrackingStopped() {
345 if (mTrackingStarted) {
346 if (DEBUG) {
347 Log.d(TAG, "onTrackingStopped");
348 }
349 mTrackingStarted = false;
350 addEvent(PhoneEvent.ON_TRACKING_STOPPED);
351 }
352 }
353
354 public void onNotificationActive() {
355 if (DEBUG) {
356 Log.d(TAG, "onNotificationActive");
357 }
358 addEvent(PhoneEvent.ON_NOTIFICATION_ACTIVE);
359 }
360
361
362 public void onNotificationDoubleTap() {
363 if (DEBUG) {
364 Log.d(TAG, "onNotificationDoubleTap");
365 }
366 addEvent(PhoneEvent.ON_NOTIFICATION_DOUBLE_TAP);
367 }
368
369 public void setNotificationExpanded() {
370 if (DEBUG) {
371 Log.d(TAG, "setNotificationExpanded");
372 }
373 addEvent(PhoneEvent.SET_NOTIFICATION_EXPANDED);
374 }
375
376 public void onNotificatonStartDraggingDown() {
377 if (DEBUG) {
378 Log.d(TAG, "onNotificationStartDraggingDown");
379 }
380 addEvent(PhoneEvent.ON_NOTIFICATION_START_DRAGGING_DOWN);
381 }
382
383 public void onNotificatonStopDraggingDown() {
384 if (DEBUG) {
385 Log.d(TAG, "onNotificationStopDraggingDown");
386 }
387 addEvent(PhoneEvent.ON_NOTIFICATION_STOP_DRAGGING_DOWN);
388 }
389
390 public void onNotificationDismissed() {
391 if (DEBUG) {
392 Log.d(TAG, "onNotificationDismissed");
393 }
394 addEvent(PhoneEvent.ON_NOTIFICATION_DISMISSED);
395 }
396
397 public void onNotificatonStartDismissing() {
398 if (DEBUG) {
399 Log.d(TAG, "onNotificationStartDismissing");
400 }
401 addEvent(PhoneEvent.ON_NOTIFICATION_START_DISMISSING);
402 }
403
404 public void onNotificatonStopDismissing() {
405 if (DEBUG) {
406 Log.d(TAG, "onNotificationStopDismissing");
407 }
408 addEvent(PhoneEvent.ON_NOTIFICATION_STOP_DISMISSING);
409 }
410
411 public void onCameraOn() {
412 if (DEBUG) {
413 Log.d(TAG, "onCameraOn");
414 }
415 addEvent(PhoneEvent.ON_CAMERA_ON);
416 }
417
418 public void onLeftAffordanceOn() {
419 if (DEBUG) {
420 Log.d(TAG, "onLeftAffordanceOn");
421 }
422 addEvent(PhoneEvent.ON_LEFT_AFFORDANCE_ON);
423 }
424
425 public void onAffordanceSwipingStarted(boolean rightCorner) {
426 if (DEBUG) {
427 Log.d(TAG, "onAffordanceSwipingStarted");
428 }
429 mCornerSwiping = true;
430 if (rightCorner) {
431 addEvent(PhoneEvent.ON_RIGHT_AFFORDANCE_SWIPING_STARTED);
432 } else {
433 addEvent(PhoneEvent.ON_LEFT_AFFORDANCE_SWIPING_STARTED);
434 }
435 }
436
437 public void onAffordanceSwipingAborted() {
438 if (mCornerSwiping) {
439 if (DEBUG) {
440 Log.d(TAG, "onAffordanceSwipingAborted");
441 }
442 mCornerSwiping = false;
443 addEvent(PhoneEvent.ON_AFFORDANCE_SWIPING_ABORTED);
444 }
445 }
446
447 public void onUnlockHintStarted() {
448 if (DEBUG) {
449 Log.d(TAG, "onUnlockHintStarted");
450 }
451 addEvent(PhoneEvent.ON_UNLOCK_HINT_STARTED);
452 }
453
454 public void onCameraHintStarted() {
455 if (DEBUG) {
456 Log.d(TAG, "onCameraHintStarted");
457 }
458 addEvent(PhoneEvent.ON_CAMERA_HINT_STARTED);
459 }
460
461 public void onLeftAffordanceHintStarted() {
462 if (DEBUG) {
463 Log.d(TAG, "onLeftAffordanceHintStarted");
464 }
465 addEvent(PhoneEvent.ON_LEFT_AFFORDANCE_HINT_STARTED);
466 }
467
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700468 public void onTouchEvent(MotionEvent event, int width, int height) {
469 if (mCurrentSession != null) {
Blazej Magnowski72323322015-07-24 11:49:40 -0700470 if (DEBUG) {
471 Log.v(TAG, "onTouchEvent(ev.action="
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700472 + MotionEvent.actionToString(event.getAction()) + ")");
Blazej Magnowski72323322015-07-24 11:49:40 -0700473 }
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700474 mCurrentSession.addMotionEvent(event);
Blazej Magnowski72323322015-07-24 11:49:40 -0700475 mCurrentSession.setTouchArea(width, height);
Blazej Magnowski72323322015-07-24 11:49:40 -0700476 }
477 }
478
479 private void addEvent(int eventType) {
Adrian Roos7bb38a92016-07-21 11:44:01 -0700480 if (isEnabled() && mCurrentSession != null) {
Blazej Magnowski72323322015-07-24 11:49:40 -0700481 mCurrentSession.addPhoneEvent(eventType, System.nanoTime());
482 }
483 }
Adrian Roos7bb38a92016-07-21 11:44:01 -0700484
485 public boolean isReportingEnabled() {
486 return mAllowReportRejectedTouch;
487 }
Selim Cinek1ed50042018-01-18 17:12:32 -0800488
489 public void onFalsingSessionStarted() {
490 sessionEntrypoint();
491 }
Blazej Magnowski72323322015-07-24 11:49:40 -0700492}