From 9c616d3b7dd768e00299640822f260892d9885a3 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 18 Jan 2022 16:49:00 -0500 Subject: [PATCH] Battery optimization banner --- .../main/java/io/heckel/ntfy/db/Repository.kt | 14 +++ .../java/io/heckel/ntfy/ui/MainActivity.kt | 38 ++++++++- app/src/main/java/io/heckel/ntfy/util/Util.kt | 15 ++++ .../drawable/ic_battery_alert_red_24dp.xml | 9 ++ app/src/main/res/layout/activity_main.xml | 85 ++++++++++++++++++- app/src/main/res/values/strings.xml | 6 ++ assets/battery_alert_black_24dp.svg | 1 + 7 files changed, 160 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/drawable/ic_battery_alert_red_24dp.xml create mode 100644 assets/battery_alert_black_24dp.svg diff --git a/app/src/main/java/io/heckel/ntfy/db/Repository.kt b/app/src/main/java/io/heckel/ntfy/db/Repository.kt index bf3f0c1..36592e7 100644 --- a/app/src/main/java/io/heckel/ntfy/db/Repository.kt +++ b/app/src/main/java/io/heckel/ntfy/db/Repository.kt @@ -225,6 +225,16 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri .apply() } + fun getBatteryOptimizationsRemindTime(): Long { + return sharedPrefs.getLong(SHARED_PREFS_BATTERY_OPTIMIZATIONS_REMIND_TIME, BATTERY_OPTIMIZATIONS_REMIND_TIME_ALWAYS) + } + + fun setBatteryOptimizationsRemindTime(timeMillis: Long) { + sharedPrefs.edit() + .putLong(SHARED_PREFS_BATTERY_OPTIMIZATIONS_REMIND_TIME, timeMillis) + .apply() + } + fun getUnifiedPushEnabled(): Boolean { return sharedPrefs.getBoolean(SHARED_PREFS_UNIFIED_PUSH_ENABLED, true) // Enabled by default } @@ -350,6 +360,7 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri const val SHARED_PREFS_CONNECTION_PROTOCOL = "ConnectionProtocol" const val SHARED_PREFS_BROADCAST_ENABLED = "BroadcastEnabled" const val SHARED_PREFS_RECORD_LOGS_ENABLED = "RecordLogs" + const val SHARED_PREFS_BATTERY_OPTIMIZATIONS_REMIND_TIME = "BatteryOptimizationsRemindTime" const val SHARED_PREFS_UNIFIED_PUSH_ENABLED = "UnifiedPushEnabled" const val SHARED_PREFS_UNIFIED_PUSH_BASE_URL = "UnifiedPushBaseURL" @@ -360,6 +371,9 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri const val CONNECTION_PROTOCOL_JSONHTTP = "jsonhttp" const val CONNECTION_PROTOCOL_WS = "ws" + const val BATTERY_OPTIMIZATIONS_REMIND_TIME_ALWAYS = 1L + const val BATTERY_OPTIMIZATIONS_REMIND_TIME_NEVER = Long.MAX_VALUE + private const val TAG = "NtfyRepository" private var instance: Repository? = null diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt index 7d43540..5666012 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt @@ -5,11 +5,14 @@ import android.animation.AnimatorListenerAdapter import android.app.AlertDialog import android.content.Intent import android.net.Uri +import android.os.Build import android.os.Bundle +import android.provider.Settings import android.view.ActionMode import android.view.Menu import android.view.MenuItem import android.view.View +import android.widget.Button import android.widget.Toast import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity @@ -20,6 +23,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.work.* import io.heckel.ntfy.R import io.heckel.ntfy.app.Application +import io.heckel.ntfy.db.Repository import io.heckel.ntfy.db.Subscription import io.heckel.ntfy.firebase.FirebaseMessenger import io.heckel.ntfy.log.Log @@ -27,10 +31,7 @@ import io.heckel.ntfy.msg.ApiService import io.heckel.ntfy.msg.NotificationDispatcher import io.heckel.ntfy.service.SubscriberService import io.heckel.ntfy.service.SubscriberServiceManager -import io.heckel.ntfy.util.fadeStatusBarColor -import io.heckel.ntfy.util.formatDateShort -import io.heckel.ntfy.util.shortUrl -import io.heckel.ntfy.util.topicShortUrl +import io.heckel.ntfy.util.* import io.heckel.ntfy.work.PollWorker import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -120,6 +121,34 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc SubscriberServiceManager.refresh(this) } + // Battery banner + val batteryRemindTimeReached = repository.getBatteryOptimizationsRemindTime() < System.currentTimeMillis() + val ignoringBatteryOptimizations = isIgnoringBatteryOptimizations(this) + val showBatteryBanner = batteryRemindTimeReached && !ignoringBatteryOptimizations + val batteryBanner = findViewById(R.id.main_banner_battery) + batteryBanner.visibility = if (showBatteryBanner) View.VISIBLE else View.GONE + Log.d(TAG, "Battery: ignoring optimizations = $ignoringBatteryOptimizations (we want this to be true); remind time reached = $batteryRemindTimeReached") + if (showBatteryBanner) { + val dismissButton = findViewById