Merge "Default column IS_VOICEMAIL_CALL to 0 in the annotated call log."
diff --git a/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java b/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java
index 32f303f..8fc80a6 100644
--- a/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java
+++ b/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java
@@ -20,6 +20,7 @@
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.provider.CallLog.Calls;
+import android.support.annotation.VisibleForTesting;
 import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.AnnotatedCallLog;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
@@ -34,6 +35,8 @@
 @Singleton
 public class AnnotatedCallLogDatabaseHelper extends SQLiteOpenHelper {
 
+  @VisibleForTesting static final int VERSION = 3;
+
   private static final String FILENAME = "annotated_call_log.db";
 
   private final Context appContext;
@@ -45,7 +48,7 @@
       @ApplicationContext Context appContext,
       @AnnotatedCallLogMaxRows int maxRows,
       @BackgroundExecutor ListeningExecutorService backgroundExecutor) {
-    super(appContext, FILENAME, null, 2);
+    super(appContext, FILENAME, null, VERSION);
 
     this.appContext = appContext;
     this.maxRows = maxRows;
@@ -73,26 +76,12 @@
           + (AnnotatedCallLog.VOICEMAIL_URI + " text, ")
           + (AnnotatedCallLog.CALL_TYPE + " integer not null, ")
           + (AnnotatedCallLog.NUMBER_ATTRIBUTES + " blob, ")
-          + (AnnotatedCallLog.IS_VOICEMAIL_CALL + " integer, ")
+          + (AnnotatedCallLog.IS_VOICEMAIL_CALL + " integer default 0, ")
           + (AnnotatedCallLog.VOICEMAIL_CALL_TAG + " text, ")
           + (AnnotatedCallLog.TRANSCRIPTION_STATE + " integer, ")
           + (AnnotatedCallLog.CALL_MAPPING_ID + " text")
           + ");";
 
-  private static final String ALTER_TABLE_SQL_ADD_CALL_MAPPING_ID_COLUMN =
-      "alter table "
-          + AnnotatedCallLog.TABLE
-          + " add column "
-          + AnnotatedCallLog.CALL_MAPPING_ID
-          + " text;";
-  private static final String UPDATE_CALL_MAPPING_ID_COLUMN =
-      "update "
-          + AnnotatedCallLog.TABLE
-          + " set "
-          + AnnotatedCallLog.CALL_MAPPING_ID
-          + " = "
-          + AnnotatedCallLog.TIMESTAMP;
-
   /**
    * Deletes all but the first maxRows rows (by timestamp, excluding voicemails) to keep the table a
    * manageable size.
@@ -159,10 +148,40 @@
 
   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-    if (oldVersion == 1 && newVersion == 2) {
-      db.execSQL(ALTER_TABLE_SQL_ADD_CALL_MAPPING_ID_COLUMN);
-      db.execSQL(UPDATE_CALL_MAPPING_ID_COLUMN);
+    if (oldVersion < 2) {
+      upgradeToV2(db);
     }
+    if (oldVersion < 3) {
+      upgradeToV3(db);
+    }
+  }
+
+  private static void upgradeToV2(SQLiteDatabase db) {
+    db.execSQL(
+        "alter table "
+            + AnnotatedCallLog.TABLE
+            + " add column "
+            + AnnotatedCallLog.CALL_MAPPING_ID
+            + " text;");
+    db.execSQL(
+        "update "
+            + AnnotatedCallLog.TABLE
+            + " set "
+            + AnnotatedCallLog.CALL_MAPPING_ID
+            + " = "
+            + AnnotatedCallLog.TIMESTAMP);
+  }
+
+  private static void upgradeToV3(SQLiteDatabase db) {
+    db.execSQL(
+        "update "
+            + AnnotatedCallLog.TABLE
+            + " set "
+            + AnnotatedCallLog.IS_VOICEMAIL_CALL
+            + " = 0 "
+            + "where "
+            + AnnotatedCallLog.IS_VOICEMAIL_CALL
+            + " is null");
   }
 
   /** Closes the database and deletes it. */
diff --git a/java/com/android/dialer/calllog/database/Coalescer.java b/java/com/android/dialer/calllog/database/Coalescer.java
index 36df87c..903b7e2 100644
--- a/java/com/android/dialer/calllog/database/Coalescer.java
+++ b/java/com/android/dialer/calllog/database/Coalescer.java
@@ -321,14 +321,11 @@
             .setFeatures(coalescedContentValues.getAsInteger(CoalescedAnnotatedCallLog.FEATURES))
             .setCallType(coalescedContentValues.getAsInteger(CoalescedAnnotatedCallLog.CALL_TYPE))
             .setNumberAttributes(numberAttributes)
+            .setIsVoicemailCall(
+                coalescedContentValues.getAsInteger(CoalescedAnnotatedCallLog.IS_VOICEMAIL_CALL)
+                    == 1)
             .setCoalescedIds(coalescedIds);
 
-    // TODO(linyuh): none of the boolean columns in the annotated call log should be null.
-    // This is a bug in VoicemailDataSource, but we should also fix the database constraints.
-    Integer isVoicemailCall =
-        coalescedContentValues.getAsInteger(CoalescedAnnotatedCallLog.IS_VOICEMAIL_CALL);
-    coalescedRowBuilder.setIsVoicemailCall(isVoicemailCall != null && isVoicemailCall == 1);
-
     String formattedNumber =
         coalescedContentValues.getAsString(CoalescedAnnotatedCallLog.FORMATTED_NUMBER);
     if (!TextUtils.isEmpty(formattedNumber)) {