Show dialog on BT entries in Settings
- When satellite mode is switched on BT entries shall show the warning dialog and avoid user uses these functions.
Bug: 337154438
Test: Manual test.
Test: atest pass
Change-Id: Ic9b6d44731684d1cfba87570d72dcacb905a66d2
(cherry picked from commit c8f52f816c
)
Merged-In: Ic9b6d44731684d1cfba87570d72dcacb905a66d2
This commit is contained in:
@@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
package com.android.settings.bluetooth;
|
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.bluetooth.BluetoothAdapter;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -23,22 +26,32 @@ import android.content.Intent;
|
|||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import com.android.settings.R;
|
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.settings.widget.SwitchWidgetController;
|
||||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||||
import com.android.settingslib.WirelessUtils;
|
import com.android.settingslib.WirelessUtils;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
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
|
* BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
|
||||||
* preference. It turns on/off Bluetooth and ensures the summary of the
|
* preference. It turns on/off Bluetooth and ensures the summary of the
|
||||||
* preference reflects the current state.
|
* preference reflects the current state.
|
||||||
*/
|
*/
|
||||||
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
|
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
|
||||||
|
private static final String TAG = BluetoothEnabler.class.getSimpleName();
|
||||||
private final SwitchWidgetController mSwitchController;
|
private final SwitchWidgetController mSwitchController;
|
||||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||||
private Context mContext;
|
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 String EVENT_DATA_IS_BT_ON = "is_bluetooth_on";
|
||||||
private static final int EVENT_UPDATE_INDEX = 0;
|
private static final int EVENT_UPDATE_INDEX = 0;
|
||||||
private final int mMetricsEvent;
|
private final int mMetricsEvent;
|
||||||
|
private SatelliteRepository mSatelliteRepository;
|
||||||
|
@VisibleForTesting
|
||||||
|
AtomicBoolean mIsSatelliteOn = new AtomicBoolean(false);
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
@@ -81,6 +97,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
}
|
}
|
||||||
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
|
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
|
||||||
mRestrictionUtils = restrictionUtils;
|
mRestrictionUtils = restrictionUtils;
|
||||||
|
mSatelliteRepository = new SatelliteRepository(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupSwitchController() {
|
public void setupSwitchController() {
|
||||||
@@ -112,6 +129,15 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
mContext.registerReceiver(mReceiver, mIntentFilter,
|
mContext.registerReceiver(mReceiver, mIntentFilter,
|
||||||
Context.RECEIVER_EXPORTED_UNAUDITED);
|
Context.RECEIVER_EXPORTED_UNAUDITED);
|
||||||
mValidListener = true;
|
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() {
|
public void pause() {
|
||||||
@@ -168,6 +194,17 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
return true;
|
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
|
// Show toast message if Bluetooth is not allowed in airplane mode
|
||||||
if (isChecked &&
|
if (isChecked &&
|
||||||
!WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
|
!WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
|
||||||
|
@@ -16,10 +16,15 @@
|
|||||||
|
|
||||||
package com.android.settings.bluetooth;
|
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.app.settings.SettingsEnums;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
@@ -27,10 +32,17 @@ import androidx.annotation.Nullable;
|
|||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import com.android.settings.R;
|
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.bluetooth.BluetoothDeviceFilter;
|
||||||
import com.android.settingslib.search.Indexable;
|
import com.android.settingslib.search.Indexable;
|
||||||
import com.android.settingslib.widget.FooterPreference;
|
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.
|
* BluetoothPairingDetail is a page to scan bluetooth devices and pair them.
|
||||||
*/
|
*/
|
||||||
@@ -55,9 +67,35 @@ public class BluetoothPairingDetail extends BluetoothDevicePairingDetailBase imp
|
|||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
|
if (mayStartSatelliteWarningDialog()) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
|
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
|
@Override
|
||||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
@@ -34,6 +34,7 @@ import android.content.Intent;
|
|||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
import android.util.AndroidRuntimeException;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
@@ -101,6 +102,19 @@ public class BluetoothEnablerTest {
|
|||||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
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
|
@Test
|
||||||
public void onSwitchToggled_shouldLogActionWithSuppliedEvent() {
|
public void onSwitchToggled_shouldLogActionWithSuppliedEvent() {
|
||||||
// WHEN the switch is toggled...
|
// WHEN the switch is toggled...
|
||||||
|
Reference in New Issue
Block a user