Skip to content

Commit

Permalink
Wake screen up on auth request
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandiny committed Jul 18, 2018
1 parent 37b9fdf commit 789c642
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.randiny_games.fingerprintauthenticator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -20,6 +28,8 @@ public class AuthActivity extends AppCompatActivity implements View.OnClickListe
private KFingerprintManager fm;
private TextView status;

private Boolean registered;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -30,22 +40,42 @@ protected void onCreate(Bundle savedInstanceState) {
status = (TextView) findViewById(R.id.authDesc);

authButton.setOnClickListener(this);
authButton.setEnabled(false);

fm = new KFingerprintManager(this,"fingerprintPam");

attemptDecrypt();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();

}
registered = false;

@Override
protected void onStart() {
super.onStart();
if(isScreenOn){
attemptDecrypt();
}else {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(screenCheck, filter);
registered = true;
}

System.out.println("masuk");

}

private final BroadcastReceiver screenCheck = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
attemptDecrypt();

unregisterReceiver(screenCheck);
registered = false;
}
};

private void attemptDecrypt(){

String encData = SecurePreferences.getStringValue("secret","");
Expand All @@ -60,23 +90,25 @@ private void attemptDecrypt(){
@Override
public void onDecryptionSuccess(@NotNull String messageDecrypted) {
SecurePreferences.setValue("decryptedKey",messageDecrypted);
System.out.println(messageDecrypted);
returnToServer();
}

@Override
public void onDecryptionFailed() {
status.setText("Encryption failed");
status.setText("Decryption failed");
failAuth();
}

@Override
public void onFingerprintNotRecognized() {
status.setText("Fingerprint not recognized");
authButton.setEnabled(true);
}

@Override
public void onAuthenticationFailedWithHelp(@Nullable String help) {
status.setText(help);
authButton.setEnabled(true);
}

@Override
Expand All @@ -88,6 +120,7 @@ public void onFingerprintNotAvailable() {
@Override
public void onCancelled() {
status.setText("Cancelled");
authButton.setEnabled(true);
}
}, getSupportFragmentManager());
}
Expand All @@ -111,4 +144,14 @@ private void failAuth(){
Toast.makeText(this,"Fail to get encrypted data, have you run setup?",Toast.LENGTH_LONG);
returnToServer();
}

@Override
protected void onDestroy() {
super.onDestroy();
if(registered){
unregisterReceiver(screenCheck);
registered = false;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ public server() {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Integer port = intent.getIntExtra("port",1234);
Integer port;

if (intent.hasExtra("port")){
port = intent.getIntExtra("port",1234);
}else{
port = 1234;
}


try {
httpServer = new MyHTTPD(this, port);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.randiny_games.fingerprintauthenticator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jesusm.kfingerprintmanager.KFingerprintManager;

Expand All @@ -20,6 +27,8 @@ public class setupActivity extends AppCompatActivity implements View.OnClickList
private String secret;
private Button retryBtn;

private Boolean registered;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -29,18 +38,41 @@ protected void onCreate(Bundle savedInstanceState) {
retryBtn = (Button) findViewById(R.id.setupRetryBtn);

retryBtn.setOnClickListener(this);
retryBtn.setEnabled(false);

fm = new KFingerprintManager(this,"fingerprintPam");

}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

@Override
protected void onStart() {
super.onStart();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();

registered = false;

if(isScreenOn){
attemptEncrypt();
}else {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(screenCheck, filter);
registered = true;
}

attemptEncrypt();
}

private final BroadcastReceiver screenCheck = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
attemptEncrypt();

unregisterReceiver(screenCheck);
registered = false;
}
};

private void attemptEncrypt(){

secret = SecurePreferences.getStringValue("decryptedKey","");
Expand All @@ -57,38 +89,36 @@ public void onEncryptionSuccess(@NotNull String messageEncrypted) {
@Override
public void onEncryptionFailed() {
status.setText("Encryption failed");
failFatal();
}

@Override
public void onFingerprintNotRecognized() {
status.setText("Fingerprint not recognized");
retryBtn.setEnabled(true);
}

@Override
public void onAuthenticationFailedWithHelp(@Nullable String help) {
status.setText(help);
retryBtn.setEnabled(true);
}

@Override
public void onFingerprintNotAvailable() {
status.setText("Fingerprint not available");
failFatal();
}

@Override
public void onCancelled() {
status.setText("Operation cancelled by user");
retryBtn.setEnabled(true);
}
}, getSupportFragmentManager());

}

@Override
protected void onPause() {
super.onPause();

returnToServer();
}

private void returnToServer(){

synchronized (MyHTTPD.syncToken) {
Expand All @@ -103,4 +133,20 @@ public void onClick(View view) {
attemptEncrypt();
}
}

private void failFatal(){
Toast.makeText(this,"Fatal error",Toast.LENGTH_LONG);
returnToServer();
}

@Override
protected void onDestroy() {
super.onDestroy();

if(registered){
unregisterReceiver(screenCheck);
}

returnToServer();
}
}

0 comments on commit 789c642

Please sign in to comment.