Add an API to get recent IMS disconnect reasons
Naveen Kalla [Wed, 15 Feb 2017 17:48:50 +0000 (17:48 +0000)]
am: 525c3a2fdc

Change-Id: Id650fb412d961230758a34547064911aff5c1c85

src/java/com/android/ims/ImsManager.java

index 6d8cef6..68d0dcc 100644 (file)
@@ -23,6 +23,7 @@ import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.IBinder;
 import android.os.Message;
+import android.os.Parcel;
 import android.os.PersistableBundle;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -35,17 +36,19 @@ import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 
 import com.android.ims.internal.IImsCallSession;
+import com.android.ims.internal.IImsConfig;
 import com.android.ims.internal.IImsEcbm;
 import com.android.ims.internal.IImsMultiEndpoint;
 import com.android.ims.internal.IImsRegistrationListener;
 import com.android.ims.internal.IImsService;
 import com.android.ims.internal.IImsUt;
 import com.android.ims.internal.ImsCallSession;
-import com.android.ims.internal.IImsConfig;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.concurrent.ConcurrentLinkedDeque;
 
 /**
  * Provides APIs for IMS services, such as initiating IMS calls, and provides access to
@@ -191,6 +194,11 @@ public class ImsManager {
     public static final String TRUE = "true";
     public static final String FALSE = "false";
 
+    // mRecentDisconnectReasons stores the last 16 disconnect reasons
+    private static final int MAX_RECENT_DISCONNECT_REASONS = 16;
+    private ConcurrentLinkedDeque<ImsReasonInfo> mRecentDisconnectReasons =
+            new ConcurrentLinkedDeque<>();
+
     /**
      * Gets a manager instance.
      *
@@ -200,8 +208,9 @@ public class ImsManager {
      */
     public static ImsManager getInstance(Context context, int phoneId) {
         synchronized (sImsManagerInstances) {
-            if (sImsManagerInstances.containsKey(phoneId))
+            if (sImsManagerInstances.containsKey(phoneId)) {
                 return sImsManagerInstances.get(phoneId);
+            }
 
             ImsManager mgr = new ImsManager(context, phoneId);
             sImsManagerInstances.put(phoneId, mgr);
@@ -1226,6 +1235,31 @@ public class ImsManager {
         }
     }
 
+    private ImsReasonInfo makeACopy(ImsReasonInfo imsReasonInfo) {
+        Parcel p = Parcel.obtain();
+        imsReasonInfo.writeToParcel(p, 0);
+        p.setDataPosition(0);
+        ImsReasonInfo clonedReasonInfo = ImsReasonInfo.CREATOR.createFromParcel(p);
+        p.recycle();
+        return clonedReasonInfo;
+    }
+
+    /**
+     * Get Recent IMS Disconnect Reasons.
+     *
+     * @return ArrayList of ImsReasonInfo objects. MAX size of the arraylist
+     * is MAX_RECENT_DISCONNECT_REASONS. The objects are in the
+     * chronological order.
+     */
+    public ArrayList<ImsReasonInfo> getRecentImsDisconnectReasons() {
+        ArrayList<ImsReasonInfo> disconnectReasons = new ArrayList<>();
+
+        for (ImsReasonInfo reason : mRecentDisconnectReasons) {
+            disconnectReasons.add(makeACopy(reason));
+        }
+        return disconnectReasons;
+    }
+
     /**
      * Get the boolean config from carrier config manager.
      *
@@ -1452,6 +1486,14 @@ public class ImsManager {
         }
     }
 
+    private void addToRecentDisconnectReasons(ImsReasonInfo reason) {
+        if (reason == null) return;
+        while (mRecentDisconnectReasons.size() >= MAX_RECENT_DISCONNECT_REASONS) {
+            mRecentDisconnectReasons.removeFirst();
+        }
+        mRecentDisconnectReasons.addLast(reason);
+    }
+
     /**
      * Death recipient class for monitoring IMS service.
      */
@@ -1543,6 +1585,8 @@ public class ImsManager {
                 log("registrationDisconnected :: imsReasonInfo" + imsReasonInfo);
             }
 
+            addToRecentDisconnectReasons(imsReasonInfo);
+
             if (mListener != null) {
                 mListener.onImsDisconnected(imsReasonInfo);
             }