Notify D2D availability.

Notify D2D availability to Telecom using a new hidden EXTRA key.

Test: Added unit test for this case.
Bug: 163085177
Change-Id: Iad4b062d5612c784014c73a4da3c554c06cb7380
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 69805cd..dba1691 100755
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -3345,6 +3345,7 @@
         if (!getPhone().getContext().getResources().getBoolean(
                 R.bool.config_use_device_to_device_communication)) {
             Log.i(this, "maybeConfigureDeviceToDeviceCommunication: not using D2D.");
+            notifyD2DAvailabilityChanged(false);
             return;
         }
         if (!isImsConnection()) {
@@ -3352,10 +3353,12 @@
             if (mCommunicator != null) {
                 mCommunicator = null;
             }
+            notifyD2DAvailabilityChanged(false);
             return;
         }
         if (mTreatAsEmergencyCall || mIsNetworkIdentifiedEmergencyCall) {
             Log.i(this, "maybeConfigureDeviceToDeviceCommunication: emergency call; no D2D");
+            notifyD2DAvailabilityChanged(false);
             return;
         }
 
@@ -3409,10 +3412,22 @@
             addTelephonyConnectionListener(mD2DCallStateAdapter);
         } else {
             Log.i(this, "maybeConfigureDeviceToDeviceCommunication: no transports; disabled.");
+            notifyD2DAvailabilityChanged(false);
         }
     }
 
     /**
+     * Notifies upper layers of the availability of D2D communication.
+     * @param isAvailable {@code true} if D2D is available, {@code false} otherwise.
+     */
+    private void notifyD2DAvailabilityChanged(boolean isAvailable) {
+        Bundle extras = new Bundle();
+        extras.putBoolean(Connection.EXTRA_IS_DEVICE_TO_DEVICE_COMMUNICATION_AVAILABLE,
+                isAvailable);
+        putTelephonyExtras(extras);
+    }
+
+    /**
      * @return The D2D communication class, or {@code null} if not set up.
      */
     public @Nullable Communicator getCommunicator() {
@@ -3472,6 +3487,15 @@
     }
 
     /**
+     * Handles report from {@link Communicator} when the availability of D2D changes.
+     * @param isAvailable {@code true} if D2D is available, {@code false} if unavailable.
+     */
+    @Override
+    public void onD2DAvailabilitychanged(boolean isAvailable) {
+        notifyD2DAvailabilityChanged(isAvailable);
+    }
+
+    /**
      * Called by a {@link ConnectionService} to notify Telecom that a {@link Conference#onMerge()}
      * operation has started.
      */