Add @TestApi annotations to CS-side RTT APIs for CTS

CTS tests need both the in-call and connection service sides of the APIs
to test Telecom functionality properly, so annotating the hidden-for-now
connection service APIs with TestApi.

Test: part of CTS
Change-Id: I3711729d7e8c8aff2735f4da9fbd04bcca6b4942
diff --git a/telecomm/java/android/telecom/Call.java b/telecomm/java/android/telecom/Call.java
index 27f7172..d79a102 100644
--- a/telecomm/java/android/telecom/Call.java
+++ b/telecomm/java/android/telecom/Call.java
@@ -981,15 +981,24 @@
          * @return A string containing text sent by the remote user, or {@code null} if the
          * conversation has been terminated or if there was an error while reading.
          */
-        public String read() {
-            try {
-                int numRead = mReceiveStream.read(mReadBuffer, 0, READ_BUFFER_SIZE);
-                if (numRead < 0) {
-                    return null;
-                }
-                return new String(mReadBuffer, 0, numRead);
-            } catch (IOException e) {
-                Log.w(this, "Exception encountered when reading from InputStreamReader: %s", e);
+        public String read() throws IOException {
+            int numRead = mReceiveStream.read(mReadBuffer, 0, READ_BUFFER_SIZE);
+            if (numRead < 0) {
+                return null;
+            }
+            return new String(mReadBuffer, 0, numRead);
+        }
+
+        /**
+         * Non-blocking version of {@link #read()}. Returns {@code null} if there is nothing to
+         * be read.
+         * @return A string containing text entered by the user, or {@code null} if the user has
+         * not entered any new text yet.
+         */
+        public String readImmediately() throws IOException {
+            if (mReceiveStream.ready()) {
+                return read();
+            } else {
                 return null;
             }
         }
diff --git a/telecomm/java/android/telecom/Connection.java b/telecomm/java/android/telecom/Connection.java
index 833affa..835eac5 100644
--- a/telecomm/java/android/telecom/Connection.java
+++ b/telecomm/java/android/telecom/Connection.java
@@ -24,6 +24,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.app.Notification;
 import android.content.Intent;
 import android.hardware.camera2.CameraManager;
@@ -395,9 +396,11 @@
     public static final int PROPERTY_SELF_MANAGED = 1<<7;
 
     /**
-     * When set, indicates that a connection has an active RTT session associated with it.
+     * Set by the framework to indicate that a connection has an active RTT session associated with
+     * it.
      * @hide
      */
+    @TestApi
     public static final int PROPERTY_IS_RTT = 1 << 8;
 
     //**********************************************************************************************
@@ -779,6 +782,7 @@
      * Provides methods to read and write RTT data to/from the in-call app.
      * @hide
      */
+    @TestApi
     public static final class RttTextStream {
         private static final int READ_BUFFER_SIZE = 1000;
         private final InputStreamReader mPipeFromInCall;
@@ -824,15 +828,24 @@
          * @return A string containing text entered by the user, or {@code null} if the
          * conversation has been terminated or if there was an error while reading.
          */
-        public String read() {
-            try {
-                int numRead = mPipeFromInCall.read(mReadBuffer, 0, READ_BUFFER_SIZE);
-                if (numRead < 0) {
-                    return null;
-                }
-                return new String(mReadBuffer, 0, numRead);
-            } catch (IOException e) {
-                Log.w(this, "Exception encountered when reading from InputStreamReader: %s", e);
+        public String read() throws IOException {
+            int numRead = mPipeFromInCall.read(mReadBuffer, 0, READ_BUFFER_SIZE);
+            if (numRead < 0) {
+                return null;
+            }
+            return new String(mReadBuffer, 0, numRead);
+        }
+
+        /**
+         * Non-blocking version of {@link #read()}. Returns {@code null} if there is nothing to
+         * be read.
+         * @return A string containing text entered by the user, or {@code null} if the user has
+         * not entered any new text yet.
+         */
+        public String readImmediately() throws IOException {
+            if (mPipeFromInCall.ready()) {
+                return read();
+            } else {
                 return null;
             }
         }
@@ -2491,7 +2504,9 @@
      * {@link #onStartRtt(ParcelFileDescriptor, ParcelFileDescriptor)} has succeeded.
      * @hide
      */
+    @TestApi
     public final void sendRttInitiationSuccess() {
+        setRttProperty();
         mListeners.forEach((l) -> l.onRttInitiationSuccess(Connection.this));
     }
 
@@ -2504,7 +2519,9 @@
      *               exception of {@link RttModifyStatus#SESSION_MODIFY_REQUEST_SUCCESS}.
      * @hide
      */
+    @TestApi
     public final void sendRttInitiationFailure(int reason) {
+        unsetRttProperty();
         mListeners.forEach((l) -> l.onRttInitiationFailure(Connection.this, reason));
     }
 
@@ -2513,6 +2530,7 @@
      * side of the coll.
      * @hide
      */
+    @TestApi
     public final void sendRttSessionRemotelyTerminated() {
         mListeners.forEach((l) -> l.onRttSessionRemotelyTerminated(Connection.this));
     }
@@ -2522,6 +2540,7 @@
      * RTT session in the call.
      * @hide
      */
+    @TestApi
     public final void sendRemoteRttRequest() {
         mListeners.forEach((l) -> l.onRemoteRttRequest(Connection.this));
     }
@@ -2740,6 +2759,7 @@
      *                      the in-call app.
      * @hide
      */
+    @TestApi
     public void onStartRtt(@NonNull RttTextStream rttTextStream) {}
 
     /**
@@ -2747,6 +2767,7 @@
      * channel. No response to Telecom is needed for this method.
      * @hide
      */
+    @TestApi
     public void onStopRtt() {}
 
     /**
@@ -2758,8 +2779,25 @@
      * @param rttTextStream The object that should be used to send text to or receive text from
      *                      the in-call app.
      */
+    @TestApi
     public void handleRttUpgradeResponse(@Nullable RttTextStream rttTextStream) {}
 
+    /**
+     * Internal method to set {@link #PROPERTY_IS_RTT}.
+     * @hide
+     */
+    void setRttProperty() {
+        setConnectionProperties(getConnectionProperties() | PROPERTY_IS_RTT);
+    }
+
+    /**
+     * Internal method to un-set {@link #PROPERTY_IS_RTT}.
+     * @hide
+     */
+    void unsetRttProperty() {
+        setConnectionProperties(getConnectionProperties() & (~PROPERTY_IS_RTT));
+    }
+
     static String toLogSafePhoneNumber(String number) {
         // For unknown number, log empty string.
         if (number == null) {
diff --git a/telecomm/java/android/telecom/ConnectionRequest.java b/telecomm/java/android/telecom/ConnectionRequest.java
index 054de4c..e169e5f 100644
--- a/telecomm/java/android/telecom/ConnectionRequest.java
+++ b/telecomm/java/android/telecom/ConnectionRequest.java
@@ -16,6 +16,7 @@
 
 package android.telecom;
 
+import android.annotation.TestApi;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Parcel;
@@ -311,6 +312,7 @@
      * if this connection request is not requesting an RTT session upon connection establishment.
      * @hide
      */
+    @TestApi
     public Connection.RttTextStream getRttTextStream() {
         if (isRequestingRtt()) {
             return new Connection.RttTextStream(mRttPipeToInCall, mRttPipeFromInCall);
@@ -324,6 +326,7 @@
      * @return {@code true} if RTT is requested, {@code false} otherwise.
      * @hide
      */
+    @TestApi
     public boolean isRequestingRtt() {
         return mRttPipeFromInCall != null && mRttPipeToInCall != null;
     }
diff --git a/telecomm/java/android/telecom/ConnectionService.java b/telecomm/java/android/telecom/ConnectionService.java
index bf8f8e4..7401dda 100644
--- a/telecomm/java/android/telecom/ConnectionService.java
+++ b/telecomm/java/android/telecom/ConnectionService.java
@@ -1603,6 +1603,7 @@
         Log.d(this, "stopRtt(%s)", callId);
         if (mConnectionById.containsKey(callId)) {
             findConnectionForAction(callId, "stopRtt").onStopRtt();
+            findConnectionForAction(callId, "stopRtt").unsetRttProperty();
         } else if (mConferenceById.containsKey(callId)) {
             Log.w(this, "stopRtt called on a conference.");
         }