diff --git a/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.kt b/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.kt index 20d0877..da61c5d 100644 --- a/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.kt @@ -18,12 +18,15 @@ package ch.dissem.apps.abit import android.app.Activity import android.content.* +import android.os.Build +import android.os.Build.VERSION_CODES.LOLLIPOP import android.os.Bundle import android.os.IBinder import android.support.v4.app.Fragment import android.support.v4.content.ContextCompat import android.support.v4.content.FileProvider.getUriForFile import android.support.v7.preference.Preference +import android.support.v7.preference.Preference.OnPreferenceChangeListener import android.support.v7.preference.PreferenceFragmentCompat import android.support.v7.preference.PreferenceScreen import android.support.v7.preference.SwitchPreferenceCompat @@ -36,6 +39,7 @@ import ch.dissem.apps.abit.synchronization.SyncAdapter import ch.dissem.apps.abit.util.Constants.PREFERENCE_SERVER_POW import ch.dissem.apps.abit.util.Constants.PREFERENCE_TRUSTED_NODE import ch.dissem.apps.abit.util.Exports +import ch.dissem.apps.abit.util.NetworkUtils import ch.dissem.apps.abit.util.Preferences import ch.dissem.bitmessage.entity.Plaintext import com.mikepenz.aboutlibraries.Libs @@ -61,6 +65,11 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP findPreference("import")?.onPreferenceClickListener = importClickListener() findPreference("status")?.onPreferenceClickListener = statusClickListener() + connectivityChangeListener().let { + findPreference("wifi_only")?.onPreferenceChangeListener = it + findPreference("require_charging")?.onPreferenceChangeListener = it + } + val emulateConversations = findPreference("emulate_conversations") as? SwitchPreferenceCompat val conversationInit = findPreference("emulate_conversations_initialize") @@ -247,11 +256,20 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP } private fun emulateConversationChangeListener(conversationInit: Preference?) = - Preference.OnPreferenceChangeListener { _, newValue -> + OnPreferenceChangeListener { _, newValue -> conversationInit?.isEnabled = newValue as Boolean true } + private fun connectivityChangeListener() = + OnPreferenceChangeListener { preference, newValue -> + val ctx = context + if (ctx != null && Build.VERSION.SDK_INT >= LOLLIPOP && Preferences.isFullNodeActive(ctx)) { + NetworkUtils.scheduleNodeStart(ctx) + } + true + } + // The why-is-it-so-damn-hard-to-group-preferences section override fun getCallbackFragment(): Fragment = this diff --git a/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt b/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt index 523e832..edc3f87 100644 --- a/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt +++ b/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt @@ -22,6 +22,7 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.net.ConnectivityManager +import android.os.BatteryManager import android.os.Handler import ch.dissem.apps.abit.notification.NetworkNotification import ch.dissem.apps.abit.notification.NetworkNotification.Companion.NETWORK_NOTIFICATION_ID @@ -61,7 +62,10 @@ class BitmessageService : Service() { override fun onCreate() { registerReceiver( connectivityReceiver, - IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) + IntentFilter().apply { + addAction(ConnectivityManager.CONNECTIVITY_ACTION) + addAction(Intent.ACTION_BATTERY_CHANGED) + } ) notification = NetworkNotification(this) running = false 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 efe559d..c401486 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 @@ -61,6 +61,7 @@ object NetworkUtils { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) fun scheduleNodeStart(ctx: Context) { + val jobScheduler = ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler val serviceComponent = ComponentName(ctx, StartupNodeOnWifiService::class.java) val builder = JobInfo.Builder(0, serviceComponent) if (Preferences.isWifiOnly(ctx)) { @@ -71,7 +72,6 @@ object NetworkUtils { } 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 b7249fd..44265b1 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 @@ -26,12 +26,18 @@ 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 +import org.jetbrains.anko.batteryManager import org.jetbrains.anko.connectivityManager import org.jetbrains.anko.defaultSharedPreferences import org.slf4j.LoggerFactory import java.io.File import java.io.IOException import java.net.InetAddress +import android.os.BatteryManager +import android.content.Intent +import android.content.IntentFilter +import android.os.Build + /** * @author Christian Basler @@ -83,7 +89,19 @@ object Preferences { 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) = isAllowedForWiFi(ctx) && isAllowedForCharging(ctx) + + private fun isAllowedForWiFi(ctx: Context) = !isWifiOnly(ctx) || !ctx.connectivityManager.isActiveNetworkMetered + + private fun isAllowedForCharging(ctx: Context) = !requireCharging(ctx) || isCharging(ctx) + + private fun isCharging(ctx: Context) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + ctx.batteryManager.isCharging + } else { + val intent = ctx.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) + val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) + status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL + } fun isWifiOnly(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_WIFI_ONLY, true)