Commit c1cc1207 authored by dimitrisCBR's avatar dimitrisCBR Committed by Yorgos Galyfos
Browse files

Allow for host app data to be backed up

parent 71f696ea
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 29
......@@ -6,9 +7,9 @@ android {
defaultConfig {
applicationId "com.futurae.futuraedemo"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 29
versionCode 5
versionCode 6
versionName "1.0.0"
resConfigs "en", "el"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
......@@ -46,7 +47,10 @@ repositories {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.core:core-ktx:1.6.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'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
......@@ -57,7 +61,7 @@ dependencies {
implementation 'com.google.firebase:firebase-core:18.0.3'
implementation 'com.google.firebase:firebase-messaging:21.1.0'
implementation('com.futurae.sdk:futuraekit:1.3.0')
// implementation('com.futurae.sdk:futuraekit:1.3.0')
// Use these if you are NOT using Maven
// implementation 'com.squareup.retrofit2:retrofit:2.4.0'
......@@ -67,7 +71,7 @@ dependencies {
// implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
// implementation 'com.github.nisrulz:easydeviceinfo-base:2.4.1'
// implementation 'com.google.code.gson:gson:2.8.6'
// implementation files('src/main/libs/futuraekit.aar')
implementation files('../../futuraekit.aar')
}
......
......@@ -15,7 +15,7 @@
<application
tools:replace="android:allowBackup"
android:allowBackup="true"
android:backupAgent=".DemoBackupAgent"
android:backupAgent=".CustomBackupAgent"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name="com.futurae.futuraedemo.AppMain"
......
package com.futurae.futuraedemo
import android.app.backup.BackupAgent
import android.app.backup.BackupDataInput
import android.app.backup.BackupDataOutput
import android.os.ParcelFileDescriptor
import android.util.Log
import java.io.*
import kotlin.random.Random
class CustomBackupAgent : BackupAgent() {
private val TAG = CustomBackupAgent::class.simpleName
private val PREFS_BACKUP_KEY = "prefs_shared_prefs_backup_key"
override fun onBackup(oldState: ParcelFileDescriptor?, data: BackupDataOutput, newState: ParcelFileDescriptor?) {
try {
//call SDK's backup agent
com.futurae.sdk.BackupAgent.onBackupAccounts(this, oldState, data, newState)
//back up your own data
val sp = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
val randomInt = Random.nextInt(10)
sp.edit().putInt(PREF_RANDOM_STRING, randomInt).apply()
val preferenceValue = randomInt.toString()
Log.d(
TAG,
"Starting backup of the prefs configuration in $PREFS_BACKUP_KEY, store: $randomInt as $preferenceValue"
)
val bufStream = ByteArrayOutputStream()
val outWriter = DataOutputStream(bufStream)
outWriter.writeUTF(preferenceValue)
val buffer = bufStream.toByteArray()
val len = buffer.size
data.writeEntityHeader(PREFS_BACKUP_KEY, len)
data.writeEntityData(buffer, len)
} catch (e: IOException) {
Log.e(TAG, e.message)
throw e
}
}
override fun onRestore(data: BackupDataInput, appVersionCode: Int, newState: ParcelFileDescriptor?) {
while (data.readNextHeader()) {
//if it's your backup key, proceed with restoring backed-up values
if (PREFS_BACKUP_KEY == data.key) {
try {
val dataSize = data.dataSize
val dataBuf = ByteArray(dataSize)
data.readEntityData(dataBuf, 0, dataSize)
val baStream = ByteArrayInputStream(dataBuf)
val `in` = DataInputStream(baStream)
val storedValue = `in`.readUTF()
if (storedValue.isBlank()) {
return
}
Log.d(
TAG,
"onRestore: Prefs restore from the backup completed. Value: ${storedValue}, calling SDK backup agent"
)
val sp = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
sp.edit().putInt(PREF_RANDOM_STRING, storedValue.toInt()).apply()
} catch (e: IOException) {
Log.e(TAG, e.message)
throw e
} catch (e: Exception) {
Log.d(TAG, e.message)
}
} else {
com.futurae.sdk.BackupAgent.onRestoreAccountsData(this, data)
}
}
}
companion object {
const val PREFS_NAME = "PREFS_NAME_CUSTOM"
const val PREF_RANDOM_STRING = "PREF_RANDOM_STRING"
}
}
\ 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
buildscript {
ext.kotlin_version = '1.5.0'
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.google.gms:google-services:4.3.10'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
......
No preview for this file type
Markdown is supported
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