Commit 91f97c4f authored by dimitrisCBR's avatar dimitrisCBR
Browse files

add sharedPrefs activity and backupagent. Plus material styles.

parent 71f696ea
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 29
......@@ -6,7 +7,7 @@ android {
defaultConfig {
applicationId "com.futurae.futuraedemo"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 29
versionCode 5
versionName "1.0.0"
......@@ -30,6 +31,10 @@ android {
}
}
buildFeatures {
viewBinding true
}
}
repositories {
......@@ -46,11 +51,14 @@ repositories {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.gms:play-services-vision:20.1.3'
implementation 'com.jakewharton:butterknife:10.2.3'
implementation 'com.google.android.material:material:1.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
implementation 'com.jakewharton.timber:timber:5.0.1'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
......
......@@ -15,7 +15,7 @@
<application
tools:replace="android:allowBackup"
android:allowBackup="true"
android:backupAgent=".DemoBackupAgent"
android:backupAgent=".CustomBackupAgentHelper"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name="com.futurae.futuraedemo.AppMain"
......@@ -54,6 +54,10 @@
android:exported="false"
android:name=".ui.FTRQRCodeActivity"
android:theme="@style/AppTheme" />
<activity android:name=".ui.SharedPrefsActivityK"
android:exported="false"
android:theme="@style/AppTheme"/>
</application>
</manifest>
\ No newline at end of file
......@@ -4,6 +4,8 @@ import android.app.Application;
import com.futurae.sdk.FuturaeClient;
import com.futurae.sdk.Kit;
import timber.log.Timber;
public class AppMain extends Application {
// overrides
......@@ -12,6 +14,10 @@ public class AppMain extends Application {
super.onCreate();
if(BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
FuturaeClient.launch(this, (Kit)null);
}
}
package com.futurae.futuraedemo
import android.app.backup.BackupAgentHelper
import android.app.backup.BackupDataInput
import android.app.backup.BackupDataOutput
import android.app.backup.SharedPreferencesBackupHelper
import android.os.ParcelFileDescriptor
import com.futurae.futuraedemo.ui.SharedPrefsActivityK
import com.futurae.sdk.BackupAgent
import timber.log.Timber
class CustomBackupAgentHelper : BackupAgentHelper() {
override fun onCreate() {
SharedPreferencesBackupHelper(this, SharedPrefsActivityK.PREFS_NAME).also {
addHelper(PREFS_BACKUP_KEY, it)
}
Timber.e("Created CustomBackupAgent and added helper")
}
override fun onBackup(
oldState: ParcelFileDescriptor?,
data: BackupDataOutput?,
newState: ParcelFileDescriptor?
) {
super.onBackup(oldState, data, newState)
Timber.e("OnBackup called")
try {
BackupAgent.onBackupAccounts(this, oldState, data, newState)
Timber.d("Backed up $data")
} catch (error: Throwable) {
Timber.e(error, "Could not backup Futurae!")
}
}
override fun onRestore(
data: BackupDataInput?,
appVersionCode: Int,
newState: ParcelFileDescriptor?
) {
super.onRestore(data, appVersionCode, newState)
Timber.e("onRestore called")
try {
BackupAgent.onRestoreAccounts(this, data, appVersionCode, newState)
Timber.d("Restored $data for version code $appVersionCode")
} catch (error: Throwable) {
Timber.e(error, "Could not restore Futurae!")
}
}
companion object {
const val PREFS_BACKUP_KEY = "CUSTOM_PREFS_BACKUP_KEY"
}
}
\ No newline at end of file
package com.futurae.futuraedemo;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import com.futurae.sdk.BackupAgent;
import java.io.IOException;
public class DemoBackupAgent extends android.app.backup.BackupAgent {
private static final String TAG = DemoBackupAgent.class.getSimpleName();
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) {
try {
BackupAgent.onBackupAccounts(this, oldState, data, newState);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) {
try {
BackupAgent.onRestoreAccounts(this, data, appVersionCode, newState);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
\ No newline at end of file
......@@ -11,12 +11,13 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import com.futurae.futuraedemo.R;
import com.futurae.sdk.FuturaeCallback;
......@@ -66,6 +67,7 @@ public class MainActivity extends AppCompatActivity {
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
......@@ -256,6 +258,13 @@ public class MainActivity extends AppCompatActivity {
);
}
@OnClick(R.id.main_btn_shared_prefs)
protected void onSharedPrefsClick() {
startActivity(
new Intent(this, SharedPrefsActivityK.class)
);
}
// QRCode callbacks
private void onEnrollQRCodeScanned(Intent data) {
// TODO: Handle enrollment response
......
package com.futurae.futuraedemo.ui
import android.content.Context
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import com.futurae.futuraedemo.databinding.ActivitySharedPrefsBinding
class SharedPrefsActivityK : AppCompatActivity() {
companion object {
const val PREFS_NAME = "shared_prefs_name"
const val PREFS_COUNTER_KEY = "counter_key"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivitySharedPrefsBinding.inflate(layoutInflater)
setContentView(binding.root)
val sharedPrefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
binding.counterTextView.text = sharedPrefs.getInt(PREFS_COUNTER_KEY, 0).toString()
binding.minusButton.setOnClickListener {
val updatedCounter = sharedPrefs.getInt(PREFS_COUNTER_KEY, 0) - 1
sharedPrefs.edit().putInt(
PREFS_COUNTER_KEY, updatedCounter
).apply()
binding.counterTextView.text = updatedCounter.toString()
}
binding.plusButton.setOnClickListener {
val updatedCounter = sharedPrefs.getInt(PREFS_COUNTER_KEY, 0) + 1
sharedPrefs.edit().putInt(
PREFS_COUNTER_KEY, updatedCounter
).apply()
binding.counterTextView.text = updatedCounter.toString()
}
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@
tools:context="com.futurae.futuraedemo.ui.MainActivity"
>
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_enroll"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -16,7 +16,7 @@
android:text="@string/main_btn_enroll"
/>
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_logout"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -24,7 +24,7 @@
android:layout_gravity="center"
android:text="@string/main_btn_logout" />
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_totp"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -32,7 +32,7 @@
android:layout_gravity="center"
android:text="@string/main_btn_totp" />
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_qr_code_auth"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -41,7 +41,7 @@
android:text="@string/main_btn_qr_code_auth"
/>
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_qr_code_offline_auth"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -49,7 +49,7 @@
android:layout_gravity="center"
android:text="@string/main_btn_qr_code_offline_auth" />
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_qr_code_generic"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -57,7 +57,7 @@
android:layout_gravity="center"
android:text="@string/main_btn_qr_code_generic" />
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_accounts_migration_check"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -65,7 +65,7 @@
android:layout_gravity="center"
android:text="@string/main_btn_accounts_migration_check" />
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_accounts_migration_execute"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
......@@ -73,4 +73,12 @@
android:layout_gravity="center"
android:text="@string/main_btn_accounts_migration_execute" />
<com.google.android.material.button.MaterialButton
android:id="@+id/main_btn_shared_prefs"
style="@style/AppTheme.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/main_btn_shared_prefs" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Use the buttons to increase the counter, stored in shared preferences. Use the backup functionality to confirm shared preferences are restored."/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/plusButton"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MINUS" />
<TextView
android:id="@+id/counterTextView"
style="@style/MaterialAlertDialog.MaterialComponents.Title.Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
tools:text="10" />
<com.google.android.material.button.MaterialButton
android:id="@+id/minusButton"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLUS" />
</LinearLayout>
</LinearLayout>
\ No newline at end of file
......@@ -10,6 +10,7 @@
<string name="main_btn_qr_code_generic">Scan QR Code</string>
<string name="main_btn_accounts_migration_check">Check account migration</string>
<string name="main_btn_accounts_migration_execute">Execute account migration</string>
<string name="main_btn_shared_prefs">Shared Preferences</string>
<!--QR CODE-->
<string name="qrcode_title">QR Code</string>
......
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Borderless" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textSize">24sp</item>
</style>
<style name="AppTheme.Borderless" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textSize">24sp</item>
</style>
</resources>
buildscript {
ext.kotlin_version = '1.5.30'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.google.gms:google-services:4.2.0'
classpath 'com.android.tools.build:gradle:4.0.2'
classpath 'com.google.gms:google-services:4.3.10'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment