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

Commit 82c8688d authored by Danesh M's avatar Danesh M Committed by Roman Birg
Browse files

BroadcastReceiver : Get sender of broadcasts

Add internal api for receiving the sender of a broadcast

Change-Id: I28ae614ef92101ab6b9beadc3c965d16b1f393f6
parent fcf02802
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -577,6 +577,15 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM
            return true;
        }

        case GET_CALLING_PACKAGE_FOR_BROADCAST_TRANSACTION: {
            data.enforceInterface(IActivityManager.descriptor);
            boolean foreground = data.readInt() == 1 ? true : false;
            String res = getCallingPackageForBroadcast(foreground);
            reply.writeNoException();
            reply.writeString(res);
            return true;
        }

        case GET_CALLING_ACTIVITY_TRANSACTION: {
            data.enforceInterface(IActivityManager.descriptor);
            IBinder token = data.readStrongBinder();
@@ -3192,6 +3201,19 @@ class ActivityManagerProxy implements IActivityManager
        reply.recycle();
        return res;
    }
    public String getCallingPackageForBroadcast(boolean foreground) throws RemoteException
    {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeInt(foreground ? 1 : 0);
        mRemote.transact(GET_CALLING_PACKAGE_FOR_BROADCAST_TRANSACTION, data, reply, 0);
        reply.readException();
        String res = reply.readString();
        data.recycle();
        reply.recycle();
        return res;
    }
    public ComponentName getCallingActivity(IBinder token)
            throws RemoteException {
        Parcel data = Parcel.obtain();
+5 −0
Original line number Diff line number Diff line
@@ -122,6 +122,7 @@ public interface IActivityManager extends IInterface {
    public void activitySlept(IBinder token) throws RemoteException;
    public void activityDestroyed(IBinder token) throws RemoteException;
    public String getCallingPackage(IBinder token) throws RemoteException;
    public String getCallingPackageForBroadcast(boolean foreground) throws RemoteException;
    public ComponentName getCallingActivity(IBinder token) throws RemoteException;
    public List<IAppTask> getAppTasks(String callingPackage) throws RemoteException;
    public int addAppTask(IBinder activityToken, Intent intent,
@@ -840,6 +841,10 @@ public interface IActivityManager extends IInterface {
    int START_IN_PLACE_ANIMATION_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+240;
    int CHECK_PERMISSION_WITH_TOKEN_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+241;
    int REGISTER_TASK_STACK_LISTENER_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+242;
    // 243: Available

    // start of CM transactions
    int GET_CALLING_PACKAGE_FOR_BROADCAST_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+244;

    // Start of M transactions
    int NOTIFY_CLEARTEXT_NETWORK_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+280;
+12 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.content;

import android.app.ActivityManager;
import android.app.ActivityManagerNative;
import android.app.ActivityThread;
import android.app.IActivityManager;
@@ -747,6 +748,17 @@ public abstract class BroadcastReceiver {
        return mPendingResult.mSendingUser;
    }

    /** @hide */
    public String getSendingPackage(Intent intent) {
        final IActivityManager mgr = ActivityManagerNative.getDefault();
        try {
            boolean fg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0;
            return mgr.getCallingPackageForBroadcast(fg);
        } catch (RemoteException ex) {
            return null;
        }
    }

    /**
     * Control inclusion of debugging help for mismatched
     * calls to {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
+12 −0
Original line number Diff line number Diff line
@@ -6727,6 +6727,18 @@ public final class ActivityManagerService extends ActivityManagerNative
        }
    }
    public String getCallingPackageForBroadcast(boolean foreground) {
        BroadcastQueue queue = foreground ? mFgBroadcastQueue : mBgBroadcastQueue;
        BroadcastRecord r = queue.getProcessingBroadcast();
        if (r != null) {
            return r.callerPackage;
        } else {
            Log.e(TAG, "Broadcast sender is only retrievable in the onReceive");
        }
        return null;
    }
    private ActivityRecord getCallingRecordLocked(IBinder token) {
        ActivityRecord r = ActivityRecord.isInStackLocked(token);
        if (r == null) {
+13 −0
Original line number Diff line number Diff line
@@ -141,6 +141,11 @@ public final class BroadcastQueue {
     */
    BroadcastRecord mPendingBroadcast = null;

     /**
      * Intent broadcast that we are currently processing
      */
    BroadcastRecord mCurrentBroadcast = null;

    /**
     * The receiver index that is pending, to restart the broadcast if needed.
     */
@@ -635,6 +640,10 @@ public final class BroadcastQueue {
                .sendToTarget();
    }

    BroadcastRecord getProcessingBroadcast() {
        return mCurrentBroadcast;
    }

    final void processNextBroadcast(boolean fromMsg) {
        synchronized(mService) {
            BroadcastRecord r;
@@ -655,6 +664,7 @@ public final class BroadcastQueue {
                r = mParallelBroadcasts.remove(0);
                r.dispatchTime = SystemClock.uptimeMillis();
                r.dispatchClockTime = System.currentTimeMillis();
                mCurrentBroadcast = r;
                final int N = r.receivers.size();
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast ["
                        + mQueueName + "] " + r);
@@ -668,6 +678,7 @@ public final class BroadcastQueue {
                addBroadcastToHistoryLocked(r);
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
                        + mQueueName + "] " + r);
                mCurrentBroadcast = null;
            }

            // Now take care of the next serialized one...
@@ -713,6 +724,7 @@ public final class BroadcastQueue {
                    return;
                }
                r = mOrderedBroadcasts.get(0);
                mCurrentBroadcast = r;
                boolean forceReceive = false;

                // Ensure that even if something goes awry with the timeout
@@ -783,6 +795,7 @@ public final class BroadcastQueue {
                    // ... and on to the next...
                    addBroadcastToHistoryLocked(r);
                    mOrderedBroadcasts.remove(0);
                    mCurrentBroadcast = null;
                    r = null;
                    looped = true;
                    continue;