diff --git a/res/layout/view_airplane_mode_networks_button.xml b/res/layout/view_airplane_mode_networks_button.xml
new file mode 100644
index 00000000000..0599274a169
--- /dev/null
+++ b/res/layout/view_airplane_mode_networks_button.xml
@@ -0,0 +1,46 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 7bd5971465f..09fc277ec85 100755
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -207,6 +207,9 @@
16dp
32dp
+
+ 24dp
+
16dp
4dp
diff --git a/res/values/strings.xml b/res/values/strings.xml
index de4e811a962..380ac931a08 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -12530,6 +12530,8 @@
network connection, internet, wireless, data, wifi, wi-fi, wi fi, cellular, mobile, cell carrier, 4g, 3g, 2g, lte
View airplane mode networks
+
+ Viewing airplane mode networks
Turn off airplane mode
diff --git a/res/xml/network_provider_settings.xml b/res/xml/network_provider_settings.xml
index c374469d754..7aac5bf037f 100644
--- a/res/xml/network_provider_settings.xml
+++ b/res/xml/network_provider_settings.xml
@@ -20,8 +20,13 @@
android:title="@string/provider_internet_settings"
settings:keywords="@string/keywords_wifi">
-
+
+
new SubSettingLauncher(getContext())
- .setDestination(ScanningSettings.class.getName())
- .setTitleRes(R.string.location_scanning_screen_title)
- .setSourceMetricsCategory(getMetricsCategory())
- .launch();
- mStatusMessagePreference.setText(title, description, clickListener);
- removeConnectedWifiEntryPreference();
- removeWifiEntryPreference();
- mStatusMessagePreference.setVisible(true);
- }
-
- private void addMessagePreference(int messageId) {
- mStatusMessagePreference.setTitle(messageId);
- mStatusMessagePreference.setVisible(true);
-
- }
-
protected void setProgressBarVisible(boolean visible) {
if (mProgressHeader != null) {
mProgressHeader.setVisibility(visible ? View.VISIBLE : View.GONE);
diff --git a/src/com/android/settings/network/ViewAirplaneModeNetworksLayoutPreferenceController.java b/src/com/android/settings/network/ViewAirplaneModeNetworksLayoutPreferenceController.java
new file mode 100644
index 00000000000..ec67fc05ff0
--- /dev/null
+++ b/src/com/android/settings/network/ViewAirplaneModeNetworksLayoutPreferenceController.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.AirplaneModeEnabler;
+import com.android.settings.R;
+import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.widget.LayoutPreference;
+
+/**
+ * This controls the airplane mode message and click button of the "View airplane mode networks"
+ * item on the Network & internet page.
+ */
+public class ViewAirplaneModeNetworksLayoutPreferenceController extends AbstractPreferenceController
+ implements LifecycleObserver, AirplaneModeEnabler.OnAirplaneModeChangedListener,
+ View.OnClickListener {
+
+ public static final String KEY = "view_airplane_mode_netwokrs_button";
+
+ private LayoutPreference mPreference;
+ @VisibleForTesting
+ TextView mTextView;
+ @VisibleForTesting
+ Button mButton;
+
+ private AirplaneModeEnabler mAirplaneModeEnabler;
+ private final WifiManager mWifiManager;
+ private final IntentFilter mIntentFilter;
+ private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
+ refreshLayout();
+ }
+ }
+ };
+
+ public ViewAirplaneModeNetworksLayoutPreferenceController(Context context,
+ Lifecycle lifecycle) {
+ super(context);
+ if (lifecycle == null) {
+ throw new IllegalArgumentException("Lifecycle must be set");
+ }
+ mAirplaneModeEnabler = new AirplaneModeEnabler(context, this);
+ mWifiManager = context.getSystemService(WifiManager.class);
+ mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
+ lifecycle.addObserver(this);
+ }
+
+ @Override
+ public boolean isAvailable() {
+ return mAirplaneModeEnabler.isAirplaneModeOn();
+ }
+
+ @Override
+ public String getPreferenceKey() {
+ return KEY;
+ }
+
+ @Override
+ public void displayPreference(PreferenceScreen screen) {
+ super.displayPreference(screen);
+ mPreference = screen.findPreference(getPreferenceKey());
+ if (isAvailable()) {
+ generateLayout();
+ }
+ }
+
+ /** Lifecycle.Event.ON_START */
+ @OnLifecycleEvent(Lifecycle.Event.ON_START)
+ public void onStart() {
+ mAirplaneModeEnabler.start();
+ }
+
+ /** Lifecycle.Event.ON_STOP */
+ @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
+ public void onStop() {
+ mAirplaneModeEnabler.stop();
+ }
+
+ /** Lifecycle.Event.ON_RESUME */
+ @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
+ public void onResume() {
+ mContext.registerReceiver(mBroadcastReceiver, mIntentFilter);
+ }
+
+ /** Lifecycle.Event.ON_PAUSE */
+ @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
+ public void onPause() {
+ mContext.unregisterReceiver(mBroadcastReceiver);
+ }
+
+ @Override
+ public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
+ if (mPreference != null) {
+ mPreference.setVisible(isAirplaneModeOn);
+ }
+ }
+
+ @Override
+ public void onClick(View v) {
+ mWifiManager.setWifiEnabled(true);
+ }
+
+ private void generateLayout() {
+ if (mPreference == null) {
+ return;
+ }
+ if (mTextView == null) {
+ mTextView = mPreference.findViewById(R.id.airplane_mode_text);
+ }
+ if (mButton == null) {
+ mButton = mPreference.findViewById(R.id.view_airplane_mode_networks_button);
+ }
+ if (mButton != null) {
+ mButton.setOnClickListener(this);
+ }
+ refreshLayout();
+ }
+
+ @VisibleForTesting
+ void refreshLayout() {
+ boolean isWifiEnabled = mWifiManager.isWifiEnabled();
+ if (mTextView != null) {
+ mTextView.setText(isWifiEnabled ? R.string.viewing_airplane_mode_networks
+ : R.string.condition_airplane_title);
+ }
+ if (mButton != null) {
+ mButton.setVisibility(isWifiEnabled ? View.GONE : View.VISIBLE);
+ }
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/ViewAirplaneModeNetworksLayoutPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/ViewAirplaneModeNetworksLayoutPreferenceControllerTest.java
new file mode 100644
index 00000000000..972e3fbd0c9
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/ViewAirplaneModeNetworksLayoutPreferenceControllerTest.java
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+import android.net.wifi.WifiManager;
+import android.os.Looper;
+import android.view.View;
+
+import androidx.preference.PreferenceManager;
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.settings.testutils.AirplaneModeRule;
+import com.android.settings.testutils.ResourcesUtils;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.widget.LayoutPreference;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+
+@RunWith(AndroidJUnit4.class)
+public class ViewAirplaneModeNetworksLayoutPreferenceControllerTest {
+
+ private static final String KEY = ViewAirplaneModeNetworksLayoutPreferenceController.KEY;
+ private static final String RES_ID_AIRPLANE_MODE_IS_ON = "condition_airplane_title";
+ private static final String RES_ID_VIEWING_AIRPLANE_MODE_NETWORKS =
+ "viewing_airplane_mode_networks";
+
+ @Rule
+ public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+ @Rule
+ public AirplaneModeRule mAirplaneModeRule = new AirplaneModeRule();
+ @Mock
+ private WifiManager mWifiManager;
+
+ private Context mContext;
+ private PreferenceScreen mScreen;
+ private LayoutPreference mPreference;
+ private ViewAirplaneModeNetworksLayoutPreferenceController mController;
+
+ @Before
+ public void setUp() {
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ doReturn(mWifiManager).when(mContext).getSystemService(Context.WIFI_SERVICE);
+
+ mController = new ViewAirplaneModeNetworksLayoutPreferenceController(mContext,
+ mock(Lifecycle.class));
+ if (Looper.myLooper() == null) {
+ Looper.prepare();
+ }
+ final PreferenceManager preferenceManager = new PreferenceManager(mContext);
+ mScreen = preferenceManager.createPreferenceScreen(mContext);
+ mPreference = new LayoutPreference(mContext,
+ ResourcesUtils.getResourcesId(
+ mContext, "layout", "view_airplane_mode_networks_button"));
+ mPreference.setKey(KEY);
+ mScreen.addPreference(mPreference);
+ }
+
+ @Test
+ public void isAvailable_airplaneModeOff_returnFalse() {
+ mAirplaneModeRule.setAirplaneMode(false);
+
+ assertThat(mController.isAvailable()).isFalse();
+ }
+
+ @Test
+ public void isAvailable_airplaneModeOn_returnTrue() {
+ mAirplaneModeRule.setAirplaneMode(true);
+
+ assertThat(mController.isAvailable()).isTrue();
+ }
+
+ @Test
+ public void displayPreference_wifiDisabled_showAirplaneModeIsOnButtonVisible() {
+ mAirplaneModeRule.setAirplaneMode(true);
+ doReturn(false).when(mWifiManager).isWifiEnabled();
+
+ mController.displayPreference(mScreen);
+
+ assertThat(mController.mTextView.getText())
+ .isEqualTo(ResourcesUtils.getResourcesString(mContext, RES_ID_AIRPLANE_MODE_IS_ON));
+ assertThat(mController.mButton.getVisibility()).isEqualTo(View.VISIBLE);
+ }
+
+ @Test
+ public void displayPreference_wifiEnabled_showViewingAirplaneModeNetworksButtonGone() {
+ mAirplaneModeRule.setAirplaneMode(true);
+ doReturn(true).when(mWifiManager).isWifiEnabled();
+
+ mController.displayPreference(mScreen);
+
+ assertThat(mController.mTextView.getText()).isEqualTo(
+ ResourcesUtils.getResourcesString(mContext, RES_ID_VIEWING_AIRPLANE_MODE_NETWORKS));
+ assertThat(mController.mButton.getVisibility()).isEqualTo(View.GONE);
+ }
+
+ @Test
+ public void refreshLayout_wifiEnabledThenDisabled_showAirplaneModeIsOnButtonVisible() {
+ mAirplaneModeRule.setAirplaneMode(true);
+ // Wi-Fi enabled
+ doReturn(true).when(mWifiManager).isWifiEnabled();
+ // Display preference
+ mController.displayPreference(mScreen);
+ // Then Wi-Fi disabled
+ doReturn(false).when(mWifiManager).isWifiEnabled();
+
+ // Refresh layout
+ mController.refreshLayout();
+
+ assertThat(mController.mTextView.getText())
+ .isEqualTo(ResourcesUtils.getResourcesString(mContext, RES_ID_AIRPLANE_MODE_IS_ON));
+ assertThat(mController.mButton.getVisibility()).isEqualTo(View.VISIBLE);
+ }
+
+ @Test
+ public void refreshLayout_wifiDisabledThenEnabled_showViewingAirplaneModeNetworksButtonGone() {
+ mAirplaneModeRule.setAirplaneMode(true);
+ // Wi-Fi disabled
+ doReturn(false).when(mWifiManager).isWifiEnabled();
+ // Display preference
+ mController.displayPreference(mScreen);
+ // Then Wi-Fi enabled
+ doReturn(true).when(mWifiManager).isWifiEnabled();
+
+ // Refresh layout
+ mController.refreshLayout();
+
+ assertThat(mController.mTextView.getText()).isEqualTo(
+ ResourcesUtils.getResourcesString(mContext, RES_ID_VIEWING_AIRPLANE_MODE_NETWORKS));
+ assertThat(mController.mButton.getVisibility()).isEqualTo(View.GONE);
+ }
+
+ @Test
+ public void onClick_shouldSetWifiEnabled() {
+ mAirplaneModeRule.setAirplaneMode(true);
+ doReturn(false).when(mWifiManager).isWifiEnabled();
+
+ mController.onClick(mock(View.class));
+
+ verify(mWifiManager).setWifiEnabled(true);
+ }
+}
diff --git a/tests/unit/src/com/android/settings/testutils/AirplaneModeRule.java b/tests/unit/src/com/android/settings/testutils/AirplaneModeRule.java
index 545f6ad89b8..459ac15de4e 100644
--- a/tests/unit/src/com/android/settings/testutils/AirplaneModeRule.java
+++ b/tests/unit/src/com/android/settings/testutils/AirplaneModeRule.java
@@ -16,9 +16,8 @@
package com.android.settings.testutils;
+import android.content.ContentResolver;
import android.content.Context;
-import android.content.Intent;
-import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
@@ -32,45 +31,41 @@ public final class AirplaneModeRule extends ExternalResource {
private static final String TAG = "AirplaneModeRule";
private Context mContext;
+ private ContentResolver mContentResolver;
private boolean mBackupValue;
private boolean mShouldRestore;
@Override
protected void before() throws Throwable {
mContext = ApplicationProvider.getApplicationContext();
+ mContentResolver = mContext.getContentResolver();
}
@Override
protected void after() {
- if (mShouldRestore) {
- Log.d(TAG, "Restore Airplane Mode value:" + mBackupValue);
- setAirplaneMode(mContext, mBackupValue);
+ if (!mShouldRestore) {
+ return;
}
+ Log.d(TAG, "Restore Airplane Mode value:" + mBackupValue);
+ Settings.Global.putInt(mContentResolver, Settings.Global.AIRPLANE_MODE_ON,
+ mBackupValue ? 1 : 0);
}
public void setAirplaneMode(boolean enable) {
- if (!mShouldRestore && isAirplaneModeOn() != enable) {
+ if (enable == isAirplaneModeOn()) {
+ return;
+ }
+ if (!mShouldRestore) {
mShouldRestore = true;
- mBackupValue = isAirplaneModeOn();
+ mBackupValue = !enable;
Log.d(TAG, "Backup Airplane Mode value:" + mBackupValue);
}
Log.d(TAG, "Set Airplane Mode enable:" + enable);
- setAirplaneMode(mContext, enable);
+ Settings.Global.putInt(mContentResolver, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0);
}
public boolean isAirplaneModeOn() {
return Settings.Global.getInt(mContext.getContentResolver(),
- Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
- }
-
- private static void setAirplaneMode(Context context, boolean enable) {
- // Change the system setting
- Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
- enable ? 1 : 0);
-
- // Post the intent
- final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
- intent.putExtra("state", enable);
- context.sendBroadcastAsUser(intent, UserHandle.ALL);
+ Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}