diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/MyApplication.kt b/app/src/main/java/com/yogeshpaliyal/keypass/MyApplication.kt index e0d31c77..99fad804 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/MyApplication.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/MyApplication.kt @@ -13,7 +13,7 @@ import javax.inject.Inject * created on 22-01-2021 22:41 */ @HiltAndroidApp -class MyApplication : Application(), Configuration.Provider { +class MyApplication : Application(), Configuration.Provider { companion object { lateinit var instance: MyApplication @@ -21,7 +21,6 @@ class MyApplication : Application(), Configuration.Provider { @Inject lateinit var workerFactory: HiltWorkerFactory - override fun onCreate() { super.onCreate() instance = this @@ -33,5 +32,4 @@ class MyApplication : Application(), Configuration.Provider { .setWorkerFactory(workerFactory) .build() } - } diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/ui/backup/BackupActivity.kt b/app/src/main/java/com/yogeshpaliyal/keypass/ui/backup/BackupActivity.kt index cb725907..3654a100 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/ui/backup/BackupActivity.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/ui/backup/BackupActivity.kt @@ -7,7 +7,6 @@ import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat -import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import androidx.preference.PreferenceCategory @@ -17,7 +16,6 @@ import com.yogeshpaliyal.keypass.AppDatabase import com.yogeshpaliyal.keypass.R import com.yogeshpaliyal.keypass.databinding.BackupActivityBinding import com.yogeshpaliyal.keypass.databinding.LayoutBackupKeypharseBinding -import com.yogeshpaliyal.keypass.db_helper.createBackup import com.yogeshpaliyal.keypass.utils.* import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch @@ -81,7 +79,6 @@ class BackupActivity : AppCompatActivity() { if (it.canUserAccessBackupDirectory(sp)) { val selectedDirectory = Uri.parse(getBackupDirectory(sp)) backup(selectedDirectory) - } } } @@ -110,9 +107,9 @@ class BackupActivity : AppCompatActivity() { return super.onPreferenceTreeClick(preference) } - fun backup(selectedDirectory: Uri){ + fun backup(selectedDirectory: Uri) { lifecycleScope.launch { - context.backupAccounts(sp, appDb,selectedDirectory)?.let { keyPair -> + context.backupAccounts(sp, appDb, selectedDirectory)?.let { keyPair -> if (keyPair.first) { val binding = LayoutBackupKeypharseBinding.inflate(layoutInflater) binding.txtCode.text = getOrCreateBackupKey(sp).second @@ -149,11 +146,11 @@ class BackupActivity : AppCompatActivity() { findPreference(getString(R.string.settings_stop_backup))?.isVisible = isBackupEnabled findPreference(getString(R.string.settings_auto_backup))?.isVisible = isBackupEnabled - findPreference(getString(R.string.settings_auto_backup))?.summary = if(isAutoBackupEnabled) getString(R.string.enabled) else getString(R.string.disabled) + findPreference(getString(R.string.settings_auto_backup))?.summary = if (isAutoBackupEnabled) getString(R.string.enabled) else getString(R.string.disabled) findPreference(getString(R.string.settings_cat_auto_backup))?.isVisible = isBackupEnabled && isAutoBackupEnabled - findPreference(getString(R.string.settings_override_auto_backup))?.summary = if(sp.overrideAutoBackup()) getString(R.string.enabled) else getString(R.string.disabled) + findPreference(getString(R.string.settings_override_auto_backup))?.summary = if (sp.overrideAutoBackup()) getString(R.string.enabled) else getString(R.string.disabled) findPreference(getString(R.string.settings_create_backup))?.isVisible = isBackupEnabled findPreference(getString(R.string.settings_create_backup))?.summary = getString(R.string.last_backup_date, getBackupTime(sp).formatCalendar("dd MMM yyyy hh:mm aa")) @@ -198,8 +195,6 @@ class BackupActivity : AppCompatActivity() { } } - - private fun changeBackupFolder() { startBackup() } @@ -212,7 +207,7 @@ class BackupActivity : AppCompatActivity() { clearBackupKey(sp) setBackupDirectory(sp, "") setBackupTime(sp, -1) - sp.apply { + sp.apply { setOverrideAutoBackup(false) setAutoBackupEnabled(false) } diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/ui/detail/DetailViewModel.kt b/app/src/main/java/com/yogeshpaliyal/keypass/ui/detail/DetailViewModel.kt index 8134b203..22f8233e 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/ui/detail/DetailViewModel.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/ui/detail/DetailViewModel.kt @@ -22,10 +22,10 @@ import javax.inject.Inject * created on 31-01-2021 11:52 */ @HiltViewModel -class DetailViewModel @Inject constructor(val app: Application, val appDb: AppDatabase,val sp: SharedPreferences) : AndroidViewModel(app) { +class DetailViewModel @Inject constructor(val app: Application, val appDb: AppDatabase, val sp: SharedPreferences) : AndroidViewModel(app) { private val _accountModel by lazy { MutableLiveData() } - val accountModel : LiveData = _accountModel + val accountModel: LiveData = _accountModel fun loadAccount(accountId: Long?) { viewModelScope.launch(Dispatchers.IO) { @@ -35,7 +35,7 @@ class DetailViewModel @Inject constructor(val app: Application, val appDb: AppDa } } - fun deleteAccount(onExecCompleted : ()->Unit){ + fun deleteAccount(onExecCompleted: () -> Unit) { viewModelScope.launch { accountModel.value?.let { withContext(Dispatchers.IO) { @@ -47,7 +47,7 @@ class DetailViewModel @Inject constructor(val app: Application, val appDb: AppDa } } - fun insertOrUpdate(onExecCompleted : ()->Unit){ + fun insertOrUpdate(onExecCompleted: () -> Unit) { viewModelScope.launch { accountModel.value?.let { withContext(Dispatchers.IO) { @@ -59,7 +59,7 @@ class DetailViewModel @Inject constructor(val app: Application, val appDb: AppDa } } - private fun autoBackup(){ + private fun autoBackup() { app.executeAutoBackup(sp) } } diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/utils/BackupUtils.kt b/app/src/main/java/com/yogeshpaliyal/keypass/utils/BackupUtils.kt index e3f9e539..2b4fa33a 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/utils/BackupUtils.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/utils/BackupUtils.kt @@ -23,9 +23,9 @@ fun getRandomString(sizeOfRandomString: Int): String { val sb = StringBuilder(sizeOfRandomString) for (i in 0 until sizeOfRandomString) sb.append( ALLOWED_CHARACTERS[ - random.nextInt( - ALLOWED_CHARACTERS.length - ) + random.nextInt( + ALLOWED_CHARACTERS.length + ) ] ) return sb.toString() @@ -38,7 +38,6 @@ fun Context?.canUserAccessBackupDirectory(sp: SharedPreferences): Boolean { return backupDirectory != null && backupDirectory.exists() && backupDirectory.canRead() && backupDirectory.canWrite() } - /** * @return Pair (Boolean to check if backup is for first time, is backup is for first time show user alert to save encryption key) * Second Value contains the encryption key @@ -46,23 +45,23 @@ fun Context?.canUserAccessBackupDirectory(sp: SharedPreferences): Boolean { suspend fun Context?.backupAccounts( sp: SharedPreferences, appDb: AppDatabase, - selectedDirectory: Uri, fileName: String? = null + selectedDirectory: Uri, + fileName: String? = null ): Pair? { this ?: return null val keyPair = getOrCreateBackupKey(sp) - val fileName = (fileName ?: "key_pass_backup_${System.currentTimeMillis()}")+".keypass" - + val fileName = (fileName ?: "key_pass_backup_${System.currentTimeMillis()}") + ".keypass" val directory = DocumentFile.fromTreeUri(this, selectedDirectory) var docFile = directory?.findFile(fileName) if (docFile == null) docFile = DocumentFile.fromTreeUri(this, selectedDirectory)?.createFile( - "*/*", - fileName) - + "*/*", + fileName + ) val response = appDb.createBackup( keyPair.second, diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/worker/AutoBackupWorker.kt b/app/src/main/java/com/yogeshpaliyal/keypass/worker/AutoBackupWorker.kt index e3df6674..ea83e1cb 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/worker/AutoBackupWorker.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/worker/AutoBackupWorker.kt @@ -18,7 +18,8 @@ import kotlinx.coroutines.withContext @HiltWorker class AutoBackupWorker @AssistedInject constructor( - @Assisted val appContext: Context, @Assisted params: WorkerParameters, + @Assisted val appContext: Context, + @Assisted params: WorkerParameters, val appDatabase: AppDatabase, val sp: SharedPreferences ) : @@ -39,4 +40,4 @@ class AutoBackupWorker @AssistedInject constructor( Result.success() } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/worker/MyWorkManager.kt b/app/src/main/java/com/yogeshpaliyal/keypass/worker/MyWorkManager.kt index 190ca623..551adf8f 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/worker/MyWorkManager.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/worker/MyWorkManager.kt @@ -7,7 +7,7 @@ import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager import com.yogeshpaliyal.keypass.utils.isAutoBackupEnabled -fun Context?.executeAutoBackup(sp: SharedPreferences){ +fun Context?.executeAutoBackup(sp: SharedPreferences) { this ?: return if (sp.isAutoBackupEnabled()) { @@ -16,4 +16,4 @@ fun Context?.executeAutoBackup(sp: SharedPreferences){ WorkManager.getInstance(this.applicationContext) .enqueueUniqueWork("AutoBackupWorker", ExistingWorkPolicy.KEEP, work) } -} \ No newline at end of file +}