mirror of
https://github.com/bitfireAT/davx5-ose.git
synced 2025-12-23 23:17:50 -05:00
Add FAB to cancel sync adapter syncs
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package at.bitfire.davdroid.ui
|
||||
|
||||
import android.accounts.Account
|
||||
import android.accounts.AccountManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
@@ -13,6 +14,8 @@ import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkRequest
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.PowerManager
|
||||
import android.provider.CalendarContract
|
||||
import android.provider.ContactsContract
|
||||
@@ -22,6 +25,7 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.work.WorkInfo
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.WorkQuery
|
||||
import at.bitfire.davdroid.R
|
||||
import at.bitfire.davdroid.db.AppDatabase
|
||||
import at.bitfire.davdroid.repository.AccountRepository
|
||||
import at.bitfire.davdroid.servicedetection.RefreshCollectionsWorker
|
||||
@@ -296,4 +300,30 @@ class AccountsModel @AssistedInject constructor(
|
||||
false
|
||||
}
|
||||
|
||||
fun cancelSyncAdapterSyncs() {
|
||||
if (Build.VERSION.SDK_INT >= 34) {
|
||||
val calendarAccountType = context.getString(R.string.account_type)
|
||||
val addressBookAccountType = context.getString(R.string.account_type_address_book)
|
||||
|
||||
// Cancel any (after an update) possibly forever pending calendar (+tasks) account syncs
|
||||
cancelSyncs(calendarAccountType, CalendarContract.AUTHORITY)
|
||||
|
||||
// Cancel any (after an update) possibly forever pending address book account syncs
|
||||
cancelSyncs(addressBookAccountType, ContactsContract.AUTHORITY)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels any (possibly forever pending) syncs for the accounts of given account type for all
|
||||
* authorities.
|
||||
*/
|
||||
private fun cancelSyncs(accountType: String, authority: String) {
|
||||
val accountManager = AccountManager.get(context)
|
||||
accountManager.getAccountsByType(accountType).forEach { account ->
|
||||
val extras = Bundle()
|
||||
logger.info("Android 14+: Canceling all (possibly forever pending) syncs for $account and $authority with $extras")
|
||||
syncFrameWork.cancelSync(account, authority, extras)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.AccountCircle
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.BatterySaver
|
||||
import androidx.compose.material.icons.filled.CancelScheduleSend
|
||||
import androidx.compose.material.icons.filled.DataSaverOn
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material.icons.filled.NotificationsOff
|
||||
@@ -111,6 +112,7 @@ fun AccountsScreen(
|
||||
}
|
||||
|
||||
AccountsScreen(
|
||||
cancelSyncAdapterSyncs = { model.cancelSyncAdapterSyncs() },
|
||||
accountsDrawerHandler = accountsDrawerHandler,
|
||||
accounts = accounts,
|
||||
showSyncAll = showSyncAll,
|
||||
@@ -131,6 +133,7 @@ fun AccountsScreen(
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class)
|
||||
@Composable
|
||||
fun AccountsScreen(
|
||||
cancelSyncAdapterSyncs: () -> Unit,
|
||||
accountsDrawerHandler: AccountsDrawerHandler,
|
||||
accounts: List<AccountsModel.AccountInfo>,
|
||||
showSyncAll: Boolean = true,
|
||||
@@ -228,6 +231,17 @@ fun AccountsScreen(
|
||||
contentDescription = stringResource(R.string.accounts_sync_all)
|
||||
)
|
||||
}
|
||||
FloatingActionButton(
|
||||
onClick = cancelSyncAdapterSyncs,
|
||||
containerColor = MaterialTheme.colorScheme.secondary,
|
||||
contentColor = MaterialTheme.colorScheme.onSecondary,
|
||||
modifier = Modifier.padding(top = 24.dp)
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.CancelScheduleSend,
|
||||
contentDescription = stringResource(R.string.accounts_sync_all)
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
snackbarHost = { SnackbarHost(snackbarHostState) }
|
||||
@@ -321,6 +335,7 @@ fun AccountsScreen(
|
||||
@Preview
|
||||
fun AccountsScreen_Preview_Empty() {
|
||||
AccountsScreen(
|
||||
cancelSyncAdapterSyncs = {},
|
||||
accountsDrawerHandler = object: AccountsDrawerHandler() {
|
||||
@Composable
|
||||
override fun MenuEntries(snackbarHostState: SnackbarHostState) {
|
||||
@@ -337,6 +352,7 @@ fun AccountsScreen_Preview_Empty() {
|
||||
@Preview
|
||||
fun AccountsScreen_Preview_OneAccount() {
|
||||
AccountsScreen(
|
||||
cancelSyncAdapterSyncs = {},
|
||||
accountsDrawerHandler = object: AccountsDrawerHandler() {
|
||||
@Composable
|
||||
override fun MenuEntries(snackbarHostState: SnackbarHostState) {
|
||||
|
||||
Reference in New Issue
Block a user