Skip to content

Commit b151beb

Browse files
authored
Merge pull request #6 from kishandonga/develop
setup for the sdk level 21
2 parents ec80ad9 + b011904 commit b151beb

File tree

8 files changed

+52
-46
lines changed

8 files changed

+52
-46
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ EasyPrefs is library for the android SharedPreferences or we can say it wrapper
66

77
It support all Primitive Data Type and String Set more api will be added soon.
88

9-
For, the secure operation used AES encryption <= 20 API Level, and for 21 >= used android provided encrypted shared preferences in both case you got same output your key and value both are stored securly.
9+
For, the secure operation used AES encryption <= 20 API Level, and for 21 >= used android provided encrypted shared preferences in both case you got same output your key and value both are stored securely.
1010

1111
This library is developed in the Kotlin and supported both language `Kotlin` as well as `Java` with the same practices.
1212

@@ -18,8 +18,6 @@ Gradle:
1818
```groovy
1919
allprojects {
2020
repositories {
21-
google()
22-
jcenter()
2321
maven { url 'https://jitpack.io' }
2422
}
2523
}
@@ -103,6 +101,8 @@ Prefs.has().key("KEY");
103101
Prefs.has(fileName).key("KEY");
104102
Prefs.has(context).key("KEY");
105103
Prefs.has(context, fileName).key("KEY");
104+
105+
Prefs.has().empty();
106106
```
107107
- Give boolean value if key exists then true else false.
108108

@@ -121,12 +121,14 @@ commit() or .apply()
121121
#### Clear Operation
122122

123123
```kotlin
124-
Prefs.clear().all().
125-
Prefs.clear(fileName).all().
126-
Prefs.clear(context).all().
127-
Prefs.clear(context, fileName).all().
124+
Prefs.clear().all()
125+
Prefs.clear(fileName).all()
126+
Prefs.clear(context).all()
127+
Prefs.clear(context, fileName).all()
128128

129129
.commit() or .apply()
130+
131+
Note: for, securely operation used same Prefs.clear() operation.
130132
```
131133
- After the clear operation commit and apply call mandatory.
132134

@@ -135,10 +137,9 @@ For, all the read, write, clear, has, remove support context and file name manua
135137
If you pass context manually then no need to initialize lib on the application class, For, more information refer [here](app/src/androidTest/java/com/sample/easyprefs)
136138

137139
## Future Scope
138-
- has and remove support provided to secure operations.
139140
- add sorting on the Set so get direct sorted data.
140-
- to make life easier adding more api in near future
141141
- callback extend as we already have in the preferences.
142+
- provide suggestions to make this lib more helpful.
142143

143144
---
144145

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ allprojects {
1919

2020
tasks.register('clean', Delete) {
2121
delete rootProject.buildDir
22+
}
23+
24+
tasks.register('testKotlinSuite', Exec) {
25+
commandLine './gradlew', 'app:connectedAndroidTest',
26+
'-Pandroid.testInstrumentationRunnerArguments.class=com.sample.easyprefs.kotlin.TestSuite -i'
2227
}

library/src/main/java/io/easyprefs/impl/HasImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HasImpl(
1515
return if (encType == Encryption.NONE) {
1616
pref.all.keys.contains(key)
1717
} else {
18-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
18+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1919
pref.all.keys.contains(key)
2020
} else {
2121
pref.all.keys.contains(Crypt.encryptKey(key))

library/src/main/java/io/easyprefs/impl/PrefProvider.kt

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,19 @@ object PrefProvider {
1010
private var fName: String = ""
1111
private lateinit var sharedPreferences: SharedPreferences
1212

13-
private fun pref(context: Context, fileName: String): SharedPreferences {
14-
15-
if (this::sharedPreferences.isInitialized && fName.isNotEmpty() && fName == fileName) {
16-
return sharedPreferences
17-
}
18-
19-
sharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
20-
return sharedPreferences
21-
}
22-
2313
fun getPref(
2414
context: Context,
2515
fileName: String,
2616
encType: Encryption
2717
): SharedPreferences {
28-
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
29-
return if (encType == Encryption.NONE) {
30-
pref(context, fileName)
31-
} else {
18+
return if (encType == Encryption.NONE) {
19+
pref(context, fileName)
20+
} else {
21+
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
3222
EncryptedPref.getPreferences(context, fileName)
23+
} else {
24+
pref(context, fileName)
3325
}
34-
} else {
35-
pref(context, fileName)
3626
}
3727
}
3828

@@ -41,17 +31,26 @@ object PrefProvider {
4131
fileName: String,
4232
encType: Encryption
4333
): SharedPreferences.Editor {
44-
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
45-
return if (encType == Encryption.NONE) {
46-
prefEditor(context, fileName)
47-
} else {
34+
return if (encType == Encryption.NONE) {
35+
prefEditor(context, fileName)
36+
} else {
37+
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
4838
EncryptedPref.getPreferences(context, fileName).edit()
39+
} else {
40+
prefEditor(context, fileName)
4941
}
50-
} else {
51-
prefEditor(context, fileName)
5242
}
5343
}
5444

45+
private fun pref(context: Context, fileName: String): SharedPreferences {
46+
if (this::sharedPreferences.isInitialized && fName.isNotEmpty() && fName == fileName) {
47+
return sharedPreferences
48+
}
49+
50+
sharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
51+
return sharedPreferences
52+
}
53+
5554
private fun prefEditor(
5655
context: Context,
5756
fileName: String

library/src/main/java/io/easyprefs/impl/ReadImpl.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ReadImpl(
1616
return if (encType == Encryption.NONE) {
1717
pref.getInt(key, defaultValue)
1818
} else {
19-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
19+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
2020
pref.getInt(key, defaultValue)
2121
} else {
2222
decrypt(key, defaultValue.toString()).toInt()
@@ -36,7 +36,7 @@ class ReadImpl(
3636
return if (encType == Encryption.NONE) {
3737
pref.getString(key, defaultValue) ?: defaultValue
3838
} else {
39-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
39+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
4040
pref.getString(key, defaultValue) ?: defaultValue
4141
} else {
4242
decrypt(key, defaultValue)
@@ -48,7 +48,7 @@ class ReadImpl(
4848
return if (encType == Encryption.NONE) {
4949
pref.getLong(key, defaultValue)
5050
} else {
51-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
51+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
5252
pref.getLong(key, defaultValue)
5353
} else {
5454
decrypt(key, defaultValue.toString()).toLong()
@@ -60,7 +60,7 @@ class ReadImpl(
6060
return if (encType == Encryption.NONE) {
6161
pref.getFloat(key, defaultValue)
6262
} else {
63-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
63+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
6464
pref.getFloat(key, defaultValue)
6565
} else {
6666
decrypt(key, defaultValue.toString()).toFloat()
@@ -76,7 +76,7 @@ class ReadImpl(
7676
return if (encType == Encryption.NONE) {
7777
pref.getBoolean(key, defaultValue)
7878
} else {
79-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
79+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
8080
pref.getBoolean(key, defaultValue)
8181
} else {
8282
decrypt(key, defaultValue.toString()).toBoolean()
@@ -88,7 +88,7 @@ class ReadImpl(
8888
return if (encType == Encryption.NONE) {
8989
pref.getStringSet(key, defaultValue) ?: defaultValue
9090
} else {
91-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
91+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
9292
pref.getStringSet(key, defaultValue) ?: defaultValue
9393
} else {
9494
val value = decrypt(key, "")
@@ -106,6 +106,7 @@ class ReadImpl(
106106
}
107107
}
108108

109+
//TODO: it give encrypted data as well
109110
override fun allContent(): Map<String, *> {
110111
return pref.all
111112
}

library/src/main/java/io/easyprefs/impl/RemoveImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RemoveImpl(
1515
if (encType == Encryption.NONE) {
1616
edit.remove(key)
1717
} else {
18-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
18+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1919
edit.remove(key)
2020
} else {
2121
edit.remove(Crypt.encryptKey(key))

library/src/main/java/io/easyprefs/impl/WriteImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WriteImpl(
1616
if (encType == Encryption.NONE) {
1717
edit.putInt(key, value)
1818
} else {
19-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
2020
edit.putInt(key, value)
2121
} else {
2222
crypt(key, value.toString())
@@ -33,7 +33,7 @@ class WriteImpl(
3333
if (encType == Encryption.NONE) {
3434
edit.putString(key, value)
3535
} else {
36-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
36+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
3737
edit.putString(key, value)
3838
} else {
3939
crypt(key, value)
@@ -46,7 +46,7 @@ class WriteImpl(
4646
if (encType == Encryption.NONE) {
4747
edit.putLong(key, value)
4848
} else {
49-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
49+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
5050
edit.putLong(key, value)
5151
} else {
5252
crypt(key, value.toString())
@@ -59,7 +59,7 @@ class WriteImpl(
5959
if (encType == Encryption.NONE) {
6060
edit.putFloat(key, value)
6161
} else {
62-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
62+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
6363
edit.putFloat(key, value)
6464
} else {
6565
crypt(key, value.toString())
@@ -77,7 +77,7 @@ class WriteImpl(
7777
if (encType == Encryption.NONE) {
7878
edit.putBoolean(key, value)
7979
} else {
80-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
80+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
8181
edit.putBoolean(key, value)
8282
} else {
8383
crypt(key, value.toString())
@@ -90,7 +90,7 @@ class WriteImpl(
9090
if (encType == Encryption.NONE) {
9191
edit.putStringSet(key, value)
9292
} else {
93-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
93+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
9494
edit.putStringSet(key, value)
9595
} else {
9696
crypt(key, JSONArray(value).toString())

library/src/main/java/io/easyprefs/secure/EncryptedPref.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import androidx.annotation.RequiresApi
77
import androidx.security.crypto.EncryptedSharedPreferences
88
import androidx.security.crypto.MasterKey
99

10-
@RequiresApi(Build.VERSION_CODES.M)
10+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
1111
object EncryptedPref {
1212

1313
private var fName: String = ""

0 commit comments

Comments
 (0)