Lock for the whole app, every chat or chosen chats, opened by the phone's own fingerprint, face or lock-screen code. A journal of entries (time, result, wrong fingers, chats opened) with optional front-camera pictures kept in a private folder, and a clear-all button. Also: the mascot and the promo scripts for this release. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
86 lines
3.7 KiB
Java
86 lines
3.7 KiB
Java
package lihont.two.privacy;
|
||
|
||
import android.app.Activity;
|
||
import android.app.KeyguardManager;
|
||
import android.content.Context;
|
||
import android.hardware.biometrics.BiometricManager;
|
||
import android.hardware.biometrics.BiometricPrompt;
|
||
import android.os.Build;
|
||
import android.os.CancellationSignal;
|
||
import android.util.Log;
|
||
|
||
/**
|
||
* The phone's own unlock: the system window that takes a fingerprint (or face) and falls back to the PIN, pattern or password of the
|
||
* lock screen. The mod never sees or keeps a finger or a code; it only hears "yes" or "no" from Android. Needs Android 10 or newer
|
||
* and a screen lock set in the phone's settings.
|
||
*/
|
||
public final class Authenticator {
|
||
private static final String TAG = "LIHONT";
|
||
|
||
public interface Result {
|
||
void ok();
|
||
|
||
void failed(String why);
|
||
|
||
/** A finger or face that did not match: the window stays and the person may try again. */
|
||
default void miss() {}
|
||
}
|
||
|
||
private Authenticator() {}
|
||
|
||
/** True when this phone can do the check: Android 10+ and a screen lock (PIN, pattern, password or a finger) is set. */
|
||
public static boolean available(Context context) {
|
||
if (Build.VERSION.SDK_INT < 29) return false;
|
||
try {
|
||
KeyguardManager keyguard = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
|
||
return keyguard != null && keyguard.isDeviceSecure();
|
||
} catch (RuntimeException unknown) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/** Why the lock cannot be used here, in words for the person; null when it can. */
|
||
public static String unavailableReason(Context context) {
|
||
if (Build.VERSION.SDK_INT < 29) return "Блокировка работает на Android 10 и новее";
|
||
if (!available(context)) return "Сначала задайте блокировку экрана (отпечаток, PIN или пароль) в настройках телефона";
|
||
return null;
|
||
}
|
||
|
||
/** Shows the system window; one of the callbacks runs on the main thread. A wrong finger is not an answer: the window stays. */
|
||
public static CancellationSignal ask(Activity activity, String title, String subtitle, final Result result) {
|
||
try {
|
||
BiometricPrompt.Builder builder = new BiometricPrompt.Builder(activity).setTitle(title);
|
||
if (subtitle != null) builder.setSubtitle(subtitle);
|
||
if (Build.VERSION.SDK_INT >= 30) {
|
||
builder.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL);
|
||
} else {
|
||
builder.setDeviceCredentialAllowed(true);
|
||
}
|
||
BiometricPrompt prompt = builder.build();
|
||
CancellationSignal signal = new CancellationSignal();
|
||
prompt.authenticate(signal, activity.getMainExecutor(), new BiometricPrompt.AuthenticationCallback() {
|
||
@Override public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult answer) {
|
||
result.ok();
|
||
}
|
||
|
||
@Override public void onAuthenticationFailed() {
|
||
result.miss();
|
||
}
|
||
|
||
@Override public void onAuthenticationError(int code, CharSequence message) {
|
||
result.failed(message == null ? "" : message.toString());
|
||
}
|
||
});
|
||
return signal;
|
||
} catch (Throwable error) {
|
||
try {
|
||
Log.w(TAG, "lock: the phone's unlock window did not open: " + error);
|
||
} catch (Throwable noLogger) {
|
||
// diagnostic only
|
||
}
|
||
result.failed("Не удалось открыть проверку телефона");
|
||
return null;
|
||
}
|
||
}
|
||
}
|