diff --git a/services/core/java/com/android/server/notification/RankingHelper.java b/services/core/java/com/android/server/notification/RankingHelper.java index 44dc61de0ea9c..221b2bb0886ad 100644 --- a/services/core/java/com/android/server/notification/RankingHelper.java +++ b/services/core/java/com/android/server/notification/RankingHelper.java @@ -232,7 +232,9 @@ public class RankingHelper implements RankingConfig { private Record getRecord(String pkg, int uid) { final String key = recordKey(pkg, uid); - return mRecords.get(key); + synchronized (mRecords) { + return mRecords.get(key); + } } private Record getOrCreateRecord(String pkg, int uid) { @@ -243,29 +245,32 @@ public class RankingHelper implements RankingConfig { private Record getOrCreateRecord(String pkg, int uid, int importance, int priority, int visibility, boolean showBadge) { final String key = recordKey(pkg, uid); - Record r = (uid == Record.UNKNOWN_UID) ? mRestoredWithoutUids.get(pkg) : mRecords.get(key); - if (r == null) { - r = new Record(); - r.pkg = pkg; - r.uid = uid; - r.importance = importance; - r.priority = priority; - r.visibility = visibility; - r.showBadge = showBadge; + synchronized (mRecords) { + Record r = (uid == Record.UNKNOWN_UID) ? mRestoredWithoutUids.get(pkg) : mRecords.get( + key); + if (r == null) { + r = new Record(); + r.pkg = pkg; + r.uid = uid; + r.importance = importance; + r.priority = priority; + r.visibility = visibility; + r.showBadge = showBadge; - try { - createDefaultChannelIfNeeded(r); - } catch (NameNotFoundException e) { - Slog.e(TAG, "createDefaultChannelIfNeeded - Exception: " + e); - } + try { + createDefaultChannelIfNeeded(r); + } catch (NameNotFoundException e) { + Slog.e(TAG, "createDefaultChannelIfNeeded - Exception: " + e); + } - if (r.uid == Record.UNKNOWN_UID) { - mRestoredWithoutUids.put(pkg, r); - } else { - mRecords.put(key, r); + if (r.uid == Record.UNKNOWN_UID) { + mRestoredWithoutUids.put(pkg, r); + } else { + mRecords.put(key, r); + } } + return r; } - return r; } private boolean shouldHaveDefaultChannel(Record r) throws NameNotFoundException { @@ -346,46 +351,48 @@ public class RankingHelper implements RankingConfig { out.startTag(null, TAG_RANKING); out.attribute(null, ATT_VERSION, Integer.toString(XML_VERSION)); - final int N = mRecords.size(); - for (int i = 0; i < N; i++) { - final Record r = mRecords.valueAt(i); - //TODO: http://b/22388012 - if (forBackup && UserHandle.getUserId(r.uid) != UserHandle.USER_SYSTEM) { - continue; - } - final boolean hasNonDefaultSettings = r.importance != DEFAULT_IMPORTANCE - || r.priority != DEFAULT_PRIORITY || r.visibility != DEFAULT_VISIBILITY - || r.showBadge != DEFAULT_SHOW_BADGE || r.channels.size() > 0 - || r.groups.size() > 0; - if (hasNonDefaultSettings) { - out.startTag(null, TAG_PACKAGE); - out.attribute(null, ATT_NAME, r.pkg); - if (r.importance != DEFAULT_IMPORTANCE) { - out.attribute(null, ATT_IMPORTANCE, Integer.toString(r.importance)); + synchronized (mRecords) { + final int N = mRecords.size(); + for (int i = 0; i < N; i++) { + final Record r = mRecords.valueAt(i); + //TODO: http://b/22388012 + if (forBackup && UserHandle.getUserId(r.uid) != UserHandle.USER_SYSTEM) { + continue; } - if (r.priority != DEFAULT_PRIORITY) { - out.attribute(null, ATT_PRIORITY, Integer.toString(r.priority)); - } - if (r.visibility != DEFAULT_VISIBILITY) { - out.attribute(null, ATT_VISIBILITY, Integer.toString(r.visibility)); - } - out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(r.showBadge)); - - if (!forBackup) { - out.attribute(null, ATT_UID, Integer.toString(r.uid)); - } - - for (NotificationChannelGroup group : r.groups.values()) { - group.writeXml(out); - } - - for (NotificationChannel channel : r.channels.values()) { - if (!forBackup || (forBackup && !channel.isDeleted())) { - channel.writeXml(out); + final boolean hasNonDefaultSettings = r.importance != DEFAULT_IMPORTANCE + || r.priority != DEFAULT_PRIORITY || r.visibility != DEFAULT_VISIBILITY + || r.showBadge != DEFAULT_SHOW_BADGE || r.channels.size() > 0 + || r.groups.size() > 0; + if (hasNonDefaultSettings) { + out.startTag(null, TAG_PACKAGE); + out.attribute(null, ATT_NAME, r.pkg); + if (r.importance != DEFAULT_IMPORTANCE) { + out.attribute(null, ATT_IMPORTANCE, Integer.toString(r.importance)); } - } + if (r.priority != DEFAULT_PRIORITY) { + out.attribute(null, ATT_PRIORITY, Integer.toString(r.priority)); + } + if (r.visibility != DEFAULT_VISIBILITY) { + out.attribute(null, ATT_VISIBILITY, Integer.toString(r.visibility)); + } + out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(r.showBadge)); - out.endTag(null, TAG_PACKAGE); + if (!forBackup) { + out.attribute(null, ATT_UID, Integer.toString(r.uid)); + } + + for (NotificationChannelGroup group : r.groups.values()) { + group.writeXml(out); + } + + for (NotificationChannel channel : r.channels.values()) { + if (!forBackup || (forBackup && !channel.isDeleted())) { + channel.writeXml(out); + } + } + + out.endTag(null, TAG_PACKAGE); + } } } out.endTag(null, TAG_RANKING); @@ -814,7 +821,9 @@ public class RankingHelper implements RankingConfig { pw.println("per-package config:"); } pw.println("Records:"); - dumpRecords(pw, prefix, filter, mRecords); + synchronized (mRecords) { + dumpRecords(pw, prefix, filter, mRecords); + } pw.println("Restored without uid:"); dumpRecords(pw, prefix, filter, mRestoredWithoutUids); } @@ -870,36 +879,38 @@ public class RankingHelper implements RankingConfig { } catch (JSONException e) { // pass } - final int N = mRecords.size(); - for (int i = 0; i < N; i++) { - final Record r = mRecords.valueAt(i); - if (filter == null || filter.matches(r.pkg)) { - JSONObject record = new JSONObject(); - try { - record.put("userId", UserHandle.getUserId(r.uid)); - record.put("packageName", r.pkg); - if (r.importance != DEFAULT_IMPORTANCE) { - record.put("importance", Ranking.importanceToString(r.importance)); + synchronized (mRecords) { + final int N = mRecords.size(); + for (int i = 0; i < N; i++) { + final Record r = mRecords.valueAt(i); + if (filter == null || filter.matches(r.pkg)) { + JSONObject record = new JSONObject(); + try { + record.put("userId", UserHandle.getUserId(r.uid)); + record.put("packageName", r.pkg); + if (r.importance != DEFAULT_IMPORTANCE) { + record.put("importance", Ranking.importanceToString(r.importance)); + } + if (r.priority != DEFAULT_PRIORITY) { + record.put("priority", Notification.priorityToString(r.priority)); + } + if (r.visibility != DEFAULT_VISIBILITY) { + record.put("visibility", Notification.visibilityToString(r.visibility)); + } + if (r.showBadge != DEFAULT_SHOW_BADGE) { + record.put("showBadge", Boolean.valueOf(r.showBadge)); + } + for (NotificationChannel channel : r.channels.values()) { + record.put("channel", channel.toJson()); + } + for (NotificationChannelGroup group : r.groups.values()) { + record.put("group", group.toJson()); + } + } catch (JSONException e) { + // pass } - if (r.priority != DEFAULT_PRIORITY) { - record.put("priority", Notification.priorityToString(r.priority)); - } - if (r.visibility != DEFAULT_VISIBILITY) { - record.put("visibility", Notification.visibilityToString(r.visibility)); - } - if (r.showBadge != DEFAULT_SHOW_BADGE) { - record.put("showBadge", Boolean.valueOf(r.showBadge)); - } - for (NotificationChannel channel : r.channels.values()) { - record.put("channel", channel.toJson()); - } - for (NotificationChannelGroup group : r.groups.values()) { - record.put("group", group.toJson()); - } - } catch (JSONException e) { - // pass + records.put(record); } - records.put(record); } } try { @@ -940,15 +951,18 @@ public class RankingHelper implements RankingConfig { } public Map getPackageBans() { - final int N = mRecords.size(); - ArrayMap packageBans = new ArrayMap<>(N); - for (int i = 0; i < N; i++) { - final Record r = mRecords.valueAt(i); - if (r.importance == NotificationManager.IMPORTANCE_NONE) { - packageBans.put(r.uid, r.pkg); + synchronized (mRecords) { + final int N = mRecords.size(); + ArrayMap packageBans = new ArrayMap<>(N); + for (int i = 0; i < N; i++) { + final Record r = mRecords.valueAt(i); + if (r.importance == NotificationManager.IMPORTANCE_NONE) { + packageBans.put(r.uid, r.pkg); + } } + + return packageBans; } - return packageBans; } /** @@ -981,15 +995,17 @@ public class RankingHelper implements RankingConfig { private Map getPackageChannels() { ArrayMap packageChannels = new ArrayMap<>(); - for (int i = 0; i < mRecords.size(); i++) { - final Record r = mRecords.valueAt(i); - int channelCount = 0; - for (int j = 0; j < r.channels.size();j++) { - if (!r.channels.valueAt(j).isDeleted()) { - channelCount++; + synchronized (mRecords) { + for (int i = 0; i < mRecords.size(); i++) { + final Record r = mRecords.valueAt(i); + int channelCount = 0; + for (int j = 0; j < r.channels.size(); j++) { + if (!r.channels.valueAt(j).isDeleted()) { + channelCount++; + } } + packageChannels.put(r.pkg, channelCount); } - packageChannels.put(r.pkg, channelCount); } return packageChannels; } @@ -1006,7 +1022,9 @@ public class RankingHelper implements RankingConfig { for (int i = 0; i < size; i++) { final String pkg = pkgList[i]; final int uid = uidList[i]; - mRecords.remove(recordKey(pkg, uid)); + synchronized (mRecords) { + mRecords.remove(recordKey(pkg, uid)); + } mRestoredWithoutUids.remove(pkg); updated = true; } @@ -1018,7 +1036,9 @@ public class RankingHelper implements RankingConfig { try { r.uid = mPm.getPackageUidAsUser(r.pkg, changeUserId); mRestoredWithoutUids.remove(pkg); - mRecords.put(recordKey(r.pkg, r.uid), r); + synchronized (mRecords) { + mRecords.put(recordKey(r.pkg, r.uid), r); + } updated = true; } catch (NameNotFoundException e) { // noop