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

Commit 4d8c54ec 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 5c764600
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -552,6 +552,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();
@@ -2935,6 +2944,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();
+2 −0
Original line number Diff line number Diff line
@@ -117,6 +117,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,
@@ -781,4 +782,5 @@ public interface IActivityManager extends IInterface {
    int BOOT_ANIMATION_COMPLETE_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+237;
    int GET_TASK_DESCRIPTION_ICON_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+238;
    int LAUNCH_ASSIST_INTENT_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+239;
    int GET_CALLING_PACKAGE_FOR_BROADCAST_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+240;
}
+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;
@@ -745,6 +746,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
@@ -6608,6 +6608,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
@@ -126,6 +126,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.
     */
@@ -540,6 +545,10 @@ public final class BroadcastQueue {
        }
    }

    BroadcastRecord getProcessingBroadcast() {
        return mCurrentBroadcast;
    }

    final void processNextBroadcast(boolean fromMsg) {
        synchronized(mService) {
            BroadcastRecord r;
@@ -560,6 +569,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, "Processing parallel broadcast ["
                        + mQueueName + "] " + r);
@@ -571,6 +581,7 @@ public final class BroadcastQueue {
                    deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
                }
                addBroadcastToHistoryLocked(r);
                mCurrentBroadcast = null;
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
                        + mQueueName + "] " + r);
            }
@@ -620,6 +631,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
@@ -693,6 +705,7 @@ public final class BroadcastQueue {
                    // ... and on to the next...
                    addBroadcastToHistoryLocked(r);
                    mOrderedBroadcasts.remove(0);
                    mCurrentBroadcast = null;
                    r = null;
                    looped = true;
                    continue;