Merge "Synchronize access to ranking helper records." into oc-dev

This commit is contained in:
TreeHugger Robot
2017-04-14 18:52:36 +00:00
committed by Android (Google) Code Review

View File

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