Loading services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java +1 −17 Original line number Original line Diff line number Diff line Loading @@ -54,7 +54,6 @@ import android.app.ActivityManagerInternal; import android.app.AppOpsManager; import android.app.AppOpsManager; import android.app.NotificationManager; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.PendingIntent; import android.bluetooth.BluetoothDevice; import android.companion.AssociationInfo; import android.companion.AssociationInfo; import android.companion.AssociationRequest; import android.companion.AssociationRequest; import android.companion.DeviceNotAssociatedException; import android.companion.DeviceNotAssociatedException; Loading Loading @@ -229,7 +228,7 @@ public class CompanionDeviceManagerService extends SystemService { loadAssociationsFromDisk(); loadAssociationsFromDisk(); mAssociationStore.registerListener(mAssociationStoreChangeListener); mAssociationStore.registerListener(mAssociationStoreChangeListener); mDevicePresenceMonitor = new CompanionDevicePresenceMonitor(mUserManager, mDevicePresenceMonitor = new CompanionDevicePresenceMonitor( mAssociationStore, mDevicePresenceCallback); mAssociationStore, mDevicePresenceCallback); mAssociationRequestsProcessor = new AssociationRequestsProcessor( mAssociationRequestsProcessor = new AssociationRequestsProcessor( Loading Loading @@ -315,21 +314,6 @@ public class CompanionDeviceManagerService extends SystemService { MINUTES.toMillis(10)); MINUTES.toMillis(10)); } } @Override public void onUserUnlocked(@NonNull TargetUser user) { // Notify and bind the app after the phone is unlocked. final int userId = user.getUserIdentifier(); final Set<BluetoothDevice> blueToothDevices = mDevicePresenceMonitor.getPendingReportConnectedDevices().get(userId); for (BluetoothDevice bluetoothDevice : blueToothDevices) { for (AssociationInfo ai: mAssociationStore.getAssociationsByAddress(bluetoothDevice.getAddress())) { Slog.i(TAG, "onUserUnlocked, device id( " + ai.getId() + " ) is connected"); mDevicePresenceMonitor.onBluetoothCompanionDeviceConnected(ai.getId()); } } } @NonNull @NonNull AssociationInfo getAssociationWithCallerChecks( AssociationInfo getAssociationWithCallerChecks( @UserIdInt int userId, @NonNull String packageName, @NonNull String macAddress) { @UserIdInt int userId, @NonNull String packageName, @NonNull String macAddress) { Loading services/companion/java/com/android/server/companion/presence/BluetoothCompanionDeviceConnectionListener.java +4 −53 Original line number Original line Diff line number Diff line Loading @@ -27,23 +27,17 @@ import android.companion.AssociationInfo; import android.net.MacAddress; import android.net.MacAddress; import android.os.Handler; import android.os.Handler; import android.os.HandlerExecutor; import android.os.HandlerExecutor; import android.os.UserHandle; import android.os.UserManager; import android.util.Log; import android.util.Log; import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; import com.android.server.companion.AssociationStore; import com.android.server.companion.AssociationStore; import java.util.Arrays; import java.util.Arrays; import java.util.HashMap; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.List; import java.util.Map; import java.util.Map; import java.util.Set; @SuppressLint("LongLogTag") @SuppressLint("LongLogTag") public class BluetoothCompanionDeviceConnectionListener class BluetoothCompanionDeviceConnectionListener extends BluetoothAdapter.BluetoothConnectionCallback extends BluetoothAdapter.BluetoothConnectionCallback implements AssociationStore.OnChangeListener { implements AssociationStore.OnChangeListener { private static final String TAG = "CDM_BluetoothCompanionDeviceConnectionListener"; private static final String TAG = "CDM_BluetoothCompanionDeviceConnectionListener"; Loading @@ -54,23 +48,15 @@ public class BluetoothCompanionDeviceConnectionListener void onBluetoothCompanionDeviceDisconnected(int associationId); void onBluetoothCompanionDeviceDisconnected(int associationId); } } private final UserManager mUserManager; private final @NonNull AssociationStore mAssociationStore; private final @NonNull AssociationStore mAssociationStore; private final @NonNull Callback mCallback; private final @NonNull Callback mCallback; /** A set of ALL connected BT device (not only companion.) */ /** A set of ALL connected BT device (not only companion.) */ private final @NonNull Map<MacAddress, BluetoothDevice> mAllConnectedDevices = new HashMap<>(); private final @NonNull Map<MacAddress, BluetoothDevice> mAllConnectedDevices = new HashMap<>(); BluetoothCompanionDeviceConnectionListener(@NonNull AssociationStore associationStore, @GuardedBy("mPendingReportConnectedDevices") @NonNull Callback callback) { @NonNull final SparseArray<Set<BluetoothDevice>> mPendingReportConnectedDevices = new SparseArray<>(); BluetoothCompanionDeviceConnectionListener(UserManager userManager, @NonNull AssociationStore associationStore, @NonNull Callback callback) { mAssociationStore = associationStore; mAssociationStore = associationStore; mCallback = callback; mCallback = callback; mUserManager = userManager; } } public void init(@NonNull BluetoothAdapter btAdapter) { public void init(@NonNull BluetoothAdapter btAdapter) { Loading @@ -90,33 +76,13 @@ public class BluetoothCompanionDeviceConnectionListener if (DEBUG) Log.i(TAG, "onDevice_Connected() " + btDeviceToString(device)); if (DEBUG) Log.i(TAG, "onDevice_Connected() " + btDeviceToString(device)); final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final int userId = UserHandle.myUserId(); if (mAllConnectedDevices.put(macAddress, device) != null) { if (mAllConnectedDevices.put(macAddress, device) != null) { if (DEBUG) Log.w(TAG, "Device " + btDeviceToString(device) + " is already connected."); if (DEBUG) Log.w(TAG, "Device " + btDeviceToString(device) + " is already connected."); return; return; } } // Try to bind and notify the app after the phone is unlocked. if (!mUserManager.isUserUnlockingOrUnlocked(UserHandle.myUserId())) { if (DEBUG) { Log.i(TAG, "Current user is not in unlocking or unlocked stage yet. Notify " + "the application when the phone is unlocked"); } synchronized (mPendingReportConnectedDevices) { Set<BluetoothDevice> bluetoothDevices = mPendingReportConnectedDevices.get(userId); if (bluetoothDevices == null) { bluetoothDevices = new HashSet<>(); mPendingReportConnectedDevices.put(userId, bluetoothDevices); } bluetoothDevices.add(device); } } else { onDeviceConnectivityChanged(device, true); onDeviceConnectivityChanged(device, true); } } } /** /** * Overrides * Overrides Loading @@ -132,8 +98,6 @@ public class BluetoothCompanionDeviceConnectionListener } } final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final int userId = UserHandle.myUserId(); if (mAllConnectedDevices.remove(macAddress) == null) { if (mAllConnectedDevices.remove(macAddress) == null) { if (DEBUG) { if (DEBUG) { Log.w(TAG, "The device wasn't tracked as connected " + btDeviceToString(device)); Log.w(TAG, "The device wasn't tracked as connected " + btDeviceToString(device)); Loading @@ -141,19 +105,6 @@ public class BluetoothCompanionDeviceConnectionListener return; return; } } // Do not need to report the connectivity since the user is not unlock the phone so // that cdm is not bind with the app yet. if (!mUserManager.isUserUnlockingOrUnlocked(userId)) { synchronized (mPendingReportConnectedDevices) { Set<BluetoothDevice> bluetoothDevices = mPendingReportConnectedDevices.get(userId); if (bluetoothDevices != null) { bluetoothDevices.remove(device); } } return; } onDeviceConnectivityChanged(device, false); onDeviceConnectivityChanged(device, false); } } Loading services/companion/java/com/android/server/companion/presence/CompanionDevicePresenceMonitor.java +5 −16 Original line number Original line Diff line number Diff line Loading @@ -23,16 +23,13 @@ import android.annotation.NonNull; import android.annotation.SuppressLint; import android.annotation.SuppressLint; import android.annotation.TestApi; import android.annotation.TestApi; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.companion.AssociationInfo; import android.companion.AssociationInfo; import android.content.Context; import android.content.Context; import android.os.Binder; import android.os.Binder; import android.os.Handler; import android.os.Handler; import android.os.Looper; import android.os.Looper; import android.os.Message; import android.os.Message; import android.os.UserManager; import android.util.Log; import android.util.Log; import android.util.SparseArray; import com.android.server.companion.AssociationStore; import com.android.server.companion.AssociationStore; Loading Loading @@ -89,12 +86,13 @@ public class CompanionDevicePresenceMonitor implements AssociationStore.OnChange private final SimulatedDevicePresenceSchedulerHelper mSchedulerHelper = private final SimulatedDevicePresenceSchedulerHelper mSchedulerHelper = new SimulatedDevicePresenceSchedulerHelper(); new SimulatedDevicePresenceSchedulerHelper(); public CompanionDevicePresenceMonitor(UserManager userManager, public CompanionDevicePresenceMonitor(@NonNull AssociationStore associationStore, @NonNull AssociationStore associationStore, @NonNull Callback callback) { @NonNull Callback callback) { mAssociationStore = associationStore; mAssociationStore = associationStore; mCallback = callback; mCallback = callback; mBtConnectionListener = new BluetoothCompanionDeviceConnectionListener(userManager, associationStore, /* BluetoothCompanionDeviceConnectionListener.Callback */ this); mBtConnectionListener = new BluetoothCompanionDeviceConnectionListener(associationStore, /* BluetoothCompanionDeviceConnectionListener.Callback */ this); mBleScanner = new BleCompanionDeviceScanner(associationStore, mBleScanner = new BleCompanionDeviceScanner(associationStore, /* BleCompanionDeviceScanner.Callback */ this); /* BleCompanionDeviceScanner.Callback */ this); } } Loading Loading @@ -300,15 +298,6 @@ public class CompanionDevicePresenceMonitor implements AssociationStore.OnChange // what's needed. // what's needed. } } /** * Return a set of devices that pending to report connectivity */ public SparseArray<Set<BluetoothDevice>> getPendingReportConnectedDevices() { synchronized (mBtConnectionListener.mPendingReportConnectedDevices) { return mBtConnectionListener.mPendingReportConnectedDevices; } } private static void enforceCallerShellOrRoot() { private static void enforceCallerShellOrRoot() { final int callingUid = Binder.getCallingUid(); final int callingUid = Binder.getCallingUid(); if (callingUid == SHELL_UID || callingUid == ROOT_UID) return; if (callingUid == SHELL_UID || callingUid == ROOT_UID) return; Loading Loading
services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java +1 −17 Original line number Original line Diff line number Diff line Loading @@ -54,7 +54,6 @@ import android.app.ActivityManagerInternal; import android.app.AppOpsManager; import android.app.AppOpsManager; import android.app.NotificationManager; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.PendingIntent; import android.bluetooth.BluetoothDevice; import android.companion.AssociationInfo; import android.companion.AssociationInfo; import android.companion.AssociationRequest; import android.companion.AssociationRequest; import android.companion.DeviceNotAssociatedException; import android.companion.DeviceNotAssociatedException; Loading Loading @@ -229,7 +228,7 @@ public class CompanionDeviceManagerService extends SystemService { loadAssociationsFromDisk(); loadAssociationsFromDisk(); mAssociationStore.registerListener(mAssociationStoreChangeListener); mAssociationStore.registerListener(mAssociationStoreChangeListener); mDevicePresenceMonitor = new CompanionDevicePresenceMonitor(mUserManager, mDevicePresenceMonitor = new CompanionDevicePresenceMonitor( mAssociationStore, mDevicePresenceCallback); mAssociationStore, mDevicePresenceCallback); mAssociationRequestsProcessor = new AssociationRequestsProcessor( mAssociationRequestsProcessor = new AssociationRequestsProcessor( Loading Loading @@ -315,21 +314,6 @@ public class CompanionDeviceManagerService extends SystemService { MINUTES.toMillis(10)); MINUTES.toMillis(10)); } } @Override public void onUserUnlocked(@NonNull TargetUser user) { // Notify and bind the app after the phone is unlocked. final int userId = user.getUserIdentifier(); final Set<BluetoothDevice> blueToothDevices = mDevicePresenceMonitor.getPendingReportConnectedDevices().get(userId); for (BluetoothDevice bluetoothDevice : blueToothDevices) { for (AssociationInfo ai: mAssociationStore.getAssociationsByAddress(bluetoothDevice.getAddress())) { Slog.i(TAG, "onUserUnlocked, device id( " + ai.getId() + " ) is connected"); mDevicePresenceMonitor.onBluetoothCompanionDeviceConnected(ai.getId()); } } } @NonNull @NonNull AssociationInfo getAssociationWithCallerChecks( AssociationInfo getAssociationWithCallerChecks( @UserIdInt int userId, @NonNull String packageName, @NonNull String macAddress) { @UserIdInt int userId, @NonNull String packageName, @NonNull String macAddress) { Loading
services/companion/java/com/android/server/companion/presence/BluetoothCompanionDeviceConnectionListener.java +4 −53 Original line number Original line Diff line number Diff line Loading @@ -27,23 +27,17 @@ import android.companion.AssociationInfo; import android.net.MacAddress; import android.net.MacAddress; import android.os.Handler; import android.os.Handler; import android.os.HandlerExecutor; import android.os.HandlerExecutor; import android.os.UserHandle; import android.os.UserManager; import android.util.Log; import android.util.Log; import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; import com.android.server.companion.AssociationStore; import com.android.server.companion.AssociationStore; import java.util.Arrays; import java.util.Arrays; import java.util.HashMap; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.List; import java.util.Map; import java.util.Map; import java.util.Set; @SuppressLint("LongLogTag") @SuppressLint("LongLogTag") public class BluetoothCompanionDeviceConnectionListener class BluetoothCompanionDeviceConnectionListener extends BluetoothAdapter.BluetoothConnectionCallback extends BluetoothAdapter.BluetoothConnectionCallback implements AssociationStore.OnChangeListener { implements AssociationStore.OnChangeListener { private static final String TAG = "CDM_BluetoothCompanionDeviceConnectionListener"; private static final String TAG = "CDM_BluetoothCompanionDeviceConnectionListener"; Loading @@ -54,23 +48,15 @@ public class BluetoothCompanionDeviceConnectionListener void onBluetoothCompanionDeviceDisconnected(int associationId); void onBluetoothCompanionDeviceDisconnected(int associationId); } } private final UserManager mUserManager; private final @NonNull AssociationStore mAssociationStore; private final @NonNull AssociationStore mAssociationStore; private final @NonNull Callback mCallback; private final @NonNull Callback mCallback; /** A set of ALL connected BT device (not only companion.) */ /** A set of ALL connected BT device (not only companion.) */ private final @NonNull Map<MacAddress, BluetoothDevice> mAllConnectedDevices = new HashMap<>(); private final @NonNull Map<MacAddress, BluetoothDevice> mAllConnectedDevices = new HashMap<>(); BluetoothCompanionDeviceConnectionListener(@NonNull AssociationStore associationStore, @GuardedBy("mPendingReportConnectedDevices") @NonNull Callback callback) { @NonNull final SparseArray<Set<BluetoothDevice>> mPendingReportConnectedDevices = new SparseArray<>(); BluetoothCompanionDeviceConnectionListener(UserManager userManager, @NonNull AssociationStore associationStore, @NonNull Callback callback) { mAssociationStore = associationStore; mAssociationStore = associationStore; mCallback = callback; mCallback = callback; mUserManager = userManager; } } public void init(@NonNull BluetoothAdapter btAdapter) { public void init(@NonNull BluetoothAdapter btAdapter) { Loading @@ -90,33 +76,13 @@ public class BluetoothCompanionDeviceConnectionListener if (DEBUG) Log.i(TAG, "onDevice_Connected() " + btDeviceToString(device)); if (DEBUG) Log.i(TAG, "onDevice_Connected() " + btDeviceToString(device)); final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final int userId = UserHandle.myUserId(); if (mAllConnectedDevices.put(macAddress, device) != null) { if (mAllConnectedDevices.put(macAddress, device) != null) { if (DEBUG) Log.w(TAG, "Device " + btDeviceToString(device) + " is already connected."); if (DEBUG) Log.w(TAG, "Device " + btDeviceToString(device) + " is already connected."); return; return; } } // Try to bind and notify the app after the phone is unlocked. if (!mUserManager.isUserUnlockingOrUnlocked(UserHandle.myUserId())) { if (DEBUG) { Log.i(TAG, "Current user is not in unlocking or unlocked stage yet. Notify " + "the application when the phone is unlocked"); } synchronized (mPendingReportConnectedDevices) { Set<BluetoothDevice> bluetoothDevices = mPendingReportConnectedDevices.get(userId); if (bluetoothDevices == null) { bluetoothDevices = new HashSet<>(); mPendingReportConnectedDevices.put(userId, bluetoothDevices); } bluetoothDevices.add(device); } } else { onDeviceConnectivityChanged(device, true); onDeviceConnectivityChanged(device, true); } } } /** /** * Overrides * Overrides Loading @@ -132,8 +98,6 @@ public class BluetoothCompanionDeviceConnectionListener } } final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final MacAddress macAddress = MacAddress.fromString(device.getAddress()); final int userId = UserHandle.myUserId(); if (mAllConnectedDevices.remove(macAddress) == null) { if (mAllConnectedDevices.remove(macAddress) == null) { if (DEBUG) { if (DEBUG) { Log.w(TAG, "The device wasn't tracked as connected " + btDeviceToString(device)); Log.w(TAG, "The device wasn't tracked as connected " + btDeviceToString(device)); Loading @@ -141,19 +105,6 @@ public class BluetoothCompanionDeviceConnectionListener return; return; } } // Do not need to report the connectivity since the user is not unlock the phone so // that cdm is not bind with the app yet. if (!mUserManager.isUserUnlockingOrUnlocked(userId)) { synchronized (mPendingReportConnectedDevices) { Set<BluetoothDevice> bluetoothDevices = mPendingReportConnectedDevices.get(userId); if (bluetoothDevices != null) { bluetoothDevices.remove(device); } } return; } onDeviceConnectivityChanged(device, false); onDeviceConnectivityChanged(device, false); } } Loading
services/companion/java/com/android/server/companion/presence/CompanionDevicePresenceMonitor.java +5 −16 Original line number Original line Diff line number Diff line Loading @@ -23,16 +23,13 @@ import android.annotation.NonNull; import android.annotation.SuppressLint; import android.annotation.SuppressLint; import android.annotation.TestApi; import android.annotation.TestApi; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.companion.AssociationInfo; import android.companion.AssociationInfo; import android.content.Context; import android.content.Context; import android.os.Binder; import android.os.Binder; import android.os.Handler; import android.os.Handler; import android.os.Looper; import android.os.Looper; import android.os.Message; import android.os.Message; import android.os.UserManager; import android.util.Log; import android.util.Log; import android.util.SparseArray; import com.android.server.companion.AssociationStore; import com.android.server.companion.AssociationStore; Loading Loading @@ -89,12 +86,13 @@ public class CompanionDevicePresenceMonitor implements AssociationStore.OnChange private final SimulatedDevicePresenceSchedulerHelper mSchedulerHelper = private final SimulatedDevicePresenceSchedulerHelper mSchedulerHelper = new SimulatedDevicePresenceSchedulerHelper(); new SimulatedDevicePresenceSchedulerHelper(); public CompanionDevicePresenceMonitor(UserManager userManager, public CompanionDevicePresenceMonitor(@NonNull AssociationStore associationStore, @NonNull AssociationStore associationStore, @NonNull Callback callback) { @NonNull Callback callback) { mAssociationStore = associationStore; mAssociationStore = associationStore; mCallback = callback; mCallback = callback; mBtConnectionListener = new BluetoothCompanionDeviceConnectionListener(userManager, associationStore, /* BluetoothCompanionDeviceConnectionListener.Callback */ this); mBtConnectionListener = new BluetoothCompanionDeviceConnectionListener(associationStore, /* BluetoothCompanionDeviceConnectionListener.Callback */ this); mBleScanner = new BleCompanionDeviceScanner(associationStore, mBleScanner = new BleCompanionDeviceScanner(associationStore, /* BleCompanionDeviceScanner.Callback */ this); /* BleCompanionDeviceScanner.Callback */ this); } } Loading Loading @@ -300,15 +298,6 @@ public class CompanionDevicePresenceMonitor implements AssociationStore.OnChange // what's needed. // what's needed. } } /** * Return a set of devices that pending to report connectivity */ public SparseArray<Set<BluetoothDevice>> getPendingReportConnectedDevices() { synchronized (mBtConnectionListener.mPendingReportConnectedDevices) { return mBtConnectionListener.mPendingReportConnectedDevices; } } private static void enforceCallerShellOrRoot() { private static void enforceCallerShellOrRoot() { final int callingUid = Binder.getCallingUid(); final int callingUid = Binder.getCallingUid(); if (callingUid == SHELL_UID || callingUid == ROOT_UID) return; if (callingUid == SHELL_UID || callingUid == ROOT_UID) return; Loading