Skip to content

Commit

Permalink
wip| #2656
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jun 27, 2024
1 parent fa28437 commit 03ca73e
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 100 deletions.
1 change: 1 addition & 0 deletions FlowCrypt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ dependencies {
implementation("androidx.navigation:navigation-ui-ktx:2.7.7")
implementation("androidx.navigation:navigation-runtime-ktx:2.7.7")
implementation("androidx.webkit:webkit:1.11.0")
implementation("androidx.datastore:datastore-preferences:1.1.1")

implementation("com.google.android.gms:play-services-base:18.5.0")
implementation("com.google.android.gms:play-services-auth:21.2.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ object TestGeneralUtil {
}

fun clearApp(context: Context) {
SharedPreferencesHelper.clear(context)
runBlocking {
context.dataStore.edit { preferences ->
preferences.clear()
}
}
FileAndDirectoryUtils.cleanDir(context.cacheDir)
FileAndDirectoryUtils.cleanDir(File(context.filesDir, MsgsCacheManager.CACHE_DIR_NAME))
FlowCryptRoomDatabase.getDatabase(context).forceDatabaseCreationIfNeeded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ object FlavorSettings : EnvironmentSettings {

private fun configureLeakCanary(context: Context) {
if (GeneralUtil.isDebugBuild()) {
val isLeakCanaryEnabled = SharedPreferencesHelper.getBoolean(
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context),
key = Constants.PREF_KEY_IS_DETECT_MEMORY_LEAK_ENABLED,
val isLeakCanaryEnabled = SharedPreferencesHelper.getValue(
context,
PreferencesKeys.KEY_IS_DETECT_MEMORY_LEAK_ENABLED,
defaultValue = false
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.flowcrypt.email.service.PassPhrasesInRAMService
import com.flowcrypt.email.ui.notifications.NotificationChannelManager
import com.flowcrypt.email.util.FlavorSettings
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.PreferencesKeys
import com.flowcrypt.email.util.SharedPreferencesHelper
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -129,9 +130,9 @@ class FlowCryptApplication : Application(), Configuration.Provider {

private fun initACRA() {
if (GeneralUtil.isDebugBuild()) {
val isAcraEnabled = SharedPreferencesHelper.getBoolean(
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this),
key = Constants.PREF_KEY_IS_ACRA_ENABLED,
val isAcraEnabled = SharedPreferencesHelper.getValue(
context = this,
key = PreferencesKeys.KEY_IS_ACRA_ENABLED,
defaultValue = BuildConfig.IS_ACRA_ENABLED
)
if (isAcraEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.provider.OpenableColumns
import android.text.TextUtils
import android.util.SparseArray
import androidx.annotation.WorkerThread
import androidx.preference.PreferenceManager
import com.flowcrypt.email.BuildConfig
import com.flowcrypt.email.Constants
import com.flowcrypt.email.R
Expand All @@ -39,6 +38,7 @@ import com.flowcrypt.email.security.pgp.PgpDecryptAndOrVerify
import com.flowcrypt.email.security.pgp.PgpEncryptAndOrSign
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.OutgoingMessagesManager
import com.flowcrypt.email.util.PreferencesKeys
import com.flowcrypt.email.util.SharedPreferencesHelper
import com.flowcrypt.email.util.exception.ExceptionUtil
import com.google.android.gms.auth.GoogleAuthException
Expand Down Expand Up @@ -329,10 +329,12 @@ class EmailUtil {
* @return true if debug enable, false - otherwise.
*/
fun hasEnabledDebug(context: Context): Boolean {
return GeneralUtil.isDebugBuild() && SharedPreferencesHelper.getBoolean(
PreferenceManager.getDefaultSharedPreferences(context.applicationContext),
Constants.PREF_KEY_IS_MAIL_DEBUG_ENABLED, BuildConfig.IS_MAIL_DEBUG_ENABLED
)
return GeneralUtil.isDebugBuild() &&
SharedPreferencesHelper.getValue(
context = context.applicationContext,
key = PreferencesKeys.KEY_IS_MAIL_DEBUG_ENABLED,
defaultValue = BuildConfig.IS_MAIL_DEBUG_ENABLED
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.flowcrypt.email.Constants
import com.flowcrypt.email.api.retrofit.okhttp.ApiVersionInterceptor
import com.flowcrypt.email.api.retrofit.okhttp.LoggingInFileInterceptor
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.PreferencesKeys
import com.flowcrypt.email.util.SharedPreferencesHelper
import com.flowcrypt.email.util.google.gson.ByteArrayJsonSerializerDeserializer
import com.flowcrypt.email.util.google.gson.CharArrayJsonSerializerDeserializer
Expand Down Expand Up @@ -84,10 +85,10 @@ class ApiHelper private constructor(context: Context) {
context: Context, okHttpClientBuilder: OkHttpClient.Builder
) {
if (GeneralUtil.isDebugBuild()) {
val isHttpLogEnabled =
SharedPreferencesHelper.getBoolean(
PreferenceManager.getDefaultSharedPreferences(context),
Constants.PREF_KEY_IS_HTTP_LOG_ENABLED, BuildConfig.IS_HTTP_LOG_ENABLED
val isHttpLogEnabled = SharedPreferencesHelper.getValue(
context = context.applicationContext,
key = PreferencesKeys.KEY_IS_HTTP_LOG_ENABLED,
defaultValue = BuildConfig.IS_HTTP_LOG_ENABLED
)

if (isHttpLogEnabled) {
Expand All @@ -98,10 +99,10 @@ class ApiHelper private constructor(context: Context) {
BuildConfig.HTTP_LOG_LEVEL
)

val isWriteLogsEnabled =
SharedPreferencesHelper.getBoolean(
PreferenceManager.getDefaultSharedPreferences(context),
Constants.PREF_KEY_IS_WRITE_LOGS_TO_FILE_ENABLED, false
val isWriteLogsEnabled = SharedPreferencesHelper.getValue(
context = context.applicationContext,
key = PreferencesKeys.KEY_IS_WRITE_LOGS_TO_FILE_ENABLED,
defaultValue = false
)

if (isWriteLogsEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ package com.flowcrypt.email.broadcastreceivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.preference.PreferenceManager
import com.flowcrypt.email.Constants
import com.flowcrypt.email.util.PreferencesKeys
import com.flowcrypt.email.util.SharedPreferencesHelper

/**
Expand All @@ -21,10 +20,7 @@ class AppUpdateBroadcastReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent?) {
if (intent != null && Intent.ACTION_MY_PACKAGE_REPLACED == intent.action) {
SharedPreferencesHelper.setBoolean(
PreferenceManager
.getDefaultSharedPreferences(context), Constants.PREF_KEY_IS_CHECK_KEYS_NEEDED, true
)
SharedPreferencesHelper.setValue(context, PreferencesKeys.KEY_IS_CHECK_KEYS_NEEDED, true)
}
}
}
12 changes: 12 additions & 0 deletions FlowCrypt/src/main/java/com/flowcrypt/email/extensions/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@ import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.widget.Toast
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.SharedPreferencesMigration
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.flowcrypt.email.BuildConfig

/**
* @author Denys Bondarenko
*/

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
name = BuildConfig.APPLICATION_ID + "_preferences",
produceMigrations = { context ->
listOf(SharedPreferencesMigration(context, BuildConfig.APPLICATION_ID + "_preferences"))
})

fun Context.toast(text: String?, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, text ?: "", duration).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
package com.flowcrypt.email.service.actionqueue.actions

import android.content.Context
import androidx.preference.PreferenceManager
import com.flowcrypt.email.Constants
import androidx.datastore.preferences.core.edit
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.extensions.dataStore
import com.flowcrypt.email.security.KeyStoreCryptoManager
import com.flowcrypt.email.security.KeysStorageImpl
import com.flowcrypt.email.security.pgp.PgpKey
import com.flowcrypt.email.security.pgp.PgpPwd
import com.flowcrypt.email.ui.notifications.SystemNotificationManager
import com.flowcrypt.email.util.SharedPreferencesHelper
import com.flowcrypt.email.util.PreferencesKeys
import com.flowcrypt.email.util.exception.ExceptionUtil
import com.flowcrypt.email.util.exception.PrivateKeyStrengthException
import com.google.android.gms.common.util.CollectionUtils
Expand Down Expand Up @@ -100,10 +100,8 @@ data class EncryptPrivateKeysIfNeededAction @JvmOverloads constructor(
}

roomDatabase.keysDao().update(modifiedKeyEntities)

SharedPreferencesHelper.setBoolean(
PreferenceManager
.getDefaultSharedPreferences(context), Constants.PREF_KEY_IS_CHECK_KEYS_NEEDED, false
)
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.KEY_IS_CHECK_KEYS_NEEDED] = false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.flowcrypt.email.ui.activity.fragment.base.BaseSingInFragment
import com.flowcrypt.email.ui.activity.fragment.dialog.TwoWayDialogFragment
import com.flowcrypt.email.ui.widget.inputfilters.InputFilters
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.PreferencesKeys
import com.flowcrypt.email.util.SharedPreferencesHelper
import com.flowcrypt.email.util.exception.AccountAlreadyAddedException
import com.flowcrypt.email.util.exception.ExceptionUtil
Expand Down Expand Up @@ -628,9 +629,10 @@ class AddOtherAccountFragment : BaseSingInFragment<FragmentAddOtherAccountBindin
authCreds?.let { if (it.useOAuth2) return }

val authCreds = generateAuthCreds().copy(password = "", smtpSignInPassword = "")
SharedPreferencesHelper.setString(
PreferenceManager.getDefaultSharedPreferences(requireContext()),
Constants.PREF_KEY_TEMP_LAST_AUTH_CREDENTIALS, Gson().toJson(authCreds)
SharedPreferencesHelper.setValue(
requireContext(),
PreferencesKeys.KEY_TEMP_LAST_AUTH_CREDENTIALS,
Gson().toJson(authCreds)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceDataStore
import androidx.preference.PreferenceManager
import com.flowcrypt.email.BuildConfig
import com.flowcrypt.email.Constants
Expand Down Expand Up @@ -130,6 +131,10 @@ open class NotificationsSettingsFragment : BasePreferenceFragment(),
filter.entries = entries
filter.onPreferenceChangeListener = this

preferenceManager.preferenceDataStore = object : PreferenceDataStore() {

}

var currentValue = SharedPreferencesHelper.getString(
PreferenceManager.getDefaultSharedPreferences(
requireContext()
Expand Down
13 changes: 6 additions & 7 deletions FlowCrypt/src/main/java/com/flowcrypt/email/util/GeneralUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import android.webkit.MimeTypeMap
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import com.flowcrypt.email.BuildConfig
import com.flowcrypt.email.Constants
import com.flowcrypt.email.R
Expand Down Expand Up @@ -350,13 +349,13 @@ class GeneralUtil {
* @return The generated order number.
*/
fun genAttOrderId(context: Context): Int {
return SharedPreferencesHelper.getInt(
PreferenceManager.getDefaultSharedPreferences(context),
Constants.PREF_KEY_LAST_ATT_ORDER_ID, 0
return SharedPreferencesHelper.getValue(
context,
PreferencesKeys.KEY_LAST_ATT_ORDER_ID, 0
).inc().apply {
SharedPreferencesHelper.setInt(
PreferenceManager.getDefaultSharedPreferences(context),
Constants.PREF_KEY_LAST_ATT_ORDER_ID, this
SharedPreferencesHelper.setValue(
context,
PreferencesKeys.KEY_LAST_ATT_ORDER_ID, this
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected]
* Contributors: denbond7
*/

package com.flowcrypt.email.util

import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import com.flowcrypt.email.Constants

/**
* @author Denys Bondarenko
*/
object PreferencesKeys {
val KEY_LAST_ATT_ORDER_ID = intPreferencesKey(Constants.PREF_KEY_LAST_ATT_ORDER_ID)
val KEY_IS_CHECK_KEYS_NEEDED = booleanPreferencesKey(Constants.PREF_KEY_IS_CHECK_KEYS_NEEDED)
val KEY_IS_DETECT_MEMORY_LEAK_ENABLED =
booleanPreferencesKey(Constants.PREF_KEY_IS_DETECT_MEMORY_LEAK_ENABLED)
val KEY_IS_ACRA_ENABLED = booleanPreferencesKey(Constants.PREF_KEY_IS_ACRA_ENABLED)
val KEY_IS_MAIL_DEBUG_ENABLED = booleanPreferencesKey(Constants.PREF_KEY_IS_MAIL_DEBUG_ENABLED)
val KEY_IS_HTTP_LOG_ENABLED = booleanPreferencesKey(Constants.PREF_KEY_IS_HTTP_LOG_ENABLED)
val KEY_IS_WRITE_LOGS_TO_FILE_ENABLED =
booleanPreferencesKey(Constants.PREF_KEY_IS_WRITE_LOGS_TO_FILE_ENABLED)
val KEY_TEMP_LAST_AUTH_CREDENTIALS =
stringPreferencesKey(Constants.PREF_KEY_TEMP_LAST_AUTH_CREDENTIALS)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ package com.flowcrypt.email.util

import android.content.Context
import android.content.SharedPreferences

import androidx.preference.PreferenceManager
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import com.flowcrypt.email.extensions.dataStore
import kotlinx.coroutines.flow.lastOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking

/**
* @author Denys Bondarenko
Expand All @@ -23,62 +27,21 @@ class SharedPreferencesHelper {
return sharedPreferences.getString(key, defaultValue)
}

fun getBoolean(
sharedPreferences: SharedPreferences,
key: String,
defaultValue: Boolean
): Boolean {
return sharedPreferences.getBoolean(key, defaultValue)
}

fun setBoolean(sharedPreferences: SharedPreferences, key: String, value: Boolean): Boolean {
val editor = sharedPreferences.edit()
editor.putBoolean(key, value)
return editor.commit()
}

fun getStringSet(
sharedPreferences: SharedPreferences,
key: String,
defValues: Set<String>
): Set<String>? {
return sharedPreferences.getStringSet(key, defValues)
}

fun getLong(sharedPreferences: SharedPreferences, key: String, defaultValue: Long): Long {
return sharedPreferences.getLong(key, defaultValue)
}

fun setLong(sharedPreferences: SharedPreferences, key: String, value: Long): Boolean {
val editor = sharedPreferences.edit()
editor.putLong(key, value)
return editor.commit()
}

fun setString(sharedPreferences: SharedPreferences, key: String, value: String): Boolean {
val editor = sharedPreferences.edit()
editor.putString(key, value)
return editor.commit()
}

fun getInt(sharedPreferences: SharedPreferences, key: String, defaultValue: Int): Int {
return sharedPreferences.getInt(key, defaultValue)
}

fun setInt(sharedPreferences: SharedPreferences, key: String, value: Int): Boolean {
val editor = sharedPreferences.edit()
editor.putInt(key, value)
return editor.commit()
fun <T> getValue(context: Context, key: Preferences.Key<T>, defaultValue: T): T {
return runBlocking {
context.dataStore.data
.map { preferences ->
preferences[key] ?: defaultValue
}.lastOrNull() ?: defaultValue
}
}

/**
* Clear the all shared preferences.
*
* @param context Interface to global information about an application environment.
* @return Returns true if the new values were successfully written to persistent storage.
*/
fun clear(context: Context): Boolean {
return PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit()
fun <T> setValue(context: Context, key: Preferences.Key<T>, value: T): Boolean {
return runBlocking {
context.dataStore.edit { preferences ->
preferences[key] = value
}
}.contains(key)
}
}
}

0 comments on commit 03ca73e

Please sign in to comment.