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)
Other changes:
- Use `SharedPreferences#edit` extension function consistently (#1545)
- Use `retrofit2.Call.awaitResponse` extension provided by Retrofit 2. (#1526)
- Fix minor typo in contribution guide (#1512)
- 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 "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-common-java8:$lifecycle_version"

View File

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

View File

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

View File

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

View File

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