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

Commit 991914d3 authored by Rahul Sabnis's avatar Rahul Sabnis Committed by Zach Johnson
Browse files

Replace logic to determine the foreground user id with the SystemService

callback

Tag: #feature
Bug: 226404651
Test: Manual
Ignore-AOSP-First: Resolving merge conflict
Change-Id: I78e6ead277785dff4cbbf8fc218dc68dccdc28a5
parent f91afd6e
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -83,7 +83,6 @@ import java.util.concurrent.TimeUnit;
/**
 * @hide
 */

public final class Utils {
    private static final String TAG = "BluetoothUtils";
    private static final int MICROS_PER_UNIT = 625;
@@ -322,8 +321,8 @@ public final class Utils {
    }

    static int sForegroundUserId = USER_HANDLE_NULL.getIdentifier();
    public static void setForegroundUserId(int uid) {
        Utils.sForegroundUserId = uid;
    public static void setForegroundUserId(int userId) {
        Utils.sForegroundUserId = userId;
    }

    /**
+13 −16
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import static com.android.bluetooth.Utils.isPackageNameAccurate;
import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.AppOpsManager;
import android.app.PendingIntent;
@@ -576,11 +575,6 @@ public class AdapterService extends Service {
            // Some platforms, such as wearables do not have a system ui.
            Log.w(TAG, "Unable to resolve SystemUI's UID.", e);
        }

        IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
        getApplicationContext().registerReceiverForAllUsers(sUserSwitchedReceiver, filter, null, null);
        int fuid = ActivityManager.getCurrentUser();
        Utils.setForegroundUserId(fuid);
    }

    @Override
@@ -606,16 +600,6 @@ public class AdapterService extends Service {
        }
    }

    public static final BroadcastReceiver sUserSwitchedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
                int fuid = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
                Utils.setForegroundUserId(fuid);
            }
        }
    };

    private boolean initMetricsLogger() {
        if (mMetricsLogger != null) {
            return false;
@@ -3727,6 +3711,19 @@ public class AdapterService extends Service {
                ParcelUuid uuid, AttributionSource attributionSource) {
            return mService.retrievePendingSocketForServiceRecord(uuid, attributionSource);
        }

        @Override
        public void setForegroundUserId(int userId, AttributionSource attributionSource) {
            AdapterService service = getService();
            if (service == null || !callerIsSystemOrActiveUser(TAG, "setForegroundUserId")
                    || !Utils.checkConnectPermissionForDataDelivery(
                    service, Utils.getCallingAttributionSource(mService),
                    "AdapterService setForegroundUserId")) {
                return;
            }
            enforceBluetoothPrivilegedPermission(service);
            Utils.setForegroundUserId(userId);
        }
    }

    // ----API Methods--------
+30 −0
Original line number Diff line number Diff line
@@ -216,6 +216,7 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
    private final ReentrantReadWriteLock mBluetoothLock = new ReentrantReadWriteLock();
    private boolean mBinding;
    private boolean mUnbinding;
    private int mForegroundUserId;

    private BluetoothModeChangeHelper mBluetoothModeChangeHelper;

@@ -543,6 +544,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {

        String value = SystemProperties.get(
                "persist.sys.fflag.override.settings_bluetooth_hearing_aid");
        mForegroundUserId = UserHandle.SYSTEM.getIdentifier();

        if (!TextUtils.isEmpty(value)) {
            boolean isHearingAidEnabled = Boolean.parseBoolean(value);
            Log.v(TAG, "set feature flag HEARING_AID_SETTINGS to " + isHearingAidEnabled);
@@ -857,6 +860,25 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
        recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
    }

    /**
     * Sends the current foreground user id to the Bluetooth process. This user id is used to
     * determine if Binder calls are coming from the active user.
     *
     * @param userId is the foreground user id we are propagating to the Bluetooth process
     */
    private void propagateForegroundUserId(int userId) {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth != null) {
                mBluetooth.setForegroundUserId(userId, mContext.getAttributionSource());
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to set foreground user id", e);
        } finally {
            mBluetoothLock.readLock().unlock();
        }
    }

    public int getState() {
        if ((Binder.getCallingUid() != Process.SYSTEM_UID) && (!checkIfCallerIsForegroundUser())) {
            Log.w(TAG, "getState(): report OFF for non-active and non system user");
@@ -2215,6 +2237,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                                BluetoothAdapter.STATE_BLE_TURNING_ON,
                                BluetoothAdapter.STATE_BLE_ON,
                                BluetoothAdapter.STATE_BLE_TURNING_OFF));
                    } else {
                        propagateForegroundUserId(mForegroundUserId);
                    }
                    break;
                }
@@ -2352,6 +2376,12 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                    }
                    mHandler.removeMessages(MESSAGE_USER_SWITCHED);

                    // Save the foreground user id and propagate to BT process after it's restarted
                    int toUserId = msg.arg1;
                    if (mForegroundUserId != toUserId) {
                        mForegroundUserId = toUserId;
                    }

                    /* disable and enable BT when detect a user switch */
                    if (mBluetooth != null && isEnabled()) {
                        restartForReason(BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH);
+3 −0
Original line number Diff line number Diff line
@@ -272,4 +272,7 @@ interface IBluetooth
    oneway void stopRfcommListener(in ParcelUuid uuid, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    oneway void retrievePendingSocketForServiceRecord(in ParcelUuid uuid, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);

    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED})")
    oneway void setForegroundUserId(in int userId, in AttributionSource attributionSource);
}