1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.keyguard.analytics;
import com.google.protobuf.nano.CodedOutputByteBufferNano;
import com.google.protobuf.nano.MessageNano;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.util.Log;
import android.view.MotionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* Tracks sessions, touch and sensor events in Keyguard.
*
* A session starts when the user is presented with the Keyguard and ends when the Keyguard is no
* longer visible to the user.
*/
public class KeyguardAnalytics implements SensorEventListener {
private static final boolean DEBUG = false;
private static final String TAG = "KeyguardAnalytics";
private static final long TIMEOUT_MILLIS = 11000; // 11 seconds.
private static final int[] SENSORS = new int[] {
Sensor.TYPE_ACCELEROMETER,
Sensor.TYPE_GYROSCOPE,
Sensor.TYPE_PROXIMITY,
Sensor.TYPE_LIGHT,
Sensor.TYPE_ROTATION_VECTOR,
};
private Session mCurrentSession = null;
// Err on the side of caution, so logging is not started after a crash even tough the screen
// is off.
private boolean mScreenOn = false;
private boolean mHidden = false;
private final SensorManager mSensorManager;
private final SessionTypeAdapter mSessionTypeAdapter;
private final File mAnalyticsFile;
public KeyguardAnalytics(Context context, SessionTypeAdapter sessionTypeAdapter,
File analyticsFile) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSessionTypeAdapter = sessionTypeAdapter;
mAnalyticsFile = analyticsFile;
}
public Callback getCallback() {
return mCallback;
}
public interface Callback {
public void onShow();
public void onHide();
public void onScreenOn();
public void onScreenOff();
public boolean onTouchEvent(MotionEvent ev, int width, int height);
public void onSetOccluded(boolean hidden);
}
public interface SessionTypeAdapter {
public int getSessionType();
}
private void sessionEntrypoint() {
if (mCurrentSession == null && mScreenOn && !mHidden) {
onSessionStart();
}
}
private void sessionExitpoint(int result) {
if (mCurrentSession != null) {
onSessionEnd(result);
}
}
private void onSessionStart() {
int type = mSessionTypeAdapter.getSessionType();
mCurrentSession = new Session(System.currentTimeMillis(), System.nanoTime(), type);
if (type == Session.TYPE_KEYGUARD_SECURE) {
mCurrentSession.setRedactTouchEvents();
}
for (int sensorType : SENSORS) {
Sensor s = mSensorManager.getDefaultSensor(sensorType);
if (s != null) {
mSensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
}
}
if (DEBUG) {
Log.d(TAG, "onSessionStart()");
}
}
private void onSessionEnd(int result) {
if (DEBUG) {
Log.d(TAG, String.format("onSessionEnd(success=%d)", result));
}
mSensorManager.unregisterListener(this);
Session session = mCurrentSession;
mCurrentSession = null;
session.end(System.currentTimeMillis(), result);
queueSession(session);
}
private void queueSession(final Session currentSession) {
if (DEBUG) {
Log.i(TAG, "Saving session.");
}
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
byte[] b = writeDelimitedProto(currentSession.toProto());
OutputStream os = new FileOutputStream(mAnalyticsFile, true /* append */);
if (DEBUG) {
Log.d(TAG, String.format("Serialized size: %d kB.", b.length / 1024));
}
try {
os.write(b);
os.flush();
} finally {
try {
os.close();
} catch (IOException e) {
Log.e(TAG, "Exception while closing file", e);
}
}
} catch (IOException e) {
Log.e(TAG, "Exception while writing file", e);
}
return null;
}
private byte[] writeDelimitedProto(MessageNano proto)
throws IOException {
byte[] result = new byte[CodedOutputByteBufferNano.computeMessageSizeNoTag(proto)];
CodedOutputByteBufferNano ob = CodedOutputByteBufferNano.newInstance(result);
ob.writeMessageNoTag(proto);
ob.checkNoSpaceLeft();
return result;
}
}.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
@Override
public synchronized void onSensorChanged(SensorEvent event) {
if (false) {
Log.v(TAG, String.format(
"onSensorChanged(name=%s, values[0]=%f)",
event.sensor.getName(), event.values[0]));
}
if (mCurrentSession != null) {
mCurrentSession.addSensorEvent(event, System.nanoTime());
enforceTimeout();
}
}
private void enforceTimeout() {
if (System.currentTimeMillis() - mCurrentSession.getStartTimestampMillis()
> TIMEOUT_MILLIS) {
onSessionEnd(Session.RESULT_UNKNOWN);
if (DEBUG) {
Log.i(TAG, "Analytics timed out.");
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private final Callback mCallback = new Callback() {
@Override
public void onShow() {
if (DEBUG) {
Log.d(TAG, "onShow()");
}
synchronized (KeyguardAnalytics.this) {
sessionEntrypoint();
}
}
@Override
public void onHide() {
if (DEBUG) {
Log.d(TAG, "onHide()");
}
synchronized (KeyguardAnalytics.this) {
sessionExitpoint(Session.RESULT_SUCCESS);
}
}
@Override
public void onScreenOn() {
if (DEBUG) {
Log.d(TAG, "onScreenOn()");
}
synchronized (KeyguardAnalytics.this) {
mScreenOn = true;
sessionEntrypoint();
}
}
@Override
public void onScreenOff() {
if (DEBUG) {
Log.d(TAG, "onScreenOff()");
}
synchronized (KeyguardAnalytics.this) {
mScreenOn = false;
sessionExitpoint(Session.RESULT_FAILURE);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev, int width, int height) {
if (DEBUG) {
Log.v(TAG, "onTouchEvent(ev.action="
+ MotionEvent.actionToString(ev.getAction()) + ")");
}
synchronized (KeyguardAnalytics.this) {
if (mCurrentSession != null) {
mCurrentSession.addMotionEvent(ev);
mCurrentSession.setTouchArea(width, height);
enforceTimeout();
}
}
return true;
}
@Override
public void onSetOccluded(boolean hidden) {
synchronized (KeyguardAnalytics.this) {
if (hidden != mHidden) {
if (DEBUG) {
Log.d(TAG, "onSetOccluded(" + hidden + ")");
}
mHidden = hidden;
if (hidden) {
// Could have gone to camera on purpose / by falsing or an app could have
// launched on top of the lockscreen.
sessionExitpoint(Session.RESULT_UNKNOWN);
} else {
sessionEntrypoint();
}
}
}
}
};
}
|