Skip to content

Commit

Permalink
Fixes to sync scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkourlas committed Feb 28, 2021
1 parent 2c35a37 commit beab524
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 148 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.18 ##

* Bug fixes

## 0.6.17 ##

* Bug fixes
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/138.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0.6.18
• Bug fixes
4 changes: 2 additions & 2 deletions voipms-sms/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId 'net.kourlas.voipms_sms'
minSdkVersion 21
targetSdkVersion 30
versionCode 137
versionName '0.6.17'
versionCode 138
versionName '0.6.18'
}
flavorDimensions 'version', 'demo'
productFlavors {
Expand Down
1 change: 0 additions & 1 deletion voipms-sms/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@
<action android:name="android.intent.action.ACTION_LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".sms.receivers.SyncIntervalReceiver" />

<!-- SERVICES -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* VoIP.ms SMS
* Copyright (C) 2017-2020 Michael Kourlas
* Copyright (C) 2017-2021 Michael Kourlas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.kourlas.voipms_sms

import android.app.Application
Expand All @@ -23,8 +24,11 @@ import androidx.appcompat.app.AppCompatDelegate
import net.kourlas.voipms_sms.network.NetworkManager.Companion.getInstance
import net.kourlas.voipms_sms.preferences.fragments.AppearancePreferencesFragment
import net.kourlas.voipms_sms.preferences.getAppTheme
import net.kourlas.voipms_sms.preferences.getSyncInterval
import net.kourlas.voipms_sms.preferences.setRawSyncInterval
import net.kourlas.voipms_sms.sms.ConversationId
import net.kourlas.voipms_sms.sms.Database
import net.kourlas.voipms_sms.sms.workers.SyncWorker
import net.kourlas.voipms_sms.utils.subscribeToDidTopics
import java.util.*

Expand Down Expand Up @@ -73,6 +77,12 @@ class CustomApplication : Application() {
// Create static reference to self.
self = this

// Limit synchronization interval to 15 minutes. Previous versions
// supported a shorter interval.
if (getSyncInterval(applicationContext) < (15.0 / (24 * 60)) ) {
setRawSyncInterval(applicationContext, "0.01041666666")
}

// Update theme
when (getAppTheme(applicationContext)) {
AppearancePreferencesFragment.SYSTEM_DEFAULT -> AppCompatDelegate.setDefaultNightMode(
Expand All @@ -96,6 +106,10 @@ class CustomApplication : Application() {

// Subscribe to topics for current DIDs
subscribeToDidTopics(applicationContext)

// Schedule a database synchronization, if required.
SyncWorker.performFullSynchronization(applicationContext,
scheduleOnly = true)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ open class ConversationsActivity(val archived: Boolean = false) :
R.id.swipe_refresh_layout)
swipeRefreshLayout.setOnRefreshListener {
adapter.refresh()
SyncWorker.performSynchronization(this, forceRecent = false)
SyncWorker.performFullSynchronization(applicationContext)
}
swipeRefreshLayout.setColorSchemeResources(R.color.swipe_refresh_icon)
}
Expand Down Expand Up @@ -329,10 +329,10 @@ open class ConversationsActivity(val archived: Boolean = false) :
performAccountDidCheck()
val firstSyncRequired = performInitialSetup()

// Refresh and perform limited synchronization\
// Refresh and perform limited synchronization
if (!firstSyncRequired) {
adapter.refresh()
SyncWorker.performSynchronization(this, forceRecent = true)
SyncWorker.performPartialSynchronization(applicationContext)
}

// Refresh on resume just in case the contacts permission was newly
Expand Down Expand Up @@ -399,7 +399,7 @@ open class ConversationsActivity(val archived: Boolean = false) :
val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(
R.id.swipe_refresh_layout)
swipeRefreshLayout.isRefreshing = true
SyncWorker.performSynchronization(this, forceRecent = false)
SyncWorker.performFullSynchronization(applicationContext)

val emptyTextView = findViewById<TextView>(
R.id.empty_text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import androidx.preference.ListPreference
import androidx.preference.Preference
import com.takisoft.preferencex.PreferenceFragmentCompat
import net.kourlas.voipms_sms.R
import net.kourlas.voipms_sms.sms.receivers.SyncIntervalReceiver
import net.kourlas.voipms_sms.sms.workers.SyncWorker
import net.kourlas.voipms_sms.utils.preferences

class SynchronizationPreferencesFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener {
// Preference change handlers
private val syncIntervalPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, _ ->
Preference.OnPreferenceChangeListener { _, newValue ->
activity?.let {
SyncIntervalReceiver.setInterval(it)
SyncWorker.performFullSynchronization(
it,
customPeriod = (newValue as String).toDouble(),
scheduleOnly = true)
}
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ fun getFirstSyncAfterSignIn(context: Context): Boolean =
context.getString(R.string.preferences_first_sync_after_sign_in_key),
false)

fun getLastCompleteSyncTime(context: Context): Long =
getLongPreference(context,
context.getString(
R.string.preferences_sync_last_complete_time_key),
0)

fun getMessageTextBoxMaximumSize(context: Context): Int =
getStringPreference(
context,
Expand Down Expand Up @@ -354,12 +348,6 @@ fun setFirstSyncAfterSignIn(context: Context, firstSyncAfterSignIn: Boolean) {
firstSyncAfterSignIn)
}

fun setLastCompleteSyncTime(context: Context,
lastCompleteSyncTime: Long) =
setLongPreference(
context, context.getString(
R.string.preferences_sync_last_complete_time_key), lastCompleteSyncTime)

fun setSetupCompletedForVersion(context: Context, version: Long) {
if (getLongPreference(context,
context.getString(
Expand All @@ -385,6 +373,11 @@ fun setStartDate(context: Context, date: Date) {
SimpleDateFormat("MM/dd/yyyy", Locale.US).format(date))
}

fun setRawSyncInterval(context: Context, string: String) {
setStringPreference(context, context.getString(
R.string.preferences_sync_interval_key), string)
}

@Suppress("SameParameterValue")
private fun getSecureStringPreference(context: Context, key: String,
default: String?): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package net.kourlas.voipms_sms.sms.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import net.kourlas.voipms_sms.preferences.getSyncInterval
import net.kourlas.voipms_sms.sms.workers.SyncWorker
import net.kourlas.voipms_sms.utils.logException

/**
Expand All @@ -36,7 +38,7 @@ class SyncBootReceiver : BroadcastReceiver() {
&& intent.action != "android.intent.action.ACTION_LOCKED_BOOT_COMPLETED") {
throw Exception("Unrecognized action " + intent.action)
}
SyncIntervalReceiver.setInterval(context)
SyncWorker.performFullSynchronization(context, scheduleOnly = true)
} catch (e: Exception) {
logException(e)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import net.kourlas.voipms_sms.notifications.Notifications
import net.kourlas.voipms_sms.preferences.*
import net.kourlas.voipms_sms.sms.ConversationId
import net.kourlas.voipms_sms.sms.Database
import net.kourlas.voipms_sms.sms.receivers.SyncIntervalReceiver
import net.kourlas.voipms_sms.utils.httpPostWithMultipartFormData
import net.kourlas.voipms_sms.utils.logException
import net.kourlas.voipms_sms.utils.toBoolean
Expand All @@ -43,6 +42,7 @@ import java.io.IOException
import java.net.URLEncoder
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.math.ceil

/**
Expand Down Expand Up @@ -134,14 +134,6 @@ class SyncWorker(context: Context, params: WorkerParameters) :
val retrievalRequests = createRetrievalRequests(
retrieveOnlyRecentMessages)
processRequests(retrievalRequests, retrieveDeletedMessages)

// If this was not an intentionally limited database
// synchronization, set a new alarm for the next sync
if (!forceRecent) {
setLastCompleteSyncTime(applicationContext,
System.currentTimeMillis())
SyncIntervalReceiver.setInterval(applicationContext)
}
} catch (e: CancellationException) {
// We need to propagate the exception from processRequests
throw e
Expand Down Expand Up @@ -414,25 +406,49 @@ class SyncWorker(context: Context, params: WorkerParameters) :
/**
* Synchronize the database with VoIP.ms.
*
* @param forceRecent If true, retrieves only the most recent messages
* regardless of the app configuration.
* @param customPeriod A custom period for synchronization, in
* fractions of a day. If not provided, the system configuration will
* be used.
* @param scheduleOnly Do not perform synchronization immediately.
*/
fun performSynchronization(context: Context,
forceRecent: Boolean = false) {
val work = OneTimeWorkRequestBuilder<SyncWorker>()
.setInputData(
workDataOf(
context.getString(
R.string.sync_force_recent) to forceRecent))
.build()
WorkManager.getInstance(context)
.enqueueUniqueWork(
fun performFullSynchronization(context: Context,
customPeriod: Double? = null,
scheduleOnly: Boolean = false) {
val delayInterval = (
(customPeriod ?: getSyncInterval(context))
* (24 * 60 * 60 * 1000)).toLong()
if (delayInterval != 0L) {
val workRequest = PeriodicWorkRequestBuilder<SyncWorker>(
delayInterval,
TimeUnit.MILLISECONDS)
if (scheduleOnly) {
workRequest.setInitialDelay(delayInterval,
TimeUnit.MILLISECONDS)
}
val work = workRequest.build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
context.getString(R.string.sync_work_id),
if (forceRecent)
ExistingWorkPolicy.KEEP
else
ExistingWorkPolicy.REPLACE,
ExistingPeriodicWorkPolicy.REPLACE,
work)
} else {
val work = OneTimeWorkRequestBuilder<SyncWorker>().build()
WorkManager.getInstance(context)
.enqueueUniqueWork(context.getString(R.string.sync_work_id),
ExistingWorkPolicy.REPLACE, work)
}
}

/**
* Check VoIP.ms for new messages.
*/
fun performPartialSynchronization(context: Context) {
val work = OneTimeWorkRequestBuilder<SyncWorker>().setInputData(
workDataOf(
context.getString(R.string.sync_force_recent) to true))
.build()
WorkManager.getInstance(context).enqueueUniqueWork(
context.getString(R.string.sync_partial_work_id),
ExistingWorkPolicy.KEEP, work)
}
}
}
Loading

0 comments on commit beab524

Please sign in to comment.