Skip to content

Commit

Permalink
- temporary remove EncryptedSharedPreferences
Browse files Browse the repository at this point in the history
- add sample
  • Loading branch information
RazibKani committed Apr 24, 2020
1 parent f9bd3b5 commit 2a7502f
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

buildscript {
ext.kotlin_version = '1.3.61'
ext.security_crypto_version = '1.0.0-rc01'
ext.gson_version = '2.8.6'
repositories {
google()
jcenter()
Expand Down
2 changes: 1 addition & 1 deletion safedepositbox/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.security:security-crypto:$security_crypto_version"
implementation "com.google.code.gson:gson:$gson_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@ package id.codepresso.safedepositbox

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.os.Build
import android.text.TextUtils
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import com.google.gson.Gson


/**
* Crafted by Razib Kani Maulidan on 14/04/20.
**/

class SafeDepositBox(private val context: Context, private val prefName: String) {

private val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
private val masterKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)

private val sharedPrefs by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
EncryptedSharedPreferences
.create(
prefName,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
} else {
context.getSharedPreferences(prefName, MODE_PRIVATE)
}
}
private val gson by lazy { Gson() }

private val sharedPrefs by lazy { context.getSharedPreferences(prefName, MODE_PRIVATE) }
private val sharedPrefsEditor = sharedPrefs.edit()

private val delimiter = "::"
Expand Down Expand Up @@ -165,6 +150,28 @@ class SafeDepositBox(private val context: Context, private val prefName: String)
sharedPrefsEditor.putString(key, TextUtils.join(delimiter, arrayDouble)).apply()
}

/**
* Store Any value into SharedPreferences
* @param key SharedPreferences key
* @param value Any value to be stored
*/
fun storeObject(key: String, value: Any) {
sharedPrefsEditor.putString(key, gson.toJson(value)).apply()
}

/**
* Store List<Any> value into SharedPreferences
* @param key SharedPreferences key
* @param value List<Any> value to be stored
*/
fun storeListObject(key: String, values: List<Any>) {
val objectStrings = mutableListOf<String>()
values.forEach { obj ->
objectStrings.add(gson.toJson(obj))
}
storeListString(key, objectStrings)
}

/**
* Get Boolean value from SharedPreferences at 'key'. If key not found, return 'defaultValue'
* @param key SharedPreferences key
Expand Down Expand Up @@ -311,6 +318,33 @@ class SafeDepositBox(private val context: Context, private val prefName: String)
}
}

/**
* Get T value from SharedPreferences at 'key'.
* @param key SharedPreferences key
* @return T value at 'key' or NPE if value not found
*/
fun <T> getObject(key: String, classOfT: Class<T>): T {
val objectString = getString(key)
return gson.fromJson(objectString, classOfT) ?: throw NullPointerException()
}

/**
* Get List<T> value from SharedPreferences at 'key'.
* @param key SharedPreferences key
* @return List<T> value at 'key'
*/
fun <T> getListObject(key: String, classOfT: Class<T>): List<T> {
val objectStrings = getListString(key)
val objects = mutableListOf<T>()

objectStrings.forEach { objectString ->
val value = gson.fromJson(objectString, classOfT)
objects.add(value)
}

return objects
}

/**
* Remove value from SharedPreferences at 'key'
* @param key SharedPreferences key
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
buildToolsVersion "29.0.2"

defaultConfig {
applicationId "id.codepresso.savedepositbox"
applicationId "id.codepresso.savedepositbox.sample"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.codepresso.safedepositbox">
package="id.codepresso.safedepositbox.sample">

<application
android:allowBackup="true"
Expand All @@ -9,7 +9,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="id.codepresso.safedepositbox.SampleActivity">
<activity android:name=".SampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package id.codepresso.safedepositbox.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import id.codepresso.safedepositbox.SafeDepositBox
import kotlinx.android.synthetic.main.activity_main.*

class SampleActivity : AppCompatActivity() {

private val safeDepositBox by lazy {
SafeDepositBox(this, "SafeDepositBoxSample")
}

private val stringLogger by lazy {
StringBuilder().append("")
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val tatang =
User("Tatang", "Sutarna")
val bejo = User("Bejo", "Surojo")

safeDepositBox.apply {
storeObject("tatang", tatang)
storeObject("bejo", bejo)
}

stringLogger.append("Storing Object Value\n\n")

val tatangFromPref = safeDepositBox.getObject("tatang", User::class.java)
val bejoFromPref = safeDepositBox.getObject("bejo", User::class.java)

stringLogger.append("Getting Object Value\n\n")
stringLogger.append(tatangFromPref.toString())
stringLogger.append("\n")
stringLogger.append(bejoFromPref.toString())

updateLogView()
}

private fun updateLogView() {
tvLogger.text = stringLogger
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package id.codepresso.safedepositbox.sample

/**
* Crafted by Razib Kani Maulidan on 25/04/20.
**/

data class User(private val firstName: String, private val lastName: String)
14 changes: 12 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
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:padding="16dp"
tools:context=".SampleActivity" />
tools:context=".SampleActivity">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvLogger"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center"
tools:text="Lorem ipsum" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 2a7502f

Please sign in to comment.