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

Commit 74f5d8c1 authored by Tom Hsu's avatar Tom Hsu Committed by Android (Google) Code Review
Browse files

Merge "Show dialog on BT entries in Settings" into main

parents 539aee42 c8f52f81
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.settings.bluetooth;

import static com.android.settings.network.SatelliteWarningDialogActivity.EXTRA_TYPE_OF_SATELLITE_WARNING_DIALOG;
import static com.android.settings.network.SatelliteWarningDialogActivity.TYPE_IS_BLUETOOTH;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -23,22 +26,32 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.VisibleForTesting;

import com.android.settings.R;
import com.android.settings.network.SatelliteRepository;
import com.android.settings.network.SatelliteWarningDialogActivity;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.WirelessUtils;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
 * preference. It turns on/off Bluetooth and ensures the summary of the
 * preference reflects the current state.
 */
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
    private static final String TAG = BluetoothEnabler.class.getSimpleName();
    private final SwitchWidgetController mSwitchController;
    private final MetricsFeatureProvider mMetricsFeatureProvider;
    private Context mContext;
@@ -51,6 +64,9 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
    private static final String EVENT_DATA_IS_BT_ON = "is_bluetooth_on";
    private static final int EVENT_UPDATE_INDEX = 0;
    private final int mMetricsEvent;
    private SatelliteRepository mSatelliteRepository;
    @VisibleForTesting
    AtomicBoolean mIsSatelliteOn = new AtomicBoolean(false);

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
@@ -81,6 +97,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
        }
        mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        mRestrictionUtils = restrictionUtils;
        mSatelliteRepository = new SatelliteRepository(context);
    }

    public void setupSwitchController() {
@@ -112,6 +129,15 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
        mContext.registerReceiver(mReceiver, mIntentFilter,
                Context.RECEIVER_EXPORTED_UNAUDITED);
        mValidListener = true;

        new Thread(() -> {
            try {
                mIsSatelliteOn.set(mSatelliteRepository.requestIsEnabled(
                        Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS));
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                Log.e(TAG, "Error to get satellite status : " + e);
            }
        }).start();
    }

    public void pause() {
@@ -168,6 +194,17 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
            return true;
        }

        if (mIsSatelliteOn.get()) {
            mContext.startActivity(
                    new Intent(mContext, SatelliteWarningDialogActivity.class)
                            .putExtra(
                                    EXTRA_TYPE_OF_SATELLITE_WARNING_DIALOG,
                                    TYPE_IS_BLUETOOTH)
            );
            mSwitchController.setChecked(!isChecked);
            return false;
        }

        // Show toast message if Bluetooth is not allowed in airplane mode
        if (isChecked &&
                !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
+38 −0
Original line number Diff line number Diff line
@@ -16,10 +16,15 @@

package com.android.settings.bluetooth;

import static com.android.settings.network.SatelliteWarningDialogActivity.EXTRA_TYPE_OF_SATELLITE_WARNING_DIALOG;
import static com.android.settings.network.SatelliteWarningDialogActivity.TYPE_IS_BLUETOOTH;

import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
@@ -27,10 +32,17 @@ import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import com.android.settings.R;
import com.android.settings.network.SatelliteRepository;
import com.android.settings.network.SatelliteWarningDialogActivity;
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.widget.FooterPreference;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * BluetoothPairingDetail is a page to scan bluetooth devices and pair them.
 */
@@ -55,9 +67,35 @@ public class BluetoothPairingDetail extends BluetoothDevicePairingDetailBase imp
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (mayStartSatelliteWarningDialog()) {
            finish();
            return;
        }
        use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
    }

    private boolean mayStartSatelliteWarningDialog() {
        SatelliteRepository satelliteRepository = new SatelliteRepository(this.getContext());
        boolean isSatelliteOn = true;
        try {
            isSatelliteOn =
                    satelliteRepository.requestIsEnabled(
                            Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            Log.e(TAG, "Error to get satellite status : " + e);
        }
        if (!isSatelliteOn) {
            return false;
        }
        startActivity(
                new Intent(getContext(), SatelliteWarningDialogActivity.class)
                        .putExtra(
                                EXTRA_TYPE_OF_SATELLITE_WARNING_DIALOG,
                                TYPE_IS_BLUETOOTH)
        );
        return true;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
+14 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.AndroidRuntimeException;
import android.view.View;

import androidx.preference.PreferenceViewHolder;
@@ -101,6 +102,19 @@ public class BluetoothEnablerTest {
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
    }

    @Test
    public void onSwitchToggled_satelliteOn_showWarningDialog() {
        mBluetoothEnabler.mIsSatelliteOn.set(true);

        try {
            mBluetoothEnabler.onSwitchToggled(true);
        } catch (AndroidRuntimeException e) {
            // Catch exception of starting activity .
        }

        verify(mContext).startActivity(any());
    }

    @Test
    public void onSwitchToggled_shouldLogActionWithSuppliedEvent() {
        // WHEN the switch is toggled...