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

Commit a372e4b6 authored by chelseahao's avatar chelseahao
Browse files

Add auto on broadcast handling.

Bug: 316985153
Test: RunSettingsLibRoboTests
Change-Id: I505ed3944f07580c64c6f07ae3507844e65c171f
parent 267df17d
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -163,6 +163,16 @@ public interface BluetoothCallback {
    default void onAclConnectionStateChanged(
            @NonNull CachedBluetoothDevice cachedDevice, int state) {}

    /**
     * Called when the Auto-on state is changed for any user. Listens to intent
     * {@link android.bluetooth.BluetoothAdapter#ACTION_AUTO_ON_STATE_CHANGED }
     *
     * @param state        the Auto-on state, the possible values are:
     *                     {@link android.bluetooth.BluetoothAdapter#AUTO_ON_STATE_ENABLED},
     *                     {@link android.bluetooth.BluetoothAdapter#AUTO_ON_STATE_DISABLED}
     */
    default void onAutoOnStateChanged(int state) {}

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = { "STATE_" }, value = {
            STATE_DISCONNECTED,
+19 −0
Original line number Diff line number Diff line
@@ -133,6 +133,8 @@ public class BluetoothEventManager {
        addHandler(BluetoothDevice.ACTION_ACL_CONNECTED, new AclStateChangedHandler());
        addHandler(BluetoothDevice.ACTION_ACL_DISCONNECTED, new AclStateChangedHandler());

        addHandler(BluetoothAdapter.ACTION_AUTO_ON_STATE_CHANGED, new AutoOnStateChangedHandler());

        registerAdapterIntentReceiver();
    }

@@ -552,4 +554,21 @@ public class BluetoothEventManager {
            dispatchAudioModeChanged();
        }
    }

    private class AutoOnStateChangedHandler implements Handler {

        @Override
        public void onReceive(Context context, Intent intent, BluetoothDevice device) {
            String action = intent.getAction();
            if (action == null) {
                Log.w(TAG, "AutoOnStateChangedHandler() action is null");
                return;
            }
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_AUTO_ON_STATE,
                    BluetoothAdapter.ERROR);
            for (BluetoothCallback callback : mCallbacks) {
                callback.onAutoOnStateChanged(state);
            }
        }
    }
}
+14 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -489,4 +490,17 @@ public class BluetoothEventManagerTest {
        verify(mErrorListener).onShowError(any(Context.class), eq(DEVICE_NAME),
                eq(R.string.bluetooth_pairing_pin_error_message));
    }

    /**
     * Intent ACTION_AUTO_ON_STATE_CHANGED should dispatch to callback.
     */
    @Test
    public void intentWithExtraState_autoOnStateChangedShouldDispatchToRegisterCallback() {
        mBluetoothEventManager.registerCallback(mBluetoothCallback);
        mIntent = new Intent(BluetoothAdapter.ACTION_AUTO_ON_STATE_CHANGED);

        mContext.sendBroadcast(mIntent);

        verify(mBluetoothCallback).onAutoOnStateChanged(anyInt());
    }
}