summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/calllog/datasources/systemcalllog
diff options
context:
space:
mode:
authorEric Erfanian <erfanian@google.com>2017-06-19 11:26:01 -0700
committerEric Erfanian <erfanian@google.com>2017-06-19 11:30:45 -0700
commit2f1c7586bcce334ca69022eb8dc6d8965ceb6a05 (patch)
treebf00ada449ee3de31ec983a14e84159200aa18c2 /java/com/android/dialer/calllog/datasources/systemcalllog
parent3d0ca68e466482971a4cf46576c50cb2bd42bcb5 (diff)
Update AOSP Dialer source from internal google3 repository at
cl/159428781. Test: make, treehugger This CL updates the AOSP Dialer source with all the changes that have gone into the private google3 repository. This includes all the changes from cl/152373142 (4/06/2017) to cl/159428781 (6/19/2017). This goal of these drops is to keep the AOSP source in sync with the internal google3 repository. Currently these sync are done by hand with very minor modifications to the internal source code. See the Android.mk file for list of modifications. Our current goal is to do frequent drops (daily if possible) and eventually switched to an automated process. Change-Id: Ie60a84b3936efd0ea3d95d7c86bf96d2b1663030
Diffstat (limited to 'java/com/android/dialer/calllog/datasources/systemcalllog')
-rw-r--r--java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java251
1 files changed, 242 insertions, 9 deletions
diff --git a/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java b/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
index ea6663fbe..f2063283f 100644
--- a/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
+++ b/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
@@ -16,28 +16,54 @@
package com.android.dialer.calllog.datasources.systemcalllog;
+import android.Manifest.permission;
+import android.annotation.TargetApi;
+import android.content.ContentValues;
import android.content.Context;
import android.database.ContentObserver;
-import android.database.sqlite.SQLiteDatabase;
+import android.database.Cursor;
import android.net.Uri;
+import android.os.Build;
import android.os.Handler;
+import android.preference.PreferenceManager;
import android.provider.CallLog;
+import android.provider.CallLog.Calls;
import android.support.annotation.MainThread;
+import android.support.annotation.Nullable;
+import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
-import com.android.dialer.calllog.database.CallLogMutations;
+import android.text.TextUtils;
+import android.util.ArraySet;
+import com.android.dialer.DialerPhoneNumber;
+import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.AnnotatedCallLog;
+import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.CoalescedAnnotatedCallLog;
import com.android.dialer.calllog.datasources.CallLogDataSource;
+import com.android.dialer.calllog.datasources.CallLogMutations;
+import com.android.dialer.calllog.datasources.util.RowCombiner;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.ThreadUtil;
+import com.android.dialer.phonenumberproto.DialerPhoneNumberUtil;
import com.android.dialer.util.PermissionsUtil;
+import com.google.i18n.phonenumbers.PhoneNumberUtil;
+import com.google.protobuf.InvalidProtocolBufferException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
import javax.inject.Inject;
/**
* Responsible for defining the rows in the annotated call log and maintaining the columns in it
* which are derived from the system call log.
*/
+@SuppressWarnings("MissingPermission")
public class SystemCallLogDataSource implements CallLogDataSource {
+ @VisibleForTesting
+ static final String PREF_LAST_TIMESTAMP_PROCESSED = "systemCallLogLastTimestampProcessed";
+
+ @Nullable private Long lastTimestampProcessed;
+
@Inject
public SystemCallLogDataSource() {}
@@ -47,6 +73,8 @@ public class SystemCallLogDataSource implements CallLogDataSource {
Context appContext, ContentObserverCallbacks contentObserverCallbacks) {
Assert.isMainThread();
+ LogUtil.enterBlock("SystemCallLogDataSource.registerContentObservers");
+
if (!PermissionsUtil.hasCallLogReadPermissions(appContext)) {
LogUtil.i("SystemCallLogDataSource.registerContentObservers", "no call log permissions");
return;
@@ -77,17 +105,222 @@ public class SystemCallLogDataSource implements CallLogDataSource {
@WorkerThread
@Override
- public void fill(
- Context appContext,
- SQLiteDatabase readableDatabase,
- long lastRebuildTimeMillis,
- CallLogMutations mutations) {
+ public void fill(Context appContext, CallLogMutations mutations) {
Assert.isWorkerThread();
+ lastTimestampProcessed = null;
+
+ if (!PermissionsUtil.hasPermission(appContext, permission.READ_CALL_LOG)) {
+ LogUtil.i("SystemCallLogDataSource.fill", "no call log permissions");
+ return;
+ }
+
// This data source should always run first so the mutations should always be empty.
- Assert.checkState(mutations.isEmpty());
+ Assert.checkArgument(mutations.isEmpty());
+
+ Set<Long> annotatedCallLogIds = getAnnotatedCallLogIds(appContext);
+
+ LogUtil.i(
+ "SystemCallLogDataSource.fill",
+ "found %d existing annotated call log ids",
+ annotatedCallLogIds.size());
+
+ handleInsertsAndUpdates(appContext, mutations, annotatedCallLogIds);
+ handleDeletes(appContext, annotatedCallLogIds, mutations);
+ }
+
+ @WorkerThread
+ @Override
+ public void onSuccessfulFill(Context appContext) {
+ // If a fill operation was a no-op, lastTimestampProcessed could still be null.
+ if (lastTimestampProcessed != null) {
+ PreferenceManager.getDefaultSharedPreferences(appContext)
+ .edit()
+ .putLong(PREF_LAST_TIMESTAMP_PROCESSED, lastTimestampProcessed)
+ .apply();
+ }
+ }
+
+ @Override
+ public ContentValues coalesce(List<ContentValues> individualRowsSortedByTimestampDesc) {
+ // TODO: Complete implementation.
+ ContentValues coalescedValues =
+ new RowCombiner(individualRowsSortedByTimestampDesc)
+ .useMostRecentLong(AnnotatedCallLog.TIMESTAMP)
+ .combine();
+
+ // All phone numbers in the provided group should be equivalent (but could be formatted
+ // differently). Arbitrarily show the raw phone number of the most recent call.
+ DialerPhoneNumber mostRecentPhoneNumber =
+ getMostRecentPhoneNumber(individualRowsSortedByTimestampDesc);
+ coalescedValues.put(
+ CoalescedAnnotatedCallLog.FORMATTED_NUMBER,
+ mostRecentPhoneNumber.getRawInput().getNumber());
+ return coalescedValues;
+ }
+
+ private static DialerPhoneNumber getMostRecentPhoneNumber(
+ List<ContentValues> individualRowsSortedByTimestampDesc) {
+ DialerPhoneNumber dialerPhoneNumber;
+ byte[] protoBytes =
+ individualRowsSortedByTimestampDesc.get(0).getAsByteArray(AnnotatedCallLog.NUMBER);
+ try {
+ dialerPhoneNumber = DialerPhoneNumber.parseFrom(protoBytes);
+ } catch (InvalidProtocolBufferException e) {
+ throw Assert.createAssertionFailException("couldn't parse DialerPhoneNumber", e);
+ }
+ return dialerPhoneNumber;
+ }
+
+ @TargetApi(Build.VERSION_CODES.M) // Uses try-with-resources
+ private void handleInsertsAndUpdates(
+ Context appContext, CallLogMutations mutations, Set<Long> existingAnnotatedCallLogIds) {
+ long previousTimestampProcessed =
+ PreferenceManager.getDefaultSharedPreferences(appContext)
+ .getLong(PREF_LAST_TIMESTAMP_PROCESSED, 0L);
+
+ DialerPhoneNumberUtil dialerPhoneNumberUtil =
+ new DialerPhoneNumberUtil(PhoneNumberUtil.getInstance());
+
+ // TODO: Really should be getting last 1000 by timestamp, not by last modified.
+ try (Cursor cursor =
+ appContext
+ .getContentResolver()
+ .query(
+ Calls.CONTENT_URI, // Excludes voicemail
+ new String[] {
+ Calls._ID, Calls.DATE, Calls.LAST_MODIFIED, Calls.NUMBER, Calls.COUNTRY_ISO
+ },
+ Calls.LAST_MODIFIED + " > ?",
+ new String[] {String.valueOf(previousTimestampProcessed)},
+ Calls.LAST_MODIFIED + " DESC LIMIT 1000")) {
- // TODO: Implementation.
+ if (cursor == null) {
+ LogUtil.e("SystemCallLogDataSource.handleInsertsAndUpdates", "null cursor");
+ return;
+ }
+
+ LogUtil.i(
+ "SystemCallLogDataSource.handleInsertsAndUpdates",
+ "found %d entries to insert/update",
+ cursor.getCount());
+
+ if (cursor.moveToFirst()) {
+ int idColumn = cursor.getColumnIndexOrThrow(Calls._ID);
+ int dateColumn = cursor.getColumnIndexOrThrow(Calls.DATE);
+ int lastModifiedColumn = cursor.getColumnIndexOrThrow(Calls.LAST_MODIFIED);
+ int numberColumn = cursor.getColumnIndexOrThrow(Calls.NUMBER);
+ int countryIsoColumn = cursor.getColumnIndexOrThrow(Calls.COUNTRY_ISO);
+
+ // The cursor orders by LAST_MODIFIED DESC, so the first result is the most recent timestamp
+ // processed.
+ lastTimestampProcessed = cursor.getLong(lastModifiedColumn);
+ do {
+ long id = cursor.getLong(idColumn);
+ long date = cursor.getLong(dateColumn);
+ String numberAsStr = cursor.getString(numberColumn);
+ String countryIso = cursor.getString(countryIsoColumn);
+
+ byte[] numberAsProtoBytes =
+ dialerPhoneNumberUtil.parse(numberAsStr, countryIso).toByteArray();
+
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(AnnotatedCallLog.TIMESTAMP, date);
+ contentValues.put(AnnotatedCallLog.NUMBER, numberAsProtoBytes);
+
+ if (existingAnnotatedCallLogIds.contains(id)) {
+ mutations.update(id, contentValues);
+ } else {
+ mutations.insert(id, contentValues);
+ }
+ } while (cursor.moveToNext());
+ } // else no new results, do nothing.
+ }
+ }
+
+ private static void handleDeletes(
+ Context appContext, Set<Long> existingAnnotatedCallLogIds, CallLogMutations mutations) {
+ Set<Long> systemCallLogIds =
+ getIdsFromSystemCallLogThatMatch(appContext, existingAnnotatedCallLogIds);
+ LogUtil.i(
+ "SystemCallLogDataSource.handleDeletes",
+ "found %d matching entries in system call log",
+ systemCallLogIds.size());
+ Set<Long> idsInAnnotatedCallLogNoLongerInSystemCallLog = new ArraySet<>();
+ idsInAnnotatedCallLogNoLongerInSystemCallLog.addAll(existingAnnotatedCallLogIds);
+ idsInAnnotatedCallLogNoLongerInSystemCallLog.removeAll(systemCallLogIds);
+
+ LogUtil.i(
+ "SystemCallLogDataSource.handleDeletes",
+ "found %d call log entries to remove",
+ idsInAnnotatedCallLogNoLongerInSystemCallLog.size());
+
+ for (long id : idsInAnnotatedCallLogNoLongerInSystemCallLog) {
+ mutations.delete(id);
+ }
+ }
+
+ @TargetApi(Build.VERSION_CODES.M) // Uses try-with-resources
+ private static Set<Long> getAnnotatedCallLogIds(Context appContext) {
+ ArraySet<Long> ids = new ArraySet<>();
+
+ try (Cursor cursor =
+ appContext
+ .getContentResolver()
+ .query(
+ AnnotatedCallLog.CONTENT_URI,
+ new String[] {AnnotatedCallLog._ID},
+ null,
+ null,
+ null)) {
+
+ if (cursor == null) {
+ LogUtil.e("SystemCallLogDataSource.getAnnotatedCallLogIds", "null cursor");
+ return ids;
+ }
+
+ if (cursor.moveToFirst()) {
+ int idColumn = cursor.getColumnIndexOrThrow(AnnotatedCallLog._ID);
+ do {
+ ids.add(cursor.getLong(idColumn));
+ } while (cursor.moveToNext());
+ }
+ }
+ return ids;
+ }
+
+ @TargetApi(Build.VERSION_CODES.M) // Uses try-with-resources
+ private static Set<Long> getIdsFromSystemCallLogThatMatch(
+ Context appContext, Set<Long> matchingIds) {
+ ArraySet<Long> ids = new ArraySet<>();
+
+ String[] questionMarks = new String[matchingIds.size()];
+ Arrays.fill(questionMarks, "?");
+ String whereClause = (Calls._ID + " in (") + TextUtils.join(",", questionMarks) + ")";
+ String[] whereArgs = new String[matchingIds.size()];
+ int i = 0;
+ for (long id : matchingIds) {
+ whereArgs[i++] = String.valueOf(id);
+ }
+
+ try (Cursor cursor =
+ appContext
+ .getContentResolver()
+ .query(Calls.CONTENT_URI, new String[] {Calls._ID}, whereClause, whereArgs, null)) {
+
+ if (cursor == null) {
+ LogUtil.e("SystemCallLogDataSource.getIdsFromSystemCallLog", "null cursor");
+ return ids;
+ }
+
+ if (cursor.moveToFirst()) {
+ int idColumn = cursor.getColumnIndexOrThrow(Calls._ID);
+ do {
+ ids.add(cursor.getLong(idColumn));
+ } while (cursor.moveToNext());
+ }
+ return ids;
+ }
}
private static class CallLogObserver extends ContentObserver {