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

Commit 24d11d5e authored by Danesh M's avatar Danesh M Committed by Gerrit Code Review
Browse files

BroadcastReceiver : Get sender of broadcasts

Add internal api for receiving the sender of a broadcast

Change-Id: I28ae614ef92101ab6b9beadc3c965d16b1f393f6
parent a461bc6b
Loading
Loading
Loading
Loading
+22 −0
Original line number Original line Diff line number Diff line
@@ -461,6 +461,15 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM
            return true;
            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: {
        case GET_CALLING_ACTIVITY_TRANSACTION: {
            data.enforceInterface(IActivityManager.descriptor);
            data.enforceInterface(IActivityManager.descriptor);
            IBinder token = data.readStrongBinder();
            IBinder token = data.readStrongBinder();
@@ -2372,6 +2381,19 @@ class ActivityManagerProxy implements IActivityManager
        reply.recycle();
        reply.recycle();
        return res;
        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)
    public ComponentName getCallingActivity(IBinder token)
            throws RemoteException {
            throws RemoteException {
        Parcel data = Parcel.obtain();
        Parcel data = Parcel.obtain();
+2 −0
Original line number Original line Diff line number Diff line
@@ -98,6 +98,7 @@ public interface IActivityManager extends IInterface {
    public void activitySlept(IBinder token) throws RemoteException;
    public void activitySlept(IBinder token) throws RemoteException;
    public void activityDestroyed(IBinder token) throws RemoteException;
    public void activityDestroyed(IBinder token) throws RemoteException;
    public String getCallingPackage(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 ComponentName getCallingActivity(IBinder token) throws RemoteException;
    public List getTasks(int maxNum, int flags,
    public List getTasks(int maxNum, int flags,
                         IThumbnailReceiver receiver) throws RemoteException;
                         IThumbnailReceiver receiver) throws RemoteException;
@@ -644,4 +645,5 @@ public interface IActivityManager extends IInterface {
    int SET_USER_IS_MONKEY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+165;
    int SET_USER_IS_MONKEY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+165;
    int HANG_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+166;
    int HANG_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+166;
    int IS_PRIVACY_GUARD_ENABLED_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+167;
    int IS_PRIVACY_GUARD_ENABLED_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+167;
    int GET_CALLING_PACKAGE_FOR_BROADCAST_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+168;
}
}
+12 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package android.content;
package android.content;


import android.app.ActivityManager;
import android.app.ActivityManagerNative;
import android.app.ActivityManagerNative;
import android.app.ActivityThread;
import android.app.ActivityThread;
import android.app.IActivityManager;
import android.app.IActivityManager;
@@ -745,6 +746,17 @@ public abstract class BroadcastReceiver {
        return mPendingResult.mSendingUser;
        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
     * Control inclusion of debugging help for mismatched
     * calls to {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
     * calls to {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
+11 −0
Original line number Original line Diff line number Diff line
@@ -4704,6 +4704,17 @@ 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) {
    private ActivityRecord getCallingRecordLocked(IBinder token) {
        ActivityRecord r = mMainStack.isInStackLocked(token);
        ActivityRecord r = mMainStack.isInStackLocked(token);
        if (r == null) {
        if (r == null) {
+13 −0
Original line number Original line Diff line number Diff line
@@ -120,6 +120,11 @@ public class BroadcastQueue {
     */
     */
    BroadcastRecord mPendingBroadcast = null;
    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.
     * The receiver index that is pending, to restart the broadcast if needed.
     */
     */
@@ -482,6 +487,10 @@ public class BroadcastQueue {
        }
        }
    }
    }


    BroadcastRecord getProcessingBroadcast() {
        return mCurrentBroadcast;
    }

    final void processNextBroadcast(boolean fromMsg) {
    final void processNextBroadcast(boolean fromMsg) {
        synchronized(mService) {
        synchronized(mService) {
            BroadcastRecord r;
            BroadcastRecord r;
@@ -502,6 +511,7 @@ public class BroadcastQueue {
                r = mParallelBroadcasts.remove(0);
                r = mParallelBroadcasts.remove(0);
                r.dispatchTime = SystemClock.uptimeMillis();
                r.dispatchTime = SystemClock.uptimeMillis();
                r.dispatchClockTime = System.currentTimeMillis();
                r.dispatchClockTime = System.currentTimeMillis();
                mCurrentBroadcast = r;
                final int N = r.receivers.size();
                final int N = r.receivers.size();
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast ["
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast ["
                        + mQueueName + "] " + r);
                        + mQueueName + "] " + r);
@@ -513,6 +523,7 @@ public class BroadcastQueue {
                    deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
                    deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
                }
                }
                addBroadcastToHistoryLocked(r);
                addBroadcastToHistoryLocked(r);
                mCurrentBroadcast = null;
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
                        + mQueueName + "] " + r);
                        + mQueueName + "] " + r);
            }
            }
@@ -562,6 +573,7 @@ public class BroadcastQueue {
                    return;
                    return;
                }
                }
                r = mOrderedBroadcasts.get(0);
                r = mOrderedBroadcasts.get(0);
                mCurrentBroadcast = r;
                boolean forceReceive = false;
                boolean forceReceive = false;


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