Enhance logging for vibration during ringing.

Adding call events for vibration which occurs during ringing.  When
vibration is suppressed, logging reason for skipping vibration.  This
will help diagnose issues where the phone does not vibrate when ringing.

Merged-In: I58713b270decb207d50b058131723bebfd8c4c59
Bug: 33376887
Change-Id: I58713b270decb207d50b058131723bebfd8c4c59
diff --git a/src/com/android/server/telecom/Log.java b/src/com/android/server/telecom/Log.java
index 2282ff0..9903eec 100644
--- a/src/com/android/server/telecom/Log.java
+++ b/src/com/android/server/telecom/Log.java
@@ -125,6 +125,9 @@
         public static final String STOP_DTMF = "STOP_DTMF";
         public static final String START_RINGER = "START_RINGER";
         public static final String STOP_RINGER = "STOP_RINGER";
+        public static final String START_VIBRATOR = "START_VIBRATOR";
+        public static final String STOP_VIBRATOR = "STOP_VIBRATOR";
+        public static final String SKIP_VIBRATION = "SKIP_VIBRATION";
         public static final String SKIP_RINGING = "SKIP_RINGING";
         public static final String START_CALL_WAITING_TONE = "START_CALL_WAITING_TONE";
         public static final String STOP_CALL_WAITING_TONE = "STOP_CALL_WAITING_TONE";
diff --git a/src/com/android/server/telecom/Ringer.java b/src/com/android/server/telecom/Ringer.java
index 262f437..ca40801 100644
--- a/src/com/android/server/telecom/Ringer.java
+++ b/src/com/android/server/telecom/Ringer.java
@@ -62,9 +62,11 @@
     private RingtoneFactory mRingtoneFactory;
 
     /**
-     * Call objects that are ringing or call-waiting. These are used only for logging purposes.
+     * Call objects that are ringing, vibrating or call-waiting. These are used only for logging
+     * purposes.
      */
     private Call mRingingCall;
+    private Call mVibratingCall;
     private Call mCallWaitingCall;
 
     /**
@@ -131,10 +133,13 @@
             Log.i(this, "startRingingOrCallWaiting, skipping because volume is 0");
         }
 
-        if (shouldVibrate(mContext) && !mIsVibrating) {
+        if (shouldVibrate(mContext, foregroundCall) && !mIsVibrating) {
+            mVibratingCall = foregroundCall;
             mVibrator.vibrate(VIBRATION_PATTERN, VIBRATION_PATTERN_REPEAT,
                     VIBRATION_ATTRIBUTES);
             mIsVibrating = true;
+        } else if (mIsVibrating) {
+            Log.event(foregroundCall, Log.Events.SKIP_VIBRATION, "already vibrating");
         }
 
         return isRingerAudible;
@@ -172,8 +177,10 @@
         mRingtonePlayer.stop();
 
         if (mIsVibrating) {
+            Log.event(mVibratingCall, Log.Events.STOP_VIBRATOR);
             mVibrator.cancel();
             mIsVibrating = false;
+            mVibratingCall = null;
         }
     }
 
@@ -200,14 +207,31 @@
         return manager.matchesCallFilter(extras);
     }
 
-    private boolean shouldVibrate(Context context) {
+    private boolean shouldVibrate(Context context, Call call) {
         AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
         int ringerMode = audioManager.getRingerModeInternal();
+        boolean shouldVibrate;
         if (getVibrateWhenRinging(context)) {
-            return ringerMode != AudioManager.RINGER_MODE_SILENT;
+            shouldVibrate = ringerMode != AudioManager.RINGER_MODE_SILENT;
         } else {
-            return ringerMode == AudioManager.RINGER_MODE_VIBRATE;
+            shouldVibrate = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
         }
+
+        // Technically this should be in the calling method, but it seemed a little odd to pass
+        // around a whole bunch of state just for logging purposes.
+        if (shouldVibrate) {
+            Log.event(call, Log.Events.START_VIBRATOR,
+                    "hasVibrator=%b, userRequestsVibrate=%b, ringerMode=%d, isVibrating=%b",
+                    mVibrator.hasVibrator(), mSystemSettingsUtil.canVibrateWhenRinging(context),
+                    ringerMode, mIsVibrating);
+        } else {
+            Log.event(call, Log.Events.SKIP_VIBRATION,
+                    "hasVibrator=%b, userRequestsVibrate=%b, ringerMode=%d, isVibrating=%b",
+                    mVibrator.hasVibrator(), mSystemSettingsUtil.canVibrateWhenRinging(context),
+                    ringerMode, mIsVibrating);
+        }
+
+        return shouldVibrate;
     }
 
     private boolean getVibrateWhenRinging(Context context) {