Skip to content

Commit

Permalink
Add android example app
Browse files Browse the repository at this point in the history
  • Loading branch information
muteeburrehman committed May 23, 2024
1 parent 2a2d0d3 commit 6869486
Show file tree
Hide file tree
Showing 49 changed files with 1,672 additions and 0 deletions.
15 changes: 15 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
45 changes: 45 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
plugins {
alias(libs.plugins.android.application)
}

android {
namespace 'io.xconn.cryptologyexample'
compileSdk 34

defaultConfig {
applicationId "io.xconn.cryptologyexample"
minSdk 24
targetSdk 34
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation libs.appcompat
implementation libs.activity
implementation libs.constraintlayout


implementation libs.cryptology
implementation libs.navigation.fragment.ktx
implementation libs.navigation.ui.ktx
implementation libs.material
implementation libs.drawerlayout


}
21 changes: 21 additions & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# 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
40 changes: 40 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature android:name="android.hardware.camera.any" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature
android:name="android.hardware.camera"
android:required="false" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".util.App"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CryptologyExample"
tools:targetApi="31">


<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
133 changes: 133 additions & 0 deletions android/app/src/main/java/io/xconn/cryptologyexample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package io.xconn.cryptologyexample;

import static io.xconn.cryptologyexample.util.Helpers.bytesToHex;
import static io.xconn.cryptologyexample.util.Helpers.convertTo32Bytes;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Objects;

import io.xconn.cryptology.KeyPair;
import io.xconn.cryptology.SealedBox;
import io.xconn.cryptology.SecretBox;
import io.xconn.cryptologyexample.fragment.CameraFragment;
import io.xconn.cryptologyexample.fragment.GalleryFragment;
import io.xconn.cryptologyexample.util.App;

public class MainActivity extends AppCompatActivity {


private androidx.appcompat.app.AlertDialog passwordDialog;

private FragmentManager fragmentManager;

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

if (!App.getBoolean("isDialogShown")) {
showPasswordDialog();
}
fragmentManager = getSupportFragmentManager();
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);

Fragment cameraFragment = new CameraFragment();
fragmentManager.beginTransaction().replace(R.id.frameLayout, cameraFragment).commit();

bottomNavigationView.setOnItemSelectedListener(item -> {
Fragment fragment = null;
if (item.getItemId() == R.id.menu_camera) {
fragment = new CameraFragment();
} else if (item.getItemId() == R.id.menu_gallery) {
fragment = new GalleryFragment();
}

if (fragment != null) {
fragmentManager.beginTransaction().replace(R.id.frameLayout, fragment).commit();
return true;
}
return false;
});
}

private void showPasswordDialog() {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_dialog_box, null);
TextInputLayout textInputLayoutPassword = dialogView.findViewById(R.id.enterPassword);
EditText editTextPassword = textInputLayoutPassword.getEditText();

builder.setView(dialogView)
.setTitle("Enter Password")
.setPositiveButton("Submit", null)
.setCancelable(false);

passwordDialog = builder.create();
passwordDialog.show();

passwordDialog.getButton(androidx.appcompat.app.AlertDialog.BUTTON_POSITIVE)
.setOnClickListener(v -> {
String enteredPassword = editTextPassword != null ?
editTextPassword.getText().toString().trim() : "";

if (enteredPassword.isEmpty()) {
textInputLayoutPassword.setError("Please enter a password");
textInputLayoutPassword.requestFocus();
} else {
KeyPair keyPair = SealedBox.generateKeyPair();
String publicKey = bytesToHex(keyPair.getPublicKey());
App.saveString(App.PREF_PUBLIC_KEY, publicKey);

byte[] nonce = SecretBox.generateNonce();
App.saveString("nonce", bytesToHex(nonce));

byte[] encryptedPrivateKey = SecretBox.box(nonce, keyPair.getPrivateKey(),
Objects.requireNonNull(convertTo32Bytes(enteredPassword)));
App.saveString(App.PREF_PRIVATE_KEY, bytesToHex(encryptedPrivateKey));

App.saveBoolean("isDialogShown", true);
passwordDialog.dismiss();

System.out.println("------passSaved ");
}
});

assert editTextPassword != null;
editTextPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// No action needed
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
passwordDialog.getButton(androidx.appcompat.app.AlertDialog.BUTTON_POSITIVE)
.setEnabled(!TextUtils.isEmpty(s));
}

@Override
public void afterTextChanged(Editable s) {
// No action needed
}
});

passwordDialog.getButton(androidx.appcompat.app.AlertDialog.BUTTON_POSITIVE)
.setEnabled(false);
}
}
Loading

0 comments on commit 6869486

Please sign in to comment.