You are on page 1of 7

package com.example.

faceapp;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.concurrent.Executor;

//import android.hardware.biometrics.BiometricPrompt;

public class biomodactivivty extends AppCompatActivity {


Executor executor;
BiometricPrompt biometricPrompt;
BiometricPrompt.PromptInfo promptInfo;
Button biometricLoginButton;

int amtval = 0;
String amt_bal = "";
String amt_add = "";
String userid = "";
String transamt = "";
EditText et1,et2;

int auth_check = 0;

@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biomodactivivty);

Intent uid = getIntent();


userid = uid.getStringExtra("userid");
// transamt = uid.getStringExtra("transamt");
et1 = findViewById(R.id.bioamt);
et2 = findViewById(R.id.bioamt1);
filreaddata();

filreaddata1();

biometricLoginButton = findViewById(R.id.biometric_login);

biometricLoginButton.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int
after) {

@Override
public void onTextChanged(CharSequence s, int start, int before, int
count) {

@Override
public void afterTextChanged(Editable s) {
if (biometricLoginButton.getText().toString().contains("TRANSACTION
SUCCESSUL"))
{
int x =
Integer.parseInt(String.valueOf(et1.getText().toString())) +
Integer.parseInt(String.valueOf(et2.getText().toString()));
Toast.makeText(getApplicationContext(),"Total :"+
x ,Toast.LENGTH_LONG ).show();
writenewbal(String.valueOf(x));
movetobalview();
}
}
});

initbiomteric();

// checkbio();

biometricLoginButton.setOnClickListener(view -> {
biometricPrompt.authenticate(promptInfo);
// checkbio();
});
}

public void checkbio() {

// biometricPrompt.authenticate(promptInfo);
if (auth_check == 1) {
// Toast.makeText(getApplicationContext(), " ALL SUCCECSS !!! ",
Toast.LENGTH_LONG).show();
biometricLoginButton.setText("TRANSACTION SUCCESSUL ! ");
}
}

public void initbiomteric() {


executor = ContextCompat.getMainExecutor(getApplicationContext());

biometricPrompt = new BiometricPrompt(this, executor, new


BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(@NonNull
BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);

Toast.makeText(getApplicationContext(), "Transaction Successfull


1", Toast.LENGTH_LONG).show();
biometricLoginButton.setText("TRANSACTION SUCCESSUL ! ");

auth_check = 1;
checkbio();
// reducebal();
}

@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence
errString) {
super.onAuthenticationError(errorCode, errString);
auth_check = 3;
Toast.makeText(getApplicationContext(), errString,
Toast.LENGTH_LONG).show();

@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
auth_check = 2;
Toast.makeText(getApplicationContext(), "FAILED",
Toast.LENGTH_LONG).show();

}
});
promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Touch id required")
.setDescription("Touch the touch id sensor")
.setNegativeButtonText("Exit")
.setConfirmationRequired(true)
.build();

public void reducebal() {

Toast.makeText(this, " Amount Reducing Function ",


Toast.LENGTH_SHORT).show();
amtval = Integer.parseInt(amt_bal) - Integer.parseInt(transamt);

Toast.makeText(this, " Balance Amount : " + amtval, Toast.LENGTH_LONG);

if (amtval >= 0) {
try {
File file = new File(getFilesDir(), "amtbalance.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);

OutputStreamWriter outputStreamWriter = new


OutputStreamWriter(fileOutputStream);

BufferedWriter bufferedWriter = new


BufferedWriter(outputStreamWriter);
Toast.makeText(this, " Before Reducing : " + amt_bal,
Toast.LENGTH_LONG);

bufferedWriter.write(String.valueOf(amtval));
Toast.makeText(this, " Amount Deducted " + amtval,
Toast.LENGTH_SHORT).show();

bufferedWriter.flush();
bufferedWriter.close();
outputStreamWriter.close();
fileOutputStream.close();
} catch (FileNotFoundException ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG);
} catch (IOException ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG);
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, " Sorry Insufficient Balance " + amtval,
Toast.LENGTH_SHORT).show();
}
movetobalview();

public void filreaddata() {


// Toast.makeText(this, " inside File Read ", Toast.LENGTH_SHORT).show();
try {
File file = new File(getFilesDir(), "amtbalance.txt");

// Toast.makeText(this, " File Exists Reading ! ",


Toast.LENGTH_SHORT).show();

FileInputStream fileInputStream = new FileInputStream(file);


StringBuffer retBuf = new StringBuffer();

if (fileInputStream != null) {

// Toast.makeText(this, " Inside Input Stream ! ",


Toast.LENGTH_SHORT).show();

InputStreamReader inputStreamReader = new


InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new
BufferedReader(inputStreamReader);
String lineData = bufferedReader.readLine();
while (lineData != null) {
retBuf.append(lineData);
lineData = bufferedReader.readLine();
}
String click_val = retBuf.toString();
amt_bal = click_val;

// et1.setText(amt_bal);

// Toast.makeText(getApplicationContext(), " Inside : " + amt_bal,


Toast.LENGTH_LONG).show();

}
// Toast.makeText(getApplicationContext(), " Outside : " + amt_bal,
Toast.LENGTH_LONG).show();

} catch (FileNotFoundException ex) {


Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
}
finally {
// Toast.makeText(getApplicationContext(), " Act Bal : " + amt_bal,
Toast.LENGTH_LONG).show();
et1.setText(String.valueOf(amt_bal));
}
}

public void filreaddata1() {


// Toast.makeText(this, " inside File Read ", Toast.LENGTH_SHORT).show();
try {
File file1 = new File(getFilesDir(), "amtaddtobal.txt");

// Toast.makeText(this, " File Exists Reading ! ",


Toast.LENGTH_SHORT).show();

FileInputStream fileInputStream1 = new FileInputStream(file1);


StringBuffer retBuf1 = new StringBuffer();

if (fileInputStream1 != null) {

// Toast.makeText(this, " Inside Input Stream ! ",


Toast.LENGTH_SHORT).show();

InputStreamReader inputStreamReader1 = new


InputStreamReader(fileInputStream1);
BufferedReader bufferedReader1 = new
BufferedReader(inputStreamReader1);
String lineData1 = bufferedReader1.readLine();
while (lineData1 != null) {
retBuf1.append(lineData1);
lineData1 = bufferedReader1.readLine();
}
String click_val = retBuf1.toString();
amt_add = click_val;

// et2.setText(amt_add);

// Toast.makeText(getApplicationContext(), " ADD AMT INSIDE : " +


amt_add, Toast.LENGTH_LONG).show();

}
// Toast.makeText(getApplicationContext(), " Outside : " + amt_add,
Toast.LENGTH_LONG).show();

} catch (FileNotFoundException ex) {


Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
}
finally {
// Toast.makeText(getApplicationContext()," Add " + amt_add ,
Toast.LENGTH_LONG).show();
et2.setText(String.valueOf(amt_add));
}
}

public void movetobalview() {


Intent balview = new Intent(getApplicationContext(), baldisplay.class);
balview.putExtra("userid", userid);
startActivity(balview);
}

public void writenewbal(String x)


{
// amtadd= et2.getText().toString();
// Toast.makeText(this," Add Amt : " + amtadd, Toast.LENGTH_LONG).show();
try {
File file = new File(getFilesDir(), "amtbalance.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);

OutputStreamWriter outputStreamWriter = new


OutputStreamWriter(fileOutputStream);

BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);


bufferedWriter.write(String.valueOf(x));

bufferedWriter.flush();
bufferedWriter.close();
outputStreamWriter.close();
fileOutputStream.close();

}
catch (FileNotFoundException ex) {
Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}

catch(Exception ex)
{
Toast.makeText(this,ex.toString(),Toast.LENGTH_LONG).show();
}
finally {
Toast.makeText(getApplicationContext(), " New Balance " + x ,
Toast.LENGTH_SHORT).show();
}
}
}

You might also like