Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Android: keep the app reachable, and see other apps' names and icons

Bee7414dffaeaff
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f2d706c..f968ed6 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -120,7 +120,7 @@ jobs:
- uses: gradle/actions/setup-gradle@v6
- name: Unit tests and debug build
working-directory: android
- run: ./gradlew --no-daemon testDebugUnitTest assembleDebug
+ run: ./gradlew --no-daemon testDebugUnitTest lintDebug assembleDebug
- uses: actions/upload-artifact@v7
with:
name: nectarlink-android-debug
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 7d0ffab..96ef446 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -16,6 +16,18 @@
<uses-permission android:name="android.permission.CAMERA" />
<!-- Find my phone. -->
<uses-permission android:name="android.permission.VIBRATE" />
+ <!-- Asking to run unrestricted in the background: phones such as Samsung's
+ otherwise freeze the app, and PCs can't reach it. -->
+ <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
+
+ <!-- Names and icons of the apps whose notifications are mirrored: Android
+ hides other apps unless they're declared here. -->
+ <queries>
+ <intent>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent>
+ </queries>
<uses-feature android:name="android.hardware.camera.any" android:required="false" />
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/BackgroundAccess.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/BackgroundAccess.kt
new file mode 100644
index 0000000..a714b90
--- /dev/null
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/BackgroundAccess.kt
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.core
+
+import android.annotation.SuppressLint
+import android.content.Context
+import android.content.Intent
+import android.os.PowerManager
+import android.provider.Settings
+import androidx.core.net.toUri
+
+/**
+ * Running unrestricted in the background. Without it, battery savers (and
+ * Samsung's app freezer in particular) pause Nectarlink between events, so
+ * a PC's requests (replies, dismissals, find my phone) time out.
+ */
+object BackgroundAccess {
+ fun isUnrestricted(context: Context): Boolean =
+ context.getSystemService(PowerManager::class.java)?.isIgnoringBatteryOptimizations(context.packageName) ?: true
+
+ /** Android's prompt to let Nectarlink run unrestricted. */
+ @SuppressLint("BatteryLife") // A companion that must stay reachable is the case this exists for.
+ fun requestIntent(context: Context): Intent =
+ Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, "package:${context.packageName}".toUri())
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
index 263b74d..5139589 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
@@ -131,13 +131,14 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener
}
/**
- * Re-checks notification access (the user may have changed it in
- * Settings) and asks Android to reconnect the listener if it's allowed
+ * Re-checks notification access and background restrictions (the user
+ * may have changed them in Settings) and asks Android to reconnect the listener if it's allowed
* but not running.
*/
fun refreshNotificationAccess() {
val granted = NotificationListener.hasAccess(context)
- _state.update { it.copy(notificationAccess = granted) }
+ val unrestricted = BackgroundAccess.isUnrestricted(context)
+ _state.update { it.copy(notificationAccess = granted, backgroundUnrestricted = unrestricted) }
if (granted && NotificationListener.instance == null) {
NotificationListenerService.requestRebind(NotificationListener.component(context))
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt
index 73a1c1e..36a9ab3 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt
@@ -57,6 +57,8 @@ data class CoreState(
val ringingFrom: String? = null,
/** Whether the user let Nectarlink read notifications (to mirror them). */
val notificationAccess: Boolean = false,
+ /** Whether Android lets Nectarlink run unrestricted in the background. */
+ val backgroundUnrestricted: Boolean = true,
) {
fun device(id: String): Device? = devices.firstOrNull { it.id == id }
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt
index 2be3ebc..055e111 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt
@@ -43,7 +43,9 @@ internal class PhonePlatform(context: Context, private val ringer: Ringer) : Pla
if (inputs.isEmpty()) throw NotificationFailure.NotFound()
val results = Bundle().apply { inputs.forEach { putCharSequence(it.resultKey, reply) } }
RemoteInput.addResultsToIntent(inputs.toTypedArray(), fillIn, results)
- RemoteInput.setResultsSource(fillIn, RemoteInput.SOURCE_FREE_FORM_INPUT)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
+ RemoteInput.setResultsSource(fillIn, RemoteInput.SOURCE_FREE_FORM_INPUT)
+ }
}
try {
intent.send(context, 0, fillIn, null, null, null, sendOptions())
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationListener.kt b/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationListener.kt
index fb0e51b..5932a3d 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationListener.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationListener.kt
@@ -3,6 +3,9 @@ package app.nectarlink.android.notifications
import android.content.ComponentName
import android.content.Context
+import android.content.Intent
+import android.os.Build
+import android.provider.Settings
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import androidx.core.app.NotificationManagerCompat
@@ -74,5 +77,16 @@ class NotificationListener : NotificationListenerService() {
NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.packageName)
fun component(context: Context) = ComponentName(context, NotificationListener::class.java)
+
+ /** Android's notification access screen, on Nectarlink's own switch where possible. */
+ fun settingsIntent(context: Context): Intent =
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
+ Intent(Settings.ACTION_NOTIFICATION_LISTENER_DETAIL_SETTINGS).putExtra(
+ Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME,
+ component(context).flattenToString(),
+ )
+ } else {
+ Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
+ }.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/PairingLinks.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/PairingLinks.kt
index 2eb11bb..d1d946e 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/PairingLinks.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/PairingLinks.kt
@@ -2,6 +2,7 @@
package app.nectarlink.android.ui
import android.content.Context
+import androidx.core.content.edit
import java.security.MessageDigest
/** Remembers pairing links already used, so each is used at most once. */
@@ -15,7 +16,7 @@ class PairingLinks(context: Context) {
.joinToString("") { "%02x".format(it) }
val used = prefs.getString(KEY, "").orEmpty().split(',').filter { it.isNotEmpty() }
if (digest in used) return false
- prefs.edit().putString(KEY, (used + digest).takeLast(MAX_REMEMBERED).joinToString(",")).apply()
+ prefs.edit { putString(KEY, (used + digest).takeLast(MAX_REMEMBERED).joinToString(",")) }
return true
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/Preferences.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/Preferences.kt
index 4fc3b2a..1290761 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/Preferences.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/Preferences.kt
@@ -2,6 +2,7 @@
package app.nectarlink.android.ui
import android.content.Context
+import androidx.core.content.edit
import app.nectarlink.android.ui.theme.Appearance
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -25,12 +26,12 @@ class Preferences(context: Context) {
fun update(change: (Appearance) -> Appearance) {
val next = change(_appearance.value)
- prefs.edit()
- .putString(THEME, next.theme)
- .putString(MODE, next.mode)
- .putString(SEED, next.seed)
- .putBoolean(DYNAMIC, next.dynamicColor)
- .apply()
+ prefs.edit {
+ putString(THEME, next.theme)
+ putString(MODE, next.mode)
+ putString(SEED, next.seed)
+ putBoolean(DYNAMIC, next.dynamicColor)
+ }
_appearance.value = next
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt
index b618cf1..caba277 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt
@@ -30,12 +30,15 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.nectarlink.android.R
+import app.nectarlink.android.core.BackgroundAccess
import app.nectarlink.android.core.CoreState
import app.nectarlink.android.core.Device
+import app.nectarlink.android.notifications.NotificationListener
import app.nectarlink.core.Link
/**
@@ -70,10 +73,42 @@ fun HomeScreen(
state.ringingFrom?.let { from ->
item { RingingBanner(from, onStopRinging) }
}
+ if (state.devices.isNotEmpty() && !state.backgroundUnrestricted) {
+ item {
+ val context = LocalContext.current
+ SetupCard(
+ stringResource(R.string.background_title),
+ stringResource(R.string.background_text),
+ ) { context.startActivity(BackgroundAccess.requestIntent(context)) }
+ }
+ }
+ if (state.devices.isNotEmpty() && !state.notificationAccess) {
+ item {
+ val context = LocalContext.current
+ SetupCard(
+ stringResource(R.string.notifications_title),
+ stringResource(R.string.notifications_off),
+ ) { context.startActivity(NotificationListener.settingsIntent(context)) }
+ }
+ }
items(state.devices, key = { it.id }) { device -> PcCard(device, onRing) }
}
}
+/** Something the phone needs before mirroring works, with the button that fixes it. */
+@Composable
+private fun SetupCard(title: String, text: String, onAllow: () -> Unit) {
+ Surface(shape = MaterialTheme.shapes.extraLarge, color = MaterialTheme.colorScheme.secondaryContainer) {
+ Column(Modifier.fillMaxWidth().padding(20.dp)) {
+ Text(title, style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onSecondaryContainer)
+ Spacer(Modifier.height(4.dp))
+ Text(text, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSecondaryContainer)
+ Spacer(Modifier.height(12.dp))
+ Button(onClick = onAllow) { Text(stringResource(R.string.action_allow)) }
+ }
+ }
+}
+
@Composable
private fun RingingBanner(from: String, onStop: () -> Unit) {
Surface(shape = MaterialTheme.shapes.extraLarge, color = MaterialTheme.colorScheme.primary) {
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt
index d12b336..8136435 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt
@@ -191,7 +191,7 @@ private fun NotificationAccess(granted: Boolean) {
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
- val open = { context.startActivity(notificationAccessIntent(context)) }
+ val open = { context.startActivity(NotificationListener.settingsIntent(context)) }
if (granted) {
OutlinedButton(onClick = open) { Text(stringResource(R.string.action_manage)) }
} else {
@@ -216,17 +216,6 @@ private fun NotificationAccess(granted: Boolean) {
}
}
-/** Android's notification access screen, on Nectarlink's own switch where possible. */
-private fun notificationAccessIntent(context: Context): Intent =
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
- Intent(Settings.ACTION_NOTIFICATION_LISTENER_DETAIL_SETTINGS).putExtra(
- Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME,
- NotificationListener.component(context).flattenToString(),
- )
- } else {
- Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
- }.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-
@Composable
private fun Section(title: String, content: @Composable () -> Unit) {
Text(
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index 7a950d8..21395ee 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -56,6 +56,8 @@
<string name="notifications_off">Nectarlink needs notification access to show this phone’s notifications on your PC.</string>
<string name="notifications_restricted">Switch greyed out? Open App info, tap ⋮, choose “Allow restricted settings”, then try again.</string>
<string name="action_allow">Allow</string>
+ <string name="background_title">Stay reachable in the background</string>
+ <string name="background_text">Your phone pauses Nectarlink to save battery, so your PC can’t reach it, and replies and dismissals from the PC time out. Let it run in the background.</string>
<string name="action_manage">Manage</string>
<string name="action_app_info">App info</string>
<string name="theme_bloom">Bloom</string>
diff --git a/scripts/check.ps1 b/scripts/check.ps1
index fc641a5..4e5b65c 100644
--- a/scripts/check.ps1
+++ b/scripts/check.ps1
@@ -14,7 +14,7 @@
Also cross-compiles the workspace for Windows ARM64.
.PARAMETER Android
- Also builds the Android app and runs its unit tests (needs the Android
+ Also builds the Android app, runs its unit tests and lint (needs the Android
SDK, NDK and cargo-ndk; see android/README.md).
.PARAMETER Fast
@@ -93,7 +93,7 @@ try {
Push-Location (Join-Path $root "android")
try {
if (-not $env:JAVA_HOME) { $env:JAVA_HOME = "C:\Program Files\Android\Android Studio\jbr" }
- & .\gradlew.bat --console=plain -q testDebugUnitTest assembleDebug
+ & .\gradlew.bat --console=plain -q testDebugUnitTest lintDebug assembleDebug
} finally {
Pop-Location
}