Use SharedPreferences#edit extension function consistently.

+ Add "androidx.core:core-ktx:1.1.0" to "matrix-sdk-android" module which
  was already used in "vector" module.
+ Sources: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-preference-release/core/core-ktx/src/main/java/androidx/core/content/SharedPreferences.kt.
This commit is contained in:
Tobias Preuss 2020-06-24 11:42:01 +02:00
parent dbb9dc4458
commit e24785015d
6 changed files with 28 additions and 27 deletions

View File

@ -24,6 +24,7 @@ Build 🧱:
- SDK is now API level 21 minimum, and so RiotX (#405) - SDK is now API level 21 minimum, and so RiotX (#405)
Other changes: Other changes:
- Use `SharedPreferences#edit` extension function consistently (#1545)
- Use `retrofit2.Call.awaitResponse` extension provided by Retrofit 2. (#1526) - Use `retrofit2.Call.awaitResponse` extension provided by Retrofit 2. (#1526)
- Fix minor typo in contribution guide (#1512) - Fix minor typo in contribution guide (#1512)
- Fix self-assignment of callback in `DefaultRoomPushRuleService#setRoomNotificationState` (#1520) - Fix self-assignment of callback in `DefaultRoomPushRuleService#setRoomNotificationState` (#1520)

View File

@ -114,6 +114,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "androidx.appcompat:appcompat:1.1.0" implementation "androidx.appcompat:appcompat:1.1.0"
implementation "androidx.core:core-ktx:1.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

View File

@ -17,6 +17,7 @@ package im.vector.matrix.android.internal.database
import android.content.Context import android.content.Context
import android.util.Base64 import android.util.Base64
import androidx.core.content.edit
import im.vector.matrix.android.BuildConfig import im.vector.matrix.android.BuildConfig
import im.vector.matrix.android.internal.session.securestorage.SecretStoringUtils import im.vector.matrix.android.internal.session.securestorage.SecretStoringUtils
import io.realm.RealmConfiguration import io.realm.RealmConfiguration
@ -67,10 +68,9 @@ internal class RealmKeysUtils @Inject constructor(context: Context,
val key = generateKeyForRealm() val key = generateKeyForRealm()
val encodedKey = Base64.encodeToString(key, Base64.NO_PADDING) val encodedKey = Base64.encodeToString(key, Base64.NO_PADDING)
val toStore = secretStoringUtils.securelyStoreString(encodedKey, alias) val toStore = secretStoringUtils.securelyStoreString(encodedKey, alias)
sharedPreferences sharedPreferences.edit {
.edit() putString("${ENCRYPTED_KEY_PREFIX}_$alias", Base64.encodeToString(toStore!!, Base64.NO_PADDING))
.putString("${ENCRYPTED_KEY_PREFIX}_$alias", Base64.encodeToString(toStore!!, Base64.NO_PADDING)) }
.apply()
return key return key
} }
@ -107,10 +107,9 @@ internal class RealmKeysUtils @Inject constructor(context: Context,
if (hasKeyForDatabase(alias)) { if (hasKeyForDatabase(alias)) {
secretStoringUtils.safeDeleteKey(alias) secretStoringUtils.safeDeleteKey(alias)
sharedPreferences sharedPreferences.edit {
.edit() remove("${ENCRYPTED_KEY_PREFIX}_$alias")
.remove("${ENCRYPTED_KEY_PREFIX}_$alias") }
.apply()
} }
} }

View File

@ -17,6 +17,7 @@
package im.vector.matrix.android.internal.database package im.vector.matrix.android.internal.database
import android.content.Context import android.content.Context
import androidx.core.content.edit
import im.vector.matrix.android.internal.database.model.SessionRealmModule import im.vector.matrix.android.internal.database.model.SessionRealmModule
import im.vector.matrix.android.internal.di.SessionFilesDirectory import im.vector.matrix.android.internal.di.SessionFilesDirectory
import im.vector.matrix.android.internal.di.SessionId import im.vector.matrix.android.internal.di.SessionId
@ -54,10 +55,9 @@ internal class SessionRealmConfigurationFactory @Inject constructor(
Timber.v("************************************************************") Timber.v("************************************************************")
deleteRealmFiles() deleteRealmFiles()
} }
sharedPreferences sharedPreferences.edit {
.edit() putBoolean("$REALM_SHOULD_CLEAR_FLAG_$sessionId", true)
.putBoolean("$REALM_SHOULD_CLEAR_FLAG_$sessionId", true) }
.apply()
val realmConfiguration = RealmConfiguration.Builder() val realmConfiguration = RealmConfiguration.Builder()
.compactOnLaunch() .compactOnLaunch()
@ -73,10 +73,9 @@ internal class SessionRealmConfigurationFactory @Inject constructor(
// Try creating a realm instance and if it succeeds we can clear the flag // Try creating a realm instance and if it succeeds we can clear the flag
Realm.getInstance(realmConfiguration).use { Realm.getInstance(realmConfiguration).use {
Timber.v("Successfully create realm instance") Timber.v("Successfully create realm instance")
sharedPreferences sharedPreferences.edit {
.edit() putBoolean("$REALM_SHOULD_CLEAR_FLAG_$sessionId", false)
.putBoolean("$REALM_SHOULD_CLEAR_FLAG_$sessionId", false) }
.apply()
} }
return realmConfiguration return realmConfiguration
} }

View File

@ -27,6 +27,7 @@ import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties import android.security.keystore.KeyProperties
import android.util.Base64 import android.util.Base64
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.content.edit
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
@ -152,9 +153,9 @@ object CompatUtil {
.build()) .build())
key = generator.generateKey() key = generator.generateKey()
sharedPreferences.edit() sharedPreferences.edit {
.putInt(SHARED_KEY_ANDROID_VERSION_WHEN_KEY_HAS_BEEN_GENERATED, Build.VERSION.SDK_INT) putInt(SHARED_KEY_ANDROID_VERSION_WHEN_KEY_HAS_BEEN_GENERATED, Build.VERSION.SDK_INT)
.apply() }
} }
} }
} else { } else {
@ -188,10 +189,10 @@ object CompatUtil {
cipher.init(Cipher.WRAP_MODE, keyPair.public) cipher.init(Cipher.WRAP_MODE, keyPair.public)
val wrappedAesKey = cipher.wrap(key) val wrappedAesKey = cipher.wrap(key)
sharedPreferences.edit() sharedPreferences.edit {
.putString(AES_WRAPPED_PROTECTION_KEY_SHARED_PREFERENCE, Base64.encodeToString(wrappedAesKey, 0)) putString(AES_WRAPPED_PROTECTION_KEY_SHARED_PREFERENCE, Base64.encodeToString(wrappedAesKey, 0))
.putInt(SHARED_KEY_ANDROID_VERSION_WHEN_KEY_HAS_BEEN_GENERATED, Build.VERSION.SDK_INT) putInt(SHARED_KEY_ANDROID_VERSION_WHEN_KEY_HAS_BEEN_GENERATED, Build.VERSION.SDK_INT)
.apply() }
} }
} }

View File

@ -21,6 +21,7 @@ import android.app.Activity
import android.content.Context import android.content.Context
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import android.widget.Toast import android.widget.Toast
import androidx.core.content.edit
import com.google.android.gms.common.ConnectionResult import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability import com.google.android.gms.common.GoogleApiAvailability
import com.google.firebase.iid.FirebaseInstanceId import com.google.firebase.iid.FirebaseInstanceId
@ -57,10 +58,9 @@ object FcmHelper {
*/ */
fun storeFcmToken(context: Context, fun storeFcmToken(context: Context,
token: String?) { token: String?) {
PreferenceManager.getDefaultSharedPreferences(context) PreferenceManager.getDefaultSharedPreferences(context).edit {
.edit() putString(PREFS_KEY_FCM_TOKEN, token)
.putString(PREFS_KEY_FCM_TOKEN, token) }
.apply()
} }
/** /**