2018-03-04 19:35:37 +00:00
|
|
|
/*
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-31 21:18:14 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-31 21:18:14 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2018-03-04 19:35:37 +00:00
|
|
|
import android.animation.Animator;
|
|
|
|
import android.app.KeyguardManager;
|
2020-12-14 07:16:16 +00:00
|
|
|
import android.content.ComponentName;
|
2018-03-04 19:35:37 +00:00
|
|
|
import android.content.Context;
|
2014-02-26 03:53:12 +00:00
|
|
|
import android.content.Intent;
|
2020-12-14 07:16:16 +00:00
|
|
|
import android.content.ServiceConnection;
|
2018-03-04 19:35:37 +00:00
|
|
|
import android.graphics.PorterDuff;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.os.Bundle;
|
2020-12-14 07:16:16 +00:00
|
|
|
import android.os.IBinder;
|
2014-05-14 03:41:16 +00:00
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.style.RelativeSizeSpan;
|
|
|
|
import android.text.style.TypefaceSpan;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.view.View;
|
2018-03-04 19:35:37 +00:00
|
|
|
import android.view.animation.Animation;
|
|
|
|
import android.view.animation.BounceInterpolator;
|
|
|
|
import android.view.animation.TranslateAnimation;
|
2020-01-09 00:35:43 +00:00
|
|
|
import android.widget.Button;
|
2018-03-04 19:35:37 +00:00
|
|
|
import android.widget.ImageView;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2020-09-08 02:04:51 +00:00
|
|
|
import androidx.core.hardware.fingerprint.FingerprintManagerCompat;
|
|
|
|
import androidx.core.os.CancellationSignal;
|
|
|
|
|
2021-07-09 05:45:15 +00:00
|
|
|
import org.thoughtcrime.securesms.util.AnimationCompleteListener;
|
2015-07-23 15:16:45 +00:00
|
|
|
import org.thoughtcrime.securesms.components.AnimatingToggle;
|
2021-05-17 23:12:33 +00:00
|
|
|
import org.session.libsignal.utilities.Log;
|
2020-12-14 07:16:16 +00:00
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
2021-01-15 05:51:53 +00:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences;
|
2012-07-31 21:18:14 +00:00
|
|
|
|
2019-07-24 02:30:23 +00:00
|
|
|
import network.loki.messenger.R;
|
|
|
|
|
2020-12-15 06:16:05 +00:00
|
|
|
//TODO Rename to ScreenLockActivity and refactor to Kotlin.
|
2020-12-14 07:16:16 +00:00
|
|
|
public class PassphrasePromptActivity extends BaseActionBarActivity {
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2018-03-04 19:35:37 +00:00
|
|
|
private static final String TAG = PassphrasePromptActivity.class.getSimpleName();
|
|
|
|
|
2019-08-30 00:24:39 +00:00
|
|
|
private ImageView fingerprintPrompt;
|
2020-01-09 00:35:43 +00:00
|
|
|
private Button lockScreenButton;
|
2018-03-04 19:35:37 +00:00
|
|
|
|
2015-07-23 15:16:45 +00:00
|
|
|
private AnimatingToggle visibilityToggle;
|
2012-07-31 21:18:14 +00:00
|
|
|
|
2018-03-04 19:35:37 +00:00
|
|
|
private FingerprintManagerCompat fingerprintManager;
|
|
|
|
private CancellationSignal fingerprintCancellationSignal;
|
|
|
|
private FingerprintListener fingerprintListener;
|
|
|
|
|
2018-03-09 01:41:27 +00:00
|
|
|
private boolean authenticated;
|
2018-03-09 18:25:29 +00:00
|
|
|
private boolean failure;
|
2018-03-09 01:41:27 +00:00
|
|
|
|
2020-12-14 07:16:16 +00:00
|
|
|
private KeyCachingService keyCachingService;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onCreate()");
|
2011-12-20 18:20:44 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.prompt_passphrase_activity);
|
|
|
|
initializeResources();
|
2020-12-14 07:16:16 +00:00
|
|
|
|
|
|
|
// Start and bind to the KeyCachingService instance.
|
|
|
|
Intent bindIntent = new Intent(this, KeyCachingService.class);
|
|
|
|
startService(bindIntent);
|
|
|
|
bindService(bindIntent, new ServiceConnection() {
|
|
|
|
@Override
|
|
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
|
keyCachingService = ((KeyCachingService.KeySetBinder)service).getService();
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onServiceDisconnected(ComponentName name) {
|
2021-04-28 05:59:30 +00:00
|
|
|
keyCachingService.setMasterSecret(new Object());
|
2020-12-14 07:16:16 +00:00
|
|
|
keyCachingService = null;
|
|
|
|
}
|
|
|
|
}, Context.BIND_AUTO_CREATE);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-31 21:18:14 +00:00
|
|
|
|
2014-12-11 23:04:32 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2018-03-04 19:35:37 +00:00
|
|
|
|
2018-03-09 18:25:29 +00:00
|
|
|
setLockTypeVisibility();
|
|
|
|
|
|
|
|
if (TextSecurePreferences.isScreenLockEnabled(this) && !authenticated && !failure) {
|
2018-03-04 19:35:37 +00:00
|
|
|
resumeScreenLock();
|
|
|
|
}
|
2018-03-09 18:25:29 +00:00
|
|
|
|
|
|
|
failure = false;
|
2018-03-04 19:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
|
|
|
if (TextSecurePreferences.isScreenLockEnabled(this)) {
|
|
|
|
pauseScreenLock();
|
|
|
|
}
|
2014-12-11 23:04:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
|
|
|
}
|
|
|
|
|
2020-12-07 06:40:47 +00:00
|
|
|
@Override
|
2018-03-04 19:35:37 +00:00
|
|
|
public void onActivityResult(int requestCode, int resultcode, Intent data) {
|
2020-12-07 06:40:47 +00:00
|
|
|
super.onActivityResult(requestCode, resultcode, data);
|
2018-03-04 19:35:37 +00:00
|
|
|
if (requestCode != 1) return;
|
|
|
|
|
|
|
|
if (resultcode == RESULT_OK) {
|
|
|
|
handleAuthenticated();
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Authentication failed");
|
2018-03-09 18:25:29 +00:00
|
|
|
failure = true;
|
2018-03-04 19:35:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleAuthenticated() {
|
2020-12-14 07:16:16 +00:00
|
|
|
authenticated = true;
|
|
|
|
//TODO Replace with a proper call.
|
2021-04-28 05:59:30 +00:00
|
|
|
if (keyCachingService != null) {
|
|
|
|
keyCachingService.setMasterSecret(new Object());
|
|
|
|
}
|
2020-12-14 07:16:16 +00:00
|
|
|
|
|
|
|
// Finish and proceed with the next intent.
|
|
|
|
Intent nextIntent = getIntent().getParcelableExtra("next_intent");
|
|
|
|
if (nextIntent != null) {
|
|
|
|
startActivity(nextIntent);
|
|
|
|
// try {
|
|
|
|
// startActivity(nextIntent);
|
|
|
|
// } catch (java.lang.SecurityException e) {
|
|
|
|
// Log.w(TAG, "Access permission not passed from PassphraseActivity, retry sharing.");
|
|
|
|
// }
|
2018-03-04 19:35:37 +00:00
|
|
|
}
|
2020-12-14 07:16:16 +00:00
|
|
|
finish();
|
2015-07-23 15:16:45 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void initializeResources() {
|
2018-03-04 19:35:37 +00:00
|
|
|
visibilityToggle = findViewById(R.id.button_toggle);
|
|
|
|
fingerprintPrompt = findViewById(R.id.fingerprint_auth_container);
|
2018-03-09 18:25:29 +00:00
|
|
|
lockScreenButton = findViewById(R.id.lock_screen_auth_container);
|
2018-03-04 19:35:37 +00:00
|
|
|
fingerprintManager = FingerprintManagerCompat.from(this);
|
|
|
|
fingerprintCancellationSignal = new CancellationSignal();
|
|
|
|
fingerprintListener = new FingerprintListener();
|
|
|
|
|
2015-01-21 07:26:58 +00:00
|
|
|
SpannableString hint = new SpannableString(" " + getString(R.string.PassphrasePromptActivity_enter_passphrase));
|
|
|
|
hint.setSpan(new RelativeSizeSpan(0.9f), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
|
2014-05-14 03:41:16 +00:00
|
|
|
hint.setSpan(new TypefaceSpan("sans-serif"), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
|
2015-01-21 07:26:58 +00:00
|
|
|
|
2018-03-04 19:35:37 +00:00
|
|
|
fingerprintPrompt.setImageResource(R.drawable.ic_fingerprint_white_48dp);
|
|
|
|
fingerprintPrompt.getBackground().setColorFilter(getResources().getColor(R.color.signal_primary), PorterDuff.Mode.SRC_IN);
|
|
|
|
|
2018-03-09 18:25:29 +00:00
|
|
|
lockScreenButton.setOnClickListener(v -> resumeScreenLock());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setLockTypeVisibility() {
|
2018-03-04 19:35:37 +00:00
|
|
|
if (TextSecurePreferences.isScreenLockEnabled(this)) {
|
2018-03-09 18:25:29 +00:00
|
|
|
if (fingerprintManager.isHardwareDetected() && fingerprintManager.hasEnrolledFingerprints()) {
|
|
|
|
fingerprintPrompt.setVisibility(View.VISIBLE);
|
|
|
|
lockScreenButton.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
fingerprintPrompt.setVisibility(View.GONE);
|
|
|
|
lockScreenButton.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2018-03-04 19:35:37 +00:00
|
|
|
} else {
|
|
|
|
fingerprintPrompt.setVisibility(View.GONE);
|
2018-03-09 18:25:29 +00:00
|
|
|
lockScreenButton.setVisibility(View.GONE);
|
2018-03-04 19:35:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void resumeScreenLock() {
|
|
|
|
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
|
|
|
|
|
|
|
|
assert keyguardManager != null;
|
|
|
|
|
2019-03-20 22:09:27 +00:00
|
|
|
if (!keyguardManager.isKeyguardSecure()) {
|
2018-03-04 19:35:37 +00:00
|
|
|
Log.w(TAG ,"Keyguard not secure...");
|
2021-04-28 05:59:30 +00:00
|
|
|
TextSecurePreferences.setScreenLockEnabled(getApplicationContext(), false);
|
|
|
|
TextSecurePreferences.setScreenLockTimeout(getApplicationContext(), 0);
|
2018-03-04 19:35:37 +00:00
|
|
|
handleAuthenticated();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-20 22:09:27 +00:00
|
|
|
if (fingerprintManager.isHardwareDetected() && fingerprintManager.hasEnrolledFingerprints()) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Listening for fingerprints...");
|
2018-03-04 19:35:37 +00:00
|
|
|
fingerprintCancellationSignal = new CancellationSignal();
|
|
|
|
fingerprintManager.authenticate(null, 0, fingerprintCancellationSignal, fingerprintListener, null);
|
2020-12-14 07:16:16 +00:00
|
|
|
} else {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "firing intent...");
|
2020-01-09 00:35:43 +00:00
|
|
|
Intent intent = keyguardManager.createConfirmDeviceCredentialIntent("Unlock Session", "");
|
2018-03-04 19:35:37 +00:00
|
|
|
startActivityForResult(intent, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void pauseScreenLock() {
|
2019-03-20 22:09:27 +00:00
|
|
|
if (fingerprintCancellationSignal != null) {
|
2018-03-04 19:35:37 +00:00
|
|
|
fingerprintCancellationSignal.cancel();
|
|
|
|
}
|
2014-05-30 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:35:37 +00:00
|
|
|
private class FingerprintListener extends FingerprintManagerCompat.AuthenticationCallback {
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationError(int errMsgId, CharSequence errString) {
|
|
|
|
Log.w(TAG, "Authentication error: " + errMsgId + " " + errString);
|
2018-08-14 20:04:33 +00:00
|
|
|
onAuthenticationFailed();
|
2018-03-04 19:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onAuthenticationSucceeded");
|
2018-03-04 19:35:37 +00:00
|
|
|
fingerprintPrompt.setImageResource(R.drawable.ic_check_white_48dp);
|
2018-08-14 20:04:33 +00:00
|
|
|
fingerprintPrompt.getBackground().setColorFilter(getResources().getColor(R.color.green_500), PorterDuff.Mode.SRC_IN);
|
2018-03-04 19:35:37 +00:00
|
|
|
fingerprintPrompt.animate().setInterpolator(new BounceInterpolator()).scaleX(1.1f).scaleY(1.1f).setDuration(500).setListener(new AnimationCompleteListener() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
handleAuthenticated();
|
2018-08-14 20:04:33 +00:00
|
|
|
|
|
|
|
fingerprintPrompt.setImageResource(R.drawable.ic_fingerprint_white_48dp);
|
|
|
|
fingerprintPrompt.getBackground().setColorFilter(getResources().getColor(R.color.signal_primary), PorterDuff.Mode.SRC_IN);
|
2018-03-04 19:35:37 +00:00
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationFailed() {
|
|
|
|
Log.w(TAG, "onAuthenticatoinFailed()");
|
|
|
|
|
2018-08-14 20:04:33 +00:00
|
|
|
fingerprintPrompt.setImageResource(R.drawable.ic_close_white_48dp);
|
|
|
|
fingerprintPrompt.getBackground().setColorFilter(getResources().getColor(R.color.red_500), PorterDuff.Mode.SRC_IN);
|
2018-03-04 19:35:37 +00:00
|
|
|
|
2018-08-14 20:04:33 +00:00
|
|
|
TranslateAnimation shake = new TranslateAnimation(0, 30, 0, 0);
|
2018-03-04 19:35:37 +00:00
|
|
|
shake.setDuration(50);
|
2018-08-14 20:04:33 +00:00
|
|
|
shake.setRepeatCount(7);
|
2018-03-04 19:35:37 +00:00
|
|
|
shake.setAnimationListener(new Animation.AnimationListener() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationStart(Animation animation) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animation animation) {
|
|
|
|
fingerprintPrompt.setImageResource(R.drawable.ic_fingerprint_white_48dp);
|
|
|
|
fingerprintPrompt.getBackground().setColorFilter(getResources().getColor(R.color.signal_primary), PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationRepeat(Animation animation) {}
|
|
|
|
});
|
|
|
|
|
|
|
|
fingerprintPrompt.startAnimation(shake);
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 07:16:16 +00:00
|
|
|
}
|