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

Commit 7159fa9f authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "Register User change only for pbap & avrcp"

parents 62cca5ac b7142ab1
Loading
Loading
Loading
Loading
+26 −9
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUtils;
import android.bluetooth.IBluetoothAvrcpTarget;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -76,6 +77,25 @@ public class AvrcpTargetService extends ProfileService {
    private AvrcpNativeInterface mNativeInterface;
    private AvrcpVolumeManager mVolumeManager;
    private ServiceFactory mFactory = new ServiceFactory();
    private final BroadcastReceiver mUserUnlockedReceiver =
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    // EXTRA_USER_HANDLE is sent for ACTION_USER_UNLOCKED
                    // (even if the documentation doesn't mention it)
                    final int userId =
                            intent.getIntExtra(
                                    Intent.EXTRA_USER_HANDLE,
                                    BluetoothUtils.USER_HANDLE_NULL.getIdentifier());
                    if (userId == BluetoothUtils.USER_HANDLE_NULL.getIdentifier()) {
                        Log.e(TAG, "userChangeReceiver received an invalid EXTRA_USER_HANDLE");
                        return;
                    }
                    if (mMediaPlayerList != null) {
                        mMediaPlayerList.init(new ListCallback());
                    }
                }
            };

    // Only used to see if the metadata has changed from its previous value
    private MediaData mCurrentData;
@@ -182,15 +202,6 @@ public class AvrcpTargetService extends ProfileService {
        return new AvrcpTargetBinder(this);
    }

    @Override
    protected void setUserUnlocked(int userId) {
        Log.i(TAG, "User unlocked, initializing the service");

        if (mMediaPlayerList != null) {
            mMediaPlayerList.init(new ListCallback());
        }
    }

    @Override
    protected boolean start() {
        if (sInstance != null) {
@@ -198,6 +209,11 @@ public class AvrcpTargetService extends ProfileService {
            return false;
        }

        IntentFilter userFilter = new IntentFilter();
        userFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        userFilter.addAction(Intent.ACTION_USER_UNLOCKED);
        getApplicationContext().registerReceiver(mUserUnlockedReceiver, userFilter);

        Log.i(TAG, "Starting the AVRCP Target Service");
        mCurrentData = new MediaData(null, null, null);

@@ -280,6 +296,7 @@ public class AvrcpTargetService extends ProfileService {
        if (mPlayerSettingsManager != null) mPlayerSettingsManager.cleanup();
        if (mMediaPlayerList != null) mMediaPlayerList.cleanup();
        if (mNativeInterface != null) mNativeInterface.cleanup();
        getApplicationContext().unregisterReceiver(mUserUnlockedReceiver);

        mPlayerSettingsManager = null;
        mMediaPlayerList = null;
+0 −57
Original line number Diff line number Diff line
@@ -20,19 +20,12 @@ import static android.Manifest.permission.BLUETOOTH_CONNECT;

import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothUtils;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import com.android.bluetooth.BluetoothMetricsProto;
@@ -62,7 +55,6 @@ public abstract class ProfileService extends Service {
    private IProfileServiceBinder mBinder;
    private final String mName;
    private AdapterService mAdapterService;
    private BroadcastReceiver mUserSwitchedReceiver;
    private boolean mProfileStarted = false;
    private volatile boolean mTestModeEnabled = false;

@@ -117,20 +109,6 @@ public abstract class ProfileService extends Service {
    @SuppressLint("AndroidFrameworkRequiresPermission")
    protected void cleanup() {}

    /**
     * @param userId is equivalent to the result of ActivityManager.getCurrentUser()
     */
    // Suppressed since this is called from framework
    @SuppressLint("AndroidFrameworkRequiresPermission")
    protected void setCurrentUser(int userId) {}

    /**
     * @param userId is equivalent to the result of ActivityManager.getCurrentUser()
     */
    // Suppressed since this is called from framework
    @SuppressLint("AndroidFrameworkRequiresPermission")
    protected void setUserUnlocked(int userId) {}

    /**
     * @param testModeEnabled if the profile should enter or exit a testing mode
     */
@@ -332,37 +310,6 @@ public abstract class ProfileService extends Service {
        }
        mAdapterService.addProfile(this);

        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(Intent.ACTION_USER_SWITCHED);
        filter.addAction(Intent.ACTION_USER_UNLOCKED);
        mUserSwitchedReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String action = intent.getAction();
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
                        BluetoothUtils.USER_HANDLE_NULL.getIdentifier());
                if (userId == BluetoothUtils.USER_HANDLE_NULL.getIdentifier()) {
                    Log.e(mName, "userChangeReceiver received an invalid EXTRA_USER_HANDLE");
                    return;
                }
                if (Intent.ACTION_USER_SWITCHED.equals(action)) {
                    Log.d(mName, "User switched to userId " + userId);
                    setCurrentUser(userId);
                } else if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) {
                    Log.d(mName, "Unlocked userId " + userId);
                    setUserUnlocked(userId);
                }
            }
        };

        getApplicationContext().registerReceiver(mUserSwitchedReceiver, filter);
        int currentUserId = ActivityManager.getCurrentUser();
        setCurrentUser(currentUserId);
        UserManager userManager = getApplicationContext().getSystemService(UserManager.class);
        if (userManager.isUserUnlocked(UserHandle.of(currentUserId))) {
            setUserUnlocked(currentUserId);
        }
        mProfileStarted = start();
        if (!mProfileStarted) {
            Log.e(mName, "Error starting profile. start() returned false.");
@@ -390,10 +337,6 @@ public abstract class ProfileService extends Service {
        if (mAdapterService != null) {
            mAdapterService.removeProfile(this);
        }
        if (mUserSwitchedReceiver != null) {
            getApplicationContext().unregisterReceiver(mUserSwitchedReceiver);
            mUserSwitchedReceiver = null;
        }
        stopSelf();
    }
}
+32 −15
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothUtils;
import android.bluetooth.IBluetoothPbap;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
@@ -303,6 +304,29 @@ public class BluetoothPbapService extends ProfileService implements IObexConnect
        }
    }

    private final BroadcastReceiver mUserChangeReceiver =
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    final String action = intent.getAction();
                    // EXTRA_USER_HANDLE is sent for both ACTION_USER_SWITCHED and
                    // ACTION_USER_UNLOCKED (even if the documentation doesn't mention it)
                    final int userId =
                            intent.getIntExtra(
                                    Intent.EXTRA_USER_HANDLE,
                                    BluetoothUtils.USER_HANDLE_NULL.getIdentifier());
                    if (userId == BluetoothUtils.USER_HANDLE_NULL.getIdentifier()) {
                        Log.e(TAG, "userChangeReceiver received an invalid EXTRA_USER_HANDLE");
                        return;
                    }
                    Log.d(TAG, "Got " + action + " to userId " + userId);
                    UserManager userManager = getSystemService(UserManager.class);
                    if (userManager.isUserUnlocked(UserHandle.of(userId))) {
                        sendUpdateRequest();
                    }
                }
            };

    @VisibleForTesting
    BroadcastReceiver mPbapReceiver = new BroadcastReceiver() {
        @Override
@@ -661,6 +685,13 @@ public class BluetoothPbapService extends ProfileService implements IObexConnect
        mDatabaseManager = Objects.requireNonNull(AdapterService.getAdapterService().getDatabase(),
            "DatabaseManager cannot be null when PbapService starts");

        IntentFilter userFilter = new IntentFilter();
        userFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        userFilter.addAction(Intent.ACTION_USER_SWITCHED);
        userFilter.addAction(Intent.ACTION_USER_UNLOCKED);

        getApplicationContext().registerReceiver(mUserChangeReceiver, userFilter);

        // Enable owned Activity component
        setComponentAvailable(PBAP_ACTIVITY, true);

@@ -728,6 +759,7 @@ public class BluetoothPbapService extends ProfileService implements IObexConnect
        synchronized (mPbapStateMachineMap) {
            mPbapStateMachineMap.clear();
        }
        getApplicationContext().unregisterReceiver(mUserChangeReceiver);
        return true;
    }

@@ -756,21 +788,6 @@ public class BluetoothPbapService extends ProfileService implements IObexConnect
        sBluetoothPbapService = instance;
    }

    @Override
    protected void setCurrentUser(int userId) {
        Log.i(TAG, "setCurrentUser(" + userId + ")");
        UserManager userManager = getSystemService(UserManager.class);
        if (userManager.isUserUnlocked(UserHandle.of(userId))) {
            setUserUnlocked(userId);
        }
    }

    @Override
    protected void setUserUnlocked(int userId) {
        Log.i(TAG, "setUserUnlocked(" + userId + ")");
        sendUpdateRequest();
    }

    @VisibleForTesting
    static class PbapBinder extends IBluetoothPbap.Stub implements IProfileServiceBinder {
        private BluetoothPbapService mService;