Merge "Remove separate recent app tracking" into qt-dev

This commit is contained in:
TreeHugger Robot
2019-05-04 14:30:29 +00:00
committed by Android (Google) Code Review
5 changed files with 26 additions and 165 deletions

View File

@@ -102,7 +102,6 @@ interface INotificationManager
NotificationChannelGroup getNotificationChannelGroup(String pkg, String channelGroupId);
ParceledListSlice getNotificationChannelGroups(String pkg);
boolean onlyHasDefaultChannel(String pkg, int uid);
ParceledListSlice getRecentNotifyingAppsForUser(int userId);
int getBlockedAppCount(int userId);
boolean areChannelsBypassingDnd();
int getAppsBypassingDndCount(int uid);

View File

@@ -26,27 +26,27 @@ import java.util.Objects;
*/
public final class NotifyingApp implements Parcelable, Comparable<NotifyingApp> {
private int mUid;
private int mUserId;
private String mPkg;
private long mLastNotified;
public NotifyingApp() {}
protected NotifyingApp(Parcel in) {
mUid = in.readInt();
mUserId = in.readInt();
mPkg = in.readString();
mLastNotified = in.readLong();
}
public int getUid() {
return mUid;
public int getUserId() {
return mUserId;
}
/**
* Sets the uid of the package that sent the notification. Returns self.
* Sets the userid of the package that sent the notification. Returns self.
*/
public NotifyingApp setUid(int mUid) {
this.mUid = mUid;
public NotifyingApp setUserId(int mUserId) {
this.mUserId = mUserId;
return this;
}
@@ -74,7 +74,7 @@ public final class NotifyingApp implements Parcelable, Comparable<NotifyingApp>
return this;
}
public static final @android.annotation.NonNull Creator<NotifyingApp> CREATOR = new Creator<NotifyingApp>() {
public static final @NonNull Creator<NotifyingApp> CREATOR = new Creator<NotifyingApp>() {
@Override
public NotifyingApp createFromParcel(Parcel in) {
return new NotifyingApp(in);
@@ -93,7 +93,7 @@ public final class NotifyingApp implements Parcelable, Comparable<NotifyingApp>
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mUid);
dest.writeInt(mUserId);
dest.writeString(mPkg);
dest.writeLong(mLastNotified);
}
@@ -103,14 +103,14 @@ public final class NotifyingApp implements Parcelable, Comparable<NotifyingApp>
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NotifyingApp that = (NotifyingApp) o;
return getUid() == that.getUid()
return getUserId() == that.getUserId()
&& getLastNotified() == that.getLastNotified()
&& Objects.equals(mPkg, that.mPkg);
}
@Override
public int hashCode() {
return Objects.hash(getUid(), mPkg, getLastNotified());
return Objects.hash(getUserId(), mPkg, getLastNotified());
}
/**
@@ -119,10 +119,10 @@ public final class NotifyingApp implements Parcelable, Comparable<NotifyingApp>
@Override
public int compareTo(NotifyingApp o) {
if (getLastNotified() == o.getLastNotified()) {
if (getUid() == o.getUid()) {
if (getUserId() == o.getUserId()) {
return getPackage().compareTo(o.getPackage());
}
return Integer.compare(getUid(), o.getUid());
return Integer.compare(getUserId(), o.getUserId());
}
return -Long.compare(getLastNotified(), o.getLastNotified());
@@ -131,7 +131,7 @@ public final class NotifyingApp implements Parcelable, Comparable<NotifyingApp>
@Override
public String toString() {
return "NotifyingApp{"
+ "mUid=" + mUid
+ "mUserId=" + mUserId
+ ", mPkg='" + mPkg + '\''
+ ", mLastNotified=" + mLastNotified
+ '}';

View File

@@ -121,6 +121,8 @@ import android.app.backup.BackupManager;
import android.app.role.OnRoleHoldersChangedListener;
import android.app.role.RoleManager;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.app.usage.UsageStatsManagerInternal;
import android.companion.ICompanionDeviceManager;
import android.content.BroadcastReceiver;
@@ -258,6 +260,8 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
@@ -413,7 +417,6 @@ public class NotificationManagerService extends SystemService {
final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>();
final ArrayList<ToastRecord> mToastQueue = new ArrayList<>();
final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>();
final ArrayMap<Integer, ArrayList<NotifyingApp>> mRecentApps = new ArrayMap<>();
// The last key in this list owns the hardware.
ArrayList<String> mLights = new ArrayList<>();
@@ -2209,7 +2212,6 @@ public class NotificationManagerService extends SystemService {
mAppUsageStats.reportInterruptiveNotification(r.sbn.getPackageName(),
r.getChannel().getId(),
getRealUserId(r.sbn.getUserId()));
logRecentLocked(r);
r.setRecordedInterruption(true);
}
}
@@ -2829,16 +2831,6 @@ public class NotificationManagerService extends SystemService {
+ " cannot read channels for " + targetPkg + " in " + userId);
}
@Override
public ParceledListSlice<NotifyingApp> getRecentNotifyingAppsForUser(int userId) {
checkCallerIsSystem();
synchronized (mNotificationLock) {
List<NotifyingApp> apps = new ArrayList<>(
mRecentApps.getOrDefault(userId, new ArrayList<>()));
return new ParceledListSlice<>(apps);
}
}
@Override
public int getBlockedAppCount(int userId) {
checkCallerIsSystem();
@@ -5526,38 +5518,6 @@ public class NotificationManagerService extends SystemService {
return record.getCriticality() < CriticalNotificationExtractor.NORMAL;
}
/**
* Keeps the last 5 packages that have notified, by user.
*/
@GuardedBy("mNotificationLock")
@VisibleForTesting
protected void logRecentLocked(NotificationRecord r) {
if (r.isUpdate) {
return;
}
ArrayList<NotifyingApp> recentAppsForUser =
mRecentApps.getOrDefault(r.getUser().getIdentifier(), new ArrayList<>(6));
NotifyingApp na = new NotifyingApp()
.setPackage(r.sbn.getPackageName())
.setUid(r.sbn.getUid())
.setLastNotified(r.sbn.getPostTime());
// A new notification gets an app moved to the front of the list
for (int i = recentAppsForUser.size() - 1; i >= 0; i--) {
NotifyingApp naExisting = recentAppsForUser.get(i);
if (na.getPackage().equals(naExisting.getPackage())
&& na.getUid() == naExisting.getUid()) {
recentAppsForUser.remove(i);
break;
}
}
// time is always increasing, so always add to the front of the list
recentAppsForUser.add(0, na);
if (recentAppsForUser.size() > 5) {
recentAppsForUser.remove(recentAppsForUser.size() -1);
}
mRecentApps.put(r.getUser().getIdentifier(), recentAppsForUser);
}
/**
* Ensures that grouped notification receive their special treatment.
*

View File

@@ -3025,104 +3025,6 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
r.getUserSentiment());
}
@Test
public void testRecents() throws Exception {
Set<NotifyingApp> expected = new HashSet<>();
final NotificationRecord oldest = new NotificationRecord(mContext,
generateSbn("p", 1000, 9, 0), mTestNotificationChannel);
mService.logRecentLocked(oldest);
for (int i = 1; i <= 5; i++) {
NotificationRecord r = new NotificationRecord(mContext,
generateSbn("p" + i, i, i*100, 0), mTestNotificationChannel);
expected.add(new NotifyingApp()
.setPackage(r.sbn.getPackageName())
.setUid(r.sbn.getUid())
.setLastNotified(r.sbn.getPostTime()));
mService.logRecentLocked(r);
}
List<NotifyingApp> apps = mBinderService.getRecentNotifyingAppsForUser(0).getList();
assertTrue(apps.size() == 5);
for (NotifyingApp actual : apps) {
assertTrue("got unexpected result: " + actual, expected.contains(actual));
}
}
@Test
public void testRecentsNoDuplicatePackages() throws Exception {
final NotificationRecord p1 = new NotificationRecord(mContext, generateSbn("p", 1, 1000, 0),
mTestNotificationChannel);
final NotificationRecord p2 = new NotificationRecord(mContext, generateSbn("p", 1, 2000, 0),
mTestNotificationChannel);
mService.logRecentLocked(p1);
mService.logRecentLocked(p2);
List<NotifyingApp> apps = mBinderService.getRecentNotifyingAppsForUser(0).getList();
assertTrue(apps.size() == 1);
NotifyingApp expected = new NotifyingApp().setPackage("p").setUid(1).setLastNotified(2000);
assertEquals(expected, apps.get(0));
}
@Test
public void testRecentsWithDuplicatePackage() throws Exception {
Set<NotifyingApp> expected = new HashSet<>();
final NotificationRecord oldest = new NotificationRecord(mContext,
generateSbn("p", 1000, 9, 0), mTestNotificationChannel);
mService.logRecentLocked(oldest);
for (int i = 1; i <= 5; i++) {
NotificationRecord r = new NotificationRecord(mContext,
generateSbn("p" + i, i, i*100, 0), mTestNotificationChannel);
expected.add(new NotifyingApp()
.setPackage(r.sbn.getPackageName())
.setUid(r.sbn.getUid())
.setLastNotified(r.sbn.getPostTime()));
mService.logRecentLocked(r);
}
NotificationRecord r = new NotificationRecord(mContext,
generateSbn("p" + 3, 3, 300000, 0), mTestNotificationChannel);
expected.remove(new NotifyingApp()
.setPackage(r.sbn.getPackageName())
.setUid(3)
.setLastNotified(300));
NotifyingApp newest = new NotifyingApp()
.setPackage(r.sbn.getPackageName())
.setUid(r.sbn.getUid())
.setLastNotified(r.sbn.getPostTime());
expected.add(newest);
mService.logRecentLocked(r);
List<NotifyingApp> apps = mBinderService.getRecentNotifyingAppsForUser(0).getList();
assertTrue(apps.size() == 5);
for (NotifyingApp actual : apps) {
assertTrue("got unexpected result: " + actual, expected.contains(actual));
}
assertEquals(newest, apps.get(0));
}
@Test
public void testRecentsMultiuser() throws Exception {
final NotificationRecord user1 = new NotificationRecord(mContext,
generateSbn("p", 1000, 9, 1), mTestNotificationChannel);
mService.logRecentLocked(user1);
final NotificationRecord user2 = new NotificationRecord(mContext,
generateSbn("p2", 100000, 9999, 2), mTestNotificationChannel);
mService.logRecentLocked(user2);
assertEquals(0, mBinderService.getRecentNotifyingAppsForUser(0).getList().size());
assertEquals(1, mBinderService.getRecentNotifyingAppsForUser(1).getList().size());
assertEquals(1, mBinderService.getRecentNotifyingAppsForUser(2).getList().size());
assertTrue(mBinderService.getRecentNotifyingAppsForUser(2).getList().contains(
new NotifyingApp()
.setPackage(user2.sbn.getPackageName())
.setUid(user2.sbn.getUid())
.setLastNotified(user2.sbn.getPostTime())));
}
@Test
public void testRestore() throws Exception {
int systemChecks = mService.countSystemChecks;

View File

@@ -36,7 +36,7 @@ public class NotifyingAppTest extends UiServiceTestCase {
@Test
public void testConstructor() {
NotifyingApp na = new NotifyingApp();
assertEquals(0, na.getUid());
assertEquals(0, na.getUserId());
assertEquals(0, na.getLastNotified());
assertEquals(null, na.getPackage());
}
@@ -49,10 +49,10 @@ public class NotifyingAppTest extends UiServiceTestCase {
}
@Test
public void testUid() {
public void testUserId() {
NotifyingApp na = new NotifyingApp();
na.setUid(90);
assertEquals(90, na.getUid());
na.setUserId(90);
assertEquals(90, na.getUserId());
}
@Test
@@ -66,7 +66,7 @@ public class NotifyingAppTest extends UiServiceTestCase {
public void testWriteToParcel() {
NotifyingApp na = new NotifyingApp();
na.setPackage("package");
na.setUid(200);
na.setUserId(200);
na.setLastNotified(4000);
Parcel parcel = Parcel.obtain();
@@ -75,19 +75,19 @@ public class NotifyingAppTest extends UiServiceTestCase {
NotifyingApp na1 = NotifyingApp.CREATOR.createFromParcel(parcel);
assertEquals(na.getLastNotified(), na1.getLastNotified());
assertEquals(na.getPackage(), na1.getPackage());
assertEquals(na.getUid(), na1.getUid());
assertEquals(na.getUserId(), na1.getUserId());
}
@Test
public void testCompareTo() {
NotifyingApp na1 = new NotifyingApp();
na1.setPackage("pkg1");
na1.setUid(1000);
na1.setUserId(1000);
na1.setLastNotified(6);
NotifyingApp na2 = new NotifyingApp();
na2.setPackage("a");
na2.setUid(999);
na2.setUserId(999);
na2.setLastNotified(1);
assertTrue(na1.compareTo(na2) < 0);