Skip to content

Commit cc71995

Browse files
authored
Merge pull request #1 from kishandonga/prefs_secure
Prefs secure
2 parents 6fbe38c + 42efc3b commit cc71995

File tree

28 files changed

+860
-97
lines changed

28 files changed

+860
-97
lines changed

.idea/codeStyles/Project.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies {
2929
implementation project(path: ':library')
3030
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3131
implementation 'androidx.core:core-ktx:1.3.1'
32-
implementation 'androidx.appcompat:appcompat:1.1.0'
32+
implementation 'androidx.appcompat:appcompat:1.2.0'
3333
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
3434

3535
testImplementation 'junit:junit:4.13'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.sample.easyprefs.secure
2+
3+
object Const {
4+
const val PREF_SAMPLE_FILE = "secure_sample_file_name"
5+
const val PREF_SAMPLE_FILE_2 = "secure_sample_file_name_two"
6+
7+
const val SAMPLE_STRING_KEY = "secure_sample_string_key"
8+
const val SAMPLE_STRING_KEY_ASYNC = "secure_sample_string_key_async"
9+
10+
const val SAMPLE_STRING_SET_KEY = "secure_sample_string_set_key"
11+
const val SAMPLE_STRING_SET_KEY_ASYNC = "secure_sample_string_set_key_async"
12+
13+
const val SAMPLE_INT_KEY = "secure_sample_int_key"
14+
const val SAMPLE_INT_KEY_ASYNC = "secure_sample_int_key_async"
15+
16+
const val SAMPLE_LONG_KEY = "secure_sample_long_key"
17+
const val SAMPLE_LONG_KEY_ASYNC = "secure_sample_long_key_async"
18+
19+
const val SAMPLE_FLOAT_KEY = "secure_sample_float_key"
20+
const val SAMPLE_FLOAT_KEY_ASYNC = "secure_sample_float_key_async"
21+
22+
const val SAMPLE_DOUBLE_KEY = "secure_sample_double_key"
23+
const val SAMPLE_DOUBLE_KEY_ASYNC = "secure_sample_double_key_async"
24+
25+
const val SAMPLE_BOOLEAN_KEY = "secure_sample_boolean_key"
26+
const val SAMPLE_BOOLEAN_KEY_ASYNC = "secure_sample_boolean_key_async"
27+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.sample.easyprefs.secure
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import androidx.test.platform.app.InstrumentationRegistry
5+
import io.easyprefs.Prefs
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertTrue
8+
import org.junit.Before
9+
import org.junit.Test
10+
import org.junit.runner.RunWith
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* See [testing documentation](http://d.android.com/tools/testing).
16+
*/
17+
@RunWith(AndroidJUnit4::class)
18+
class SecurePrefsReadAsyncTest {
19+
20+
@Before
21+
fun initApp() {
22+
Prefs.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
23+
}
24+
25+
@Test
26+
fun testStringAsync() {
27+
val value = "Async..."
28+
val data = Prefs.securely().read().string(Const.SAMPLE_STRING_KEY_ASYNC, "")
29+
assertEquals(value, data)
30+
}
31+
32+
@Test
33+
fun testIntAsync() {
34+
val value = Int.MAX_VALUE
35+
val data = Prefs.securely().read().int(Const.SAMPLE_INT_KEY_ASYNC, Int.MIN_VALUE)
36+
assertEquals(value, data)
37+
}
38+
39+
@Test
40+
fun testFloatAsync() {
41+
val value = Float.MAX_VALUE
42+
val data = Prefs.securely().read().float(Const.SAMPLE_FLOAT_KEY_ASYNC, Float.MIN_VALUE)
43+
assertEquals(value, data)
44+
}
45+
46+
@Test
47+
fun testLongAsync() {
48+
val value = Long.MAX_VALUE
49+
val data = Prefs.securely().read().long(Const.SAMPLE_LONG_KEY_ASYNC, Long.MIN_VALUE)
50+
assertEquals(value, data)
51+
}
52+
53+
@Test
54+
fun testDoubleAsync() {
55+
val value = Double.MAX_VALUE
56+
val data = Prefs.securely().read().double(Const.SAMPLE_DOUBLE_KEY_ASYNC, Double.MIN_VALUE)
57+
assertEquals(value.toString(), data.toString())
58+
}
59+
60+
@Test
61+
fun testBooleanAsync() {
62+
val value = false
63+
val data = Prefs.securely().read().boolean(Const.SAMPLE_BOOLEAN_KEY_ASYNC, true)
64+
assertEquals(value, data)
65+
}
66+
67+
@Test
68+
fun testStringSetAsync() {
69+
val value = setOf("A", "B", "C", "D")
70+
val data = Prefs.securely().read().stringSet(Const.SAMPLE_STRING_SET_KEY_ASYNC, setOf())
71+
assertEquals(value, data)
72+
}
73+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.sample.easyprefs.secure
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import androidx.test.platform.app.InstrumentationRegistry
5+
import io.easyprefs.Prefs
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertTrue
8+
import org.junit.Before
9+
import org.junit.Test
10+
import org.junit.runner.RunWith
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* See [testing documentation](http://d.android.com/tools/testing).
16+
*/
17+
@RunWith(AndroidJUnit4::class)
18+
class SecurePrefsTest {
19+
20+
@Before
21+
fun initApp() {
22+
Prefs.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
23+
}
24+
25+
@Test
26+
fun testString() {
27+
val value = "Hello..."
28+
assertTrue(Prefs.securely().write().string(Const.SAMPLE_STRING_KEY, value))
29+
30+
val data = Prefs.securely().read().string(Const.SAMPLE_STRING_KEY, "")
31+
assertEquals(value, data)
32+
}
33+
34+
@Test
35+
fun testStringAsync() {
36+
val value = "Async..."
37+
Prefs.securely().write().stringAsync(Const.SAMPLE_STRING_KEY_ASYNC, value)
38+
}
39+
40+
@Test
41+
fun testInt() {
42+
val value = Int.MAX_VALUE
43+
assertTrue(Prefs.securely().write().int(Const.SAMPLE_INT_KEY, value))
44+
45+
val data = Prefs.securely().read().int(Const.SAMPLE_INT_KEY, Int.MIN_VALUE)
46+
assertEquals(value, data)
47+
}
48+
49+
@Test
50+
fun testIntAsync() {
51+
val value = Int.MAX_VALUE
52+
Prefs.securely().write().intAsync(Const.SAMPLE_INT_KEY_ASYNC, value)
53+
}
54+
55+
@Test
56+
fun testFloat() {
57+
val value = Float.MAX_VALUE
58+
assertTrue(Prefs.securely().write().float(Const.SAMPLE_FLOAT_KEY, value))
59+
60+
val data = Prefs.securely().read().float(Const.SAMPLE_FLOAT_KEY, Float.MIN_VALUE)
61+
assertEquals(value, data)
62+
}
63+
64+
@Test
65+
fun testFloatAsync() {
66+
val value = Float.MAX_VALUE
67+
Prefs.securely().write().floatAsync(Const.SAMPLE_FLOAT_KEY_ASYNC, value)
68+
}
69+
70+
@Test
71+
fun testLong() {
72+
val value = Long.MAX_VALUE
73+
assertTrue(Prefs.securely().write().long(Const.SAMPLE_LONG_KEY, value))
74+
75+
val data = Prefs.securely().read().long(Const.SAMPLE_LONG_KEY, Long.MIN_VALUE)
76+
assertEquals(value, data)
77+
}
78+
79+
@Test
80+
fun testLongAsync() {
81+
val value = Long.MAX_VALUE
82+
Prefs.securely().write().longAsync(Const.SAMPLE_LONG_KEY_ASYNC, value)
83+
}
84+
85+
@Test
86+
fun testDouble() {
87+
val value = Double.MAX_VALUE
88+
assertTrue(Prefs.securely().write().double(Const.SAMPLE_DOUBLE_KEY, value))
89+
90+
val data = Prefs.securely().read().double(Const.SAMPLE_DOUBLE_KEY, Double.MIN_VALUE)
91+
assertEquals(value.toString(), data.toString())
92+
}
93+
94+
@Test
95+
fun testDoubleAsync() {
96+
val value = Double.MAX_VALUE
97+
Prefs.securely().write().doubleAsync(Const.SAMPLE_DOUBLE_KEY_ASYNC, value)
98+
}
99+
100+
@Test
101+
fun testBoolean() {
102+
val value = false
103+
assertTrue(Prefs.securely().write().boolean(Const.SAMPLE_BOOLEAN_KEY, value))
104+
105+
val data = Prefs.securely().read().boolean(Const.SAMPLE_BOOLEAN_KEY, true)
106+
assertEquals(value, data)
107+
}
108+
109+
@Test
110+
fun testBooleanAsync() {
111+
val value = false
112+
Prefs.securely().write().booleanAsync(Const.SAMPLE_BOOLEAN_KEY_ASYNC, value)
113+
}
114+
115+
@Test
116+
fun testStringSet() {
117+
val value = setOf("A", "B", "C", "D")
118+
assertTrue(Prefs.securely().write().stringSet(Const.SAMPLE_STRING_SET_KEY, value))
119+
120+
val data = Prefs.securely().read().stringSet(Const.SAMPLE_STRING_SET_KEY, setOf())
121+
assertEquals(value, data)
122+
}
123+
124+
@Test
125+
fun testStringSetAsync() {
126+
val value = setOf("A", "B", "C", "D")
127+
Prefs.securely().write().stringSetAsync(Const.SAMPLE_STRING_SET_KEY_ASYNC, value)
128+
}
129+
}

app/src/main/java/com/sample/easyprefs/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.sample.easyprefs
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5+
import io.easyprefs.Prefs
56

67
class MainActivity : AppCompatActivity() {
78
override fun onCreate(savedInstanceState: Bundle?) {

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
33
ext.kotlin_version = "1.3.72"
4+
ext.kotlin_coroutines_version = "1.3.4"
45
repositories {
56
google()
67
jcenter()

library/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ android {
2727
dependencies {
2828
implementation fileTree(dir: "libs", include: ["*.jar"])
2929
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
30+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
31+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
3032
}

library/src/main/java/io/easyprefs/Prefs.kt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,37 @@ package io.easyprefs
22

33
import android.content.Context
44
import io.easyprefs.contract.Edit
5-
import io.easyprefs.contract.PrefProvider
65
import io.easyprefs.contract.Read
6+
import io.easyprefs.contract.Secure
77
import io.easyprefs.contract.Write
8+
import io.easyprefs.contract.provider.PrefProvider
89
import io.easyprefs.error.PrefsEditContextException
910
import io.easyprefs.error.PrefsReadContextException
1011
import io.easyprefs.error.PrefsWriteContextException
12+
import io.easyprefs.ext.Pass
1113
import io.easyprefs.impl.EasyPrefImpl
14+
import io.easyprefs.impl.SecureImpl
15+
import io.easyprefs.typedef.Encryption
1216

1317
object Prefs : PrefProvider {
1418

1519
private lateinit var context: Context
1620

17-
fun initializeApp(context: Context) {
21+
override fun initializeApp(context: Context) {
1822
this.context = context.applicationContext
1923
}
2024

25+
override fun securely(): Secure {
26+
return securely(Pass.empty)
27+
}
28+
29+
override fun securely(aesKey: String): Secure {
30+
val secure = SecureImpl
31+
secure.context = context
32+
secure.aesKey = aesKey
33+
return secure
34+
}
35+
2136
override fun write(): Write {
2237
if (this::context.isInitialized) {
2338
return write(context)
@@ -40,15 +55,15 @@ object Prefs : PrefProvider {
4055
}
4156

4257
override fun write(context: Context): Write {
43-
return EasyPrefImpl.write(context)
58+
return EasyPrefImpl.write(context, Encryption.NONE, Pass.empty)
4459
}
4560

4661
override fun write(context: Context, fileName: String): Write {
47-
return EasyPrefImpl.writeOn(context, fileName)
62+
return EasyPrefImpl.writeOn(context, fileName, Encryption.NONE, Pass.empty)
4863
}
4964

5065
override fun write(context: Context, fileName: String, mode: Int): Write {
51-
return EasyPrefImpl.writeOn(context, fileName, mode)
66+
return EasyPrefImpl.writeOn(context, fileName, mode, Encryption.NONE, Pass.empty)
5267
}
5368

5469
override fun read(): Read {
@@ -73,15 +88,15 @@ object Prefs : PrefProvider {
7388
}
7489

7590
override fun read(context: Context): Read {
76-
return EasyPrefImpl.read(context)
91+
return EasyPrefImpl.read(context, Encryption.NONE, Pass.empty)
7792
}
7893

7994
override fun read(context: Context, fileName: String): Read {
80-
return EasyPrefImpl.readOn(context, fileName)
95+
return EasyPrefImpl.readOn(context, fileName, Encryption.NONE, Pass.empty)
8196
}
8297

8398
override fun read(context: Context, fileName: String, mode: Int): Read {
84-
return EasyPrefImpl.readOn(context, fileName, mode)
99+
return EasyPrefImpl.readOn(context, fileName, mode, Encryption.NONE, Pass.empty)
85100
}
86101

87102
override fun edit(): Edit {
@@ -106,14 +121,14 @@ object Prefs : PrefProvider {
106121
}
107122

108123
override fun edit(context: Context): Edit {
109-
return EasyPrefImpl.edit(context)
124+
return EasyPrefImpl.edit(context, Encryption.NONE, Pass.empty)
110125
}
111126

112127
override fun edit(context: Context, fileName: String): Edit {
113-
return EasyPrefImpl.editOn(context, fileName)
128+
return EasyPrefImpl.editOn(context, fileName, Encryption.NONE, Pass.empty)
114129
}
115130

116131
override fun edit(context: Context, fileName: String, mode: Int): Edit {
117-
return EasyPrefImpl.editOn(context, fileName, mode)
132+
return EasyPrefImpl.editOn(context, fileName, mode, Encryption.NONE, Pass.empty)
118133
}
119134
}

0 commit comments

Comments
 (0)