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

Commit 1bc37f4b authored by Sal Savage's avatar Sal Savage
Browse files

Refactor HID logging to be unguarded

With the recent addition of a process minimum default log level, the
Android Log framework will now enforce the set log level against the
various log invocations we make in code. We no longer need to guard log
invocations on our own.

Tag: #refactor
Flag: EXEMPT, logging only change
Bug: 315046089
Test: atest BluetoothInstrumentationTests
Change-Id: I4ca60b59e49ff149dda13cc3e7a8901960a11f37
parent e8593771
Loading
Loading
Loading
Loading
+39 −90
Original line number Original line Diff line number Diff line
@@ -57,7 +57,6 @@ import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Objects;


public class HidDeviceService extends ProfileService {
public class HidDeviceService extends ProfileService {
    private static final boolean DBG = false;
    private static final String TAG = HidDeviceService.class.getSimpleName();
    private static final String TAG = HidDeviceService.class.getSimpleName();


    private static final int MESSAGE_APPLICATION_STATE_CHANGED = 1;
    private static final int MESSAGE_APPLICATION_STATE_CHANGED = 1;
@@ -102,9 +101,7 @@ public class HidDeviceService extends ProfileService {


        @Override
        @Override
        public void handleMessage(Message msg) {
        public void handleMessage(Message msg) {
            if (DBG) {
            Log.d(TAG, "handleMessage(): msg.what=" + msg.what);
            Log.d(TAG, "handleMessage(): msg.what=" + msg.what);
            }


            switch (msg.what) {
            switch (msg.what) {
                case MESSAGE_APPLICATION_STATE_CHANGED: {
                case MESSAGE_APPLICATION_STATE_CHANGED: {
@@ -332,7 +329,7 @@ public class HidDeviceService extends ProfileService {
                BluetoothHidDeviceAppQosSettings inQos, BluetoothHidDeviceAppQosSettings outQos,
                BluetoothHidDeviceAppQosSettings inQos, BluetoothHidDeviceAppQosSettings outQos,
                IBluetoothHidDeviceCallback callback, AttributionSource source,
                IBluetoothHidDeviceCallback callback, AttributionSource source,
                SynchronousResultReceiver receiver) {
                SynchronousResultReceiver receiver) {
            if (DBG) Log.d(TAG, "registerApp()");
            Log.d(TAG, "registerApp()");
            try {
            try {
                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                boolean defaultValue = false;
                boolean defaultValue = false;
@@ -348,7 +345,7 @@ public class HidDeviceService extends ProfileService {
        @Override
        @Override
        public void unregisterApp(AttributionSource source, SynchronousResultReceiver receiver) {
        public void unregisterApp(AttributionSource source, SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "unregisterApp()");
                Log.d(TAG, "unregisterApp()");
                boolean defaultValue = false;
                boolean defaultValue = false;


                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
@@ -366,7 +363,7 @@ public class HidDeviceService extends ProfileService {
        public void sendReport(BluetoothDevice device, int id, byte[] data,
        public void sendReport(BluetoothDevice device, int id, byte[] data,
                AttributionSource source, SynchronousResultReceiver receiver) {
                AttributionSource source, SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "sendReport(): device=" + device + "  id=" + id);
                Log.d(TAG, "sendReport(): device=" + device + "  id=" + id);
                boolean defaultValue = false ;
                boolean defaultValue = false ;


                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
@@ -384,7 +381,7 @@ public class HidDeviceService extends ProfileService {
        public void replyReport(BluetoothDevice device, byte type, byte id, byte[] data,
        public void replyReport(BluetoothDevice device, byte type, byte id, byte[] data,
                AttributionSource source, SynchronousResultReceiver receiver) {
                AttributionSource source, SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "replyReport(): device=" + device
                Log.d(TAG, "replyReport(): device=" + device
                        + " type=" + type + " id=" + id);
                        + " type=" + type + " id=" + id);
                boolean defaultValue = false;
                boolean defaultValue = false;
                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
@@ -401,7 +398,7 @@ public class HidDeviceService extends ProfileService {
        public void unplug(BluetoothDevice device, AttributionSource source,
        public void unplug(BluetoothDevice device, AttributionSource source,
                SynchronousResultReceiver receiver) {
                SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "unplug(): device=" + device);
                Log.d(TAG, "unplug(): device=" + device);
                boolean defaultValue = false;
                boolean defaultValue = false;
                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                if (service != null) {
                if (service != null) {
@@ -417,7 +414,7 @@ public class HidDeviceService extends ProfileService {
        public void connect(BluetoothDevice device, AttributionSource source,
        public void connect(BluetoothDevice device, AttributionSource source,
                SynchronousResultReceiver receiver) {
                SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "connect(): device=" + device);
                Log.d(TAG, "connect(): device=" + device);
                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                boolean defaultValue = false;
                boolean defaultValue = false;
                if (service != null) {
                if (service != null) {
@@ -434,7 +431,7 @@ public class HidDeviceService extends ProfileService {
        public void disconnect(BluetoothDevice device, AttributionSource source,
        public void disconnect(BluetoothDevice device, AttributionSource source,
                SynchronousResultReceiver receiver) {
                SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "disconnect(): device=" + device);
                Log.d(TAG, "disconnect(): device=" + device);
                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                boolean defaultValue = false;
                boolean defaultValue = false;
                if (service != null) {
                if (service != null) {
@@ -451,10 +448,8 @@ public class HidDeviceService extends ProfileService {
        public void setConnectionPolicy(BluetoothDevice device, int connectionPolicy,
        public void setConnectionPolicy(BluetoothDevice device, int connectionPolicy,
                AttributionSource source, SynchronousResultReceiver receiver) {
                AttributionSource source, SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) {
                Log.d(TAG, "setConnectionPolicy(): device=" + device + " connectionPolicy="
                Log.d(TAG, "setConnectionPolicy(): device=" + device + " connectionPolicy="
                        + connectionPolicy);
                        + connectionPolicy);
                }
                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                boolean defaultValue = false;
                boolean defaultValue = false;
                if (service != null) {
                if (service != null) {
@@ -471,7 +466,7 @@ public class HidDeviceService extends ProfileService {
        public void reportError(BluetoothDevice device, byte error, AttributionSource source,
        public void reportError(BluetoothDevice device, byte error, AttributionSource source,
                SynchronousResultReceiver receiver) {
                SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "reportError(): device=" + device + " error=" + error);
                Log.d(TAG, "reportError(): device=" + device + " error=" + error);


                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                boolean defaultValue = false;
                boolean defaultValue = false;
@@ -488,7 +483,7 @@ public class HidDeviceService extends ProfileService {
        public void getConnectionState(BluetoothDevice device, AttributionSource source,
        public void getConnectionState(BluetoothDevice device, AttributionSource source,
                SynchronousResultReceiver receiver) {
                SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) Log.d(TAG, "getConnectionState(): device=" + device);
                Log.d(TAG, "getConnectionState(): device=" + device);


                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                int defaultValue = BluetoothHidDevice.STATE_DISCONNECTED;
                int defaultValue = BluetoothHidDevice.STATE_DISCONNECTED;
@@ -504,7 +499,7 @@ public class HidDeviceService extends ProfileService {
        @Override
        @Override
        public void getConnectedDevices(AttributionSource source,
        public void getConnectedDevices(AttributionSource source,
                SynchronousResultReceiver receiver) {
                SynchronousResultReceiver receiver) {
            if (DBG) Log.d(TAG, "getConnectedDevices()");
            Log.d(TAG, "getConnectedDevices()");


            getDevicesMatchingConnectionStates(new int[] { BluetoothProfile.STATE_CONNECTED },
            getDevicesMatchingConnectionStates(new int[] { BluetoothProfile.STATE_CONNECTED },
                    source, receiver);
                    source, receiver);
@@ -514,10 +509,8 @@ public class HidDeviceService extends ProfileService {
        public void getDevicesMatchingConnectionStates(int[] states,
        public void getDevicesMatchingConnectionStates(int[] states,
                AttributionSource source, SynchronousResultReceiver receiver) {
                AttributionSource source, SynchronousResultReceiver receiver) {
            try {
            try {
                if (DBG) {
                Log.d(TAG, "getDevicesMatchingConnectionStates(): states="
                Log.d(TAG, "getDevicesMatchingConnectionStates(): states="
                        + Arrays.toString(states));
                        + Arrays.toString(states));
                }
                HidDeviceService service = getService(source);
                HidDeviceService service = getService(source);
                List<BluetoothDevice> defaultValue = new ArrayList<BluetoothDevice>(0);
                List<BluetoothDevice> defaultValue = new ArrayList<BluetoothDevice>(0);
                if (service != null) {
                if (service != null) {
@@ -575,9 +568,7 @@ public class HidDeviceService extends ProfileService {
        }
        }


        int callingUid = Binder.getCallingUid();
        int callingUid = Binder.getCallingUid();
        if (DBG) {
        Log.d(TAG, "registerApp(): calling uid=" + callingUid);
        Log.d(TAG, "registerApp(): calling uid=" + callingUid);
        }
        if (callingUid >= Process.FIRST_APPLICATION_UID
        if (callingUid >= Process.FIRST_APPLICATION_UID
                && mActivityManager.getUidImportance(callingUid) > FOREGROUND_IMPORTANCE_CUTOFF) {
                && mActivityManager.getUidImportance(callingUid) > FOREGROUND_IMPORTANCE_CUTOFF) {
            Log.w(TAG, "registerApp(): failed because the app is not foreground");
            Log.w(TAG, "registerApp(): failed because the app is not foreground");
@@ -615,51 +606,39 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized boolean unregisterApp() {
    synchronized boolean unregisterApp() {
        if (DBG) {
        Log.d(TAG, "unregisterApp()");
        Log.d(TAG, "unregisterApp()");
        }


        int callingUid = Binder.getCallingUid();
        int callingUid = Binder.getCallingUid();
        return unregisterAppUid(callingUid);
        return unregisterAppUid(callingUid);
    }
    }


    private synchronized boolean unregisterAppUid(int uid) {
    private synchronized boolean unregisterAppUid(int uid) {
        if (DBG) {
        Log.d(TAG, "unregisterAppUid(): uid=" + uid);
        Log.d(TAG, "unregisterAppUid(): uid=" + uid);
        }


        if (mUserUid != 0 && (uid == mUserUid || uid < Process.FIRST_APPLICATION_UID)) {
        if (mUserUid != 0 && (uid == mUserUid || uid < Process.FIRST_APPLICATION_UID)) {
            mUserUid = 0;
            mUserUid = 0;
            return mHidDeviceNativeInterface.unregisterApp();
            return mHidDeviceNativeInterface.unregisterApp();
        }
        }
        if (DBG) {
        Log.d(TAG, "unregisterAppUid(): caller UID doesn't match user UID");
        Log.d(TAG, "unregisterAppUid(): caller UID doesn't match user UID");
        }
        return false;
        return false;
    }
    }


    synchronized boolean sendReport(BluetoothDevice device, int id, byte[] data) {
    synchronized boolean sendReport(BluetoothDevice device, int id, byte[] data) {
        if (DBG) {
        Log.d(TAG, "sendReport(): device=" + device + " id=" + id);
        Log.d(TAG, "sendReport(): device=" + device + " id=" + id);
        }


        return checkDevice(device) && checkCallingUid()
        return checkDevice(device) && checkCallingUid()
                && mHidDeviceNativeInterface.sendReport(id, data);
                && mHidDeviceNativeInterface.sendReport(id, data);
    }
    }


    synchronized boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) {
    synchronized boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) {
        if (DBG) {
        Log.d(TAG, "replyReport(): device=" + device + " type=" + type + " id=" + id);
        Log.d(TAG, "replyReport(): device=" + device + " type=" + type + " id=" + id);
        }


        return checkDevice(device) && checkCallingUid()
        return checkDevice(device) && checkCallingUid()
                && mHidDeviceNativeInterface.replyReport(type, id, data);
                && mHidDeviceNativeInterface.replyReport(type, id, data);
    }
    }


    synchronized boolean unplug(BluetoothDevice device) {
    synchronized boolean unplug(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "unplug(): device=" + device);
        Log.d(TAG, "unplug(): device=" + device);
        }


        return checkDevice(device) && checkCallingUid()
        return checkDevice(device) && checkCallingUid()
                && mHidDeviceNativeInterface.unplug();
                && mHidDeviceNativeInterface.unplug();
@@ -672,9 +651,7 @@ public class HidDeviceService extends ProfileService {
     * @return true if the connection is successful, false otherwise
     * @return true if the connection is successful, false otherwise
     */
     */
    public synchronized boolean connect(BluetoothDevice device) {
    public synchronized boolean connect(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "connect(): device=" + device);
        Log.d(TAG, "connect(): device=" + device);
        }


        return checkCallingUid() && mHidDeviceNativeInterface.connect(device);
        return checkCallingUid() && mHidDeviceNativeInterface.connect(device);
    }
    }
@@ -686,9 +663,7 @@ public class HidDeviceService extends ProfileService {
     * @return true if the disconnection is successful, false otherwise
     * @return true if the disconnection is successful, false otherwise
     */
     */
    public synchronized boolean disconnect(BluetoothDevice device) {
    public synchronized boolean disconnect(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "disconnect(): device=" + device);
        Log.d(TAG, "disconnect(): device=" + device);
        }


        int callingUid = Binder.getCallingUid();
        int callingUid = Binder.getCallingUid();
        if (callingUid != mUserUid && callingUid >= Process.FIRST_APPLICATION_UID) {
        if (callingUid != mUserUid && callingUid >= Process.FIRST_APPLICATION_UID) {
@@ -717,9 +692,7 @@ public class HidDeviceService extends ProfileService {
    public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) {
    public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) {
        enforceCallingOrSelfPermission(
        enforceCallingOrSelfPermission(
                BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
                BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        if (DBG) {
        Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }


        if (!mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.HID_DEVICE,
        if (!mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.HID_DEVICE,
                  connectionPolicy)) {
                  connectionPolicy)) {
@@ -754,9 +727,7 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized boolean reportError(BluetoothDevice device, byte error) {
    synchronized boolean reportError(BluetoothDevice device, byte error) {
        if (DBG) {
        Log.d(TAG, "reportError(): device=" + device + " error=" + error);
        Log.d(TAG, "reportError(): device=" + device + " error=" + error);
        }


        return checkDevice(device) && checkCallingUid()
        return checkDevice(device) && checkCallingUid()
                && mHidDeviceNativeInterface.reportError(error);
                && mHidDeviceNativeInterface.reportError(error);
@@ -772,9 +743,7 @@ public class HidDeviceService extends ProfileService {


    @Override
    @Override
    public void start() {
    public void start() {
        if (DBG) {
        Log.d(TAG, "start()");
        Log.d(TAG, "start()");
        }


        mDatabaseManager = Objects.requireNonNull(AdapterService.getAdapterService().getDatabase(),
        mDatabaseManager = Objects.requireNonNull(AdapterService.getAdapterService().getDatabase(),
                "DatabaseManager cannot be null when HidDeviceService starts");
                "DatabaseManager cannot be null when HidDeviceService starts");
@@ -791,9 +760,7 @@ public class HidDeviceService extends ProfileService {


    @Override
    @Override
    public void stop() {
    public void stop() {
        if (DBG) {
        Log.d(TAG, "stop()");
        Log.d(TAG, "stop()");
        }


        if (sHidDeviceService == null) {
        if (sHidDeviceService == null) {
            Log.w(TAG, "stop() called before start()");
            Log.w(TAG, "stop() called before start()");
@@ -826,9 +793,7 @@ public class HidDeviceService extends ProfileService {


    @VisibleForTesting
    @VisibleForTesting
    static synchronized void setHidDeviceService(HidDeviceService instance) {
    static synchronized void setHidDeviceService(HidDeviceService instance) {
        if (DBG) {
        Log.d(TAG, "setHidDeviceService(): set to: " + instance);
        Log.d(TAG, "setHidDeviceService(): set to: " + instance);
        }
        sHidDeviceService = instance;
        sHidDeviceService = instance;
    }
    }


@@ -863,9 +828,7 @@ public class HidDeviceService extends ProfileService {


    synchronized void onApplicationStateChangedFromNative(BluetoothDevice device,
    synchronized void onApplicationStateChangedFromNative(BluetoothDevice device,
            boolean registered) {
            boolean registered) {
        if (DBG) {
        Log.d(TAG, "onApplicationStateChanged(): registered=" + registered);
        Log.d(TAG, "onApplicationStateChanged(): registered=" + registered);
        }


        Message msg = mHandler.obtainMessage(MESSAGE_APPLICATION_STATE_CHANGED);
        Message msg = mHandler.obtainMessage(MESSAGE_APPLICATION_STATE_CHANGED);
        msg.obj = device;
        msg.obj = device;
@@ -874,10 +837,8 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized void onConnectStateChangedFromNative(BluetoothDevice device, int state) {
    synchronized void onConnectStateChangedFromNative(BluetoothDevice device, int state) {
        if (DBG) {
        Log.d(TAG, "onConnectStateChanged(): device="
        Log.d(TAG, "onConnectStateChanged(): device="
                + device + " state=" + state);
                + device + " state=" + state);
        }


        Message msg = mHandler.obtainMessage(MESSAGE_CONNECT_STATE_CHANGED);
        Message msg = mHandler.obtainMessage(MESSAGE_CONNECT_STATE_CHANGED);
        msg.obj = device;
        msg.obj = device;
@@ -886,9 +847,7 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized void onGetReportFromNative(byte type, byte id, short bufferSize) {
    synchronized void onGetReportFromNative(byte type, byte id, short bufferSize) {
        if (DBG) {
        Log.d(TAG, "onGetReport(): type=" + type + " id=" + id + " bufferSize=" + bufferSize);
        Log.d(TAG, "onGetReport(): type=" + type + " id=" + id + " bufferSize=" + bufferSize);
        }


        Message msg = mHandler.obtainMessage(MESSAGE_GET_REPORT);
        Message msg = mHandler.obtainMessage(MESSAGE_GET_REPORT);
        msg.obj = bufferSize > 0 ? Integer.valueOf(bufferSize) : null;
        msg.obj = bufferSize > 0 ? Integer.valueOf(bufferSize) : null;
@@ -898,9 +857,7 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized void onSetReportFromNative(byte reportType, byte reportId, byte[] data) {
    synchronized void onSetReportFromNative(byte reportType, byte reportId, byte[] data) {
        if (DBG) {
        Log.d(TAG, "onSetReport(): reportType=" + reportType + " reportId=" + reportId);
        Log.d(TAG, "onSetReport(): reportType=" + reportType + " reportId=" + reportId);
        }


        ByteBuffer bb = ByteBuffer.wrap(data);
        ByteBuffer bb = ByteBuffer.wrap(data);


@@ -912,9 +869,7 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized void onSetProtocolFromNative(byte protocol) {
    synchronized void onSetProtocolFromNative(byte protocol) {
        if (DBG) {
        Log.d(TAG, "onSetProtocol(): protocol=" + protocol);
        Log.d(TAG, "onSetProtocol(): protocol=" + protocol);
        }


        Message msg = mHandler.obtainMessage(MESSAGE_SET_PROTOCOL);
        Message msg = mHandler.obtainMessage(MESSAGE_SET_PROTOCOL);
        msg.arg1 = protocol;
        msg.arg1 = protocol;
@@ -922,9 +877,7 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized void onInterruptDataFromNative(byte reportId, byte[] data) {
    synchronized void onInterruptDataFromNative(byte reportId, byte[] data) {
        if (DBG) {
        Log.d(TAG, "onInterruptData(): reportId=" + reportId);
        Log.d(TAG, "onInterruptData(): reportId=" + reportId);
        }


        ByteBuffer bb = ByteBuffer.wrap(data);
        ByteBuffer bb = ByteBuffer.wrap(data);


@@ -935,19 +888,15 @@ public class HidDeviceService extends ProfileService {
    }
    }


    synchronized void onVirtualCableUnplugFromNative() {
    synchronized void onVirtualCableUnplugFromNative() {
        if (DBG) {
        Log.d(TAG, "onVirtualCableUnplug()");
        Log.d(TAG, "onVirtualCableUnplug()");
        }


        Message msg = mHandler.obtainMessage(MESSAGE_VC_UNPLUG);
        Message msg = mHandler.obtainMessage(MESSAGE_VC_UNPLUG);
        mHandler.sendMessage(msg);
        mHandler.sendMessage(msg);
    }
    }


    private void setAndBroadcastConnectionState(BluetoothDevice device, int newState) {
    private void setAndBroadcastConnectionState(BluetoothDevice device, int newState) {
        if (DBG) {
        Log.d(TAG, "setAndBroadcastConnectionState(): device=" + device
        Log.d(TAG, "setAndBroadcastConnectionState(): device=" + device
                + " oldState=" + mHidDeviceState + " newState=" + newState);
                + " oldState=" + mHidDeviceState + " newState=" + newState);
        }


        if (mHidDevice != null && !mHidDevice.equals(device)) {
        if (mHidDevice != null && !mHidDevice.equals(device)) {
            Log.w(TAG, "Connection state changed for unknown device, ignoring");
            Log.w(TAG, "Connection state changed for unknown device, ignoring");
+6 −7
Original line number Original line Diff line number Diff line
@@ -24,7 +24,6 @@ import com.android.internal.annotations.VisibleForTesting;
/** Provides Bluetooth Hid Host profile, as a service in the Bluetooth application. */
/** Provides Bluetooth Hid Host profile, as a service in the Bluetooth application. */
public class HidHostNativeInterface {
public class HidHostNativeInterface {
    private static final String TAG = HidHostNativeInterface.class.getSimpleName();
    private static final String TAG = HidHostNativeInterface.class.getSimpleName();
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);


    private HidHostService mHidHostService;
    private HidHostService mHidHostService;


@@ -130,34 +129,34 @@ public class HidHostNativeInterface {
    /**********************************************************************************************/
    /**********************************************************************************************/


    private void onConnectStateChanged(byte[] address, int addressType, int transport, int state) {
    private void onConnectStateChanged(byte[] address, int addressType, int transport, int state) {
        if (DBG) Log.d(TAG, "onConnectStateChanged: state=" + state);
        Log.d(TAG, "onConnectStateChanged: state=" + state);
        mHidHostService.onConnectStateChanged(
        mHidHostService.onConnectStateChanged(
                address, addressType, transport, convertHalState(state));
                address, addressType, transport, convertHalState(state));
    }
    }


    private void onGetProtocolMode(byte[] address, int addressType, int transport, int mode) {
    private void onGetProtocolMode(byte[] address, int addressType, int transport, int mode) {
        if (DBG) Log.d(TAG, "onGetProtocolMode()");
        Log.d(TAG, "onGetProtocolMode()");
        mHidHostService.onGetProtocolMode(address, addressType, transport, mode);
        mHidHostService.onGetProtocolMode(address, addressType, transport, mode);
    }
    }


    private void onGetReport(
    private void onGetReport(
            byte[] address, int addressType, int transport, byte[] report, int rptSize) {
            byte[] address, int addressType, int transport, byte[] report, int rptSize) {
        if (DBG) Log.d(TAG, "onGetReport()");
        Log.d(TAG, "onGetReport()");
        mHidHostService.onGetReport(address, addressType, transport, report, rptSize);
        mHidHostService.onGetReport(address, addressType, transport, report, rptSize);
    }
    }


    private void onHandshake(byte[] address, int addressType, int transport, int status) {
    private void onHandshake(byte[] address, int addressType, int transport, int status) {
        if (DBG) Log.d(TAG, "onHandshake: status=" + status);
        Log.d(TAG, "onHandshake: status=" + status);
        mHidHostService.onHandshake(address, addressType, transport, status);
        mHidHostService.onHandshake(address, addressType, transport, status);
    }
    }


    private void onVirtualUnplug(byte[] address, int addressType, int transport, int status) {
    private void onVirtualUnplug(byte[] address, int addressType, int transport, int status) {
        if (DBG) Log.d(TAG, "onVirtualUnplug: status=" + status);
        Log.d(TAG, "onVirtualUnplug: status=" + status);
        mHidHostService.onVirtualUnplug(address, addressType, transport, status);
        mHidHostService.onVirtualUnplug(address, addressType, transport, status);
    }
    }


    private void onGetIdleTime(byte[] address, int addressType, int transport, int idleTime) {
    private void onGetIdleTime(byte[] address, int addressType, int transport, int idleTime) {
        if (DBG) Log.d(TAG, "onGetIdleTime()");
        Log.d(TAG, "onGetIdleTime()");
        mHidHostService.onGetIdleTime(address, addressType, transport, idleTime);
        mHidHostService.onGetIdleTime(address, addressType, transport, idleTime);
    }
    }


+30 −63
Original line number Original line Diff line number Diff line
@@ -63,7 +63,6 @@ import java.util.stream.Collectors;
 * the Bluetooth application.
 * the Bluetooth application.
 */
 */
public class HidHostService extends ProfileService {
public class HidHostService extends ProfileService {
    private static final boolean DBG = false;
    private static final String TAG = "BluetoothHidHostService";
    private static final String TAG = "BluetoothHidHostService";


    private static class InputDevice {
    private static class InputDevice {
@@ -167,14 +166,12 @@ public class HidHostService extends ProfileService {


    @Override
    @Override
    public void stop() {
    public void stop() {
        if (DBG) {
        Log.d(TAG, "Stopping Bluetooth HidHostService");
        Log.d(TAG, "Stopping Bluetooth HidHostService");
    }
    }
    }


    @Override
    @Override
    public void cleanup() {
    public void cleanup() {
        if (DBG) Log.d(TAG, "Stopping Bluetooth HidHostService");
        Log.d(TAG, "Stopping Bluetooth HidHostService");
        if (mNativeAvailable) {
        if (mNativeAvailable) {
            mNativeInterface.cleanup();
            mNativeInterface.cleanup();
            mNativeAvailable = false;
            mNativeAvailable = false;
@@ -291,9 +288,7 @@ public class HidHostService extends ProfileService {
    }
    }


    private static synchronized void setHidHostService(HidHostService instance) {
    private static synchronized void setHidHostService(HidHostService instance) {
        if (DBG) {
        Log.d(TAG, "setHidHostService(): set to: " + instance);
        Log.d(TAG, "setHidHostService(): set to: " + instance);
        }
        sHidHostService = instance;
        sHidHostService = instance;
    }
    }


@@ -357,7 +352,8 @@ public class HidHostService extends ProfileService {
            new Handler() {
            new Handler() {
                @Override
                @Override
                public void handleMessage(Message msg) {
                public void handleMessage(Message msg) {
                    if (DBG) Log.v(TAG, "handleMessage(): msg.what=" + msg.what);
                    Log.v(TAG, "handleMessage(): msg.what=" + msg.what);

                    switch (msg.what) {
                    switch (msg.what) {
                        case MESSAGE_CONNECT:
                        case MESSAGE_CONNECT:
                            handleMessageConnect(msg);
                            handleMessageConnect(msg);
@@ -1068,7 +1064,7 @@ public class HidHostService extends ProfileService {
     * @return true if connection request is passed down to mHandler.
     * @return true if connection request is passed down to mHandler.
     */
     */
    public boolean connect(BluetoothDevice device) {
    public boolean connect(BluetoothDevice device) {
        if (DBG) Log.d(TAG, "connect: " + device);
        Log.d(TAG, "connect: " + device);
        int state = getConnectionState(device);
        int state = getConnectionState(device);
        if (state != BluetoothProfile.STATE_DISCONNECTED) {
        if (state != BluetoothProfile.STATE_DISCONNECTED) {
            Log.e(TAG, "Hid Device not disconnected: " + device + ", state: " + state);
            Log.e(TAG, "Hid Device not disconnected: " + device + ", state: " + state);
@@ -1091,9 +1087,7 @@ public class HidHostService extends ProfileService {
     * @return true
     * @return true
     */
     */
    private boolean disconnect(BluetoothDevice device, int connectionPolicy) {
    private boolean disconnect(BluetoothDevice device, int connectionPolicy) {
        if (DBG) {
        Log.d(TAG, "disconnect: " + device + ", connection policy: " + connectionPolicy);
        Log.d(TAG, "disconnect: " + device + ", connection policy: " + connectionPolicy);
        }
        Message msg = mHandler.obtainMessage(MESSAGE_DISCONNECT, device);
        Message msg = mHandler.obtainMessage(MESSAGE_DISCONNECT, device);
        msg.arg1 = connectionPolicy;
        msg.arg1 = connectionPolicy;
        mHandler.sendMessage(msg);
        mHandler.sendMessage(msg);
@@ -1121,7 +1115,7 @@ public class HidHostService extends ProfileService {
     * {@link BluetoothProfile#STATE_DISCONNECTING} if this profile is being disconnected
     * {@link BluetoothProfile#STATE_DISCONNECTING} if this profile is being disconnected
     */
     */
    public int getConnectionState(BluetoothDevice device) {
    public int getConnectionState(BluetoothDevice device) {
        if (DBG) Log.d(TAG, "getConnectionState: " + device);
        Log.d(TAG, "getConnectionState: " + device);
        InputDevice inputDevice = mInputDevices.get(device);
        InputDevice inputDevice = mInputDevices.get(device);
        if (inputDevice != null) {
        if (inputDevice != null) {
            return inputDevice.getState();
            return inputDevice.getState();
@@ -1130,8 +1124,7 @@ public class HidHostService extends ProfileService {
    }
    }


    List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
    List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
        if (DBG) Log.d(TAG, "getDevicesMatchingConnectionStates()");
        Log.d(TAG, "getDevicesMatchingConnectionStates()");

        return mInputDevices.entrySet().stream()
        return mInputDevices.entrySet().stream()
                .filter(entry -> Ints.asList(states).contains(entry.getValue().getState()))
                .filter(entry -> Ints.asList(states).contains(entry.getValue().getState()))
                .map(Map.Entry::getKey)
                .map(Map.Entry::getKey)
@@ -1154,17 +1147,13 @@ public class HidHostService extends ProfileService {
     * @return true if connectionPolicy is set, false on error
     * @return true if connectionPolicy is set, false on error
     */
     */
    public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) {
    public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) {
        if (DBG) {
        Log.d(TAG, "setConnectionPolicy: " + device);
        Log.d(TAG, "setConnectionPolicy: " + device);
        }


        if (!mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.HID_HOST,
        if (!mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.HID_HOST,
                  connectionPolicy)) {
                  connectionPolicy)) {
            return false;
            return false;
        }
        }
        if (DBG) {
        Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }
        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            connect(device);
            connect(device);
        } else if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
        } else if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
@@ -1177,9 +1166,7 @@ public class HidHostService extends ProfileService {
     * @see BluetoothHidHost#setPreferredTransport
     * @see BluetoothHidHost#setPreferredTransport
     */
     */
    boolean setPreferredTransport(BluetoothDevice device, int transport) {
    boolean setPreferredTransport(BluetoothDevice device, int transport) {
        if (DBG) {
        Log.i(TAG, "setPreferredTransport: " + device + " transport: " + transport);
        Log.i(TAG, "setPreferredTransport: " + device + " transport: " + transport);
        }


        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
            Log.w(TAG, "Device not bonded" + device);
            Log.w(TAG, "Device not bonded" + device);
@@ -1215,9 +1202,7 @@ public class HidHostService extends ProfileService {
     * @return connection policy of the device
     * @return connection policy of the device
     */
     */
    public int getConnectionPolicy(BluetoothDevice device) {
    public int getConnectionPolicy(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "getConnectionPolicy: " + device);
        Log.d(TAG, "getConnectionPolicy: " + device);
        }
        return mDatabaseManager
        return mDatabaseManager
                .getProfileConnectionPolicy(device, BluetoothProfile.HID_HOST);
                .getProfileConnectionPolicy(device, BluetoothProfile.HID_HOST);
    }
    }
@@ -1226,9 +1211,7 @@ public class HidHostService extends ProfileService {
     * @see BluetoothHidHost#getPreferredTransport
     * @see BluetoothHidHost#getPreferredTransport
     */
     */
    int getPreferredTransport(BluetoothDevice device) {
    int getPreferredTransport(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "getPreferredTransport: " + device);
        Log.d(TAG, "getPreferredTransport: " + device);
        }


        // TODO: Access to mInputDevices should be protected in binder thread
        // TODO: Access to mInputDevices should be protected in binder thread
        return getTransport(device);
        return getTransport(device);
@@ -1236,9 +1219,7 @@ public class HidHostService extends ProfileService {


    /* The following APIs regarding test app for compliance */
    /* The following APIs regarding test app for compliance */
    boolean getProtocolMode(BluetoothDevice device) {
    boolean getProtocolMode(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "getProtocolMode: " + device);
        Log.d(TAG, "getProtocolMode: " + device);
        }
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1249,9 +1230,7 @@ public class HidHostService extends ProfileService {
    }
    }


    boolean virtualUnplug(BluetoothDevice device) {
    boolean virtualUnplug(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "virtualUnplug: " + device);
        Log.d(TAG, "virtualUnplug: " + device);
        }
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1262,9 +1241,7 @@ public class HidHostService extends ProfileService {
    }
    }


    boolean setProtocolMode(BluetoothDevice device, int protocolMode) {
    boolean setProtocolMode(BluetoothDevice device, int protocolMode) {
        if (DBG) {
        Log.d(TAG, "setProtocolMode: " + device);
        Log.d(TAG, "setProtocolMode: " + device);
        }
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1277,9 +1254,7 @@ public class HidHostService extends ProfileService {
    }
    }


    boolean getReport(BluetoothDevice device, byte reportType, byte reportId, int bufferSize) {
    boolean getReport(BluetoothDevice device, byte reportType, byte reportId, int bufferSize) {
        if (DBG) {
        Log.d(TAG, "getReport: " + device);
        Log.d(TAG, "getReport: " + device);
        }
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1296,9 +1271,7 @@ public class HidHostService extends ProfileService {
    }
    }


    boolean setReport(BluetoothDevice device, byte reportType, String report) {
    boolean setReport(BluetoothDevice device, byte reportType, String report) {
        if (DBG) {
        Log.d(TAG, "setReport: " + device);
        Log.d(TAG, "setReport: " + device);
        }
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1314,9 +1287,7 @@ public class HidHostService extends ProfileService {
    }
    }


    boolean sendData(BluetoothDevice device, String report) {
    boolean sendData(BluetoothDevice device, String report) {
        if (DBG) {
        Log.d(TAG, "sendData: " + device);
        Log.d(TAG, "sendData: " + device);
        }
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1330,7 +1301,7 @@ public class HidHostService extends ProfileService {
    }
    }


    boolean getIdleTime(BluetoothDevice device) {
    boolean getIdleTime(BluetoothDevice device) {
        if (DBG) Log.d(TAG, "getIdleTime: " + device);
        Log.d(TAG, "getIdleTime: " + device);
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1341,7 +1312,7 @@ public class HidHostService extends ProfileService {
    }
    }


    boolean setIdleTime(BluetoothDevice device, byte idleTime) {
    boolean setIdleTime(BluetoothDevice device, byte idleTime) {
        if (DBG) Log.d(TAG, "setIdleTime: " + device);
        Log.d(TAG, "setIdleTime: " + device);
        int state = this.getConnectionState(device);
        int state = this.getConnectionState(device);
        if (state != BluetoothProfile.STATE_CONNECTED) {
        if (state != BluetoothProfile.STATE_CONNECTED) {
            return false;
            return false;
@@ -1356,7 +1327,7 @@ public class HidHostService extends ProfileService {
    }
    }


    void onGetProtocolMode(byte[] address, int addressType, int transport, int mode) {
    void onGetProtocolMode(byte[] address, int addressType, int transport, int mode) {
        if (DBG) Log.d(TAG, "onGetProtocolMode()");
        Log.d(TAG, "onGetProtocolMode()");
        Message msg = mHandler.obtainMessage(MESSAGE_ON_GET_PROTOCOL_MODE);
        Message msg = mHandler.obtainMessage(MESSAGE_ON_GET_PROTOCOL_MODE);
        msg.obj = address;
        msg.obj = address;
        msg.arg1 = transport;
        msg.arg1 = transport;
@@ -1365,7 +1336,7 @@ public class HidHostService extends ProfileService {
    }
    }


    void onGetIdleTime(byte[] address, int addressType, int transport, int idleTime) {
    void onGetIdleTime(byte[] address, int addressType, int transport, int idleTime) {
        if (DBG) Log.d(TAG, "onGetIdleTime()");
        Log.d(TAG, "onGetIdleTime()");
        Message msg = mHandler.obtainMessage(MESSAGE_ON_GET_IDLE_TIME);
        Message msg = mHandler.obtainMessage(MESSAGE_ON_GET_IDLE_TIME);
        msg.obj = address;
        msg.obj = address;
        msg.arg1 = transport;
        msg.arg1 = transport;
@@ -1374,7 +1345,7 @@ public class HidHostService extends ProfileService {
    }
    }


    void onGetReport(byte[] address, int addressType, int transport, byte[] report, int rptSize) {
    void onGetReport(byte[] address, int addressType, int transport, byte[] report, int rptSize) {
        if (DBG) Log.d(TAG, "onGetReport()");
        Log.d(TAG, "onGetReport()");
        Message msg = mHandler.obtainMessage(MESSAGE_ON_GET_REPORT);
        Message msg = mHandler.obtainMessage(MESSAGE_ON_GET_REPORT);
        msg.obj = address;
        msg.obj = address;
        msg.arg1 = transport;
        msg.arg1 = transport;
@@ -1386,7 +1357,7 @@ public class HidHostService extends ProfileService {
    }
    }


    void onHandshake(byte[] address, int addressType, int transport, int status) {
    void onHandshake(byte[] address, int addressType, int transport, int status) {
        if (DBG) Log.d(TAG, "onHandshake: status=" + status);
        Log.d(TAG, "onHandshake: status=" + status);
        Message msg = mHandler.obtainMessage(MESSAGE_ON_HANDSHAKE);
        Message msg = mHandler.obtainMessage(MESSAGE_ON_HANDSHAKE);
        msg.obj = address;
        msg.obj = address;
        msg.arg1 = transport;
        msg.arg1 = transport;
@@ -1395,7 +1366,7 @@ public class HidHostService extends ProfileService {
    }
    }


    void onVirtualUnplug(byte[] address, int addressType, int transport, int status) {
    void onVirtualUnplug(byte[] address, int addressType, int transport, int status) {
        if (DBG) Log.d(TAG, "onVirtualUnplug: status=" + status);
        Log.d(TAG, "onVirtualUnplug: status=" + status);
        Message msg = mHandler.obtainMessage(MESSAGE_ON_VIRTUAL_UNPLUG);
        Message msg = mHandler.obtainMessage(MESSAGE_ON_VIRTUAL_UNPLUG);
        msg.obj = address;
        msg.obj = address;
        msg.arg1 = transport;
        msg.arg1 = transport;
@@ -1404,7 +1375,7 @@ public class HidHostService extends ProfileService {
    }
    }


    void onConnectStateChanged(byte[] address, int addressType, int transport, int state) {
    void onConnectStateChanged(byte[] address, int addressType, int transport, int state) {
        if (DBG) Log.d(TAG, "onConnectStateChanged: state=" + state);
        Log.d(TAG, "onConnectStateChanged: state=" + state);
        Message msg = mHandler.obtainMessage(MESSAGE_CONNECT_STATE_CHANGED, address);
        Message msg = mHandler.obtainMessage(MESSAGE_CONNECT_STATE_CHANGED, address);
        msg.arg1 = transport;
        msg.arg1 = transport;
        msg.arg2 = state;
        msg.arg2 = state;
@@ -1534,10 +1505,8 @@ public class HidHostService extends ProfileService {
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
                Utils.getTempAllowlistBroadcastOptions());
        if (DBG) {
        Log.d(TAG, "Protocol Mode (" + device + "): " + protocolMode);
        Log.d(TAG, "Protocol Mode (" + device + "): " + protocolMode);
    }
    }
    }


    private void broadcastReport(BluetoothDevice device, byte[] report, int rptSize) {
    private void broadcastReport(BluetoothDevice device, byte[] report, int rptSize) {
        Intent intent = new Intent(BluetoothHidHost.ACTION_REPORT);
        Intent intent = new Intent(BluetoothHidHost.ACTION_REPORT);
@@ -1565,10 +1534,8 @@ public class HidHostService extends ProfileService {
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
                Utils.getTempAllowlistBroadcastOptions());
        if (DBG) {
        Log.d(TAG, "Idle time (" + device + "): " + idleTime);
        Log.d(TAG, "Idle time (" + device + "): " + idleTime);
    }
    }
    }


    /**
    /**
     * Check whether can connect to a peer device.
     * Check whether can connect to a peer device.