2024-08-17 12:35:07 -04:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
2024-08-05 16:03:46 -04:00
|
|
|
From: Pinyao Ting <pinyaoting@google.com>
|
|
|
|
Date: Thu, 30 Nov 2023 23:12:39 +0000
|
|
|
|
Subject: [PATCH] Added throttle when reporting shortcut usage
|
|
|
|
|
|
|
|
Bug: 304290201
|
|
|
|
Test: manual
|
|
|
|
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:76121eb73d4c40829d5513b073871333520fe0a2)
|
|
|
|
Merged-In: I96370cbd4f6a55f894c1a93307e5f82dfd394652
|
|
|
|
Change-Id: I96370cbd4f6a55f894c1a93307e5f82dfd394652
|
|
|
|
---
|
|
|
|
.../android/server/pm/ShortcutPackage.java | 35 +++++++++++++++++++
|
|
|
|
.../android/server/pm/ShortcutService.java | 12 +++----
|
|
|
|
.../server/pm/ShortcutManagerTest2.java | 2 ++
|
|
|
|
3 files changed, 41 insertions(+), 8 deletions(-)
|
|
|
|
|
|
|
|
diff --git a/services/core/java/com/android/server/pm/ShortcutPackage.java b/services/core/java/com/android/server/pm/ShortcutPackage.java
|
2024-08-17 12:35:07 -04:00
|
|
|
index c6bc7576147f..da018ad04179 100644
|
2024-08-05 16:03:46 -04:00
|
|
|
--- a/services/core/java/com/android/server/pm/ShortcutPackage.java
|
|
|
|
+++ b/services/core/java/com/android/server/pm/ShortcutPackage.java
|
2024-08-17 12:35:07 -04:00
|
|
|
@@ -19,6 +19,7 @@ import android.annotation.NonNull;
|
2024-08-05 16:03:46 -04:00
|
|
|
import android.annotation.Nullable;
|
|
|
|
import android.annotation.UserIdInt;
|
|
|
|
import android.app.Person;
|
|
|
|
+import android.app.usage.UsageStatsManagerInternal;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2024-08-17 12:35:07 -04:00
|
|
|
@@ -28,12 +29,14 @@ import android.content.pm.ShortcutInfo;
|
2024-08-05 16:03:46 -04:00
|
|
|
import android.content.pm.ShortcutManager;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.os.PersistableBundle;
|
|
|
|
+import android.os.SystemClock;
|
|
|
|
import android.text.format.Formatter;
|
|
|
|
import android.util.ArrayMap;
|
|
|
|
import android.util.ArraySet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Slog;
|
|
|
|
|
|
|
|
+import com.android.internal.annotations.GuardedBy;
|
|
|
|
import com.android.internal.annotations.VisibleForTesting;
|
|
|
|
import com.android.internal.util.ArrayUtils;
|
|
|
|
import com.android.internal.util.Preconditions;
|
|
|
|
@@ -119,6 +122,11 @@ class ShortcutPackage extends ShortcutPackageItem {
|
|
|
|
private static final String KEY_BITMAPS = "bitmaps";
|
|
|
|
private static final String KEY_BITMAP_BYTES = "bitmapBytes";
|
|
|
|
|
|
|
|
+ @VisibleForTesting
|
|
|
|
+ public static final int REPORT_USAGE_BUFFER_SIZE = 3;
|
|
|
|
+
|
|
|
|
+ private final Object mLock = new Object();
|
|
|
|
+
|
|
|
|
/**
|
|
|
|
* All the shortcuts from the package, keyed on IDs.
|
|
|
|
*/
|
|
|
|
@@ -143,6 +151,9 @@ class ShortcutPackage extends ShortcutPackageItem {
|
|
|
|
|
|
|
|
private long mLastKnownForegroundElapsedTime;
|
|
|
|
|
|
|
|
+ @GuardedBy("mLock")
|
|
|
|
+ private List<Long> mLastReportedTime = new ArrayList<>();
|
|
|
|
+
|
|
|
|
private ShortcutPackage(ShortcutUser shortcutUser,
|
|
|
|
int packageUserId, String packageName, ShortcutPackageInfo spi) {
|
|
|
|
super(shortcutUser, packageUserId, packageName,
|
2024-08-17 12:35:07 -04:00
|
|
|
@@ -1352,6 +1363,30 @@ class ShortcutPackage extends ShortcutPackageItem {
|
2024-08-05 16:03:46 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ void reportShortcutUsed(@NonNull final UsageStatsManagerInternal usageStatsManagerInternal,
|
|
|
|
+ @NonNull final String shortcutId) {
|
|
|
|
+ synchronized (mLock) {
|
|
|
|
+ final long currentTS = SystemClock.elapsedRealtime();
|
|
|
|
+ final ShortcutService s = mShortcutUser.mService;
|
|
|
|
+ if (mLastReportedTime.isEmpty()
|
|
|
|
+ || mLastReportedTime.size() < REPORT_USAGE_BUFFER_SIZE) {
|
|
|
|
+ mLastReportedTime.add(currentTS);
|
|
|
|
+ } else if (currentTS - mLastReportedTime.get(0) > s.mSaveDelayMillis) {
|
|
|
|
+ mLastReportedTime.remove(0);
|
|
|
|
+ mLastReportedTime.add(currentTS);
|
|
|
|
+ } else {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ final long token = s.injectClearCallingIdentity();
|
|
|
|
+ try {
|
|
|
|
+ usageStatsManagerInternal.reportShortcutUsage(getPackageName(), shortcutId,
|
|
|
|
+ getUser().getUserId());
|
|
|
|
+ } finally {
|
|
|
|
+ s.injectRestoreCallingIdentity(token);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
|
|
|
|
pw.println();
|
|
|
|
|
|
|
|
diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java
|
2024-08-17 12:35:07 -04:00
|
|
|
index 2e2883dcb2a5..c18cdcb89140 100644
|
2024-08-05 16:03:46 -04:00
|
|
|
--- a/services/core/java/com/android/server/pm/ShortcutService.java
|
|
|
|
+++ b/services/core/java/com/android/server/pm/ShortcutService.java
|
2024-08-17 12:35:07 -04:00
|
|
|
@@ -315,7 +315,7 @@ public class ShortcutService extends IShortcutService.Stub {
|
2024-08-05 16:03:46 -04:00
|
|
|
private CompressFormat mIconPersistFormat;
|
|
|
|
private int mIconPersistQuality;
|
|
|
|
|
|
|
|
- private int mSaveDelayMillis;
|
|
|
|
+ int mSaveDelayMillis;
|
|
|
|
|
|
|
|
private final IPackageManager mIPackageManager;
|
|
|
|
private final PackageManagerInternal mPackageManagerInternal;
|
2024-08-17 12:35:07 -04:00
|
|
|
@@ -2285,10 +2285,11 @@ public class ShortcutService extends IShortcutService.Stub {
|
2024-08-05 16:03:46 -04:00
|
|
|
shortcutId, packageName, userId));
|
|
|
|
}
|
|
|
|
|
|
|
|
+ final ShortcutPackage ps;
|
|
|
|
synchronized (mLock) {
|
|
|
|
throwIfUserLockedL(userId);
|
|
|
|
|
|
|
|
- final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
|
|
|
|
+ ps = getPackageShortcutsForPublisherLocked(packageName, userId);
|
|
|
|
|
|
|
|
if (ps.findShortcutById(shortcutId) == null) {
|
|
|
|
Log.w(TAG, String.format("reportShortcutUsed: package %s doesn't have shortcut %s",
|
2024-08-17 12:35:07 -04:00
|
|
|
@@ -2297,12 +2298,7 @@ public class ShortcutService extends IShortcutService.Stub {
|
2024-08-05 16:03:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- final long token = injectClearCallingIdentity();
|
|
|
|
- try {
|
|
|
|
- mUsageStatsManagerInternal.reportShortcutUsage(packageName, shortcutId, userId);
|
|
|
|
- } finally {
|
|
|
|
- injectRestoreCallingIdentity(token);
|
|
|
|
- }
|
|
|
|
+ ps.reportShortcutUsed(mUsageStatsManagerInternal, shortcutId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
|
2024-08-17 12:35:07 -04:00
|
|
|
index 18970322d854..27cf3502d489 100644
|
2024-08-05 16:03:46 -04:00
|
|
|
--- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
|
|
|
|
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
|
2024-08-17 12:35:07 -04:00
|
|
|
@@ -1940,6 +1940,8 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest {
|
2024-08-05 16:03:46 -04:00
|
|
|
|
|
|
|
public void testReportShortcutUsed() {
|
|
|
|
mRunningUsers.put(USER_10, true);
|
|
|
|
+ mService.updateConfigurationLocked(
|
|
|
|
+ ShortcutService.ConfigConstants.KEY_SAVE_DELAY_MILLIS + "=1");
|
|
|
|
|
|
|
|
runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
|
|
|
|
reset(mMockUsageStatsManagerInternal);
|