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

Commit 5e556b3f authored by Danesh M's avatar Danesh M Committed by Gerrit Code Review
Browse files

Merge "BroadcastReceiver : Get sender of broadcasts" into cm-10.2

parents daa24597 24d11d5e
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -461,6 +461,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();
@@ -2372,6 +2381,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
@@ -98,6 +98,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 getTasks(int maxNum, int flags,
                         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 HANG_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+166;
    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 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)
+11 −0
Original line number 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) {
        ActivityRecord r = mMainStack.isInStackLocked(token);
        if (r == null) {
+13 −0
Original line number Diff line number Diff line
@@ -120,6 +120,11 @@ public 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.
     */
@@ -482,6 +487,10 @@ public class BroadcastQueue {
        }
    }

    BroadcastRecord getProcessingBroadcast() {
        return mCurrentBroadcast;
    }

    final void processNextBroadcast(boolean fromMsg) {
        synchronized(mService) {
            BroadcastRecord r;
@@ -502,6 +511,7 @@ public 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);
@@ -513,6 +523,7 @@ public class BroadcastQueue {
                    deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
                }
                addBroadcastToHistoryLocked(r);
                mCurrentBroadcast = null;
                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
                        + mQueueName + "] " + r);
            }
@@ -562,6 +573,7 @@ public class BroadcastQueue {
                    return;
                }
                r = mOrderedBroadcasts.get(0);
                mCurrentBroadcast = r;
                boolean forceReceive = false;

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