CarrierApp: Do not send broadcast if there is no carrier app.

Avoids an unnecessary broadcast.

Also fixes an NPE bug.

BUG=16454767
Change-Id: Idc8bff4c7a35b7ea0bb7446cd74515cf1675868d
diff --git a/src/java/com/android/internal/telephony/InboundSmsHandler.java b/src/java/com/android/internal/telephony/InboundSmsHandler.java
index 522aad2..74bb63a 100644
--- a/src/java/com/android/internal/telephony/InboundSmsHandler.java
+++ b/src/java/com/android/internal/telephony/InboundSmsHandler.java
@@ -718,10 +718,16 @@
         }
 
         Intent intent = new Intent(Intents.SMS_FILTER_ACTION);
-        intent.setPackage(
-                UiccController.getInstance().getUiccCard().getCarrierPackageNameForBroadcastIntent(
-                        mContext.getPackageManager(), intent));
-        intent.putExtra("destport", destPort);
+        String carrierPackage =
+            UiccController.getInstance().getUiccCard().getCarrierPackageNameForBroadcastIntent(
+                mContext.getPackageManager(), intent);
+        if (carrierPackage != null) {
+            intent.setPackage(carrierPackage);
+            intent.putExtra("destport", destPort);
+        } else {
+            setAndDirectIntent(intent, destPort);
+        }
+
         intent.putExtra("pdus", pdus);
         intent.putExtra("format", tracker.getFormat());
         dispatchIntent(intent, android.Manifest.permission.RECEIVE_SMS,
diff --git a/src/java/com/android/internal/telephony/SMSDispatcher.java b/src/java/com/android/internal/telephony/SMSDispatcher.java
index 5bdf3dd..f694926 100644
--- a/src/java/com/android/internal/telephony/SMSDispatcher.java
+++ b/src/java/com/android/internal/telephony/SMSDispatcher.java
@@ -62,6 +62,7 @@
 
 import com.android.internal.R;
 import com.android.internal.telephony.GsmAlphabet.TextEncodingDetails;
+import com.android.internal.telephony.uicc.UiccCard;
 import com.android.internal.telephony.uicc.UiccController;
 
 import java.util.ArrayList;
@@ -1464,7 +1465,8 @@
     }
 
     protected String getCarrierAppPackageName(Intent intent) {
-        return UiccController.getInstance().getUiccCard().getCarrierPackageNameForBroadcastIntent(
+        UiccCard card = UiccController.getInstance().getUiccCard();
+        return card == null ? null : card.getCarrierPackageNameForBroadcastIntent(
             mContext.getPackageManager(), intent);
     }
 
diff --git a/src/java/com/android/internal/telephony/uicc/UiccCard.java b/src/java/com/android/internal/telephony/uicc/UiccCard.java
index 51521dd..12884fe 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccCard.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccCard.java
@@ -503,7 +503,7 @@
      */
     public String getCarrierPackageNameForBroadcastIntent(
             PackageManager packageManager, Intent intent) {
-        return mCarrierPrivilegeRules == null ? "" :
+        return mCarrierPrivilegeRules == null ? null :
             mCarrierPrivilegeRules.getCarrierPackageNameForBroadcastIntent(
                     packageManager, intent);
     }
diff --git a/src/java/com/android/internal/telephony/uicc/UiccCarrierPrivilegeRules.java b/src/java/com/android/internal/telephony/uicc/UiccCarrierPrivilegeRules.java
index 55ddef8..0dd9b79 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccCarrierPrivilegeRules.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccCarrierPrivilegeRules.java
@@ -270,8 +270,8 @@
      *
      * @param packageManager PackageManager for getting receivers.
      * @param intent Intent that will be broadcast.
-     * @return packageName name of package that should handle the intent. Will return an empty
-     *         string if no matching package is found.
+     * @return packageName name of package that should handle the intent. If null is returned,
+     *         no carrier app was found.
      */
     public String getCarrierPackageNameForBroadcastIntent(
             PackageManager packageManager, Intent intent) {
@@ -287,9 +287,8 @@
             }
         }
 
-        // Return an empty package name so that no packages match.
-        // TODO: This creates an unnecessary ordered broadcast that can be avoided.
-        return "";
+        // No carrier app, send null.
+        return null;
     }
 
     @Override