Merge "Stop overwriting files" into rvc-dev am: 5ebbb01465

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11777182

Change-Id: Ia8a0cd96d8d071c99ebfe8e665b5383add21e7cd
This commit is contained in:
TreeHugger Robot
2020-06-08 15:41:35 +00:00
committed by Automerger Merge Worker
3 changed files with 37 additions and 30 deletions

View File

@@ -108,7 +108,7 @@ public class NotificationHistoryDatabase {
public void init() { public void init() {
synchronized (mLock) { synchronized (mLock) {
try { try {
if (!mHistoryDir.mkdir()) { if (!mHistoryDir.exists() && !mHistoryDir.mkdir()) {
throw new IllegalStateException("could not create history directory"); throw new IllegalStateException("could not create history directory");
} }
mVersionFile.createNewFile(); mVersionFile.createNewFile();
@@ -197,7 +197,7 @@ public class NotificationHistoryDatabase {
readLocked( readLocked(
file, notifications, new NotificationHistoryFilter.Builder().build()); file, notifications, new NotificationHistoryFilter.Builder().build());
} catch (Exception e) { } catch (Exception e) {
Slog.e(TAG, "error reading " + file.getBaseFile().getName(), e); Slog.e(TAG, "error reading " + file.getBaseFile().getAbsolutePath(), e);
} }
} }
@@ -223,7 +223,7 @@ public class NotificationHistoryDatabase {
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
Slog.e(TAG, "error reading " + file.getBaseFile().getName(), e); Slog.e(TAG, "error reading " + file.getBaseFile().getAbsolutePath(), e);
} }
} }
@@ -279,7 +279,7 @@ public class NotificationHistoryDatabase {
} }
file.delete(); file.delete();
// TODO: delete all relevant bitmaps, once they exist // TODO: delete all relevant bitmaps, once they exist
mHistoryFiles.removeLast(); mHistoryFiles.remove(file);
} }
private void scheduleDeletion(File file, long creationTime, int retentionDays) { private void scheduleDeletion(File file, long creationTime, int retentionDays) {
@@ -317,11 +317,17 @@ public class NotificationHistoryDatabase {
private static void readLocked(AtomicFile file, NotificationHistory notificationsOut, private static void readLocked(AtomicFile file, NotificationHistory notificationsOut,
NotificationHistoryFilter filter) throws IOException { NotificationHistoryFilter filter) throws IOException {
try (FileInputStream in = file.openRead()) { FileInputStream in = null;
try {
in = file.openRead();
NotificationHistoryProtoHelper.read(in, notificationsOut, filter); NotificationHistoryProtoHelper.read(in, notificationsOut, filter);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Slog.e(TAG, "Cannot file " + file.getBaseFile().getName(), e); Slog.e(TAG, "Cannot open " + file.getBaseFile().getAbsolutePath(), e);
throw e; throw e;
} finally {
if (in != null) {
in.close();
}
} }
} }
@@ -334,9 +340,15 @@ public class NotificationHistoryDatabase {
} }
if (ACTION_HISTORY_DELETION.equals(action)) { if (ACTION_HISTORY_DELETION.equals(action)) {
try { try {
final String filePath = intent.getStringExtra(EXTRA_KEY); synchronized (mLock) {
AtomicFile fileToDelete = new AtomicFile(new File(filePath)); final String filePath = intent.getStringExtra(EXTRA_KEY);
fileToDelete.delete(); AtomicFile fileToDelete = new AtomicFile(new File(filePath));
if (DEBUG) {
Slog.d(TAG, "Removed " + fileToDelete.getBaseFile().getName());
}
fileToDelete.delete();
mHistoryFiles.remove(fileToDelete);
}
} catch (Exception e) { } catch (Exception e) {
Slog.e(TAG, "Failed to delete notification history file", e); Slog.e(TAG, "Failed to delete notification history file", e);
} }
@@ -345,27 +357,23 @@ public class NotificationHistoryDatabase {
}; };
final class WriteBufferRunnable implements Runnable { final class WriteBufferRunnable implements Runnable {
long currentTime = 0;
AtomicFile latestNotificationsFile;
@Override @Override
public void run() { public void run() {
if (DEBUG) Slog.d(TAG, "WriteBufferRunnable"); long time = System.currentTimeMillis();
run(time, new AtomicFile(new File(mHistoryDir, String.valueOf(time))));
}
void run(long time, AtomicFile file) {
synchronized (mLock) { synchronized (mLock) {
if (currentTime == 0) { if (DEBUG) Slog.d(TAG, "WriteBufferRunnable "
currentTime = System.currentTimeMillis(); + file.getBaseFile().getAbsolutePath());
}
if (latestNotificationsFile == null) {
latestNotificationsFile = new AtomicFile(
new File(mHistoryDir, String.valueOf(currentTime)));
}
try { try {
writeLocked(latestNotificationsFile, mBuffer); writeLocked(file, mBuffer);
mHistoryFiles.addFirst(latestNotificationsFile); mHistoryFiles.addFirst(file);
mBuffer = new NotificationHistory(); mBuffer = new NotificationHistory();
scheduleDeletion(latestNotificationsFile.getBaseFile(), currentTime, scheduleDeletion(file.getBaseFile(), time, HISTORY_RETENTION_DAYS);
HISTORY_RETENTION_DAYS);
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write buffer to disk. not flushing buffer", e); Slog.e(TAG, "Failed to write buffer to disk. not flushing buffer", e);
} }
@@ -382,7 +390,7 @@ public class NotificationHistoryDatabase {
@Override @Override
public void run() { public void run() {
if (DEBUG) Slog.d(TAG, "RemovePackageRunnable"); if (DEBUG) Slog.d(TAG, "RemovePackageRunnable " + mPkg);
synchronized (mLock) { synchronized (mLock) {
// Remove packageName entries from pending history // Remove packageName entries from pending history
mBuffer.removeNotificationsFromWrite(mPkg); mBuffer.removeNotificationsFromWrite(mPkg);
@@ -398,7 +406,7 @@ public class NotificationHistoryDatabase {
writeLocked(af, notifications); writeLocked(af, notifications);
} catch (Exception e) { } catch (Exception e) {
Slog.e(TAG, "Cannot clean up file on pkg removal " Slog.e(TAG, "Cannot clean up file on pkg removal "
+ af.getBaseFile().getName(), e); + af.getBaseFile().getAbsolutePath(), e);
} }
} }
} }

View File

@@ -2672,7 +2672,7 @@ public class NotificationManagerService extends SystemService {
mHistoryManager.addNotification(new HistoricalNotification.Builder() mHistoryManager.addNotification(new HistoricalNotification.Builder()
.setPackage(r.getSbn().getPackageName()) .setPackage(r.getSbn().getPackageName())
.setUid(r.getSbn().getUid()) .setUid(r.getSbn().getUid())
.setUserId(r.getUserId()) .setUserId(r.getSbn().getNormalizedUserId())
.setChannelId(r.getChannel().getId()) .setChannelId(r.getChannel().getId())
.setChannelName(r.getChannel().getName().toString()) .setChannelName(r.getChannel().getName().toString())
.setPostedTimeMs(System.currentTimeMillis()) .setPostedTimeMs(System.currentTimeMillis())

View File

@@ -360,13 +360,12 @@ public class NotificationHistoryDatabaseTest extends UiServiceTestCase {
mDataBase.new WriteBufferRunnable(); mDataBase.new WriteBufferRunnable();
mDataBase.mBuffer = nh; mDataBase.mBuffer = nh;
wbr.currentTime = 5; AtomicFile af = mock(AtomicFile.class);
wbr.latestNotificationsFile = mock(AtomicFile.class);
File file = mock(File.class); File file = mock(File.class);
when(file.getName()).thenReturn("5"); when(file.getName()).thenReturn("5");
when(wbr.latestNotificationsFile.getBaseFile()).thenReturn(file); when(af.getBaseFile()).thenReturn(file);
wbr.run(); wbr.run(5, af);
assertThat(mDataBase.mHistoryFiles.size()).isEqualTo(1); assertThat(mDataBase.mHistoryFiles.size()).isEqualTo(1);
assertThat(mDataBase.mBuffer).isNotEqualTo(nh); assertThat(mDataBase.mBuffer).isNotEqualTo(nh);