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 c8f52f816c97b2d98909db6bbf698d5cc15e9bc1)
Merged-In: Ic9b6d44731684d1cfba87570d72dcacb905a66d2
diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java
index eaa0b96..df5cc72 100644
--- a/src/com/android/settings/bluetooth/BluetoothEnabler.java
+++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java
@@ -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.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 @@
     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 @@
         }
         mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
         mRestrictionUtils = restrictionUtils;
+        mSatelliteRepository = new SatelliteRepository(context);
     }
 
     public void setupSwitchController() {
@@ -112,6 +129,15 @@
         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 @@
             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)) {
diff --git a/src/com/android/settings/bluetooth/BluetoothPairingDetail.java b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
index 234d6d2..ca53854 100644
--- a/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
+++ b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
@@ -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.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 @@
     @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);
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothEnablerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothEnablerTest.java
index 4b82782..9052268 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothEnablerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothEnablerTest.java
@@ -34,6 +34,7 @@
 import android.content.IntentFilter;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.util.AndroidRuntimeException;
 import android.view.View;
 
 import androidx.preference.PreferenceViewHolder;
@@ -102,6 +103,19 @@
     }
 
     @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...
         mBluetoothEnabler.onSwitchToggled(false);