summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorSudheer Shanka <sudheersai@google.com>2022-02-16 17:05:35 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2022-02-16 17:05:35 +0000
commita44a2100e1d90ff3d529ae774f2c702853b251b9 (patch)
tree75a31731ac7d87868445e73280c0770f310e526d /core/java/android
parent639fa579eed20f1751db23e14882b2a5443bebfa (diff)
parentd807ffe41f9007207fc38ddedc5bfd3915e29407 (diff)
Merge "Add an API to bulk query broadcast response stats for all pkgs."
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/app/usage/BroadcastResponseStats.java54
-rw-r--r--core/java/android/app/usage/BroadcastResponseStatsList.aidl20
-rw-r--r--core/java/android/app/usage/BroadcastResponseStatsList.java83
-rw-r--r--core/java/android/app/usage/IUsageStatsManager.aidl5
-rw-r--r--core/java/android/app/usage/UsageStatsManager.java67
5 files changed, 210 insertions, 19 deletions
diff --git a/core/java/android/app/usage/BroadcastResponseStats.java b/core/java/android/app/usage/BroadcastResponseStats.java
index 5acc3dda9e48..e1d37e1b1ae0 100644
--- a/core/java/android/app/usage/BroadcastResponseStats.java
+++ b/core/java/android/app/usage/BroadcastResponseStats.java
@@ -23,6 +23,8 @@ import android.app.BroadcastOptions;
import android.os.Parcel;
import android.os.Parcelable;
+import java.util.Objects;
+
/**
* Class containing a collection of stats related to response events started from an app
* after receiving a broadcast.
@@ -32,17 +34,30 @@ import android.os.Parcelable;
@SystemApi
public final class BroadcastResponseStats implements Parcelable {
private final String mPackageName;
+ private final long mId;
private int mBroadcastsDispatchedCount;
private int mNotificationsPostedCount;
private int mNotificationsUpdatedCount;
private int mNotificationsCancelledCount;
- public BroadcastResponseStats(@NonNull String packageName) {
+ /**
+ * Creates a new {@link BroadcastResponseStats} object that contain the stats for broadcasts
+ * with {@code id} (specified using
+ * {@link BroadcastOptions#recordResponseEventWhileInBackground(long)} by the sender) that
+ * were sent to {@code packageName}.
+ *
+ * @param packageName the name of the package that broadcasts were sent to.
+ * @param id the ID specified by the sender using
+ * {@link BroadcastOptions#recordResponseEventWhileInBackground(long)}.
+ */
+ public BroadcastResponseStats(@NonNull String packageName, @IntRange(from = 1) long id) {
mPackageName = packageName;
+ mId = id;
}
private BroadcastResponseStats(@NonNull Parcel in) {
mPackageName = in.readString8();
+ mId = in.readLong();
mBroadcastsDispatchedCount = in.readInt();
mNotificationsPostedCount = in.readInt();
mNotificationsUpdatedCount = in.readInt();
@@ -58,6 +73,14 @@ public final class BroadcastResponseStats implements Parcelable {
}
/**
+ * @return the ID of the broadcasts that the stats in this object correspond to.
+ */
+ @IntRange(from = 1)
+ public long getId() {
+ return mId;
+ }
+
+ /**
* Returns the total number of broadcasts that were dispatched to the app by the caller.
*
* <b> Note that the returned count will only include the broadcasts that the caller explicitly
@@ -148,9 +171,35 @@ public final class BroadcastResponseStats implements Parcelable {
}
@Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || !(obj instanceof BroadcastResponseStats)) {
+ return false;
+ }
+ final BroadcastResponseStats other = (BroadcastResponseStats) obj;
+ return this.mBroadcastsDispatchedCount == other.mBroadcastsDispatchedCount
+ && this.mNotificationsPostedCount == other.mNotificationsPostedCount
+ && this.mNotificationsUpdatedCount == other.mNotificationsUpdatedCount
+ && this.mNotificationsCancelledCount == other.mNotificationsCancelledCount
+ && this.mId == other.mId
+ && this.mPackageName.equals(other.mPackageName);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(mPackageName, mId, mBroadcastsDispatchedCount,
+ mNotificationsPostedCount, mNotificationsUpdatedCount,
+ mNotificationsCancelledCount);
+ }
+
+ @Override
public @NonNull String toString() {
return "stats {"
- + "broadcastsSent=" + mBroadcastsDispatchedCount
+ + "package=" + mPackageName
+ + ",id=" + mId
+ + ",broadcastsSent=" + mBroadcastsDispatchedCount
+ ",notificationsPosted=" + mNotificationsPostedCount
+ ",notificationsUpdated=" + mNotificationsUpdatedCount
+ ",notificationsCancelled=" + mNotificationsCancelledCount
@@ -165,6 +214,7 @@ public final class BroadcastResponseStats implements Parcelable {
@Override
public void writeToParcel(@NonNull Parcel dest, @WriteFlags int flags) {
dest.writeString8(mPackageName);
+ dest.writeLong(mId);
dest.writeInt(mBroadcastsDispatchedCount);
dest.writeInt(mNotificationsPostedCount);
dest.writeInt(mNotificationsUpdatedCount);
diff --git a/core/java/android/app/usage/BroadcastResponseStatsList.aidl b/core/java/android/app/usage/BroadcastResponseStatsList.aidl
new file mode 100644
index 000000000000..73803599c19e
--- /dev/null
+++ b/core/java/android/app/usage/BroadcastResponseStatsList.aidl
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.app.usage;
+
+/** {@hide} */
+parcelable BroadcastResponseStatsList; \ No newline at end of file
diff --git a/core/java/android/app/usage/BroadcastResponseStatsList.java b/core/java/android/app/usage/BroadcastResponseStatsList.java
new file mode 100644
index 000000000000..4d2ff2866d0e
--- /dev/null
+++ b/core/java/android/app/usage/BroadcastResponseStatsList.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.app.usage;
+
+import android.annotation.NonNull;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/** @hide */
+public final class BroadcastResponseStatsList implements Parcelable {
+ private List<BroadcastResponseStats> mBroadcastResponseStats;
+
+ public BroadcastResponseStatsList(
+ @NonNull List<BroadcastResponseStats> broadcastResponseStats) {
+ mBroadcastResponseStats = broadcastResponseStats;
+ }
+
+ private BroadcastResponseStatsList(@NonNull Parcel in) {
+ mBroadcastResponseStats = new ArrayList<>();
+ final byte[] bytes = in.readBlob();
+ final Parcel data = Parcel.obtain();
+ try {
+ data.unmarshall(bytes, 0, bytes.length);
+ data.setDataPosition(0);
+ data.readTypedList(mBroadcastResponseStats, BroadcastResponseStats.CREATOR);
+ } finally {
+ data.recycle();
+ }
+ }
+
+ @NonNull
+ public List<BroadcastResponseStats> getList() {
+ return mBroadcastResponseStats == null ? Collections.emptyList() : mBroadcastResponseStats;
+ }
+
+ @Override
+ public @ContentsFlags int describeContents() {
+ return 0;
+ }
+
+ @Override
+ public void writeToParcel(@NonNull Parcel dest, @WriteFlags int flags) {
+ final Parcel data = Parcel.obtain();
+ try {
+ data.writeTypedList(mBroadcastResponseStats);
+ dest.writeBlob(data.marshall());
+ } finally {
+ data.recycle();
+ }
+ }
+
+ public static final @NonNull Creator<BroadcastResponseStatsList> CREATOR =
+ new Creator<BroadcastResponseStatsList>() {
+ @Override
+ public @NonNull BroadcastResponseStatsList createFromParcel(
+ @NonNull Parcel source) {
+ return new BroadcastResponseStatsList(source);
+ }
+
+ @Override
+ public @NonNull BroadcastResponseStatsList[] newArray(int size) {
+ return new BroadcastResponseStatsList[size];
+ }
+ };
+}
diff --git a/core/java/android/app/usage/IUsageStatsManager.aidl b/core/java/android/app/usage/IUsageStatsManager.aidl
index 6f8fea15f200..a43071418b2f 100644
--- a/core/java/android/app/usage/IUsageStatsManager.aidl
+++ b/core/java/android/app/usage/IUsageStatsManager.aidl
@@ -18,6 +18,7 @@ package android.app.usage;
import android.app.PendingIntent;
import android.app.usage.BroadcastResponseStats;
+import android.app.usage.BroadcastResponseStatsList;
import android.app.usage.UsageEvents;
import android.content.pm.ParceledListSlice;
@@ -73,9 +74,11 @@ interface IUsageStatsManager {
void forceUsageSourceSettingRead();
long getLastTimeAnyComponentUsed(String packageName, String callingPackage);
@JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS)")
- BroadcastResponseStats queryBroadcastResponseStats(
+ BroadcastResponseStatsList queryBroadcastResponseStats(
String packageName, long id, String callingPackage, int userId);
@JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS)")
void clearBroadcastResponseStats(String packageName, long id, String callingPackage,
int userId);
+ @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS)")
+ void clearBroadcastEvents(String callingPackage, int userId);
}
diff --git a/core/java/android/app/usage/UsageStatsManager.java b/core/java/android/app/usage/UsageStatsManager.java
index b81c62d29076..d7152b3676df 100644
--- a/core/java/android/app/usage/UsageStatsManager.java
+++ b/core/java/android/app/usage/UsageStatsManager.java
@@ -1397,29 +1397,46 @@ public final class UsageStatsManager {
* Returns the broadcast response stats since the last boot corresponding to
* {@code packageName} and {@code id}.
*
- * <p>Broadcast response stats will include the aggregated data of what actions an app took upon
- * receiving a broadcast. This data will consider the broadcasts that the caller sent to
+ * <p> Broadcast response stats will include the aggregated data of what actions an app took
+ * upon receiving a broadcast. This data will consider the broadcasts that the caller sent to
* {@code packageName} and explicitly requested to record the response events using
* {@link BroadcastOptions#recordResponseEventWhileInBackground(long)}.
*
- * @param packageName The name of the package that the caller wants to query for.
- * @param id The ID corresponding to the broadcasts that the caller wants to query for. This is
- * the ID the caller specifies when requesting a broadcast response event to be
- * recorded using {@link BroadcastOptions#recordResponseEventWhileInBackground(long)}.
+ * <p> The returned list could one or more {@link BroadcastResponseStats} objects or be empty
+ * depending on the {@code packageName} and {@code id} and whether there is any data
+ * corresponding to these. If the {@code packageName} is not {@code null} and {@code id} is
+ * {@code > 0}, then the returned list would contain at most one {@link BroadcastResponseStats}
+ * object. Otherwise, the returned list could contain more than one
+ * {@link BroadcastResponseStats} object in no particular order.
*
- * @return the broadcast response stats corresponding to {@code packageName} and {@code id}.
+ * <p> Note: It is possible that same {@code id} was used for broadcasts sent to different
+ * packages. So, callers can query the data corresponding to
+ * all broadcasts with a particular {@code id} by passing {@code packageName} as {@code null}.
*
+ * @param packageName The name of the package that the caller wants to query for
+ * or {@code null} to indicate that data corresponding to all packages
+ * should be returned.
+ * @param id The ID corresponding to the broadcasts that the caller wants to query for, or
+ * {@code 0} to indicate that data corresponding to all IDs should be returned.
+ * This is the ID the caller specifies when requesting a broadcast response event
+ * to be recorded using
+ * {@link BroadcastOptions#recordResponseEventWhileInBackground(long)}.
+ *
+ * @return the list of broadcast response stats corresponding to {@code packageName}
+ * and {@code id}.
+ *
+ * @see #clearBroadcastResponseStats(String, long)
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS)
@UserHandleAware
@NonNull
- public BroadcastResponseStats queryBroadcastResponseStats(
- @NonNull String packageName, @IntRange(from = 1) long id) {
+ public List<BroadcastResponseStats> queryBroadcastResponseStats(
+ @Nullable String packageName, @IntRange(from = 0) long id) {
try {
return mService.queryBroadcastResponseStats(packageName, id,
- mContext.getOpPackageName(), mContext.getUserId());
+ mContext.getOpPackageName(), mContext.getUserId()).getList();
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}
@@ -1428,12 +1445,15 @@ public final class UsageStatsManager {
/**
* Clears the broadcast response stats corresponding to {@code packageName} and {@code id}.
*
- * When a caller uses this API, stats related to the events occurring till that point will be
- * cleared and subsequent calls to {@link #queryBroadcastResponseStats(String, long)} will
+ * <p> When a caller uses this API, stats related to the events occurring till that point will
+ * be cleared and subsequent calls to {@link #queryBroadcastResponseStats(String, long)} will
* return stats related to events occurring after this.
*
- * @param packageName The name of the package that the caller wants to clear the data for.
- * @param id The ID corresponding to the broadcasts that the caller wants to clear the data for.
+ * @param packageName The name of the package that the caller wants to clear the data for or
+ * {@code null} to indicate that data corresponding to all packages should
+ * be cleared.
+ * @param id The ID corresponding to the broadcasts that the caller wants to clear the data
+ * for, or {code 0} to indicate that data corresponding to all IDs should be deleted.
* This is the ID the caller specifies when requesting a broadcast response event
* to be recorded using
* {@link BroadcastOptions#recordResponseEventWhileInBackground(long)}.
@@ -1444,8 +1464,8 @@ public final class UsageStatsManager {
@SystemApi
@RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS)
@UserHandleAware
- public void clearBroadcastResponseStats(@NonNull String packageName,
- @IntRange(from = 1) long id) {
+ public void clearBroadcastResponseStats(@Nullable String packageName,
+ @IntRange(from = 0) long id) {
try {
mService.clearBroadcastResponseStats(packageName, id,
mContext.getOpPackageName(), mContext.getUserId());
@@ -1453,4 +1473,19 @@ public final class UsageStatsManager {
throw re.rethrowFromSystemServer();
}
}
+
+ /**
+ * Clears the broadcast events that were sent by the caller uid.
+ *
+ * @hide
+ */
+ @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS)
+ @UserHandleAware
+ public void clearBroadcastEvents() {
+ try {
+ mService.clearBroadcastEvents(mContext.getOpPackageName(), mContext.getUserId());
+ } catch (RemoteException re) {
+ throw re.rethrowFromSystemServer();
+ }
+ }
}