Fixed that extra vibration was played

The alert vibration did not stop at 10.5 seconds because
the pre-recorded 10.5 seconds tone took more than 10.5s to
finish playing. It caused the vibration pattern to repeat
and vibrated for extra 0.2 seconds.

The solution was that we only play the vibration once if
we also play the tone once, which is the case for most
of the countries.

Test: Manual
Bug: 118358949
Change-Id: I76ee30e75b36f78b4e9b1fc16ee422bb139f1677
(cherry picked from commit b22c671d0b1a0f7082e8a382e871e815ffd7690a)
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java
index abb3235..e75d09b 100644
--- a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java
+++ b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java
@@ -349,6 +349,8 @@
         // Vibration duration in milliseconds
         long vibrateDuration = 0;
 
+        // Get the alert tone duration. Negative tone duration value means we only play the tone
+        // once, not repeat it.
         int customAlertDuration = getResources().getInteger(R.integer.alert_duration);
 
         // Start the vibration first.
@@ -365,8 +367,12 @@
             AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
             attrBuilder.setUsage(AudioAttributes.USAGE_ALARM);
             AudioAttributes attr = attrBuilder.build();
-            VibrationEffect effect = VibrationEffect.createWaveform(vibrationPattern, 0);
-            log("vibrate: effect=" + effect + ", attr=" + attr);
+            // If we only play the tone once, then we also play the vibration pattern once.
+            int repeatIndex = (customAlertDuration < 0)
+                    ? -1 /* not repeat */ : 0 /* index to repeat */;
+            VibrationEffect effect = VibrationEffect.createWaveform(vibrationPattern, repeatIndex);
+            log("vibrate: effect=" + effect + ", attr=" + attr + ", duration="
+                    + customAlertDuration);
             mVibrator.vibrate(effect, attr);
         }