📡 Improve and simplify connectivity handling
This commit is contained in:
parent
90bb538692
commit
2ddd78dfe2
@ -6,6 +6,7 @@
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
|
||||
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
|
||||
|
@ -24,11 +24,9 @@ class StartupNodeOnWifiService : JobService() {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onStopJob(params: JobParameters?) = if (Preferences.isWifiOnly(this)) {
|
||||
// Don't actually stop the service, otherwise it will be stopped after 1 or 10 minutes
|
||||
// depending on Android version.
|
||||
Preferences.isFullNodeActive(this)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
/**
|
||||
* Don't actually stop the service, otherwise it will be stopped after 1 or 10 minutes
|
||||
* depending on Android version.
|
||||
*/
|
||||
override fun onStopJob(params: JobParameters?) = Preferences.isFullNodeActive(this)
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import java.util.regex.Pattern
|
||||
*/
|
||||
object Constants {
|
||||
const val PREFERENCE_WIFI_ONLY = "wifi_only"
|
||||
const val PREFERENCE_REQUIRE_CHARGING = "require_charging"
|
||||
const val PREFERENCE_EMULATE_CONVERSATIONS = "emulate_conversations"
|
||||
const val PREFERENCE_TRUSTED_NODE = "trusted_node"
|
||||
const val PREFERENCE_SYNC_TIMEOUT = "sync_timeout"
|
||||
|
@ -19,30 +19,37 @@ object NetworkUtils {
|
||||
|
||||
fun enableNode(ctx: Context, ask: Boolean = true) {
|
||||
Preferences.setFullNodeActive(ctx, true)
|
||||
if (Preferences.isWifiOnly(ctx)) {
|
||||
if (Preferences.isConnectionAllowed(ctx)) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
scheduleNodeStart(ctx)
|
||||
} else {
|
||||
doStartBitmessageService(ctx)
|
||||
MainActivity.updateNodeSwitch()
|
||||
}
|
||||
} else if (ask) {
|
||||
val dialogIntent = Intent(ctx, FullNodeDialogActivity::class.java)
|
||||
if (ctx !is Activity) {
|
||||
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
ctx.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
|
||||
}
|
||||
ctx.startActivity(dialogIntent)
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if (Preferences.isConnectionAllowed(ctx) || !ask) {
|
||||
scheduleNodeStart(ctx)
|
||||
} else {
|
||||
askForConnection(ctx)
|
||||
}
|
||||
} else {
|
||||
doStartBitmessageService(ctx)
|
||||
MainActivity.updateNodeSwitch()
|
||||
if (Preferences.isWifiOnly(ctx)) {
|
||||
if (Preferences.isConnectionAllowed(ctx)) {
|
||||
doStartBitmessageService(ctx)
|
||||
MainActivity.updateNodeSwitch()
|
||||
} else if (ask) {
|
||||
askForConnection(ctx)
|
||||
}
|
||||
} else {
|
||||
doStartBitmessageService(ctx)
|
||||
MainActivity.updateNodeSwitch()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun askForConnection(ctx: Context) {
|
||||
val dialogIntent = Intent(ctx, FullNodeDialogActivity::class.java)
|
||||
if (ctx !is Activity) {
|
||||
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
ctx.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
|
||||
}
|
||||
ctx.startActivity(dialogIntent)
|
||||
}
|
||||
|
||||
fun doStartBitmessageService(ctx: Context) {
|
||||
ContextCompat.startForegroundService(ctx, Intent(ctx, BitmessageService::class.java))
|
||||
}
|
||||
@ -56,8 +63,14 @@ object NetworkUtils {
|
||||
fun scheduleNodeStart(ctx: Context) {
|
||||
val serviceComponent = ComponentName(ctx, StartupNodeOnWifiService::class.java)
|
||||
val builder = JobInfo.Builder(0, serviceComponent)
|
||||
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
|
||||
if (Preferences.isWifiOnly(ctx)) {
|
||||
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
|
||||
}
|
||||
if (Preferences.requireCharging(ctx)) {
|
||||
builder.setRequiresCharging(true)
|
||||
}
|
||||
builder.setBackoffCriteria(0L, JobInfo.BACKOFF_POLICY_LINEAR)
|
||||
builder.setPersisted(true)
|
||||
val jobScheduler = ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
||||
jobScheduler.schedule(builder.build())
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import ch.dissem.apps.abit.notification.ErrorNotification
|
||||
import ch.dissem.apps.abit.util.Constants.PREFERENCE_EMULATE_CONVERSATIONS
|
||||
import ch.dissem.apps.abit.util.Constants.PREFERENCE_FULL_NODE
|
||||
import ch.dissem.apps.abit.util.Constants.PREFERENCE_REQUEST_ACK
|
||||
import ch.dissem.apps.abit.util.Constants.PREFERENCE_REQUIRE_CHARGING
|
||||
import ch.dissem.apps.abit.util.Constants.PREFERENCE_SYNC_TIMEOUT
|
||||
import ch.dissem.apps.abit.util.Constants.PREFERENCE_TRUSTED_NODE
|
||||
import ch.dissem.apps.abit.util.Constants.PREFERENCE_WIFI_ONLY
|
||||
@ -78,17 +79,13 @@ object Preferences {
|
||||
return 8444
|
||||
}
|
||||
|
||||
fun getTimeoutInSeconds(ctx: Context): Long =
|
||||
getPreference(ctx, PREFERENCE_SYNC_TIMEOUT)?.toLong() ?: 120
|
||||
fun getTimeoutInSeconds(ctx: Context): Long = getPreference(ctx, PREFERENCE_SYNC_TIMEOUT)?.toLong() ?: 120
|
||||
|
||||
private fun getPreference(ctx: Context, name: String): String? =
|
||||
ctx.defaultSharedPreferences.getString(name, null)
|
||||
private fun getPreference(ctx: Context, name: String): String? = ctx.defaultSharedPreferences.getString(name, null)
|
||||
|
||||
fun isConnectionAllowed(ctx: Context) =
|
||||
!isWifiOnly(ctx) || !ctx.connectivityManager.isActiveNetworkMetered
|
||||
fun isConnectionAllowed(ctx: Context) = !isWifiOnly(ctx) || !ctx.connectivityManager.isActiveNetworkMetered
|
||||
|
||||
fun isWifiOnly(ctx: Context) =
|
||||
ctx.defaultSharedPreferences.getBoolean(PREFERENCE_WIFI_ONLY, true)
|
||||
fun isWifiOnly(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_WIFI_ONLY, true)
|
||||
|
||||
fun setWifiOnly(ctx: Context, status: Boolean) {
|
||||
ctx.defaultSharedPreferences.edit()
|
||||
@ -96,6 +93,14 @@ object Preferences {
|
||||
.apply()
|
||||
}
|
||||
|
||||
fun requireCharging(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_REQUIRE_CHARGING, true)
|
||||
|
||||
fun setRequireCharging(ctx: Context, status: Boolean) {
|
||||
ctx.defaultSharedPreferences.edit()
|
||||
.putBoolean(PREFERENCE_REQUIRE_CHARGING, status)
|
||||
.apply()
|
||||
}
|
||||
|
||||
fun isEmulateConversations(ctx: Context) =
|
||||
ctx.defaultSharedPreferences.getBoolean(PREFERENCE_EMULATE_CONVERSATIONS, true)
|
||||
|
||||
@ -111,8 +116,7 @@ object Preferences {
|
||||
|
||||
fun getExportDirectory(ctx: Context) = File(ctx.filesDir, "exports")
|
||||
|
||||
fun requestAcknowledgements(ctx: Context) =
|
||||
ctx.defaultSharedPreferences.getBoolean(PREFERENCE_REQUEST_ACK, true)
|
||||
fun requestAcknowledgements(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_REQUEST_ACK, true)
|
||||
|
||||
fun cleanupExportDirectory(ctx: Context) {
|
||||
val exportDirectory = getExportDirectory(ctx)
|
||||
|
@ -149,6 +149,8 @@ As an alternative you could configure a trusted node in the settings, but as of
|
||||
<string name="preference_group_advanced_summary"></string>
|
||||
<string name="preference_group_experimental">Experimental</string>
|
||||
<string name="preference_group_experimental_summary">Only change if you know what you\'re doing</string>
|
||||
<string name="require_charging">Require charging</string>
|
||||
<string name="require_charging_summary">Only connect when device is plugged in</string>
|
||||
<string name="unknown">Unknown</string>
|
||||
<string name="ok">OK</string>
|
||||
</resources>
|
||||
|
@ -31,6 +31,12 @@
|
||||
android:key="wifi_only"
|
||||
android:summary="@string/wifi_only_summary"
|
||||
android:title="@string/wifi_only" />
|
||||
<SwitchPreferenceCompat
|
||||
android:defaultValue="false"
|
||||
android:key="require_charging"
|
||||
android:enabled="@bool/is_post_api_21"
|
||||
android:summary="@string/require_charging_summary"
|
||||
android:title="@string/require_charging" />
|
||||
<SwitchPreferenceCompat
|
||||
android:defaultValue="true"
|
||||
android:key="request_acknowledgements"
|
||||
|
Loading…
Reference in New Issue
Block a user