Skip to content

Commit

Permalink
add support for custom notification action in AppSwitchNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
octocorvus committed May 11, 2024
1 parent dd7e6a8 commit 31ffeb5
Showing 1 changed file with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* Example:
* Title: $APP tried to access a resource
* Text: Tap to open settings (tapping on notification opens a per-app AppSwitch setting)
* Actions (optional): "Don't show again", "More info"
* Actions (optional): "Don't show again", "More info", a custom action
*
* Rate-limiting is applied for each (app, setting) pair. At most one notification can be shown
* for each such pair within 30 seconds.
Expand All @@ -59,6 +59,9 @@ public class AppSwitchNotification {
@Nullable public Intent moreInfoIntent; // optional, shows up as "More info" notif action
public int gosPsFlagSuppressNotif; // optional, adds "Don't show again" notif action

private IntentReceiver customNotifActionReceiver;
private int customNotifActionTitleRes;

@Nullable
public static AppSwitchNotification maybeCreate(Context ctx, String firstPackageName,
int packageUid, String settingsIntentAction) {
Expand Down Expand Up @@ -119,6 +122,14 @@ private void checkInited() {
requireNonNull(settingsIntentAction, "settingsIntentAction");
}

private Bundle getDefaultNotifArgs(int notifId) {
var args = new Bundle();
args.putString(Intent.EXTRA_PACKAGE_NAME, pkgName);
args.putParcelable(Intent.EXTRA_USER, userHandle);
args.putInt(EXTRA_NOTIF_ID, notifId);
return args;
}

public void maybeShow() {
final long timestamp = SystemClock.uptimeMillis();

Expand Down Expand Up @@ -189,40 +200,71 @@ public void maybeShow() {
}

if (gosPsFlagSuppressNotif != 0) {
var args = new Bundle();
args.putString(Intent.EXTRA_PACKAGE_NAME, pkgName);
args.putParcelable(Intent.EXTRA_USER, userHandle);
Bundle args = getDefaultNotifArgs(notifId);
args.putInt(EXTRA_GOSPS_FLAG_SUPPRESS_NOTIF, gosPsFlagSuppressNotif);
args.putInt(EXTRA_NOTIF_ID, notifId);

PendingIntent dontShowAgainPi = IntentReceiver.getPendingIntent(
NotifActionReceiver.class, NotifActionReceiver::new, args, ctx);

addNotifAction(ctx, dontShowAgainPi, R.string.notification_action_dont_show_again, nb);
}

if (customNotifActionReceiver != null) {
Bundle args = getDefaultNotifArgs(notifId);

PendingIntent customNotifActionPi = customNotifActionReceiver.getPendingIntent(args);

addNotifAction(ctx, customNotifActionPi, customNotifActionTitleRes, nb);
}

ctx.getSystemService(NotificationManager.class)
.notifyAsUser(null, notifId, nb.build(), userHandle);
}

public void setCustomNotifAction(IntentReceiver receiver, int titleRes) {
customNotifActionReceiver = receiver;
customNotifActionTitleRes = titleRes;
}

static final String EXTRA_GOSPS_FLAG_SUPPRESS_NOTIF = "gosps_flag_suppress_notif";
static final String EXTRA_NOTIF_ID = "notif_id";

static class NotifActionReceiver extends IntentReceiver {
static class NotifActionReceiver extends CustomNotifActionReceiver {
@Override
public void onReceive(Context ctx, Bundle args) {
String packageName = args.getString(Intent.EXTRA_PACKAGE_NAME);
UserHandle user = args.getParcelable(Intent.EXTRA_USER, UserHandle.class);
public void onReceive(Context ctx, String packageName, UserHandle user) {
Bundle args = getArgs();

int gosPsFlagSuppressNotif = args.getNumber(EXTRA_GOSPS_FLAG_SUPPRESS_NOTIF);

var pmi = LocalServices.getService(PackageManagerInternal.class);

GosPackageStatePm.getEditor(pmi, packageName, user.getIdentifier())
.addFlags(gosPsFlagSuppressNotif)
.apply();
}
}

public abstract static class CustomNotifActionReceiver extends IntentReceiver {
private Bundle args;

final protected Bundle getArgs() {
return args;
}

@Override
final public void onReceive(Context ctx, Bundle args) {
this.args = args;

String packageName = args.getString(Intent.EXTRA_PACKAGE_NAME);
UserHandle user = args.getParcelable(Intent.EXTRA_USER, UserHandle.class);

onReceive(ctx, packageName, user);

int notifId = args.getNumber(EXTRA_NOTIF_ID);

ctx.getSystemService(NotificationManager.class).cancelAsUser(null, notifId, user);
}

public abstract void onReceive(Context ctx, String packageName, UserHandle user);
}
}

0 comments on commit 31ffeb5

Please sign in to comment.