Skip to content

Commit

Permalink
Add Java-based sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusM committed Jun 10, 2017
1 parent c3374e5 commit f87e3d3
Show file tree
Hide file tree
Showing 20 changed files with 474 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions javasample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
31 changes: 31 additions & 0 deletions javasample/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.application'


android {
compileSdkVersion parent.ext.compileSdkVersion
buildToolsVersion parent.ext.buildToolsVersion


defaultConfig {
applicationId "com.jesusm.kfingerprintmanager.javasample"
minSdkVersion parent.ext.minSdkVersion
targetSdkVersion parent.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':kfingerprintmanager')

compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
25 changes: 25 additions & 0 deletions javasample/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/jesus/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
20 changes: 20 additions & 0 deletions javasample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jesusm.fingerprintmanager.javasample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.jesusm.fingerprintmanager.javasample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package com.jesusm.fingerprintmanager.javasample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jesusm.kfingerprintmanager.KFingerprintManager;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MainActivity extends AppCompatActivity {

private static final String KEY = "KEY";
private int dialogTheme;
private TextView messageText;
private EditText encryptionMessageEditText;
private Button encryptButton;
private Button decryptButton;
private String messageToDecrypt;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

selectView(findViewById(R.id.buttonDialogThemeLight));
messageText = (TextView) findViewById(R.id.message);
encryptionMessageEditText = (EditText) findViewById(R.id.editText);
encryptButton = (Button) findViewById(R.id.buttonEncrypt);
decryptButton = (Button) findViewById(R.id.buttonDecrypt);

initClickListeners();
}

private void initClickListeners() {
findViewById(R.id.buttonDialogThemeLight).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectView(v);

deselectView(findViewById(R.id.buttonDialogThemeDark));

dialogTheme = R.style.DialogThemeLight;
}
});

findViewById(R.id.buttonDialogThemeDark).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

selectView(v);

deselectView(findViewById(R.id.buttonDialogThemeLight));

dialogTheme = R.style.DialogThemeDark;
}
});

findViewById(R.id.buttonAuthenticate).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createFingerprintManagerInstance().authenticate(new KFingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationSuccess() {
messageText.setText("Successfully authenticated");
}

@Override
public void onSuccessWithManualPassword(@NotNull String password) {
messageText.setText("Manual password: " + password);
}

@Override
public void onFingerprintNotRecognized() {
messageText.setText("Fingerprint not recognized");
}

@Override
public void onAuthenticationFailedWithHelp(@Nullable String help) {
messageText.setText(help);
}

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

@Override
public void onCancelled() {
messageText.setText("Operation cancelled by user");
}
}, getSupportFragmentManager());
}
});

encryptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
messageToDecrypt = encryptionMessageEditText.getText().toString();
createFingerprintManagerInstance().encrypt(messageToDecrypt, new KFingerprintManager.EncryptionCallback() {
@Override
public void onEncryptionSuccess(@NotNull String messageEncrypted) {
String message = getString(R.string.encrypt_message_success, messageEncrypted);
messageText.setText(message);
encryptionMessageEditText.setText(messageEncrypted);
encryptButton.setVisibility(View.GONE);
decryptButton.setVisibility(View.VISIBLE);
}

@Override
public void onEncryptionFailed() {
messageText.setText("Encryption failed");
}

@Override
public void onFingerprintNotRecognized() {
messageText.setText("Fingerprint not recognized");
}

@Override
public void onAuthenticationFailedWithHelp(@Nullable String help) {
messageText.setText(help);
}

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

@Override
public void onCancelled() {
messageText.setText("Operation cancelled by user");
}
}, getSupportFragmentManager());
}
});

decryptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
messageToDecrypt = encryptionMessageEditText.getText().toString();
createFingerprintManagerInstance().decrypt(messageToDecrypt, new KFingerprintManager.DecryptionCallback() {
@Override
public void onDecryptionSuccess(@NotNull String messageDecrypted) {
String message = getString(R.string.decrypt_message_success, messageDecrypted);
messageText.setText(message);
encryptionMessageEditText.setText("");
decryptButton.setVisibility(View.GONE);
encryptButton.setVisibility(View.VISIBLE);
}

@Override
public void onDecryptionFailed() {
messageText.setText("Encryption failed");
}

@Override
public void onFingerprintNotRecognized() {
messageText.setText("Fingerprint not recognized");
}

@Override
public void onAuthenticationFailedWithHelp(@Nullable String help) {
messageText.setText(help);
}

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

@Override
public void onCancelled() {
messageText.setText("Operation cancelled by user");
}
}, getSupportFragmentManager());
}
});
}

private KFingerprintManager createFingerprintManagerInstance() {
KFingerprintManager fingerprintManager = new KFingerprintManager(this, KEY);
fingerprintManager.setAuthenticationDialogStyle(dialogTheme);
return fingerprintManager;
}

private void selectView(View view) {
view.setSelected(true);
view.setElevation(32f);
}

private void deselectView(View view) {
view.setSelected(true);
view.setElevation(0f);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_shortAnimTime"
android:exitFadeDuration="@android:integer/config_shortAnimTime">

<item android:drawable="@color/dialog_dark_theme_background_selected" android:state_selected="true"/>
<item android:drawable="@color/dialog_dark_theme_background"/>

</selector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_shortAnimTime"
android:exitFadeDuration="@android:integer/config_shortAnimTime">

<item android:drawable="@color/dialog_light_theme_background_selected" android:state_selected="true"/>
<item android:drawable="@color/dialog_light_theme_background"/>

</selector>
Loading

0 comments on commit f87e3d3

Please sign in to comment.