|
|
|
@ -3,15 +3,18 @@ package org.tasks.preferences.fragments
|
|
|
|
import android.app.Activity.RESULT_OK
|
|
|
|
import android.app.Activity.RESULT_OK
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Bundle
|
|
|
|
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.SwitchPreferenceCompat
|
|
|
|
import androidx.preference.SwitchPreferenceCompat
|
|
|
|
import com.google.api.services.drive.DriveScopes
|
|
|
|
import com.google.api.services.drive.DriveScopes
|
|
|
|
import com.todoroo.andlib.utility.DateUtilities
|
|
|
|
import com.todoroo.andlib.utility.DateUtilities
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import org.tasks.PermissionUtil
|
|
|
|
import org.tasks.PermissionUtil
|
|
|
|
import org.tasks.R
|
|
|
|
import org.tasks.R
|
|
|
|
import org.tasks.dialogs.ExportTasksDialog
|
|
|
|
import org.tasks.dialogs.ExportTasksDialog
|
|
|
|
import org.tasks.dialogs.ImportTasksDialog
|
|
|
|
import org.tasks.dialogs.ImportTasksDialog
|
|
|
|
|
|
|
|
import org.tasks.drive.DriveInvoker
|
|
|
|
import org.tasks.drive.DriveLoginActivity
|
|
|
|
import org.tasks.drive.DriveLoginActivity
|
|
|
|
import org.tasks.files.FileHelper
|
|
|
|
import org.tasks.files.FileHelper
|
|
|
|
import org.tasks.gtasks.GoogleAccountManager
|
|
|
|
import org.tasks.gtasks.GoogleAccountManager
|
|
|
|
@ -37,6 +40,7 @@ class Backups : InjectingPreferenceFragment() {
|
|
|
|
@Inject lateinit var toaster: Toaster
|
|
|
|
@Inject lateinit var toaster: Toaster
|
|
|
|
@Inject lateinit var googleAccountManager: GoogleAccountManager
|
|
|
|
@Inject lateinit var googleAccountManager: GoogleAccountManager
|
|
|
|
@Inject lateinit var locale: Locale
|
|
|
|
@Inject lateinit var locale: Locale
|
|
|
|
|
|
|
|
@Inject lateinit var driveInvoker: DriveInvoker
|
|
|
|
|
|
|
|
|
|
|
|
override fun getPreferenceXml() = R.xml.preferences_backups
|
|
|
|
override fun getPreferenceXml() = R.xml.preferences_backups
|
|
|
|
|
|
|
|
|
|
|
|
@ -58,12 +62,36 @@ class Backups : InjectingPreferenceFragment() {
|
|
|
|
.show(parentFragmentManager, FRAG_TAG_EXPORT_TASKS)
|
|
|
|
.show(parentFragmentManager, FRAG_TAG_EXPORT_TASKS)
|
|
|
|
false
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
findPreference(R.string.google_drive_backup)
|
|
|
|
|
|
|
|
.setOnPreferenceChangeListener(this@Backups::onGoogleDriveCheckChanged)
|
|
|
|
|
|
|
|
findPreference(R.string.p_google_drive_backup_account)
|
|
|
|
|
|
|
|
.setOnPreferenceClickListener {
|
|
|
|
|
|
|
|
requestGoogleDriveLogin()
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
super.onResume()
|
|
|
|
|
|
|
|
|
|
|
|
updateGoogleDriveCheckbox()
|
|
|
|
updateDriveAccount()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val driveBackup = findPreference(R.string.google_drive_backup) as SwitchPreferenceCompat
|
|
|
|
|
|
|
|
driveBackup.isChecked = driveAccount != null
|
|
|
|
|
|
|
|
if (driveAccount != null) {
|
|
|
|
|
|
|
|
lifecycleScope.launch {
|
|
|
|
|
|
|
|
val folder = preferences.getStringValue(R.string.p_google_drive_backup_folder)
|
|
|
|
|
|
|
|
val files = driveInvoker.getFilesByPrefix(folder, "auto.", "user.")
|
|
|
|
|
|
|
|
driveBackup.summary = getString(
|
|
|
|
|
|
|
|
R.string.last_backup,
|
|
|
|
|
|
|
|
if (files.isEmpty()) {
|
|
|
|
|
|
|
|
getString(R.string.last_backup_never)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
DateUtilities.getLongDateStringWithTime(files[0].modifiedTime.value, locale)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val lastBackup = preferences.getLong(R.string.p_backups_android_backup_last, 0L)
|
|
|
|
val lastBackup = preferences.getLong(R.string.p_backups_android_backup_last, 0L)
|
|
|
|
findPreference(R.string.p_backups_android_backup_enabled).summary =
|
|
|
|
findPreference(R.string.p_backups_android_backup_enabled).summary =
|
|
|
|
@ -123,35 +151,46 @@ class Backups : InjectingPreferenceFragment() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun updateGoogleDriveCheckbox() {
|
|
|
|
private val driveAccount: String?
|
|
|
|
val account = preferences.getStringValue(R.string.p_google_drive_backup_account)
|
|
|
|
get() {
|
|
|
|
val pref = findPreference(R.string.google_drive_backup) as SwitchPreferenceCompat
|
|
|
|
val account = preferences.getStringValue(R.string.p_google_drive_backup_account)
|
|
|
|
pref.isChecked = preferences.getBoolean(R.string.p_google_drive_backup, false)
|
|
|
|
val enabled = !account.isNullOrBlank()
|
|
|
|
&& googleAccountManager.canAccessAccount(account)
|
|
|
|
&& preferences.getBoolean(R.string.p_google_drive_backup, false)
|
|
|
|
&& !preferences.alreadyNotified(account, DriveScopes.DRIVE_FILE)
|
|
|
|
&& googleAccountManager.canAccessAccount(account)
|
|
|
|
pref.summary = if (pref.isChecked) account else null
|
|
|
|
&& !preferences.alreadyNotified(account, DriveScopes.DRIVE_FILE)
|
|
|
|
findPreference(R.string.google_drive_backup)
|
|
|
|
return if (enabled) account else null
|
|
|
|
.setOnPreferenceChangeListener(this@Backups::onGoogleDriveCheckChanged)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun onGoogleDriveCheckChanged(preference: Preference, newValue: Any?) = when {
|
|
|
|
private fun onGoogleDriveCheckChanged(preference: Preference, newValue: Any?) = when {
|
|
|
|
newValue as Boolean -> {
|
|
|
|
newValue as Boolean -> {
|
|
|
|
if (permissionRequestor.requestAccountPermissions()) {
|
|
|
|
requestGoogleDriveLogin()
|
|
|
|
requestGoogleDriveLogin()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
else -> {
|
|
|
|
preference.summary = null
|
|
|
|
preference.summary = null
|
|
|
|
|
|
|
|
preferences.setString(R.string.p_google_drive_backup_account, null)
|
|
|
|
|
|
|
|
updateDriveAccount()
|
|
|
|
true
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun updateDriveAccount() {
|
|
|
|
|
|
|
|
val account = driveAccount
|
|
|
|
|
|
|
|
val pref = findPreference(R.string.p_google_drive_backup_account)
|
|
|
|
|
|
|
|
pref.isEnabled = account != null
|
|
|
|
|
|
|
|
pref.summary =
|
|
|
|
|
|
|
|
account
|
|
|
|
|
|
|
|
?.takeIf { it.isNotBlank() }
|
|
|
|
|
|
|
|
?: getString(R.string.none)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun requestGoogleDriveLogin() {
|
|
|
|
private fun requestGoogleDriveLogin() {
|
|
|
|
startActivityForResult(
|
|
|
|
if (permissionRequestor.requestAccountPermissions()) {
|
|
|
|
Intent(context, DriveLoginActivity::class.java),
|
|
|
|
startActivityForResult(
|
|
|
|
REQUEST_DRIVE_BACKUP
|
|
|
|
Intent(context, DriveLoginActivity::class.java),
|
|
|
|
)
|
|
|
|
REQUEST_DRIVE_BACKUP
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun initializeBackupDirectory() {
|
|
|
|
private fun initializeBackupDirectory() {
|
|
|
|
|