Three APKs: the notification listener leaves the standard builds; releases drop x86_64
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
Google Play Protect blocked installing beta.7, most likely for the
notification listener (notification summaries).
- A second flavour dimension, `reading`: `standard` declares no listener and
no permission of it; `notifications` is the full build plus the listener,
with the same application id and key. Variants: fullStandard,
fullNotifications, fdroidStandard (no F-Droid build with the listener).
The listener class and its manifest entry live in src/notifications only.
- Settings in a standard build says where notification summaries went and
why. The self-updater installs only its own kind: a notifications build
takes latest.json's optional `notifications {url, sha256}` or nothing.
- Release builds carry arm64-v8a and armeabi-v7a only; debug keeps x86_64
for the emulator. The engine's unused C and C++ API libraries are left out.
- scripts/check-apk.sh checks the built files (listener only where it may
be, model stored, no x86 in a release); CI runs it on the three debug APKs
and on an unsigned release build, release.yml on what it publishes.
- release.yml publishes llembas-android-<version>.apk, -notifications.apk
and -fdroid.apk with SHA256SUMS.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
28 files changed
+403
-140
No files matched your search
@@ -17,6 +17,7 @@ import eu.llembas.android.api.ModelInfo
|
||||
import eu.llembas.android.api.Step
|
||||
import eu.llembas.android.api.StreamClient
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.notify.NotificationAccess
|
||||
import eu.llembas.android.notify.NotificationReader
|
||||
import eu.llembas.android.push.Notifications
|
||||
import eu.llembas.android.ui.explain
|
||||
@@ -152,7 +153,7 @@ class AssistController(private val context: Context, private val scope: Coroutin
|
||||
signedIn = instance != null,
|
||||
instanceName = instance?.name?.ifBlank { instance.base }.orEmpty(),
|
||||
voiceReply = prefs.readAloudSpoken,
|
||||
canReadNotifications = prefs.notificationSummaries && NotificationReader.granted(context),
|
||||
canReadNotifications = prefs.notificationSummaries && NotificationAccess.granted(context),
|
||||
last = prefs.lastAssistChat.takeIf { id -> id.isNotBlank() && !prefs.assistantTemporary }
|
||||
?.let { id -> LastAssist(id, prefs.lastAssistTitle, prefs.lastAssistAt) },
|
||||
)
|
||||
@@ -398,10 +399,10 @@ class AssistController(private val context: Context, private val scope: Coroutin
|
||||
if (withScreen) now.screenshot?.let { shot -> add(api.upload("screen.png", "image/png", png(shot), now.chatId).id) }
|
||||
}
|
||||
// The notifications, when asked for by the chip or by name ("my notifications") --
|
||||
// read now, only while unlocked (NotificationReader.read), and for this question only.
|
||||
// read now, only while unlocked (NotificationAccess.read), and for this question only.
|
||||
val phrases = context.getString(R.string.assist_notifications_phrases).split("|")
|
||||
val notices = if (now.canReadNotifications && (notifications || NotificationReader.asksAbout(question, phrases))) {
|
||||
NotificationReader.asText(NotificationReader.read(context)).ifBlank { NO_NOTIFICATIONS }
|
||||
NotificationReader.asText(NotificationAccess.read(context)).ifBlank { NO_NOTIFICATIONS }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
@@ -1,85 +1,14 @@
|
||||
package eu.llembas.android.notify
|
||||
|
||||
import android.app.KeyguardManager
|
||||
import android.app.Notification
|
||||
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
|
||||
|
||||
/**
|
||||
* Notification summaries on request: the assistant reads the notifications on the phone only when
|
||||
* the person asks it to ("summarise my notifications", or the sheet's chip), and sends their text
|
||||
* with that one question. Off by default, twice over: Android's own notification-access switch
|
||||
* (which only the person can turn on, in the system's settings) and Settings › Assistant's.
|
||||
*
|
||||
* The listener keeps nothing: it is how Android lets an app read the notifications that are up
|
||||
* right now ([activeNotifications]), and nothing is read until a question asks for it. Never over
|
||||
* the lock screen ([read] returns nothing while the phone is locked).
|
||||
*/
|
||||
class LembasNotificationListener : NotificationListenerService() {
|
||||
override fun onListenerConnected() {
|
||||
super.onListenerConnected()
|
||||
current = this
|
||||
}
|
||||
|
||||
override fun onListenerDisconnected() {
|
||||
if (current === this) current = null
|
||||
super.onListenerDisconnected()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
var current: LembasNotificationListener? = null
|
||||
private set
|
||||
}
|
||||
}
|
||||
|
||||
/** One notification, as the question carries it. */
|
||||
data class Notice(val app: String, val title: String, val text: String, val at: Long)
|
||||
|
||||
/**
|
||||
* Notification summaries, the half every build has: how the notifications are written into a
|
||||
* question, and when a question asks for them. Reading them is [NotificationAccess]'s, which only
|
||||
* the `notifications` build can do.
|
||||
*/
|
||||
object NotificationReader {
|
||||
/** Whether the person gave LLeMbas notification access in the system's settings. */
|
||||
fun granted(context: Context): Boolean = context.packageName in NotificationManagerCompat.getEnabledListenerPackages(context)
|
||||
|
||||
/** The system's page for this app's notification access (the detail page where Android has one). */
|
||||
fun settingsIntent(context: Context): Intent = if (Build.VERSION.SDK_INT >= 30) {
|
||||
Intent(Settings.ACTION_NOTIFICATION_LISTENER_DETAIL_SETTINGS)
|
||||
.putExtra(Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME, ComponentName(context, LembasNotificationListener::class.java).flattenToString())
|
||||
} else {
|
||||
Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
|
||||
}.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
|
||||
/** The notifications up now, newest first -- or none, when locked or not allowed. */
|
||||
fun read(context: Context): List<Notice> {
|
||||
if (context.getSystemService(KeyguardManager::class.java).isKeyguardLocked) return emptyList()
|
||||
val listener = LembasNotificationListener.current ?: return emptyList()
|
||||
val shown = runCatching { listener.activeNotifications }.getOrNull().orEmpty()
|
||||
return shown
|
||||
.filter { it.packageName != context.packageName && !it.isOngoing && it.notification.flags and Notification.FLAG_GROUP_SUMMARY == 0 }
|
||||
.sortedByDescending { it.postTime }
|
||||
.mapNotNull { notice(context, it) }
|
||||
.take(MAX_NOTICES)
|
||||
}
|
||||
|
||||
private fun notice(context: Context, sbn: StatusBarNotification): Notice? {
|
||||
val extras = sbn.notification.extras
|
||||
val title = extras.getCharSequence(Notification.EXTRA_TITLE)?.toString().orEmpty()
|
||||
val lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES)?.joinToString("\n").orEmpty()
|
||||
val text = (extras.getCharSequence(Notification.EXTRA_BIG_TEXT) ?: extras.getCharSequence(Notification.EXTRA_TEXT))?.toString()
|
||||
?.ifBlank { null } ?: lines
|
||||
if (title.isBlank() && text.isBlank()) return null
|
||||
val app = runCatching {
|
||||
val pm = context.packageManager
|
||||
pm.getApplicationLabel(pm.getApplicationInfo(sbn.packageName, 0)).toString()
|
||||
}.getOrDefault(sbn.packageName)
|
||||
return Notice(app, title, text.take(MAX_TEXT), sbn.postTime)
|
||||
}
|
||||
|
||||
/** The notifications as the question's attachment of text: one block per notification. */
|
||||
fun asText(notices: List<Notice>, now: Long = System.currentTimeMillis()): String = notices.joinToString("\n\n") { n ->
|
||||
val minutes = ((now - n.at) / 60_000).coerceAtLeast(0)
|
||||
@@ -96,7 +25,5 @@ object NotificationReader {
|
||||
return phrases.map { it.trim().lowercase() }.any { it.isNotEmpty() && it in q }
|
||||
}
|
||||
|
||||
private const val MAX_NOTICES = 40
|
||||
private const val MAX_TEXT = 600
|
||||
private const val MAX_CHARS = 12_000
|
||||
}
|
||||
@@ -96,7 +96,7 @@ import eu.llembas.android.assist.PhoneRecognizer
|
||||
import eu.llembas.android.auth.Instance
|
||||
import eu.llembas.android.auth.Prefs
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.notify.NotificationReader
|
||||
import eu.llembas.android.notify.NotificationAccess
|
||||
import eu.llembas.android.push.Push
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
import eu.llembas.android.update.AppVersion
|
||||
@@ -179,6 +179,8 @@ data class SettingsState(
|
||||
val wakeTest: WakeTestShown = WakeTestShown(),
|
||||
/** Whether Android's notification access is given to LLeMbas. */
|
||||
val notificationAccess: Boolean = false,
|
||||
/** Whether this build can read notifications at all (only the `notifications` build declares the listener). */
|
||||
val notificationsBuild: Boolean = false,
|
||||
/** The navigation apps installed: NavApp ids. */
|
||||
val navApps: List<String> = emptyList(),
|
||||
)
|
||||
@@ -318,11 +320,11 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
// On: Android's own switch is the person's to flip, so its page opens if it is off.
|
||||
override fun notificationSummaries(on: Boolean) {
|
||||
scope.launch { store.savePrefs { it.copy(notificationSummaries = on) } }
|
||||
if (on && !NotificationReader.granted(context)) notificationAccess()
|
||||
if (on && !NotificationAccess.granted(context)) notificationAccess()
|
||||
}
|
||||
|
||||
override fun notificationAccess() {
|
||||
runCatching { context.startActivity(NotificationReader.settingsIntent(context)) }
|
||||
NotificationAccess.settingsIntent(context)?.let { page -> runCatching { context.startActivity(page) } }
|
||||
}
|
||||
|
||||
override fun navApp(id: String) {
|
||||
@@ -391,7 +393,8 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
wakeState = wakeState,
|
||||
wakeTest = WakeTestShown(running = clock < trial.until, done = tested && clock >= trial.until, heard = if (tested) trial.heard else 0),
|
||||
// Read again each time Settings is composed: the person comes back from the system's page.
|
||||
notificationAccess = NotificationReader.granted(context),
|
||||
notificationAccess = NotificationAccess.granted(context),
|
||||
notificationsBuild = NotificationAccess.AVAILABLE,
|
||||
navApps = remember { NavApp.installedApps(context).map { it.id } },
|
||||
),
|
||||
actions,
|
||||
@@ -815,36 +818,51 @@ private fun PhoneActionsGroup(state: SettingsState, actions: SettingsActions) {
|
||||
)
|
||||
}
|
||||
|
||||
/** Notification summaries: off by default; on needs Android's notification access too, which this explains. */
|
||||
/**
|
||||
* Notification summaries: off by default; on needs Android's notification access too, which this
|
||||
* explains. In a build without the listener the group says which build has it, and why.
|
||||
*/
|
||||
@Composable
|
||||
private fun NotificationSummaryGroup(state: SettingsState, actions: SettingsActions) {
|
||||
val palette = LocalPalette.current
|
||||
val on = state.prefs.notificationSummaries
|
||||
Group(stringResource(R.string.notification_summaries)) {
|
||||
SettingRow(
|
||||
Icons.Filled.Notifications,
|
||||
stringResource(R.string.notification_summaries_switch),
|
||||
subtitle = stringResource(R.string.notification_summaries_line),
|
||||
onClick = { actions.notificationSummaries(!on) },
|
||||
trailing = { Switch(checked = on, onCheckedChange = actions::notificationSummaries) },
|
||||
)
|
||||
if (on) {
|
||||
Rule()
|
||||
if (!state.notificationsBuild) {
|
||||
SettingRow(
|
||||
Icons.Filled.Security,
|
||||
stringResource(R.string.notification_access),
|
||||
subtitle = stringResource(if (state.notificationAccess) R.string.notification_access_on else R.string.notification_access_off),
|
||||
tint = if (state.notificationAccess) null else palette.warning,
|
||||
onClick = actions::notificationAccess,
|
||||
Icons.Filled.Notifications,
|
||||
stringResource(R.string.notification_summaries_switch),
|
||||
subtitle = stringResource(R.string.notification_summaries_unavailable),
|
||||
enabled = false,
|
||||
onClick = {},
|
||||
)
|
||||
} else {
|
||||
SettingRow(
|
||||
Icons.Filled.Notifications,
|
||||
stringResource(R.string.notification_summaries_switch),
|
||||
subtitle = stringResource(R.string.notification_summaries_line),
|
||||
onClick = { actions.notificationSummaries(!on) },
|
||||
trailing = { Switch(checked = on, onCheckedChange = actions::notificationSummaries) },
|
||||
)
|
||||
if (on) {
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.Security,
|
||||
stringResource(R.string.notification_access),
|
||||
subtitle = stringResource(if (state.notificationAccess) R.string.notification_access_on else R.string.notification_access_off),
|
||||
tint = if (state.notificationAccess) null else palette.warning,
|
||||
onClick = actions::notificationAccess,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Text(
|
||||
stringResource(R.string.notification_summaries_privacy),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = palette.inkFaint,
|
||||
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
)
|
||||
if (state.notificationsBuild) {
|
||||
Text(
|
||||
stringResource(R.string.notification_summaries_privacy),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = palette.inkFaint,
|
||||
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@@ -13,6 +13,7 @@ import eu.llembas.android.R
|
||||
import eu.llembas.android.api.LembasJson
|
||||
import eu.llembas.android.api.SharedHttp
|
||||
import eu.llembas.android.api.await
|
||||
import eu.llembas.android.notify.NotificationAccess
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.Serializable
|
||||
@@ -20,9 +21,24 @@ import okhttp3.Request
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
|
||||
/** What llembas.eu says the newest release is (`site/android/latest.json`, written at release). */
|
||||
/** One APK of a release: where it is and its SHA-256. */
|
||||
@Serializable
|
||||
data class Latest(val version: String, val url: String, val sha256: String, val notes: String = "")
|
||||
data class LatestFile(val url: String, val sha256: String)
|
||||
|
||||
/**
|
||||
* What llembas.eu says the newest release is (`site/android/latest.json`, written at release):
|
||||
* the standard APK as [url] and [sha256], and the `notifications` build's (the same app with the
|
||||
* notification listener) as [notifications] when the release has one.
|
||||
*/
|
||||
@Serializable
|
||||
data class Latest(val version: String, val url: String, val sha256: String, val notes: String = "", val notifications: LatestFile? = null) {
|
||||
/**
|
||||
* The file this build updates itself to: its own kind, never the other. A `notifications`
|
||||
* build that installed the standard APK would lose its listener without a word (same id,
|
||||
* same key: Android takes it), so without its own file it installs nothing.
|
||||
*/
|
||||
fun fileFor(notificationsBuild: Boolean): LatestFile? = if (notificationsBuild) notifications else LatestFile(url, sha256)
|
||||
}
|
||||
|
||||
/**
|
||||
* The `full` build updates itself, as LLeMbas Desktop does: the manifest on the website names
|
||||
@@ -47,12 +63,13 @@ class Updater(private val context: Context) {
|
||||
)
|
||||
return@withContext context.getString(R.string.update_allow_installs)
|
||||
}
|
||||
val file = latest.fileFor(NotificationAccess.AVAILABLE) ?: return@withContext context.getString(R.string.update_notifications_manual)
|
||||
val apk = File(context.cacheDir, "update.apk")
|
||||
SharedHttp.newCall(Request.Builder().url(latest.url).build()).await().use { response ->
|
||||
SharedHttp.newCall(Request.Builder().url(file.url).build()).await().use { response ->
|
||||
if (!response.isSuccessful) return@withContext context.getString(R.string.update_unreachable)
|
||||
apk.outputStream().use { out -> response.body!!.byteStream().copyTo(out) }
|
||||
}
|
||||
if (sha256(apk) != latest.sha256.lowercase()) {
|
||||
if (sha256(apk) != file.sha256.lowercase()) {
|
||||
apk.delete()
|
||||
return@withContext context.getString(R.string.update_bad_file)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user