Merge "No need to use PermissionChecker to check own permission"
diff --git a/res/values/config.xml b/res/values/config.xml
index 3b86e25..3ac256a 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -18,4 +18,9 @@
     <!-- If this value is true, SMS encoded as octet is decoded by utf8 decoder.
       If false, decoded by Latin decoder. -->
     <bool name="config_sms_utf8_support">false</bool>
+
+    <!-- Package names of the default cell broadcast receivers -->
+    <string-array name="config_defaultCellBroadcastReceiverPkgs" translatable="false">
+        <item>com.android.cellbroadcastreceiver</item>
+    </string-array>
 </resources>
diff --git a/res/values/symbols.xml b/res/values/symbols.xml
new file mode 100644
index 0000000..867c3a2
--- /dev/null
+++ b/res/values/symbols.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<resources>
+    <!-- Private symbols that we need to reference from framework code.  See
+      frameworks/base/core/res/MakeJavaSymbols.sed for how to easily generate
+      this.
+
+      Can be referenced in java code as: com.android.internal.R.<type>.<name>
+      and in layout xml as: "@*android:<type>/<name>" -->
+
+    <java-symbol type="array" name="config_defaultCellBroadcastReceiverPkgs" />
+</resources>
diff --git a/src/com/android/cellbroadcastservice/CdmaServiceCategoryProgramHandler.java b/src/com/android/cellbroadcastservice/CdmaServiceCategoryProgramHandler.java
new file mode 100644
index 0000000..4b254b7
--- /dev/null
+++ b/src/com/android/cellbroadcastservice/CdmaServiceCategoryProgramHandler.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cellbroadcastservice;
+
+import android.Manifest;
+import android.app.Activity;
+import android.app.AppOpsManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.Message;
+import android.provider.Telephony.Sms.Intents;
+import android.telephony.SubscriptionManager;
+import android.telephony.cdma.CdmaSmsCbProgramData;
+
+import java.util.ArrayList;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.function.Consumer;
+
+/**
+ * Handle CDMA Service Category Program Data requests and responses.
+ */
+public final class CdmaServiceCategoryProgramHandler extends WakeLockStateMachine {
+
+    // hold the callbacks provided from the framework, executed after SCP messages are broadcast
+    private ConcurrentLinkedQueue<Consumer<Bundle>> mScpCallback = new ConcurrentLinkedQueue<>();
+
+    /**
+     * Create a new CDMA inbound SMS handler.
+     */
+    CdmaServiceCategoryProgramHandler(Context context) {
+        super("CdmaServiceCategoryProgramHandler", context);
+        mContext = context;
+    }
+
+    /**
+     * Create a new State machine for SCPD requests.
+     *
+     * @param context the context to use
+     * @return the new SCPD handler
+     */
+    static CdmaServiceCategoryProgramHandler makeScpHandler(Context context) {
+        CdmaServiceCategoryProgramHandler handler = new CdmaServiceCategoryProgramHandler(
+                context);
+        handler.start();
+        return handler;
+    }
+
+    /**
+     * Handle a CDMA SCP message.
+     *
+     * @param slotIndex          the index of the slot which received the message
+     * @param programData        the SMS CB program data of the message
+     * @param originatingAddress the originating address of the message
+     * @param callback           a callback to run after each cell broadcast receiver has handled
+     *                           the SCP message
+     */
+    public void onCdmaScpMessage(int slotIndex, ArrayList<CdmaSmsCbProgramData> programData,
+            String originatingAddress, Consumer<Bundle> callback) {
+        onCdmaCellBroadcastSms(new CdmaScpMessage(slotIndex, programData, originatingAddress,
+                callback));
+    }
+
+    /**
+     * Class for holding parts of a CDMA Service Program Category message.
+     */
+    private class CdmaScpMessage {
+        int mSlotIndex;
+        ArrayList<CdmaSmsCbProgramData> mProgamData;
+        String mOriginatingAddress;
+        Consumer<Bundle> mCallback;
+
+        CdmaScpMessage(int slotIndex, ArrayList<CdmaSmsCbProgramData> programData,
+                String originatingAddress, Consumer<Bundle> callback) {
+            mSlotIndex = slotIndex;
+            mProgamData = programData;
+            mOriginatingAddress = originatingAddress;
+            mCallback = callback;
+        }
+    }
+
+
+    /**
+     * Handle Cell Broadcast messages from {@code CdmaInboundSmsHandler}.
+     * 3GPP-format Cell Broadcast messages sent from radio are handled in the subclass.
+     *
+     * @param message the message to process
+     * @return true if an ordered broadcast was sent; false on failure
+     */
+    @Override
+    protected boolean handleSmsMessage(Message message) {
+        if (message.obj instanceof CdmaScpMessage) {
+            CdmaScpMessage cdmaScpMessage = (CdmaScpMessage) message.obj;
+            return handleServiceCategoryProgramData(cdmaScpMessage.mProgamData,
+                    cdmaScpMessage.mOriginatingAddress, cdmaScpMessage.mSlotIndex,
+                    cdmaScpMessage.mCallback);
+        } else {
+            loge("handleMessage got object of type: " + message.obj.getClass().getName());
+            return false;
+        }
+    }
+
+    /**
+     * Send SCPD request to CellBroadcastReceiver as an ordered broadcast.
+     *
+     * @param programData        the program data of the SCP message
+     * @param originatingAddress the address of the sender
+     * @param phoneId            the phoneId that the message was received on
+     * @param callback           a callback to run after each broadcast
+     * @return true if an ordered broadcast was sent; false on failure
+     */
+    private boolean handleServiceCategoryProgramData(ArrayList<CdmaSmsCbProgramData> programData,
+            String originatingAddress, int phoneId, Consumer<Bundle> callback) {
+        if (programData == null) {
+            loge("handleServiceCategoryProgramData: program data list is null!");
+            return false;
+        }
+
+        Intent intent = new Intent(Intents.SMS_SERVICE_CATEGORY_PROGRAM_DATA_RECEIVED_ACTION);
+        intent.putExtra("sender", originatingAddress);
+        intent.putParcelableArrayListExtra("program_data", programData);
+        SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
+
+        // TODO: move this resource and its overlays to the CellBroadcastService directory
+        String[] pkgs = mContext.getResources().getStringArray(
+                com.android.internal.R.array.config_defaultCellBroadcastReceiverPkgs);
+        mReceiverCount.addAndGet(pkgs.length);
+        for (String pkg : pkgs) {
+            intent.setPackage(pkg);
+            mContext.sendOrderedBroadcast(intent, Manifest.permission.RECEIVE_SMS,
+                    AppOpsManager.OP_RECEIVE_SMS, mScpResultsReceiver,
+                    getHandler(), Activity.RESULT_OK, null, null);
+            mScpCallback.add(callback);
+        }
+        return true;
+    }
+
+    /**
+     * Broadcast receiver to handle results of ordered broadcast. Sends the results back to the
+     * framework through a provided callback.
+     */
+    private final BroadcastReceiver mScpResultsReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            int resultCode = getResultCode();
+            if ((resultCode != Activity.RESULT_OK) && (resultCode != Intents.RESULT_SMS_HANDLED)) {
+                loge("SCP results error: result code = " + resultCode);
+                return;
+            }
+            Bundle extras = getResultExtras(false);
+            Consumer<Bundle> callback = mScpCallback.poll();
+            callback.accept(extras);
+            if (DBG) log("mScpResultsReceiver finished");
+            if (mReceiverCount.decrementAndGet() == 0) {
+                sendMessage(EVENT_BROADCAST_COMPLETE);
+            }
+        }
+    };
+}
diff --git a/src/com/android/cellbroadcastservice/DefaultCellBroadcastService.java b/src/com/android/cellbroadcastservice/DefaultCellBroadcastService.java
index 3b60e51..7b03fef 100644
--- a/src/com/android/cellbroadcastservice/DefaultCellBroadcastService.java
+++ b/src/com/android/cellbroadcastservice/DefaultCellBroadcastService.java
@@ -17,13 +17,19 @@
 package com.android.cellbroadcastservice;
 
 import android.content.Context;
+import android.os.Bundle;
 import android.telephony.CellBroadcastService;
 import android.telephony.SmsCbLocation;
 import android.telephony.SmsCbMessage;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.telephony.cdma.CdmaSmsCbProgramData;
 import android.util.Log;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
+
 /**
  * The default implementation of CellBroadcastService, which is used for handling GSM and CDMA
  * cell broadcast messages.
@@ -31,6 +37,7 @@
 public class DefaultCellBroadcastService extends CellBroadcastService {
     private GsmCellBroadcastHandler mGsmCellBroadcastHandler;
     private CellBroadcastHandler mCdmaCellBroadcastHandler;
+    private CdmaServiceCategoryProgramHandler mCdmaScpHandler;
 
     private static final String TAG = "DefaultCellBroadcastService";
 
@@ -44,6 +51,8 @@
                 GsmCellBroadcastHandler.makeGsmCellBroadcastHandler(getApplicationContext());
         mCdmaCellBroadcastHandler =
                 CellBroadcastHandler.makeCellBroadcastHandler(getApplicationContext());
+        mCdmaScpHandler =
+                CdmaServiceCategoryProgramHandler.makeScpHandler(getApplicationContext());
     }
 
     @Override
@@ -73,6 +82,15 @@
         }
     }
 
+    @Override
+    public void onCdmaScpMessage(int slotIndex, List<CdmaSmsCbProgramData> programData,
+            String originatingAddress, Consumer<Bundle> callback) {
+        Log.d(TAG, "onCdmaScpMessage received message on slotId=" + slotIndex);
+        mCdmaScpHandler.onCdmaScpMessage(slotIndex, new ArrayList<>(programData),
+                originatingAddress, callback);
+    }
+
+
     /**
      * Parses a CDMA broadcast SMS
      *