Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit af06916e authored by Koushik Dutta's avatar Koushik Dutta
Browse files

Refactor SMS Middleware and SMS Send path.

Introduce a new Broadcast, ACTION_NEW_OUTGOING_SMS, that is similar to the existing call
equivalent ACTION_NEW_OUTGOING_CALL. This allows a receiver to rewrite the intent
or cancel it entirely.

This removes the need for injecting middleware.

Change-Id: I063288461161979f951932f32ade2cfbd72f8b73
parent b7abe4ee
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ include $(CLEAR_VARS)
LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/src/java
LOCAL_SRC_FILES := \
	src/java/com/android/internal/telephony/ISms.aidl \
	src/java/com/android/internal/telephony/ISmsMiddleware.aidl \
    src/java/com/android/internal/telephony/IIccPhoneBook.aidl \
    src/java/com/android/internal/telephony/EventLogTags.logtags \

+0 −2
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.internal.telephony;

import android.app.PendingIntent;
import com.android.internal.telephony.SmsRawData;
import com.android.internal.telephony.ISmsMiddleware;

import java.util.List;

@@ -37,7 +36,6 @@ import java.util.List;
 */

interface ISms {
    void registerSmsMiddleware(String name, ISmsMiddleware middleware);
    void synthesizeMessages(String originatingAddress, String scAddress, in List<String> messages, long timestampMillis);

    /**
+0 −12
Original line number Diff line number Diff line
package com.android.internal.telephony;

import android.app.PendingIntent;

interface ISmsMiddleware {
    boolean onSendText(in String destAddr, in String scAddr, in String text,
            in PendingIntent sentIntent, in PendingIntent deliveryIntent);

    boolean onSendMultipartText(in String destinationAddress, in String scAddress,
            in List<String> parts, in List<PendingIntent> sentIntents,
            in List<PendingIntent> deliveryIntents);
}
 No newline at end of file
+11 −10
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.internal.telephony;

import android.app.PendingIntent;
import android.content.Context;
import android.os.Binder;
import android.os.RemoteException;
import android.util.Log;

@@ -49,10 +50,6 @@ public abstract class IccSmsInterfaceManager extends ISms.Stub {
                "android.permission.SEND_SMS", message);
    }

    @Override
    public void registerSmsMiddleware(String name, ISmsMiddleware middleware) throws android.os.RemoteException {
    }

    @Override
    public void synthesizeMessages(String originatingAddress, String scAddress, List<String> messages, long timestampMillis) throws RemoteException {
    }
@@ -121,9 +118,11 @@ public abstract class IccSmsInterfaceManager extends ISms.Stub {
     */
    public void sendText(String destAddr, String scAddr,
            String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
        if (Binder.getCallingPid() != android.os.Process.myPid()) {
            mPhone.getContext().enforceCallingPermission(
                    "android.permission.SEND_SMS",
                    "Sending SMS message");
        }
        if (Log.isLoggable("SMS", Log.VERBOSE)) {
            log("sendText: destAddr=" + destAddr + " scAddr=" + scAddr +
                " text='"+ text + "' sentIntent=" +
@@ -159,9 +158,11 @@ public abstract class IccSmsInterfaceManager extends ISms.Stub {
     */
    public void sendMultipartText(String destAddr, String scAddr, List<String> parts,
            List<PendingIntent> sentIntents, List<PendingIntent> deliveryIntents) {
        if (Binder.getCallingPid() != android.os.Process.myPid()) {
            mPhone.getContext().enforceCallingPermission(
                    "android.permission.SEND_SMS",
                    "Sending SMS message");
        }
        if (Log.isLoggable("SMS", Log.VERBOSE)) {
            int i = 0;
            for (String part : parts) {
+71 −39
Original line number Diff line number Diff line
@@ -16,23 +16,58 @@

package com.android.internal.telephony;

import java.util.Hashtable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.provider.Telephony.Sms.Intents;
import android.telephony.SmsMessage;

public class IccSmsInterfaceManagerProxy extends ISms.Stub {
    private IccSmsInterfaceManager mIccSmsInterfaceManager;
    private Hashtable<String, ISmsMiddleware> mMiddleware = new Hashtable<String, ISmsMiddleware>();

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            // check if the message was aborted
            if (getResultCode() != Activity.RESULT_OK) {
                return;
            }
            String destAddr = getResultData();
            String scAddr = intent.getStringExtra("scAddr");
            ArrayList<String> parts = intent.getStringArrayListExtra("parts");
            ArrayList<PendingIntent> sentIntents = intent.getParcelableArrayListExtra("sentIntent");
            ArrayList<PendingIntent> deliveryIntents = intent.getParcelableArrayListExtra("deliveryIntents");

            if (intent.getBooleanExtra("multipart", false)) {
                mIccSmsInterfaceManager.sendMultipartText(destAddr, scAddr,
                        parts, sentIntents, deliveryIntents);
                return;
            }

            PendingIntent sentIntent = null;
            if (sentIntents != null && sentIntents.size() > 0) {
                sentIntent = sentIntents.get(0);
            }
            PendingIntent deliveryIntent = null;
            if (deliveryIntents != null && deliveryIntents.size() > 0) {
                deliveryIntent = deliveryIntents.get(0);
            }
            String text = null;
            if (parts != null && parts.size() > 0) {
                text = parts.get(0);
            }
            mIccSmsInterfaceManager.sendText(destAddr, scAddr, text,
                    sentIntent, deliveryIntent);
        }
    };

    public IccSmsInterfaceManagerProxy(Context context,
            IccSmsInterfaceManager iccSmsInterfaceManager) {
@@ -51,19 +86,10 @@ public class IccSmsInterfaceManagerProxy extends ISms.Stub {

    private void createWakelock() {
        PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SMSDispatcher");
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "IccSmsInterfaceManager");
        mWakeLock.setReferenceCounted(true);
    }

    @Override
    public void registerSmsMiddleware(String name, ISmsMiddleware middleware) throws android.os.RemoteException {
        if (!"1".equals(SystemProperties.get("persist.sys.sms_debug", "0"))) {
            mContext.enforceCallingPermission(
                    "android.permission.INTERCEPT_SMS", "");
        }
        mMiddleware.put(name, middleware);
    }

    private Context mContext;
    private PowerManager.WakeLock mWakeLock;
    private static final int WAKE_LOCK_TIMEOUT = 5000;
@@ -83,11 +109,8 @@ public class IccSmsInterfaceManagerProxy extends ISms.Stub {

    @Override
    public void synthesizeMessages(String originatingAddress, String scAddress, List<String> messages, long timestampMillis) throws RemoteException {
        // if not running in debug mode
        if (!"1".equals(SystemProperties.get("persist.sys.sms_debug", "0"))) {
        mContext.enforceCallingPermission(
                    "android.permission.BROADCAST_SMS", "");
        }
                android.Manifest.permission.BROADCAST_SMS, "");
        byte[][] pdus = new byte[messages.size()][];
        for (int i = 0; i < messages.size(); i++) {
            SyntheticSmsMessage message = new SyntheticSmsMessage(originatingAddress, scAddress, messages.get(i), timestampMillis);
@@ -116,34 +139,43 @@ public class IccSmsInterfaceManagerProxy extends ISms.Stub {
                sentIntent, deliveryIntent);
    }

    private void broadcastOutgoingSms(String destAddr, String scAddr,
            boolean multipart, ArrayList<String> parts,
            ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents) {
        Intent broadcast = new Intent(Intent.ACTION_NEW_OUTGOING_SMS);
        broadcast.putExtra("destAddr", destAddr);
        broadcast.putExtra("scAddr", scAddr);
        broadcast.putExtra("multipart", multipart);
        broadcast.putStringArrayListExtra("parts", parts);
        broadcast.putParcelableArrayListExtra("sentIntents", sentIntents);
        broadcast.putParcelableArrayListExtra("deliveryIntents", deliveryIntents);
        mContext.sendOrderedBroadcastAsUser(broadcast, UserHandle.OWNER,
                android.Manifest.permission.INTERCEPT_SMS,
                mReceiver, null, Activity.RESULT_OK, destAddr, null);
    }

    public void sendText(String destAddr, String scAddr,
            String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
        for (ISmsMiddleware middleware: mMiddleware.values()) {
            try {
                if (middleware.onSendText(destAddr, scAddr, text, sentIntent, deliveryIntent))
                    return;
            }
            catch (Exception e) {
                // TOOD: remove the busted middleware?
            }
        }
        mIccSmsInterfaceManager.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent);
        mContext.enforceCallingPermission(
                android.Manifest.permission.SEND_SMS,
                "Sending SMS message");
        ArrayList<String> parts = new ArrayList<String>();
        parts.add(text);
        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        sentIntents.add(sentIntent);
        ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
        deliveryIntents.add(deliveryIntent);
        broadcastOutgoingSms(destAddr, scAddr, false, parts, sentIntents, deliveryIntents);
    }

    public void sendMultipartText(String destAddr, String scAddr,
            List<String> parts, List<PendingIntent> sentIntents,
            List<PendingIntent> deliveryIntents) throws android.os.RemoteException {
        for (ISmsMiddleware middleware: mMiddleware.values()) {
            try {
                if (middleware.onSendMultipartText(destAddr, scAddr, parts, sentIntents, deliveryIntents))
                    return;
            }
            catch (Exception e) {
                // TOOD: remove the busted middleware?
            }
        }
        mIccSmsInterfaceManager.sendMultipartText(destAddr, scAddr,
                parts, sentIntents, deliveryIntents);
        mContext.enforceCallingPermission(
                android.Manifest.permission.SEND_SMS,
                "Sending SMS message");
        broadcastOutgoingSms(destAddr, scAddr, true, new ArrayList<String>(parts),
                new ArrayList<PendingIntent>(sentIntents), new ArrayList<PendingIntent>(deliveryIntents));
    }

    public boolean enableCellBroadcast(int messageIdentifier) throws android.os.RemoteException {