Skip to content

Commit

Permalink
Add "Allow sync on metered connections" preference
Browse files Browse the repository at this point in the history
If disabled, auto-sync isn't triggered on metered connections and the user is asked if they want to continue if they trigger a manual sync

If enabled, sync occurs normally
  • Loading branch information
BrayanDSO committed Aug 26, 2022
1 parent ba45d90 commit 3cc6fb9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
39 changes: 23 additions & 16 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import com.ichi2.libanki.utils.TimeManager
import com.ichi2.themes.StyledProgressDialog
import com.ichi2.ui.BadgeDrawableBuilder
import com.ichi2.utils.*
import com.ichi2.utils.NetworkUtils.isActiveNetworkMetered
import com.ichi2.utils.Permissions.hasStorageAccessPermission
import com.ichi2.widget.WidgetStatus
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -923,14 +924,15 @@ open class DeckPicker :
private fun automaticSync() {
val preferences = AnkiDroidApp.getSharedPrefs(baseContext)

// Check whether the option is selected, the user is signed in and last sync was AUTOMATIC_SYNC_TIME ago
// (currently 10 minutes)
// Check whether the option is selected, the user is signed in, last sync was AUTOMATIC_SYNC_TIME ago
// (currently 10 minutes), and is not under a metered connection (if not allowed by preference)
val isLoggedIn = preferences.getString("hkey", "")!!.isNotEmpty()
val lastSyncTime = preferences.getLong("lastSyncTime", 0)
val autoSyncIsEnabled = preferences.getBoolean("automaticSyncMode", false)
val syncIntervalPassed = TimeManager.time.intTimeMS() - lastSyncTime > AUTOMATIC_SYNC_MIN_INTERVAL
val isNotBlockedByMeteredConnection = preferences.getBoolean(getString(R.string.metered_sync_key), false) || !isActiveNetworkMetered()

if (isLoggedIn && autoSyncIsEnabled && Connection.isOnline && syncIntervalPassed) {
if (isLoggedIn && autoSyncIsEnabled && Connection.isOnline && syncIntervalPassed && isNotBlockedByMeteredConnection) {
Timber.i("Triggering Automatic Sync")
sync()
}
Expand Down Expand Up @@ -1563,25 +1565,30 @@ open class DeckPicker :
Timber.w("User not logged in")
mPullToSyncWrapper.isRefreshing = false
showSyncErrorDialog(SyncErrorDialog.DIALOG_USER_NOT_LOGGED_IN_SYNC)
} else {
return
}
/** Nested function that makes the connection to
* the sync server and starts syncing the data */
fun doSync() {
val syncMedia = preferences.getBoolean("syncFetchesMedia", true)

if (!BackendFactory.defaultLegacySchema) {
handleNewSync(conflict, syncMedia)
} else {
Connection.sync(
mSyncListener,
Connection.Payload(
arrayOf(
hkey,
syncMedia,
conflict,
HostNumFactory.getInstance(baseContext)
)
)
)
val data = arrayOf(hkey, syncMedia, conflict, HostNumFactory.getInstance(baseContext))
Connection.sync(mSyncListener, Connection.Payload(data))
}
}
// Warn the user in case the connection is metered
val meteredSyncIsAllowed = preferences.getBoolean(getString(R.string.metered_sync_key), false)
if (!meteredSyncIsAllowed && isActiveNetworkMetered()) {
MaterialDialog(this).show {
message(R.string.metered_sync_warning)
positiveButton(R.string.dialog_continue) { doSync() }
negativeButton(R.string.dialog_cancel)
}
} else {
doSync()
}
}

private val mSyncListener: Connection.TaskListener = object : CancellableTaskListener {
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/03-dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
<item quantity="one">An automatic sync may be triggered in %d second</item>
<item quantity="other">An automatic sync may be triggered in %d seconds</item>
</plurals>
<string name="metered_sync_warning">Your connection is metered. Data transfer may cost money</string>
<string name="deck_picker_new">Number of new cards to see today in this deck.</string>
<string name="deck_picker_rev">Number of cards due today in this deck.</string>
<string name="deck_picker_lrn">Number of cards in learning in this deck.</string> <!-- This description is valid fos
Expand Down
3 changes: 3 additions & 0 deletions AnkiDroid/src/main/res/values/10-preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<string name="swipe_sensitivity" maxLength="41">Swipe sensitivity</string>
<string name="tts" maxLength="41">Text to speech</string>
<string name="tts_summ">Reads out question and answer if no sound file is included</string>
<!-- Sync -->
<string name="sync_fetch_missing_media" maxLength="41">Fetch media on sync</string>
<string name="sync_fetch_missing_media_summ">Automatically fetch missing media when syncing</string>
<string name="sync_account" maxLength="41">AnkiWeb account</string>
Expand All @@ -109,6 +110,8 @@
minutes ago.</string>
<string name="sync_status_badge" maxLength="41">Display synchronization status</string>
<string name="sync_status_badge_summ">Change the sync icon when changes can be uploaded</string>
<string name="metered_sync_title" maxLength="41">Allow sync on metered connections</string>
<string name="metered_sync_summary">If disabled, you will be warned if you try to sync on a metered connection</string>
<string name="app_theme" maxLength="41">Theme</string>
<string name="day_theme" maxLength="41">Day theme</string>
<string name="night_theme" maxLength="41">Night theme</string>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<string name="automatic_sync_choice_key">automaticSyncMode</string>
<string name="force_full_sync_key">force_full_sync</string>
<string name="sync_status_badge_key">showSyncStatusBadge</string>
<string name="metered_sync_key">allowMetered</string>
<string name="custom_sync_server_key">custom_sync_server_link</string>
<!-- Appearance -->
<string name="pref_appearance_screen_key">appearance_preference_group</string>
Expand Down
5 changes: 5 additions & 0 deletions AnkiDroid/src/main/res/xml/preferences_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
android:key="@string/sync_status_badge_key"
android:summary="@string/sync_status_badge_summ"
android:title="@string/sync_status_badge" />
<SwitchPreference
android:defaultValue="false"
android:key="@string/metered_sync_key"
android:title="@string/metered_sync_title"
android:summary="@string/metered_sync_summary" />
<PreferenceCategory android:title="@string/pref_cat_advanced">
<Preference
android:key="@string/force_full_sync_key"
Expand Down

0 comments on commit 3cc6fb9

Please sign in to comment.