From 2ddd78dfe24d60ff7271e1da86a847fc1b1e85ec Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Mon, 4 Jun 2018 12:55:00 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A1=20Improve=20and=20simplify=20conne?= =?UTF-8?q?ctivity=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 1 + .../abit/service/StartupNodeOnWifiService.kt | 12 ++--- .../ch/dissem/apps/abit/util/Constants.kt | 1 + .../ch/dissem/apps/abit/util/NetworkUtils.kt | 51 ++++++++++++------- .../ch/dissem/apps/abit/util/Preferences.kt | 24 +++++---- app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/preferences.xml | 6 +++ 7 files changed, 61 insertions(+), 36 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2c41ccd..a9d4f55 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + diff --git a/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt b/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt index 5a3ee01..372393a 100644 --- a/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt +++ b/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt @@ -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) } diff --git a/app/src/main/java/ch/dissem/apps/abit/util/Constants.kt b/app/src/main/java/ch/dissem/apps/abit/util/Constants.kt index a57a4b2..5fd22a9 100644 --- a/app/src/main/java/ch/dissem/apps/abit/util/Constants.kt +++ b/app/src/main/java/ch/dissem/apps/abit/util/Constants.kt @@ -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" diff --git a/app/src/main/java/ch/dissem/apps/abit/util/NetworkUtils.kt b/app/src/main/java/ch/dissem/apps/abit/util/NetworkUtils.kt index 7dd32cb..efe559d 100644 --- a/app/src/main/java/ch/dissem/apps/abit/util/NetworkUtils.kt +++ b/app/src/main/java/ch/dissem/apps/abit/util/NetworkUtils.kt @@ -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()) } diff --git a/app/src/main/java/ch/dissem/apps/abit/util/Preferences.kt b/app/src/main/java/ch/dissem/apps/abit/util/Preferences.kt index 7b75161..b7249fd 100644 --- a/app/src/main/java/ch/dissem/apps/abit/util/Preferences.kt +++ b/app/src/main/java/ch/dissem/apps/abit/util/Preferences.kt @@ -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) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c4cdb65..bd1f3a8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -149,6 +149,8 @@ As an alternative you could configure a trusted node in the settings, but as of Experimental Only change if you know what you\'re doing + Require charging + Only connect when device is plugged in Unknown OK diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index bb4b957..32629e5 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -31,6 +31,12 @@ android:key="wifi_only" android:summary="@string/wifi_only_summary" android:title="@string/wifi_only" /> +