Log last 20 CAT proactive commands.

These commands are useful for debugging issues where SIMs lock, since most
would invoke some kind of display message before locing the SIM.

Bug: 19440660
Change-Id: I5d24818e4122c875b630aead5bcc6f1f252bdd1f
diff --git a/src/java/com/android/internal/telephony/cat/CatService.java b/src/java/com/android/internal/telephony/cat/CatService.java
index 1ffe08c..ad8b4e2 100644
--- a/src/java/com/android/internal/telephony/cat/CatService.java
+++ b/src/java/com/android/internal/telephony/cat/CatService.java
@@ -362,6 +362,14 @@
     private void handleCommand(CommandParams cmdParams, boolean isProactiveCmd) {
         CatLog.d(this, cmdParams.getCommandType().name());
 
+        // Log all proactive commands.
+        if (isProactiveCmd) {
+            if (mUiccController != null) {
+                mUiccController.addCardLog("ProactiveCommand mSlotId=" + mSlotId +
+                        " cmdParams=" + cmdParams);
+            }
+        }
+
         CharSequence message;
         CatCmdMessage cmdMsg = new CatCmdMessage(cmdParams);
         switch (cmdParams.getCommandType()) {
diff --git a/src/java/com/android/internal/telephony/cat/CommandParams.java b/src/java/com/android/internal/telephony/cat/CommandParams.java
index 2f9d0fd..93520f7 100644
--- a/src/java/com/android/internal/telephony/cat/CommandParams.java
+++ b/src/java/com/android/internal/telephony/cat/CommandParams.java
@@ -57,6 +57,11 @@
         }
         return false;
     }
+
+    @Override
+    public String toString() {
+        return "TextMessage=" + mTextMsg + " " + super.toString();
+    }
 }
 
 class LaunchBrowserParams extends CommandParams {
@@ -80,6 +85,11 @@
         }
         return false;
     }
+
+    @Override
+    public String toString() {
+        return "TextMessage=" + mConfirmMsg + " " + super.toString();
+    }
 }
 
 class SetEventListParams extends CommandParams {
diff --git a/src/java/com/android/internal/telephony/cat/TextMessage.java b/src/java/com/android/internal/telephony/cat/TextMessage.java
index 515f6c5..926c927 100644
--- a/src/java/com/android/internal/telephony/cat/TextMessage.java
+++ b/src/java/com/android/internal/telephony/cat/TextMessage.java
@@ -72,4 +72,12 @@
             return new TextMessage[size];
         }
     };
-}
\ No newline at end of file
+
+    @Override
+    public String toString() {
+        return "title=" + title + " text=" + text + " icon=" + icon +
+            " iconSelfExplanatory=" + iconSelfExplanatory + " isHighPriority=" +
+            isHighPriority + " responseNeeded=" + responseNeeded + " userClear=" +
+            userClear + " duration=" + duration;
+    }
+}
diff --git a/src/java/com/android/internal/telephony/uicc/UiccController.java b/src/java/com/android/internal/telephony/uicc/UiccController.java
index e314f82..4f3b58b 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccController.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccController.java
@@ -25,6 +25,7 @@
 import android.os.SystemProperties;
 import android.telephony.TelephonyManager;
 import android.telephony.Rlog;
+import android.text.format.Time;
 
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.PhoneConstants;
@@ -32,6 +33,7 @@
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.util.LinkedList;
 
 /**
  * This class is responsible for keeping all knowledge about
@@ -98,6 +100,10 @@
 
     protected RegistrantList mIccChangedRegistrants = new RegistrantList();
 
+    // Logging for dumpsys. Useful in cases when the cards run into errors.
+    private static final int MAX_PROACTIVE_COMMANDS_TO_LOG = 20;
+    private LinkedList<String> mCardLogs = new LinkedList<String>();
+
     public static UiccController make(Context c, CommandsInterface[] ci) {
         synchronized (mLock) {
             if (mInstance != null) {
@@ -340,6 +346,15 @@
         Rlog.d(LOG_TAG, string);
     }
 
+    // TODO: This is hacky. We need a better way of saving the logs.
+    public void addCardLog(String data) {
+        Time t = new Time();
+        t.setToNow();
+        mCardLogs.addLast(t.format("%m-%d %H:%M:%S") + " " + data);
+        if (mCardLogs.size() > MAX_PROACTIVE_COMMANDS_TO_LOG) {
+            mCardLogs.removeFirst();
+        }
+    }
 
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("UiccController: " + this);
@@ -361,5 +376,9 @@
                 mUiccCards[i].dump(fd, pw, args);
             }
         }
+        pw.println("mCardLogs: ");
+        for (int i = 0; i < mCardLogs.size(); ++i) {
+            pw.println("  " + mCardLogs.get(i));
+        }
     }
 }