Make a fallback tag for listener alarms

The deprecated .set API does not allow callers to pass in a tag to
identify their listener alarms. Making a fallback with a unique format
to identify the calls from this API.

Test: Manually inspect the alarm dump.

Bug: 141785404
Change-Id: Ifacc260a53f1d3d377dc156ce3a2404cb87684cb
This commit is contained in:
Suprabh Shukla
2023-02-06 18:29:38 -08:00
parent 88f9d7935c
commit 23fa8ac9f4

View File

@@ -36,7 +36,9 @@ import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.WorkSource;
import android.text.TextUtils;
import android.util.Log;
@@ -90,6 +92,14 @@ import java.util.concurrent.Executor;
public class AlarmManager {
private static final String TAG = "AlarmManager";
/**
* Prefix used by {{@link #makeTag(long, WorkSource)}} to make a tag on behalf of the caller
* when the {@link #set(int, long, long, long, OnAlarmListener, Handler, WorkSource)} API is
* used. This prefix is a unique sequence of characters to differentiate with other tags that
* apps may provide to other APIs that accept a listener callback.
*/
private static final String GENERATED_TAG_PREFIX = "$android.alarm.generated";
/** @hide */
@IntDef(prefix = { "RTC", "ELAPSED" }, value = {
RTC_WAKEUP,
@@ -911,6 +921,24 @@ public class AlarmManager {
targetHandler, workSource, null);
}
/**
* This is only used to make an identifying tag for the deprecated
* {@link #set(int, long, long, long, OnAlarmListener, Handler, WorkSource)} API which doesn't
* accept a tag. For all other APIs, the tag provided by the app is used, even if it is
* {@code null}.
*/
private static String makeTag(long triggerMillis, WorkSource ws) {
final StringBuilder tagBuilder = new StringBuilder(GENERATED_TAG_PREFIX);
tagBuilder.append(":");
final int attributionUid =
(ws == null || ws.isEmpty()) ? Process.myUid() : ws.getAttributionUid();
tagBuilder.append(UserHandle.formatUid(attributionUid));
tagBuilder.append(":");
tagBuilder.append(triggerMillis);
return tagBuilder.toString();
}
/**
* Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}.
* Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
@@ -937,8 +965,8 @@ public class AlarmManager {
public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
long intervalMillis, @NonNull OnAlarmListener listener, @Nullable Handler targetHandler,
@Nullable WorkSource workSource) {
setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null,
targetHandler, workSource, null);
setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener,
makeTag(triggerAtMillis, workSource), targetHandler, workSource, null);
}
/**