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

Commit aa93b84c authored by Hemant Gupta's avatar Hemant Gupta Committed by Linux Build Service Account
Browse files

Bluetooth: Fix to avoid framework reboot during monkey.

A case where monkey is running & sometimes framework is
disconnected due to unhandled java Exception during the
unbinding of HID device service on unsuccessful unbinding.
Handling java Illegal Argument exception while unbinding
HID device service solves this issue.

Change-Id: I61a5620e756119d25f2a235e67d6404b216f4cbf
CRs-Fixed: 601533
parent 2f517338
Loading
Loading
Loading
Loading
+36 −13
Original line number Original line Diff line number Diff line
@@ -148,18 +148,27 @@ public final class BluetoothHidDevice implements BluetoothProfile {


        public void onBluetoothStateChange(boolean up) {
        public void onBluetoothStateChange(boolean up) {
            Log.d(TAG, "onBluetoothStateChange: up=" + up);
            Log.d(TAG, "onBluetoothStateChange: up=" + up);

            synchronized (mConnection) {
            synchronized (mConnection) {
                if (!up) {
                if (!up) {
                    Log.d(TAG,"Unbinding service...");
                    if (mService != null) {
                        mService = null;
                        mService = null;
                        try {
                            mContext.unbindService(mConnection);
                            mContext.unbindService(mConnection);
                        } catch (IllegalArgumentException e) {
                            Log.e(TAG,"onBluetoothStateChange: could not unbind service:", e);
                        }
                    }
                } else {
                } else {
                    try {
                        if (mService == null) {
                        if (mService == null) {
                        Log.v(TAG, "Binding service");
                            Log.d(TAG,"Binding HID Device service...");
                        if (!mContext.bindService(new Intent(IBluetoothHidDevice.class.getName()),
                            doBind();
                            mConnection, 0)) {
                            Log.e(TAG, "Could not bind service");
                        }
                        }
                    } catch (IllegalStateException e) {
                        Log.e(TAG,"onBluetoothStateChange: could not bind to HID Dev service: ", e);
                    } catch (SecurityException e) {
                        Log.e(TAG,"onBluetoothStateChange: could not bind to HID Dev service: ", e);
                    }
                    }
                }
                }
            }
            }
@@ -191,7 +200,7 @@ public final class BluetoothHidDevice implements BluetoothProfile {
    };
    };


    BluetoothHidDevice(Context context, ServiceListener listener) {
    BluetoothHidDevice(Context context, ServiceListener listener) {
        Log.v(TAG, "BluetoothInputDevice()");
        Log.v(TAG, "BluetoothHidDevice");


        mContext = context;
        mContext = context;
        mServiceListener = listener;
        mServiceListener = listener;
@@ -206,10 +215,20 @@ public final class BluetoothHidDevice implements BluetoothProfile {
            }
            }
        }
        }


        if (!context.bindService(new Intent(IBluetoothHidDevice.class.getName()),
        doBind();
            mConnection, 0)) {
            Log.e(TAG, "Could not bind service");
    }
    }

    boolean doBind() {
        Intent intent = new Intent(IBluetoothHidDevice.class.getName());
        ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
        intent.setComponent(comp);
        if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
                android.os.Process.myUserHandle())) {
            Log.e(TAG, "Could not bind to Bluetooth HID Device Service with " + intent);
            return false;
        }
        Log.d(TAG, "Bound to HID Device Service");
        return true;
    }
    }


    void close() {
    void close() {
@@ -227,7 +246,11 @@ public final class BluetoothHidDevice implements BluetoothProfile {
        synchronized (mConnection) {
        synchronized (mConnection) {
            if (mService != null) {
            if (mService != null) {
                mService = null;
                mService = null;
                try {
                    mContext.unbindService(mConnection);
                    mContext.unbindService(mConnection);
                } catch (IllegalArgumentException e) {
                    Log.e(TAG,"close: could not unbind HID Dev service: ", e);
                }
           }
           }
        }
        }