Merge pull request #10 from MatMaul/wakelock

Make permanent wakelock optional
This commit is contained in:
Philipp C. Heckel 2022-01-11 22:41:56 -05:00 committed by GitHub
commit ff6ecd5524
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 12 deletions

View file

@ -158,6 +158,16 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri
return sharedPrefs.getInt(SHARED_PREFS_MIN_PRIORITY, 1) // 1/low means all priorities return sharedPrefs.getInt(SHARED_PREFS_MIN_PRIORITY, 1) // 1/low means all priorities
} }
fun getWakelockEnabled(): Boolean {
return sharedPrefs.getBoolean(SHARED_PREFS_WAKELOCK_ENABLED, false) // Disabled by default
}
fun setWakelockEnabled(enabled: Boolean) {
sharedPrefs.edit()
.putBoolean(SHARED_PREFS_WAKELOCK_ENABLED, enabled)
.apply()
}
fun getBroadcastEnabled(): Boolean { fun getBroadcastEnabled(): Boolean {
return sharedPrefs.getBoolean(SHARED_PREFS_BROADCAST_ENABLED, true) // Enabled by default return sharedPrefs.getBoolean(SHARED_PREFS_BROADCAST_ENABLED, true) // Enabled by default
} }
@ -288,6 +298,7 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri
const val SHARED_PREFS_AUTO_RESTART_WORKER_VERSION = "AutoRestartWorkerVersion" const val SHARED_PREFS_AUTO_RESTART_WORKER_VERSION = "AutoRestartWorkerVersion"
const val SHARED_PREFS_MUTED_UNTIL_TIMESTAMP = "MutedUntil" const val SHARED_PREFS_MUTED_UNTIL_TIMESTAMP = "MutedUntil"
const val SHARED_PREFS_MIN_PRIORITY = "MinPriority" const val SHARED_PREFS_MIN_PRIORITY = "MinPriority"
const val SHARED_PREFS_WAKELOCK_ENABLED = "WakelockEnabled"
const val SHARED_PREFS_BROADCAST_ENABLED = "BroadcastEnabled" const val SHARED_PREFS_BROADCAST_ENABLED = "BroadcastEnabled"
const val SHARED_PREFS_UNIFIED_PUSH_ENABLED = "UnifiedPushEnabled" const val SHARED_PREFS_UNIFIED_PUSH_ENABLED = "UnifiedPushEnabled"
const val SHARED_PREFS_UNIFIED_PUSH_BASE_URL = "UnifiedPushBaseURL" const val SHARED_PREFS_UNIFIED_PUSH_BASE_URL = "UnifiedPushBaseURL"

View file

@ -43,7 +43,7 @@ class NotificationService(val context: Context) {
notificationManager.notify(notification.notificationId, notificationBuilder.build()) notificationManager.notify(notification.notificationId, notificationBuilder.build())
} }
private fun setContentIntent(builder: NotificationCompat.Builder, subscription: Subscription, notification: Notification): NotificationCompat.Builder? { private fun setContentIntent(builder: NotificationCompat.Builder, subscription: Subscription, notification: Notification): NotificationCompat.Builder {
if (notification.click == "") { if (notification.click == "") {
return builder.setContentIntent(detailActivityIntent(subscription)) return builder.setContentIntent(detailActivityIntent(subscription))
} }

View file

@ -4,10 +4,7 @@ import android.app.*
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.*
import android.os.IBinder
import android.os.PowerManager
import android.os.SystemClock
import android.util.Log import android.util.Log
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@ -94,6 +91,7 @@ class SubscriberService : Service() {
override fun onDestroy() { override fun onDestroy() {
Log.d(TAG, "Subscriber service has been destroyed") Log.d(TAG, "Subscriber service has been destroyed")
stopService()
sendBroadcast(Intent(this, AutoRestartReceiver::class.java)) // Restart it if necessary! sendBroadcast(Intent(this, AutoRestartReceiver::class.java)) // Restart it if necessary!
super.onDestroy() super.onDestroy()
} }
@ -107,9 +105,10 @@ class SubscriberService : Service() {
isServiceStarted = true isServiceStarted = true
saveServiceState(this, ServiceState.STARTED) saveServiceState(this, ServiceState.STARTED)
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run { wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG).apply { newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG)
acquire() }
} if (repository.getWakelockEnabled()) {
wakeLock?.acquire()
} }
refreshConnections() refreshConnections()
} }
@ -124,10 +123,12 @@ class SubscriberService : Service() {
// Releasing wake-lock and stopping ourselves // Releasing wake-lock and stopping ourselves
try { try {
wakeLock?.let { wakeLock?.let {
if (it.isHeld) { // Release all acquire()
while (it.isHeld) {
it.release() it.release()
} }
} }
wakeLock = null
stopForeground(true) stopForeground(true)
stopSelf() stopSelf()
} catch (e: Exception) { } catch (e: Exception) {
@ -201,6 +202,14 @@ class SubscriberService : Service() {
} }
private fun onNotificationReceived(subscription: Subscription, notification: io.heckel.ntfy.data.Notification) { private fun onNotificationReceived(subscription: Subscription, notification: io.heckel.ntfy.data.Notification) {
// If permanent wakelock is not enabled, still take the wakelock while notifications are being dispatched
if (!repository.getWakelockEnabled()) {
// Wakelocks are reference counted by default so that should work neatly here
wakeLock?.let {
it.acquire()
}
}
val url = topicUrl(subscription.baseUrl, subscription.topic) val url = topicUrl(subscription.baseUrl, subscription.topic)
Log.d(TAG, "[$url] Received notification: $notification") Log.d(TAG, "[$url] Received notification: $notification")
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
@ -208,6 +217,14 @@ class SubscriberService : Service() {
Log.d(TAG, "[$url] Dispatching notification $notification") Log.d(TAG, "[$url] Dispatching notification $notification")
dispatcher.dispatch(subscription, notification) dispatcher.dispatch(subscription, notification)
} }
if (!repository.getWakelockEnabled()) {
wakeLock?.let {
if (it.isHeld) {
it.release()
}
}
}
} }
} }

View file

@ -1,8 +1,6 @@
package io.heckel.ntfy.ui package io.heckel.ntfy.ui
import android.content.ClipData import android.content.*
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils
import android.util.Log import android.util.Log
@ -15,6 +13,7 @@ import io.heckel.ntfy.BuildConfig
import io.heckel.ntfy.R import io.heckel.ntfy.R
import io.heckel.ntfy.app.Application import io.heckel.ntfy.app.Application
import io.heckel.ntfy.data.Repository import io.heckel.ntfy.data.Repository
import io.heckel.ntfy.service.SubscriberService
import io.heckel.ntfy.util.formatDateShort import io.heckel.ntfy.util.formatDateShort
import io.heckel.ntfy.util.toPriorityString import io.heckel.ntfy.util.toPriorityString
@ -106,6 +105,31 @@ class SettingsActivity : AppCompatActivity() {
} }
} }
// Permanent wakelock enabled
val wakelockEnabledPrefId = context?.getString(R.string.settings_advanced_wakelock_key) ?: return
val wakelockEnabled: SwitchPreference? = findPreference(wakelockEnabledPrefId)
wakelockEnabled?.isChecked = repository.getWakelockEnabled()
wakelockEnabled?.preferenceDataStore = object : PreferenceDataStore() {
override fun putBoolean(key: String?, value: Boolean) {
repository.setWakelockEnabled(value)
val context = this@SettingsFragment.context
Intent(context, SubscriberService::class.java).also { intent ->
// Service will autorestart
context?.stopService(intent)
}
}
override fun getBoolean(key: String?, defValue: Boolean): Boolean {
return repository.getWakelockEnabled()
}
}
wakelockEnabled?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { pref ->
if (pref.isChecked) {
getString(R.string.settings_advanced_wakelock_summary_enabled)
} else {
getString(R.string.settings_advanced_wakelock_summary_disabled)
}
}
// Broadcast enabled // Broadcast enabled
val broadcastEnabledPrefId = context?.getString(R.string.settings_advanced_broadcast_key) ?: return val broadcastEnabledPrefId = context?.getString(R.string.settings_advanced_broadcast_key) ?: return
val broadcastEnabled: SwitchPreference? = findPreference(broadcastEnabledPrefId) val broadcastEnabled: SwitchPreference? = findPreference(broadcastEnabledPrefId)

View file

@ -172,6 +172,10 @@
<string name="settings_unified_push_base_url_title">Server URL</string> <string name="settings_unified_push_base_url_title">Server URL</string>
<string name="settings_unified_push_base_url_default_summary">%1$s (default)</string> <string name="settings_unified_push_base_url_default_summary">%1$s (default)</string>
<string name="settings_advanced_header">Advanced</string> <string name="settings_advanced_header">Advanced</string>
<string name="settings_advanced_wakelock_key">WakelockEnabled</string>
<string name="settings_advanced_wakelock_title">Permanent wakelock (needed for some devices)</string>
<string name="settings_advanced_wakelock_summary_enabled">Higher battery usage but should always wake up</string>
<string name="settings_advanced_wakelock_summary_disabled">Minimal battery drain, some devices will not wake up</string>
<string name="settings_advanced_broadcast_key">BroadcastEnabled</string> <string name="settings_advanced_broadcast_key">BroadcastEnabled</string>
<string name="settings_advanced_broadcast_title">Broadcast messages</string> <string name="settings_advanced_broadcast_title">Broadcast messages</string>
<string name="settings_advanced_broadcast_summary_enabled">Apps can receive incoming notifications as broadcasts</string> <string name="settings_advanced_broadcast_summary_enabled">Apps can receive incoming notifications as broadcasts</string>

View file

@ -28,6 +28,10 @@
app:dependency="@string/settings_unified_push_enabled_key"/> app:dependency="@string/settings_unified_push_enabled_key"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="@string/settings_advanced_header"> <PreferenceCategory app:title="@string/settings_advanced_header">
<SwitchPreference
app:key="@string/settings_advanced_wakelock_key"
app:title="@string/settings_advanced_wakelock_title"
app:enabled="true"/>
<SwitchPreference <SwitchPreference
app:key="@string/settings_advanced_broadcast_key" app:key="@string/settings_advanced_broadcast_key"
app:title="@string/settings_advanced_broadcast_title" app:title="@string/settings_advanced_broadcast_title"