Add unique id for sms APIs to be able to trigger correct smstracker

In old APIs the SmsTracker itself was being passed to the RIL
and onSendComplete the tracker's pending intent was triggered
and then the updated messageRef was used as the id.

Instead of passing the tracker, passing a unique id to be used
for the lifetime of the message.

Doing same thing for receiving flow.

Test: None, APIs not exercised yet.
BUG=69846044
Merged-In: Id19f854e2c48649db8f2031ee4f49cdac331451c
Change-Id: Id19f854e2c48649db8f2031ee4f49cdac331451c
diff --git a/telephony/java/android/telephony/ims/internal/SmsImplBase.java b/telephony/java/android/telephony/ims/internal/SmsImplBase.java
index 47414cf..eb805a8 100644
--- a/telephony/java/android/telephony/ims/internal/SmsImplBase.java
+++ b/telephony/java/android/telephony/ims/internal/SmsImplBase.java
@@ -124,70 +124,79 @@
    * method should be implemented by the IMS providers to provide implementation of sending an SMS
    * over IMS.
    *
-   * @param smsc the Short Message Service Center address.
-   * @param format the format of the message. Valid values are {@link SmsMessage#FORMAT_3GPP} and
-   * {@link SmsMessage#FORMAT_3GPP2}.
+   * @param token unique token generated by the platform that should be used when triggering
+   *             callbacks for this specific message.
    * @param messageRef the message reference.
+   * @param format the format of the message. Valid values are {@link SmsMessage#FORMAT_3GPP} and
+   * @param smsc the Short Message Service Center address.
+   * {@link SmsMessage#FORMAT_3GPP2}.
    * @param isRetry whether it is a retry of an already attempted message or not.
    * @param pdu PDUs representing the contents of the message.
    */
-  public void sendSms(int messageRef, String format, String smsc, boolean isRetry, byte[] pdu) {
+  public void sendSms(int token, int messageRef, String format, String smsc, boolean isRetry,
+                      byte[] pdu) {
     // Base implementation returns error. Should be overridden.
     try {
-      onSendSmsResult(messageRef, SEND_STATUS_ERROR, SmsManager.RESULT_ERROR_GENERIC_FAILURE);
+      onSendSmsResult(token, messageRef, SEND_STATUS_ERROR,
+              SmsManager.RESULT_ERROR_GENERIC_FAILURE);
     } catch (RemoteException e) {
       Log.e(LOG_TAG, "Can not send sms: " + e.getMessage());
     }
   }
 
   /**
-   * This method will be triggered by the platform after {@link #onSmsReceived(String, byte[])} has
-   * been called to deliver the result to the IMS provider.
+   * This method will be triggered by the platform after {@link #onSmsReceived(int, String, byte[])}
+   * has been called to deliver the result to the IMS provider.
    *
+   * @param token token provided in {@link #onSmsReceived(int, String, byte[])}
    * @param result result of delivering the message. Valid values are defined in
    * {@link DeliverStatusResult}
-   * @param messageRef the message reference or -1 of unavailable.
+   * @param messageRef the message reference
    */
-  public void acknowledgeSms(int messageRef, @DeliverStatusResult int result) {
+  public void acknowledgeSms(int token, int messageRef, @DeliverStatusResult int result) {
 
   }
 
   /**
    * This method will be triggered by the platform after
-   * {@link #onSmsStatusReportReceived(int, int, byte[])} has been called to provide the result to
-   * the IMS provider.
+   * {@link #onSmsStatusReportReceived(int, int, String, byte[])} has been called to provide the
+   * result to the IMS provider.
    *
+   * @param token token provided in {@link #sendSms(int, int, String, String, boolean, byte[])}
    * @param result result of delivering the message. Valid values are defined in
    * {@link StatusReportResult}
-   * @param messageRef the message reference or -1 of unavailable.
+   * @param messageRef the message reference
    */
-  public void acknowledgeSmsReport(int messageRef, @StatusReportResult int result) {
+  public void acknowledgeSmsReport(int token, int messageRef, @StatusReportResult int result) {
 
   }
 
   /**
    * This method should be triggered by the IMS providers when there is an incoming message. The
    * platform will deliver the message to the messages database and notify the IMS provider of the
-   * result by calling {@link #acknowledgeSms(int, int)}.
+   * result by calling {@link #acknowledgeSms(int, int, int)}.
    *
    * This method must not be called before {@link MmTelFeature#onFeatureReady()} is called.
    *
+   * @param token unique token generated by IMS providers that the platform will use to trigger
+   *              callbacks for this message.
    * @param format the format of the message. Valid values are {@link SmsMessage#FORMAT_3GPP} and
    * {@link SmsMessage#FORMAT_3GPP2}.
    * @param pdu PDUs representing the contents of the message.
    * @throws IllegalStateException if called before {@link MmTelFeature#onFeatureReady()}
    */
-  public final void onSmsReceived(String format, byte[] pdu) throws IllegalStateException {
+  public final void onSmsReceived(int token, String format, byte[] pdu)
+          throws IllegalStateException {
     synchronized (mLock) {
       if (mListener == null) {
         throw new IllegalStateException("Feature not ready.");
       }
       try {
-        mListener.onSmsReceived(format, pdu);
-        acknowledgeSms(-1, DELIVER_STATUS_OK);
+        mListener.onSmsReceived(token, format, pdu);
+        acknowledgeSms(token, 0, DELIVER_STATUS_OK);
       } catch (RemoteException e) {
         Log.e(LOG_TAG, "Can not deliver sms: " + e.getMessage());
-        acknowledgeSms(-1, DELIVER_STATUS_ERROR);
+        acknowledgeSms(token, 0, DELIVER_STATUS_ERROR);
       }
     }
   }
@@ -198,6 +207,7 @@
    *
    * This method must not be called before {@link MmTelFeature#onFeatureReady()} is called.
    *
+   * @param token token provided in {@link #sendSms(int, int, String, String, boolean, byte[])}
    * @param messageRef the message reference. Should be between 0 and 255 per TS.123.040
    * @param status result of sending the SMS. Valid values are defined in {@link SendStatusResult}
    * @param reason reason in case status is failure. Valid values are:
@@ -213,35 +223,37 @@
    * @throws RemoteException if the connection to the framework is not available. If this happens
    *  attempting to send the SMS should be aborted.
    */
-  public final void onSendSmsResult(int messageRef, @SendStatusResult int status, int reason)
-      throws IllegalStateException, RemoteException {
+  public final void onSendSmsResult(int token, int messageRef, @SendStatusResult int status,
+      int reason) throws IllegalStateException, RemoteException {
     synchronized (mLock) {
       if (mListener == null) {
         throw new IllegalStateException("Feature not ready.");
       }
-      mListener.onSendSmsResult(messageRef, status, reason);
+      mListener.onSendSmsResult(token, messageRef, status, reason);
     }
   }
 
   /**
    * Sets the status report of the sent message.
    *
+   * @param token token provided in {@link #sendSms(int, int, String, String, boolean, byte[])}
    * @param messageRef the message reference.
    * @param format the format of the message. Valid values are {@link SmsMessage#FORMAT_3GPP} and
    * {@link SmsMessage#FORMAT_3GPP2}.
    * @param pdu PDUs representing the content of the status report.
    * @throws IllegalStateException if called before {@link MmTelFeature#onFeatureReady()}
    */
-  public final void onSmsStatusReportReceived(int messageRef, String format, byte[] pdu) {
+  public final void onSmsStatusReportReceived(int token, int messageRef, String format,
+      byte[] pdu) {
     synchronized (mLock) {
       if (mListener == null) {
         throw new IllegalStateException("Feature not ready.");
       }
       try {
-        mListener.onSmsStatusReportReceived(messageRef, format, pdu);
+        mListener.onSmsStatusReportReceived(token, messageRef, format, pdu);
       } catch (RemoteException e) {
         Log.e(LOG_TAG, "Can not process sms status report: " + e.getMessage());
-        acknowledgeSmsReport(messageRef, STATUS_REPORT_STATUS_ERROR);
+        acknowledgeSmsReport(token, messageRef, STATUS_REPORT_STATUS_ERROR);
       }
     }
   }
diff --git a/telephony/java/android/telephony/ims/internal/aidl/IImsMmTelFeature.aidl b/telephony/java/android/telephony/ims/internal/aidl/IImsMmTelFeature.aidl
index d976686..785113f 100644
--- a/telephony/java/android/telephony/ims/internal/aidl/IImsMmTelFeature.aidl
+++ b/telephony/java/android/telephony/ims/internal/aidl/IImsMmTelFeature.aidl
@@ -52,8 +52,9 @@
             IImsCapabilityCallback c);
     // SMS APIs
     void setSmsListener(IImsSmsListener l);
-    oneway void sendSms(int messageRef, String format, String smsc, boolean retry, in byte[] pdu);
-    oneway void acknowledgeSms(int messageRef, int result);
-    oneway void acknowledgeSmsReport(int messageRef, int result);
+    oneway void sendSms(in int token, int messageRef, String format, String smsc, boolean retry,
+            in byte[] pdu);
+    oneway void acknowledgeSms(int token, int messageRef, int result);
+    oneway void acknowledgeSmsReport(int token, int messageRef, int result);
     String getSmsFormat();
 }
diff --git a/telephony/java/android/telephony/ims/internal/aidl/IImsSmsListener.aidl b/telephony/java/android/telephony/ims/internal/aidl/IImsSmsListener.aidl
index 468629a..bf8d90b 100644
--- a/telephony/java/android/telephony/ims/internal/aidl/IImsSmsListener.aidl
+++ b/telephony/java/android/telephony/ims/internal/aidl/IImsSmsListener.aidl
@@ -21,7 +21,8 @@
  * {@hide}
  */
 interface IImsSmsListener {
-    void onSendSmsResult(in int messageRef, in int status, in int reason);
-    void onSmsStatusReportReceived(in int messageRef, in String format, in byte[] pdu);
-    void onSmsReceived(in String format, in byte[] pdu);
+    void onSendSmsResult(in int token, in int messageRef, in int status, in int reason);
+    void onSmsStatusReportReceived(in int token, in int messageRef, in String format,
+            in byte[] pdu);
+    void onSmsReceived(in int token, in String format, in byte[] pdu);
 }
\ No newline at end of file
diff --git a/telephony/java/android/telephony/ims/internal/feature/MmTelFeature.java b/telephony/java/android/telephony/ims/internal/feature/MmTelFeature.java
index 057c9a86..8d888c2 100644
--- a/telephony/java/android/telephony/ims/internal/feature/MmTelFeature.java
+++ b/telephony/java/android/telephony/ims/internal/feature/MmTelFeature.java
@@ -154,23 +154,24 @@
         }
 
         @Override
-        public void sendSms(int messageRef, String format, String smsc, boolean retry, byte[] pdu) {
+        public void sendSms(int token, int messageRef, String format, String smsc, boolean retry,
+                byte[] pdu) {
             synchronized (mLock) {
-                MmTelFeature.this.sendSms(messageRef, format, smsc, retry, pdu);
+                MmTelFeature.this.sendSms(token, messageRef, format, smsc, retry, pdu);
             }
         }
 
         @Override
-        public void acknowledgeSms(int messageRef, int result) {
+        public void acknowledgeSms(int token, int messageRef, int result) {
             synchronized (mLock) {
-                MmTelFeature.this.acknowledgeSms(messageRef, result);
+                MmTelFeature.this.acknowledgeSms(token, messageRef, result);
             }
         }
 
         @Override
-        public void acknowledgeSmsReport(int messageRef, int result) {
+        public void acknowledgeSmsReport(int token, int messageRef, int result) {
             synchronized (mLock) {
-                MmTelFeature.this.acknowledgeSmsReport(messageRef, result);
+                MmTelFeature.this.acknowledgeSmsReport(token, messageRef, result);
             }
         }
 
@@ -456,16 +457,17 @@
         // Base Implementation - Should be overridden
     }
 
-    private void sendSms(int messageRef, String format, String smsc, boolean isRetry, byte[] pdu) {
-        getSmsImplementation().sendSms(messageRef, format, smsc, isRetry, pdu);
+    private void sendSms(int token, int messageRef, String format, String smsc, boolean isRetry,
+            byte[] pdu) {
+        getSmsImplementation().sendSms(token, messageRef, format, smsc, isRetry, pdu);
     }
 
-    private void acknowledgeSms(int messageRef, @DeliverStatusResult int result) {
-        getSmsImplementation().acknowledgeSms(messageRef, result);
+    private void acknowledgeSms(int token, int messageRef, @DeliverStatusResult int result) {
+        getSmsImplementation().acknowledgeSms(token, messageRef, result);
     }
 
-    private void acknowledgeSmsReport(int messageRef, @StatusReportResult int result) {
-        getSmsImplementation().acknowledgeSmsReport(messageRef, result);
+    private void acknowledgeSmsReport(int token, int messageRef, @StatusReportResult int result) {
+        getSmsImplementation().acknowledgeSmsReport(token, messageRef, result);
     }
 
     private String getSmsFormat() {