Merge "Support bulk inserted voicemails."
diff --git a/java/com/android/dialer/app/AndroidManifest.xml b/java/com/android/dialer/app/AndroidManifest.xml
index 2ef5dad..ad77149 100644
--- a/java/com/android/dialer/app/AndroidManifest.xml
+++ b/java/com/android/dialer/app/AndroidManifest.xml
@@ -86,6 +86,11 @@
           android:mimeType="vnd.android.cursor.item/voicemail"
           android:scheme="content"
           />
+        <data
+            android:host="com.android.voicemail"
+            android:mimeType="vnd.android.cursor.dir/voicemails"
+            android:scheme="content"
+            />
       </intent-filter>
       <intent-filter android:priority="100">
         <action android:name="android.intent.action.BOOT_COMPLETED"/>
diff --git a/java/com/android/dialer/app/calllog/VisualVoicemailNotifier.java b/java/com/android/dialer/app/calllog/VisualVoicemailNotifier.java
index 2fd9a21..4fc956f 100644
--- a/java/com/android/dialer/app/calllog/VisualVoicemailNotifier.java
+++ b/java/com/android/dialer/app/calllog/VisualVoicemailNotifier.java
@@ -64,11 +64,16 @@
    */
   private static final String GROUP_KEY = "VisualVoicemailGroup";
 
+  /**
+   * @param shouldAlert whether ringtone or vibration should be made when the notification is posted
+   *     or updated. Should only be true when there is a real new voicemail.
+   */
   public static void showNotifications(
       @NonNull Context context,
       @NonNull List<NewCall> newCalls,
       @NonNull Map<String, ContactInfo> contactInfos,
-      @Nullable String callers) {
+      @Nullable String callers,
+      boolean shouldAlert) {
     LogUtil.enterBlock("VisualVoicemailNotifier.showNotifications");
     PendingIntent deleteIntent =
         CallLogNotificationsService.createMarkAllNewVoicemailsAsOldIntent(context);
@@ -86,7 +91,15 @@
             .setContentIntent(newVoicemailIntent(context, null));
 
     if (VERSION.SDK_INT >= VERSION_CODES.O) {
-      groupSummary.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN);
+      if (shouldAlert) {
+        groupSummary.setOnlyAlertOnce(false);
+        // Group summary will alert when posted/updated
+        groupSummary.setGroupAlertBehavior(Notification.GROUP_ALERT_ALL);
+      } else {
+        // Only children will alert. but since all children are set to "only alert summary" it is
+        // effectively silenced.
+        groupSummary.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN);
+      }
       PhoneAccountHandle handle = getAccountForCall(context, newCalls.get(0));
       groupSummary.setChannelId(NotificationChannelManager.getVoicemailChannelId(context, handle));
     }
@@ -203,6 +216,7 @@
 
     if (VERSION.SDK_INT >= VERSION_CODES.O) {
       builder.setChannelId(NotificationChannelManager.getVoicemailChannelId(context, handle));
+      builder.setGroupAlertBehavior(Notification.GROUP_ALERT_SUMMARY);
     }
 
     ContactPhotoLoader loader = new ContactPhotoLoader(context, contactInfo);
diff --git a/java/com/android/dialer/app/calllog/VisualVoicemailUpdateTask.java b/java/com/android/dialer/app/calllog/VisualVoicemailUpdateTask.java
index e30623b..77b4c11 100644
--- a/java/com/android/dialer/app/calllog/VisualVoicemailUpdateTask.java
+++ b/java/com/android/dialer/app/calllog/VisualVoicemailUpdateTask.java
@@ -67,7 +67,7 @@
       // Query failed, just return
       return;
     }
-
+    boolean shouldAlert = !voicemailsToNotify.isEmpty();
     voicemailsToNotify.addAll(getAndUpdateVoicemailsWithExistingNotification(context, queryHelper));
     voicemailsToNotify = filterBlockedNumbers(context, queryHandler, voicemailsToNotify);
     if (voicemailsToNotify.isEmpty()) {
@@ -100,7 +100,8 @@
         }
       }
     }
-    VisualVoicemailNotifier.showNotifications(context, voicemailsToNotify, contactInfos, callers);
+    VisualVoicemailNotifier.showNotifications(
+        context, voicemailsToNotify, contactInfos, callers, shouldAlert);
 
     // Set trigger to update notifications when database changes.
     VoicemailNotificationJobService.scheduleJob(context);
diff --git a/java/com/android/dialer/simulator/impl/SimulatorNotifications.java b/java/com/android/dialer/simulator/impl/SimulatorNotifications.java
index 3f402d3..4ed7c9b 100644
--- a/java/com/android/dialer/simulator/impl/SimulatorNotifications.java
+++ b/java/com/android/dialer/simulator/impl/SimulatorNotifications.java
@@ -16,12 +16,15 @@
 
 package com.android.dialer.simulator.impl;
 
+import android.content.ContentValues;
 import android.content.Context;
 import android.provider.VoicemailContract.Voicemails;
 import android.support.annotation.NonNull;
 import android.view.ActionProvider;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.databasepopulator.VoicemailPopulator;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 /** Implements the simulator submenu. */
@@ -45,6 +48,7 @@
 
   private static void addVoicemailNotifications(@NonNull Context context) {
     LogUtil.enterBlock("SimulatorNotifications.addVoicemailNotifications");
+    List<ContentValues> voicemails = new ArrayList<>();
     for (int i = NOTIFICATION_COUNT; i > 0; i--) {
       VoicemailPopulator.Voicemail voicemail =
           VoicemailPopulator.Voicemail.builder()
@@ -54,11 +58,12 @@
               .setIsRead(false)
               .setTimeMillis(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(i))
               .build();
-      context
-          .getContentResolver()
-          .insert(
-              Voicemails.buildSourceUri(context.getPackageName()),
-              voicemail.getAsContentValues(context));
+      voicemails.add(voicemail.getAsContentValues(context));
     }
+    context
+        .getContentResolver()
+        .bulkInsert(
+            Voicemails.buildSourceUri(context.getPackageName()),
+            voicemails.toArray(new ContentValues[voicemails.size()]));
   }
 }