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

Commit 123d59a7 authored by Weichin Weng's avatar Weichin Weng Committed by Gerrit Code Review
Browse files

Merge "Revert "DO NOT MERGE Hearing Aid: Use separate time for L/R in connect() and use whitelist""

parents a25a47f5 8cfb2eed
Loading
Loading
Loading
Loading
+0 −36
Original line number Diff line number Diff line
@@ -194,40 +194,6 @@ static jboolean disconnectHearingAidNative(JNIEnv* env, jobject object,
  return JNI_TRUE;
}

static jboolean addToWhiteListNative(JNIEnv* env, jobject object,
                                     jbyteArray address) {
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  if (!sHearingAidInterface) return JNI_FALSE;

  jbyte* addr = env->GetByteArrayElements(address, nullptr);
  if (!addr) {
    jniThrowIOException(env, EINVAL);
    return JNI_FALSE;
  }

  RawAddress* tmpraw = (RawAddress*)addr;
  sHearingAidInterface->AddToWhiteList(*tmpraw);
  env->ReleaseByteArrayElements(address, addr, 0);
  return JNI_TRUE;
}

static jboolean removeFromWhiteListNative(JNIEnv* env, jobject object,
                                          jbyteArray address) {
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  if (!sHearingAidInterface) return JNI_FALSE;

  jbyte* addr = env->GetByteArrayElements(address, nullptr);
  if (!addr) {
    jniThrowIOException(env, EINVAL);
    return JNI_FALSE;
  }

  RawAddress* tmpraw = (RawAddress*)addr;
  sHearingAidInterface->RemoveFromWhiteList(*tmpraw);
  env->ReleaseByteArrayElements(address, addr, 0);
  return JNI_TRUE;
}

static void setVolumeNative(JNIEnv* env, jclass clazz, jint volume) {
  if (!sHearingAidInterface) {
    LOG(ERROR) << __func__
@@ -243,8 +209,6 @@ static JNINativeMethod sMethods[] = {
    {"cleanupNative", "()V", (void*)cleanupNative},
    {"connectHearingAidNative", "([B)Z", (void*)connectHearingAidNative},
    {"disconnectHearingAidNative", "([B)Z", (void*)disconnectHearingAidNative},
    {"addToWhiteListNative", "([B)Z", (void*)addToWhiteListNative},
    {"removeFromWhiteListNative", "([B)Z", (void*)removeFromWhiteListNative},
    {"setVolumeNative", "(I)V", (void*)setVolumeNative},
};

+0 −24
Original line number Diff line number Diff line
@@ -104,28 +104,6 @@ public class HearingAidNativeInterface {
        return disconnectHearingAidNative(getByteAddress(device));
    }

    /**
     * Add a hearing aid device to white list.
     *
     * @param device the remote device
     * @return true on success, otherwise false.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public boolean addToWhiteList(BluetoothDevice device) {
        return addToWhiteListNative(getByteAddress(device));
    }

    /**
     * Remove a hearing aid device from white list.
     *
     * @param device the remote device
     * @return true on success, otherwise false.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public boolean removeFromWhiteList(BluetoothDevice device) {
        return removeFromWhiteListNative(getByteAddress(device));
    }

    /**
     * Sets the HearingAid volume
     * @param volume
@@ -190,7 +168,5 @@ public class HearingAidNativeInterface {
    private native void cleanupNative();
    private native boolean connectHearingAidNative(byte[] address);
    private native boolean disconnectHearingAidNative(byte[] address);
    private native boolean addToWhiteListNative(byte[] address);
    private native boolean removeFromWhiteListNative(byte[] address);
    private native void setVolumeNative(int volume);
}
+13 −22
Original line number Diff line number Diff line
@@ -58,10 +58,6 @@ public class HearingAidService extends ProfileService {
    // Upper limit of all HearingAid devices: Bonded or Connected
    private static final int MAX_HEARING_AID_STATE_MACHINES = 10;
    private static HearingAidService sHearingAidService;
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    static int sConnectTimeoutForEachSideMs = 8000;
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    static int sCheckWhitelistTimeoutMs = 16000;

    private AdapterService mAdapterService;
    private HandlerThread mStateMachinesThread;
@@ -254,6 +250,14 @@ public class HearingAidService extends ProfileService {
            }
        }

        synchronized (mStateMachines) {
            HearingAidStateMachine smConnect = getOrCreateStateMachine(device);
            if (smConnect == null) {
                Log.e(TAG, "Cannot connect to " + device + " : no state machine");
            }
            smConnect.sendMessage(HearingAidStateMachine.CONNECT);
        }

        for (BluetoothDevice storedDevice : mDeviceHiSyncIdMap.keySet()) {
            if (device.equals(storedDevice)) {
                continue;
@@ -266,27 +270,14 @@ public class HearingAidService extends ProfileService {
                        Log.e(TAG, "Ignored connect request for " + device + " : no state machine");
                        continue;
                    }
                    sm.sendMessage(HearingAidStateMachine.CONNECT,
                            sConnectTimeoutForEachSideMs);
                    sm.sendMessageDelayed(HearingAidStateMachine.CHECK_WHITELIST_CONNECTION,
                            sCheckWhitelistTimeoutMs);
                    sm.sendMessage(HearingAidStateMachine.CONNECT);
                }
                if (hiSyncId == BluetoothHearingAid.HI_SYNC_ID_INVALID
                        && !device.equals(storedDevice)) {
                    break;
                }
            }

        synchronized (mStateMachines) {
            HearingAidStateMachine smConnect = getOrCreateStateMachine(device);
            if (smConnect == null) {
                Log.e(TAG, "Cannot connect to " + device + " : no state machine");
            } else {
                smConnect.sendMessage(HearingAidStateMachine.CONNECT,
                        sConnectTimeoutForEachSideMs * 2);
                smConnect.sendMessageDelayed(HearingAidStateMachine.CHECK_WHITELIST_CONNECTION,
                        sCheckWhitelistTimeoutMs);
        }
        }

        return true;
    }

+10 −17
Original line number Diff line number Diff line
@@ -69,15 +69,13 @@ final class HearingAidStateMachine extends StateMachine {

    static final int CONNECT = 1;
    static final int DISCONNECT = 2;
    static final int CHECK_WHITELIST_CONNECTION = 3;
    @VisibleForTesting
    static final int STACK_EVENT = 101;
    private static final int CONNECT_TIMEOUT = 201;

    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    static int sConnectTimeoutMs = 16000;
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    static int sDisconnectTimeoutMs = 16000;
    // NOTE: the value is not "final" - it is modified in the unit tests
    @VisibleForTesting
    static int sConnectTimeoutMs = 30000;        // 30s

    private Disconnected mDisconnected;
    private Connecting mConnecting;
@@ -173,12 +171,6 @@ final class HearingAidStateMachine extends StateMachine {
                case DISCONNECT:
                    Log.w(TAG, "Disconnected: DISCONNECT ignored: " + mDevice);
                    break;
                case CHECK_WHITELIST_CONNECTION:
                    if (mService.getConnectedDevices().isEmpty()) {
                        log("No device connected, remove this device from white list");
                        mNativeInterface.removeFromWhiteList(mDevice);
                    }
                    break;
                case STACK_EVENT:
                    HearingAidStackEvent event = (HearingAidStackEvent) message.obj;
                    if (DBG) {
@@ -267,13 +259,14 @@ final class HearingAidStateMachine extends StateMachine {
                    deferMessage(message);
                    break;
                case CONNECT_TIMEOUT:
                    Log.w(TAG, "Connecting connection timeout: " + mDevice + ". Try whitelist");
                    Log.w(TAG, "Connecting connection timeout: " + mDevice);
                    mNativeInterface.disconnectHearingAid(mDevice);
                    mNativeInterface.addToWhiteList(mDevice);
                    transitionTo(mDisconnected);
                    break;
                case CHECK_WHITELIST_CONNECTION:
                    deferMessage(message);
                    HearingAidStackEvent disconnectEvent =
                            new HearingAidStackEvent(
                                    HearingAidStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
                    disconnectEvent.device = mDevice;
                    disconnectEvent.valueInt1 = HearingAidStackEvent.CONNECTION_STATE_DISCONNECTED;
                    sendMessage(STACK_EVENT, disconnectEvent);
                    break;
                case DISCONNECT:
                    log("Connecting: connection canceled to " + mDevice);
+1 −4
Original line number Diff line number Diff line
@@ -118,8 +118,6 @@ public class HearingAidServiceTest {
                .getBondState(any(BluetoothDevice.class));
        doReturn(new ParcelUuid[]{BluetoothUuid.HearingAid}).when(mAdapterService)
                .getRemoteUuids(any(BluetoothDevice.class));
        HearingAidService.sConnectTimeoutForEachSideMs = 1000;
        HearingAidService.sCheckWhitelistTimeoutMs = 2000;
    }

    @After
@@ -362,8 +360,7 @@ public class HearingAidServiceTest {
                mService.getConnectionState(mLeftDevice));

        // Verify the connection state broadcast, and that we are in Disconnected state
        verifyConnectionStateIntent(HearingAidService.sConnectTimeoutForEachSideMs * 3,
                mLeftDevice,
        verifyConnectionStateIntent(HearingAidStateMachine.sConnectTimeoutMs * 2, mLeftDevice,
                BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTING);
        Assert.assertEquals(BluetoothProfile.STATE_DISCONNECTED,
Loading