diff options
author | Santos Cordon <santoscordon@google.com> | 2015-02-27 17:34:16 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-02-27 17:34:17 +0000 |
commit | ae8f8932b836437e548405387e080388c3686964 (patch) | |
tree | c6257aa2ca5b5ba70b9c394744cb7dd0a47e36af /src | |
parent | 3e2e34d46cca69ba09715b7d2e4c165d84d19ee5 (diff) | |
parent | 52a1f4d4882c1e1db7a597f1bddb1cbeaf7af5de (diff) | |
download | packages_providers_ContactsProvider-ae8f8932b836437e548405387e080388c3686964.zip packages_providers_ContactsProvider-ae8f8932b836437e548405387e080388c3686964.tar.gz packages_providers_ContactsProvider-ae8f8932b836437e548405387e080388c3686964.tar.bz2 |
Merge "More tests for CallLogBackupAgent"
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/providers/contacts/CallLogBackupAgent.java | 125 |
1 files changed, 77 insertions, 48 deletions
diff --git a/src/com/android/providers/contacts/CallLogBackupAgent.java b/src/com/android/providers/contacts/CallLogBackupAgent.java index e5c77e6..8e160c8 100644 --- a/src/com/android/providers/contacts/CallLogBackupAgent.java +++ b/src/com/android/providers/contacts/CallLogBackupAgent.java @@ -26,6 +26,7 @@ import android.os.ParcelFileDescriptor; import android.provider.CallLog; import android.provider.CallLog.Calls; import android.telecom.PhoneAccountHandle; +import android.text.TextUtils; import android.util.Log; import com.android.internal.annotations.VisibleForTesting; @@ -41,6 +42,8 @@ import java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.util.LinkedList; +import java.util.List; import java.util.SortedSet; import java.util.TreeSet; @@ -55,7 +58,8 @@ public class CallLogBackupAgent extends BackupAgent { SortedSet<Integer> callIds; } - private static class Call { + @VisibleForTesting + static class Call { int id; long date; long duration; @@ -82,7 +86,8 @@ public class CallLogBackupAgent extends BackupAgent { private static final String TAG = "CallLogBackupAgent"; /** Current version of CallLogBackup. Used to track the backup format. */ - private static final int VERSION = 1; + @VisibleForTesting + static final int VERSION = 1002; /** Version indicating that there exists no previous backup entry. */ @VisibleForTesting static final int VERSION_NO_PREVIOUS_STATE = 0; @@ -119,7 +124,7 @@ public class CallLogBackupAgent extends BackupAgent { } // Run the actual backup of data - runBackup(state, data); + runBackup(state, data, getAllCallLogEntries()); // Rewrite the backup state. DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream( @@ -151,59 +156,62 @@ public class CallLogBackupAgent extends BackupAgent { } @VisibleForTesting - void runBackup(CallLogBackupState state, BackupDataOutput data) { + void runBackup(CallLogBackupState state, BackupDataOutput data, Iterable<Call> calls) { SortedSet<Integer> callsToRemove = new TreeSet<>(state.callIds); - // Get all the existing call log entries. - Cursor cursor = getAllCallLogEntries(); - if (cursor == null) { - return; - } - - try { - // Loop through all the call log entries to identify: - // (1) new calls - // (2) calls which have been deleted. - while (cursor.moveToNext()) { - Call call = readCallFromCursor(cursor); - - if (!state.callIds.contains(call.id)) { - - if (isDebug()) { - Log.d(TAG, "Adding call to backup: " + call); - } - - // This call new (not in our list from the last backup), lets back it up. - addCallToBackup(data, call); - state.callIds.add(call.id); - } else { - // This call still exists in the current call log so delete it from the - // "callsToRemove" set since we want to keep it. - callsToRemove.remove(call.id); - } - } + // Loop through all the call log entries to identify: + // (1) new calls + // (2) calls which have been deleted. + for (Call call : calls) { + if (!state.callIds.contains(call.id)) { - // Remove calls which no longer exist in the set. - for (Integer i : callsToRemove) { if (isDebug()) { - Log.d(TAG, "Removing call from backup: " + i); + Log.d(TAG, "Adding call to backup: " + call); } - removeCallFromBackup(data, i); - state.callIds.remove(i); + // This call new (not in our list from the last backup), lets back it up. + addCallToBackup(data, call); + state.callIds.add(call.id); + } else { + // This call still exists in the current call log so delete it from the + // "callsToRemove" set since we want to keep it. + callsToRemove.remove(call.id); } + } - } finally { - cursor.close(); + // Remove calls which no longer exist in the set. + for (Integer i : callsToRemove) { + if (isDebug()) { + Log.d(TAG, "Removing call from backup: " + i); + } + + removeCallFromBackup(data, i); + state.callIds.remove(i); } } - private Cursor getAllCallLogEntries() { + private Iterable<Call> getAllCallLogEntries() { + List<Call> calls = new LinkedList<>(); + // We use the API here instead of querying ContactsDatabaseHelper directly because // CallLogProvider has special locks in place for sychronizing when to read. Using the APIs // gives us that for free. ContentResolver resolver = getContentResolver(); - return resolver.query(CallLog.Calls.CONTENT_URI, CALL_LOG_PROJECTION, null, null, null); + Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, CALL_LOG_PROJECTION, null, null, null); + if (cursor != null) { + try { + while (cursor.moveToNext()) { + Call call = readCallFromCursor(cursor); + if (call != null) { + calls.add(call); + } + } + } finally { + cursor.close(); + } + } + + return calls; } private void writeCallToProvider(Call call) { @@ -277,10 +285,10 @@ public class CallLogBackupAgent extends BackupAgent { if (version >= 1) { call.date = dataInput.readLong(); call.duration = dataInput.readLong(); - call.number = dataInput.readUTF(); + call.number = readString(dataInput, version); call.type = dataInput.readInt(); call.numberPresentation = dataInput.readInt(); - call.accountComponentName = dataInput.readUTF(); + call.accountComponentName = readString(dataInput, version); call.accountId = dataInput.readUTF(); call.accountAddress = dataInput.readUTF(); call.dataUsage = dataInput.readLong(); @@ -322,13 +330,13 @@ public class CallLogBackupAgent extends BackupAgent { data.writeInt(VERSION); data.writeLong(call.date); data.writeLong(call.duration); - data.writeUTF(call.number); + writeString(data, call.number); data.writeInt(call.type); data.writeInt(call.numberPresentation); - data.writeUTF(call.accountComponentName); - data.writeUTF(call.accountId); - data.writeUTF(call.accountAddress); - data.writeLong(call.dataUsage); + writeString(data, call.accountComponentName); + writeString(data, call.accountId); + writeString(data, call.accountAddress); + data.writeLong(call.dataUsage == null ? 0 : call.dataUsage); data.writeInt(call.features); data.flush(); @@ -343,6 +351,27 @@ public class CallLogBackupAgent extends BackupAgent { } } + private void writeString(DataOutputStream data, String str) throws IOException { + if (str == null) { + data.writeBoolean(false); + } else { + data.writeBoolean(true); + data.writeUTF(str); + } + } + + private String readString(DataInputStream data, int version) throws IOException { + if (version == 1) { + return data.readUTF(); + } else { + if (data.readBoolean()) { + return data.readUTF(); + } else { + return null; + } + } + } + private void removeCallFromBackup(BackupDataOutput output, int callId) { try { output.writeEntityHeader(Integer.toString(callId), -1); |