Merge pull request #100 from yogeshpaliyal/feature/datastore

Remove sharedpreference causing crash
This commit is contained in:
Yogesh Choudhary Paliyal
2022-02-07 09:29:27 +05:30
committed by GitHub
8 changed files with 4 additions and 107 deletions

View File

@@ -20,8 +20,8 @@ android {
applicationId appPackageId
minSdkVersion 22
targetSdkVersion 31
versionCode 1405
versionName "1.4.5"
versionCode 1406
versionName "1.4.6"
testInstrumentationRunner "com.yogeshpaliyal.keypass.CustomTestRunner"
}

View File

@@ -17,7 +17,6 @@ import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import com.google.android.material.transition.MaterialElevationScale
import com.yogeshpaliyal.common.worker.migrateToDatastore
import com.yogeshpaliyal.keypass.R
import com.yogeshpaliyal.keypass.databinding.ActivityDashboardBinding
import com.yogeshpaliyal.keypass.ui.addTOTP.AddTOTPActivity
@@ -120,8 +119,6 @@ class DashboardActivity :
}
setOnMenuItemClickListener(this@DashboardActivity)
}
migrateToDatastore()
}
override fun onMenuItemClick(item: MenuItem?): Boolean {

View File

@@ -1,13 +1,11 @@
package com.yogeshpaliyal.common.di.module
import android.content.Context
import android.content.SharedPreferences
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.yogeshpaliyal.common.AppDatabase
import com.yogeshpaliyal.common.R
import com.yogeshpaliyal.common.utils.MySharedPreferences
import com.yogeshpaliyal.common.utils.getRandomString
import dagger.Module
import dagger.Provides
@@ -45,10 +43,4 @@ object AppModule {
})
.build()
}
@Provides
@Singleton
fun getSharedPre(@ApplicationContext context: Context): SharedPreferences {
return MySharedPreferences(context).sharedPref
}
}

View File

@@ -1,36 +0,0 @@
package com.yogeshpaliyal.common.utils
import android.content.Context
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
class MySharedPreferences(context: Context) {
private val masterKeyAlias = MasterKey.Builder(context).also {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
// this is equivalent to using deprecated MasterKeys.AES256_GCM_SPEC
// this is equivalent to using deprecated MasterKeys.AES256_GCM_SPEC
val spec = KeyGenParameterSpec.Builder(
MasterKey.DEFAULT_MASTER_KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(256)
.build()
it.setKeyGenParameterSpec(spec)
}
// it.setUserAuthenticationRequired(true)
}
.build()
val sharedPref = EncryptedSharedPreferences.create(
context,
"secret_shared_prefs",
masterKeyAlias,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}

View File

@@ -16,11 +16,3 @@ suspend fun Context?.executeAutoBackup() {
.enqueueUniqueWork("AutoBackupWorker", ExistingWorkPolicy.KEEP, work)
}
}
fun Context?.migrateToDatastore() {
this ?: return
val work = OneTimeWorkRequestBuilder<SharedPrefMigrationWorker>().build()
WorkManager.getInstance(this.applicationContext)
.enqueueUniqueWork("SharedPrefMigrationWorker", ExistingWorkPolicy.KEEP, work)
}

View File

@@ -1,49 +0,0 @@
package com.yogeshpaliyal.common.worker
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.datastore.preferences.core.*
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.yogeshpaliyal.common.utils.dataStore
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.lang.Exception
@HiltWorker
class SharedPrefMigrationWorker @AssistedInject constructor(
@Assisted val context: Context,
@Assisted workerParams: WorkerParameters,
private val sharedPreferences: SharedPreferences
) :
CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
try {
withContext(Dispatchers.IO) {
for (mutableEntry in sharedPreferences.all) {
context.dataStore.edit { editor ->
val value = mutableEntry.value
when (value) {
is String -> editor[stringPreferencesKey(mutableEntry.key)] = value
is Boolean -> editor[booleanPreferencesKey(mutableEntry.key)] = value
is Long -> editor[longPreferencesKey(mutableEntry.key)] = value
is Int -> editor[intPreferencesKey(mutableEntry.key)] = value
is Double -> editor[doublePreferencesKey(mutableEntry.key)] = value
is Float -> editor[floatPreferencesKey(mutableEntry.key)] = value
}
sharedPreferences.edit {
remove(mutableEntry.key)
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return Result.success()
}
}

View File

@@ -0,0 +1 @@
Bug Fix and improvements

View File

@@ -1 +1 @@
Bug Fix and improvements
Crash Fix and improvements