-
Notifications
You must be signed in to change notification settings - Fork 121
/
FODCircleView.java
492 lines (399 loc) · 15 KB
/
FODCircleView.java
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/**
* Copyright (C) 2019 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.systemui.biometrics;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.biometrics.BiometricSourceType;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.os.RemoteException;
import android.provider.Settings;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.KeyguardUpdateMonitorCallback;
import com.android.systemui.R;
import com.android.systemui.Dependency;
import com.android.systemui.statusbar.policy.ConfigurationController;
import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
import vendor.lineage.biometrics.fingerprint.inscreen.V1_0.IFingerprintInscreen;
import vendor.lineage.biometrics.fingerprint.inscreen.V1_0.IFingerprintInscreenCallback;
import java.util.NoSuchElementException;
import java.util.Timer;
import java.util.TimerTask;
public class FODCircleView extends ImageView implements ConfigurationListener {
private final int mPositionX;
private final int mPositionY;
private final int mSize;
private final int mDreamingMaxOffset;
private final int mNavigationBarSize;
private final boolean mShouldBoostBrightness;
private final Paint mPaintFingerprint = new Paint();
private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
private final WindowManager mWindowManager;
private IFingerprintInscreen mFingerprintInscreenDaemon;
private int mDreamingOffsetY;
private boolean mIsBouncer;
private boolean mIsDreaming;
private boolean mIsKeyguard;
private boolean mIsShowing;
private boolean mIsCircleShowing;
private boolean mIsAuthenticated;
private Handler mHandler;
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
private Timer mBurnInProtectionTimer;
private final boolean mFodPressedImage;
private FODAnimation mFODAnimation;
private IFingerprintInscreenCallback mFingerprintInscreenCallback =
new IFingerprintInscreenCallback.Stub() {
@Override
public void onFingerDown() {
mHandler.post(() -> showCircle());
}
@Override
public void onFingerUp() {
mHandler.post(() -> hideCircle());
}
};
private KeyguardUpdateMonitor mUpdateMonitor;
private KeyguardUpdateMonitorCallback mMonitorCallback = new KeyguardUpdateMonitorCallback() {
@Override
public void onDreamingStateChanged(boolean dreaming) {
mIsDreaming = dreaming;
updateAlpha();
if (dreaming) {
mBurnInProtectionTimer = new Timer();
mBurnInProtectionTimer.schedule(new BurnInProtectionTask(), 0, 60 * 1000);
} else if (mBurnInProtectionTimer != null) {
mBurnInProtectionTimer.cancel();
}
}
@Override
public void onKeyguardVisibilityChanged(boolean showing) {
mIsKeyguard = showing;
updatePosition();
if (mFODAnimation != null) {
mFODAnimation.setAnimationKeyguard(mIsKeyguard);
}
}
@Override
public void onKeyguardBouncerChanged(boolean isBouncer) {
mIsBouncer = isBouncer;
if (isBouncer) {
hide();
} else if (mUpdateMonitor.isFingerprintDetectionRunning()) {
show();
}
}
@Override
public void onBiometricAuthenticated(int userId, BiometricSourceType biometricSourceType) {
super.onBiometricAuthenticated(userId, biometricSourceType);
mIsAuthenticated = true;
}
@Override
public void onScreenTurnedOff() {
hide();
}
@Override
public void onScreenTurnedOn() {
if (mUpdateMonitor.isFingerprintDetectionRunning()) {
show();
}
}
};
private boolean mCutoutMasked;
private int mStatusbarHeight;
public FODCircleView(Context context) {
super(context);
IFingerprintInscreen daemon = getFingerprintInScreenDaemon();
if (daemon == null) {
throw new RuntimeException("Unable to get IFingerprintInscreen");
}
try {
mShouldBoostBrightness = daemon.shouldBoostBrightness();
mPositionX = daemon.getPositionX();
mPositionY = daemon.getPositionY();
mSize = daemon.getSize();
} catch (RemoteException e) {
throw new RuntimeException("Failed to retrieve FOD circle position or size");
}
Resources res = context.getResources();
mPaintFingerprint.setAntiAlias(true);
mPaintFingerprint.setColor(res.getColor(R.color.config_fodColor));
mWindowManager = context.getSystemService(WindowManager.class);
mNavigationBarSize = res.getDimensionPixelSize(R.dimen.navigation_bar_size);
mDreamingMaxOffset = (int) (mSize * 0.1f);
mHandler = new Handler(Looper.getMainLooper());
mParams.height = mSize;
mParams.width = mSize;
mParams.format = PixelFormat.TRANSLUCENT;
mParams.setTitle("Fingerprint on display");
mParams.packageName = "android";
mParams.type = WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_DIM_BEHIND |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mParams.gravity = Gravity.TOP | Gravity.LEFT;
mWindowManager.addView(this, mParams);
updatePosition();
hide();
mUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
mUpdateMonitor.registerCallback(mMonitorCallback);
updateCutoutFlags();
Dependency.get(ConfigurationController.class).addCallback(this);
mPowerManager = context.getSystemService(PowerManager.class);
mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
FODCircleView.class.getSimpleName());
mFodPressedImage = res.getBoolean(R.bool.config_fodPressedImage);
mFODAnimation = new FODAnimation(context, mPositionX, mPositionY);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mIsCircleShowing) {
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2.0f, mPaintFingerprint);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mIsCircleShowing) {
dispatchPress();
} else {
dispatchRelease();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getAxisValue(MotionEvent.AXIS_X);
float y = event.getAxisValue(MotionEvent.AXIS_Y);
boolean newIsInside = (x > 0 && x < mSize) && (y > 0 && y < mSize);
if (event.getAction() == MotionEvent.ACTION_DOWN && newIsInside) {
showCircle();
mHandler.post(() -> mFODAnimation.showFODanimation());
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
hideCircle();
mHandler.post(() -> mFODAnimation.hideFODanimation());
return true;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}
mHandler.post(() -> mFODAnimation.hideFODanimation());
return false;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
updatePosition();
}
public IFingerprintInscreen getFingerprintInScreenDaemon() {
if (mFingerprintInscreenDaemon == null) {
try {
mFingerprintInscreenDaemon = IFingerprintInscreen.getService();
if (mFingerprintInscreenDaemon != null) {
mFingerprintInscreenDaemon.setCallback(mFingerprintInscreenCallback);
mFingerprintInscreenDaemon.asBinder().linkToDeath((cookie) -> {
mFingerprintInscreenDaemon = null;
}, 0);
}
} catch (NoSuchElementException | RemoteException e) {
// do nothing
}
}
return mFingerprintInscreenDaemon;
}
public void dispatchPress() {
IFingerprintInscreen daemon = getFingerprintInScreenDaemon();
try {
daemon.onPress();
} catch (RemoteException e) {
// do nothing
}
}
public void dispatchRelease() {
IFingerprintInscreen daemon = getFingerprintInScreenDaemon();
try {
daemon.onRelease();
} catch (RemoteException e) {
// do nothing
}
}
public void dispatchShow() {
IFingerprintInscreen daemon = getFingerprintInScreenDaemon();
try {
daemon.onShowFODView();
} catch (RemoteException e) {
// do nothing
}
}
public void dispatchHide() {
IFingerprintInscreen daemon = getFingerprintInScreenDaemon();
try {
daemon.onHideFODView();
} catch (RemoteException e) {
// do nothing
}
}
public void showCircle() {
if (mIsAuthenticated) {
return;
}
mIsCircleShowing = true;
setKeepScreenOn(true);
if (mIsDreaming) {
mWakeLock.acquire(500);
}
setDim(true);
updateAlpha();
if (mFodPressedImage) {
setImageResource(R.drawable.fod_icon_pressed);
} else {
setImageDrawable(null);
}
invalidate();
}
public void hideCircle() {
mIsCircleShowing = false;
setImageResource(R.drawable.fod_icon_default);
invalidate();
setDim(false);
updateAlpha();
setKeepScreenOn(false);
}
public void show() {
if (!mUpdateMonitor.isScreenOn()) {
// Keyguard is shown just after screen turning off
return;
}
if (mIsBouncer) {
// Ignore show calls when Keyguard pin screen is being shown
return;
}
mIsShowing = true;
mIsAuthenticated = false;
dispatchShow();
setVisibility(View.VISIBLE);
}
public void hide() {
mIsShowing = false;
setVisibility(View.GONE);
hideCircle();
dispatchHide();
}
private void updateAlpha() {
if (mIsCircleShowing) {
setAlpha(1.0f);
} else {
setAlpha(mIsDreaming ? 0.75f : 1.0f);
}
}
private void updatePosition() {
Display defaultDisplay = mWindowManager.getDefaultDisplay();
Point size = new Point();
defaultDisplay.getRealSize(size);
int rotation = defaultDisplay.getRotation();
int cutoutMaskedExtra = mCutoutMasked ? mStatusbarHeight : 0;
switch (rotation) {
case Surface.ROTATION_0:
mParams.x = mPositionX;
mParams.y = mPositionY - cutoutMaskedExtra;
break;
case Surface.ROTATION_90:
mParams.x = mPositionY;
mParams.y = mPositionX - cutoutMaskedExtra;
break;
case Surface.ROTATION_180:
mParams.x = mPositionX;
mParams.y = size.y - mPositionY - mSize - cutoutMaskedExtra;
break;
case Surface.ROTATION_270:
mParams.x = size.x - mPositionY - mSize - mNavigationBarSize - cutoutMaskedExtra;
mParams.y = mPositionX;
break;
default:
throw new IllegalArgumentException("Unknown rotation: " + rotation);
}
if (mIsKeyguard) {
mParams.x = mPositionX;
mParams.y = mPositionY - cutoutMaskedExtra;
}
if (mIsDreaming) {
mParams.y += mDreamingOffsetY;
mFODAnimation.updateParams(mParams.y);
}
mWindowManager.updateViewLayout(this, mParams);
}
private void setDim(boolean dim) {
if (dim) {
int curBrightness = Settings.System.getInt(getContext().getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 100);
int dimAmount = 0;
IFingerprintInscreen daemon = getFingerprintInScreenDaemon();
try {
dimAmount = daemon.getDimAmount(curBrightness);
} catch (RemoteException e) {
// do nothing
}
if (mShouldBoostBrightness) {
mParams.screenBrightness = 1.0f;
}
mParams.dimAmount = dimAmount / 255.0f;
} else {
mParams.screenBrightness = 0.0f;
mParams.dimAmount = 0.0f;
}
mWindowManager.updateViewLayout(this, mParams);
}
private class BurnInProtectionTask extends TimerTask {
@Override
public void run() {
long now = System.currentTimeMillis() / 1000 / 60;
// Let y to be not synchronized with x, so that we get maximum movement
mDreamingOffsetY = (int) ((now + mDreamingMaxOffset / 3) % (mDreamingMaxOffset * 2));
mDreamingOffsetY -= mDreamingMaxOffset;
mHandler.post(() -> updatePosition());
}
};
@Override
public void onOverlayChanged() {
updateCutoutFlags();
}
private void updateCutoutFlags() {
mStatusbarHeight = getContext().getResources().getDimensionPixelSize(
com.android.internal.R.dimen.status_bar_height_portrait);
boolean cutoutMasked = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_maskMainBuiltInDisplayCutout);
if (mCutoutMasked != cutoutMasked){
mCutoutMasked = cutoutMasked;
updatePosition();
}
}
}