Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions apps/app/android/alpha-notifications/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.library'

android {
namespace 'ai.alphacompute.notifications'
compileSdk 36

defaultConfig {
minSdk 29
targetSdk 36
consumerProguardFiles 'consumer-rules.pro'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

testOptions {
unitTests.returnDefaultValues = true
}
}

dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.json:json:20250517'
}
1 change: 1 addition & 0 deletions apps/app/android/alpha-notifications/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class ai.alphacompute.notifications.** { *; }
18 changes: 18 additions & 0 deletions apps/app/android/alpha-notifications/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<queries>
<package android:name="ai.alphacompute.notificationassistant" />
</queries>

<application>
<service
android:name="ai.alphacompute.notifications.AlphaNotificationSummaryBridgeService"
android:directBootAware="false"
android:exported="true"
android:permission="android.permission.STATUS_BAR_SERVICE">
<intent-filter>
<action android:name="ai.alphacompute.notifications.ACTION_NOTIFICATION_SUMMARY_BRIDGE" />
</intent-filter>
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package ai.alphacompute.notifications;

import android.app.Service;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.json.JSONException;

/**
* Binder bridge hosted inside {@code ai.alphacompute.alphaphone} that receives sanitized
* notification batches solely from the platform-signed {@code AlphaNotificationAssistant}
* system package and forwards them to Alpha's local agent router via {@link NotificationSummaryClient}.
*/
public final class AlphaNotificationSummaryBridgeService extends Service {
/** Sole authorized platform-signed NAS caller package. */
public static final String NAS_PACKAGE = "ai.alphacompute.notificationassistant";

/** Shared Binder descriptor matching {@code AlphaNotificationAssistantService}. */
public static final String BRIDGE_DESCRIPTOR =
"ai.alphacompute.notifications.IAlphaNotificationSummaryBridge";

/** Binder transaction code for single-batch summary requests. */
public static final int TRANSACTION_SUMMARIZE = IBinder.FIRST_CALL_TRANSACTION;

private static final AtomicReference<NotificationSummaryClient> CLIENT_REF =
new AtomicReference<>();
private static final AtomicReference<Set<String>> OPTED_OUT_REF =
new AtomicReference<>(Collections.emptySet());

/** Configures the active {@link NotificationSummaryClient} bound to the resident agent. */
public static void configureClient(NotificationSummaryClient client) {
CLIENT_REF.set(client);
}

/** Updates the cached set of owner-excluded packages mirrored from agent policy. */
public static void setOptedOutPackages(Set<String> packages) {
OPTED_OUT_REF.set(packages == null ? Collections.emptySet() : Set.copyOf(packages));
}

private final Binder binder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws RemoteException {
if (code != TRANSACTION_SUMMARIZE) {
return super.onTransact(code, data, reply, flags);
}
data.enforceInterface(BRIDGE_DESCRIPTOR);
enforceVerifiedNasCaller();
final String requestJson = data.readString();
final NotificationSummaryClient client = CLIENT_REF.get();
final NotificationSummaryClient effective = client != null
? client
: new NotificationSummaryClient(null);
try {
final String response = effective
.handleBridgeRequestJson(requestJson, OPTED_OUT_REF.get())
.toString();
reply.writeNoException();
reply.writeString(response);
return true;
} catch (JSONException failed) {
reply.writeNoException();
reply.writeString("{\"redacted\":true}");
return true;
}
}
};

@Override
public IBinder onBind(Intent intent) {
return binder;
}

private void enforceVerifiedNasCaller() {
final int callingUid = Binder.getCallingUid();
if (callingUid <= 0) {
throw new SecurityException("Invalid calling UID");
}
final PackageManager pm = getPackageManager();
if (pm == null) {
throw new SecurityException("PackageManager unavailable");
}
final String[] packages = pm.getPackagesForUid(callingUid);
if (packages == null || packages.length != 1 || !NAS_PACKAGE.equals(packages[0])) {
throw new SecurityException("Caller is not AlphaNotificationAssistant");
}
try {
final PackageInfo info = pm.getPackageInfo(NAS_PACKAGE, PackageManager.MATCH_SYSTEM_ONLY);
if (info == null || info.applicationInfo == null) {
throw new SecurityException("AlphaNotificationAssistant is not a system package");
}
final int flags = info.applicationInfo.flags;
final boolean isSystem = (flags
& (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
if (!isSystem || !info.applicationInfo.enabled) {
throw new SecurityException("AlphaNotificationAssistant is inactive");
}
if (pm.checkSignatures("android", NAS_PACKAGE) != PackageManager.SIGNATURE_MATCH) {
throw new SecurityException("AlphaNotificationAssistant is not platform-signed");
}
} catch (PackageManager.NameNotFoundException missing) {
throw new SecurityException("AlphaNotificationAssistant not installed", missing);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package ai.alphacompute.notifications;

import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;

/**
* Fail-closed privacy classifier for incoming Android notifications.
*
* <p>Guarantees that 2FA/OTP codes, password managers, authenticators, banking/payment alerts,
* lockscreen-private notifications, and owner-excluded packages are redacted before any local
* LLM inference, cloud fallback, or digest persistence.
*/
public final class NotificationPrivacyFilter {
/** Matches {@code android.app.Notification.VISIBILITY_SECRET}. */
public static final int VISIBILITY_SECRET = -1;

/** Matches {@code android.app.Notification.VISIBILITY_PRIVATE}. */
public static final int VISIBILITY_PRIVATE = 0;

/** Matches {@code android.app.Notification.VISIBILITY_PUBLIC}. */
public static final int VISIBILITY_PUBLIC = 1;

/** Privacy classification mirroring {@code spec/app/notifications/index.md}. */
public enum Sensitivity {
CLEAR,
OTP_CODE,
CREDENTIAL,
FINANCIAL,
LOCKSCREEN_PRIVATE,
OPTED_OUT
}

/** Normalized notification payload inspected by the privacy filter. */
public static final class NotificationInput {
public final String packageName;
public final String title;
public final String text;
public final String bigText;
public final String category;
public final int visibility;
public final boolean deviceLocked;
public final boolean userUnlocked;

public NotificationInput(
String packageName,
String title,
String text,
String bigText,
String category,
int visibility,
boolean deviceLocked,
boolean userUnlocked) {
this.packageName = packageName == null ? "" : packageName.trim();
this.title = title == null ? "" : title.trim();
this.text = text == null ? "" : text.trim();
this.bigText = bigText == null ? "" : bigText.trim();
this.category = category == null ? "" : category.trim();
this.visibility = visibility;
this.deviceLocked = deviceLocked;
this.userUnlocked = userUnlocked;
}
}

/** Immutable result of fail-closed privacy screening. */
public static final class FilterDecision {
public final boolean allowed;
public final Sensitivity sensitivity;
public final String reason;

public FilterDecision(boolean allowed, Sensitivity sensitivity, String reason) {
this.allowed = allowed;
this.sensitivity = sensitivity;
this.reason = reason;
}
}

private static final Set<String> CREDENTIAL_PACKAGE_TOKENS = Set.of(
"1password", "onepassword", "bitwarden", "keepass", "proton.android.pass",
"dashlane", "lastpass", "enpass", "nordpass", "roboform", "keychain",
"authenticator", "authy", "duomobile", "yubico", "aegis", "2fas",
"freeotp", "ente.auth", "permissioncontroller", "packageinstaller",
"com.android.settings");

private static final Set<String> FINANCIAL_PACKAGE_TOKENS = Set.of(
"wallet", "bank", "chase", "wellsfargo", "bofa", "bankofamerica",
"citibank", "citi.mobile", "amex", "americanexpress", "capitalone",
"paypal", "venmo", "cashapp", "squareup.cash", "coinbase", "robinhood",
"schwab", "fidelity", "vanguard", "stripe", "wise", "transferwise",
"revolut", "monzo", "chime", "zelle", "metamask", "phantom",
"ai.alphacompute.wallet");

private static final Pattern OTP_DIGITS_PATTERN = Pattern.compile(
"(?:^|[^0-9A-Za-z])(?:[A-Za-z]-)?\\d{4,8}(?:$|[^0-9A-Za-z])|"
+ "(?:^|[^0-9])\\d{3}[-\\s]\\d{3}(?:$|[^0-9])");

private static final Pattern OTP_CONTEXT_PATTERN = Pattern.compile(
"(?i)\\b(otp|2fa|mfa|totp|one[-\\s]?time|passcode|verification\\s+code|"
+ "security\\s+code|login\\s+code|sign[-\\s]?in\\s+code|auth(?:entication)?\\s+code|"
+ "confirm(?:ation)?\\s+code|do\\s+not\\s+share\\s+this\\s+code|"
+ "enter\\s+(?:this\\s+)?code|verify\\s+your\\s+(?:account|login|identity))\\b");

private static final Pattern CREDENTIAL_CONTENT_PATTERN = Pattern.compile(
"(?i)\\b(password\\s+reset|reset\\s+your\\s+password|temporary\\s+password|"
+ "recovery\\s+(?:code|phrase|key)|seed\\s+phrase|private\\s+key|passkey|"
+ "magic\\s+link|approve\\s+(?:this\\s+)?sign[-\\s]?in|login\\s+request|"
+ "two[-\\s]?factor|credential\\s+manager)\\b");

private static final Pattern FINANCIAL_CONTENT_PATTERN = Pattern.compile(
"(?i)\\b(wire\\s+transfer|direct\\s+deposit|bank\\s+account|routing\\s+number|"
+ "account\\s+balance|card\\s+ending\\s+in|credit\\s+card|debit\\s+card|"
+ "cvv|pin\\s+number|ssn|social\\s+security|fraud\\s+alert|suspicious\\s+charge|"
+ "payment\\s+(?:sent|received|declined|authorized)|withdrawal|ach\\s+transfer|"
+ "transaction\\s+alert|charge\\s+of\\s+\\$?\\d)\\b");

private NotificationPrivacyFilter() {}

/** Evaluates whether {@code input} may be summarized, failing closed on any sensitive signal. */
public static FilterDecision evaluate(NotificationInput input) {
return evaluate(input, Collections.emptySet());
}

/** Evaluates whether {@code input} may be summarized given the owner's opted-out packages. */
public static FilterDecision evaluate(NotificationInput input, Set<String> optedOutPackages) {
if (input == null || input.packageName.isEmpty()) {
return new FilterDecision(false, Sensitivity.LOCKSCREEN_PRIVATE, "missing_input");
}
if (!input.userUnlocked) {
return new FilterDecision(false, Sensitivity.LOCKSCREEN_PRIVATE, "user_locked");
}
if (input.visibility == VISIBILITY_SECRET) {
return new FilterDecision(false, Sensitivity.LOCKSCREEN_PRIVATE, "visibility_secret");
}
if (input.deviceLocked && input.visibility == VISIBILITY_PRIVATE) {
return new FilterDecision(false, Sensitivity.LOCKSCREEN_PRIVATE, "lockscreen_private");
}
if (optedOutPackages != null && optedOutPackages.contains(input.packageName)) {
return new FilterDecision(false, Sensitivity.OPTED_OUT, "package_opted_out");
}
final Sensitivity packageSensitivity = classifyPackage(input.packageName);
if (packageSensitivity != Sensitivity.CLEAR) {
return new FilterDecision(false, packageSensitivity, "sensitive_package");
}
final String combined = joinNonEmpty(input.title, input.text, input.bigText);
final Sensitivity textSensitivity = classifyText(combined);
if (textSensitivity != Sensitivity.CLEAR) {
return new FilterDecision(false, textSensitivity, "sensitive_content");
}
return new FilterDecision(true, Sensitivity.CLEAR, "clear");
}

/** Classifies sensitivity from package identity alone. */
public static Sensitivity classifyPackage(String packageName) {
if (packageName == null || packageName.trim().isEmpty()) {
return Sensitivity.LOCKSCREEN_PRIVATE;
}
final String lower = packageName.toLowerCase(Locale.ROOT);
if (containsAnyToken(lower, CREDENTIAL_PACKAGE_TOKENS)) {
return Sensitivity.CREDENTIAL;
}
if (containsAnyToken(lower, FINANCIAL_PACKAGE_TOKENS)) {
return Sensitivity.FINANCIAL;
}
return Sensitivity.CLEAR;
}

/** Classifies sensitivity from notification text or model output. */
public static Sensitivity classifyText(String text) {
if (text == null || text.trim().isEmpty()) {
return Sensitivity.CLEAR;
}
if (OTP_CONTEXT_PATTERN.matcher(text).find() && OTP_DIGITS_PATTERN.matcher(text).find()) {
return Sensitivity.OTP_CODE;
}
if (CREDENTIAL_CONTENT_PATTERN.matcher(text).find()) {
return Sensitivity.CREDENTIAL;
}
if (FINANCIAL_CONTENT_PATTERN.matcher(text).find()) {
return Sensitivity.FINANCIAL;
}
return Sensitivity.CLEAR;
}

/** Returns true when {@code text} contains OTP, credential, or financial indicators. */
public static boolean isSensitiveText(String text) {
return classifyText(text) != Sensitivity.CLEAR;
}

/** Returns true when {@code packageName} belongs to a protected credential or financial app. */
public static boolean isSensitivePackage(String packageName) {
return classifyPackage(packageName) != Sensitivity.CLEAR;
}

private static boolean containsAnyToken(String haystack, Set<String> tokens) {
for (String token : tokens) {
if (haystack.contains(token)) {
return true;
}
}
return false;
}

private static String joinNonEmpty(String a, String b, String c) {
final StringBuilder sb = new StringBuilder();
appendPiece(sb, a);
appendPiece(sb, b);
appendPiece(sb, c);
return sb.toString();
}

private static void appendPiece(StringBuilder sb, String piece) {
if (piece == null) {
return;
}
final String trimmed = piece.trim();
if (trimmed.isEmpty()) {
return;
}
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(trimmed);
}
}
Loading
Loading