Merge "[Provider Model] View airplane mode networks button"
This commit is contained in:
46
res/layout/view_airplane_mode_networks_button.xml
Normal file
46
res/layout/view_airplane_mode_networks_button.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/airplane_mode_text"
|
||||
android:text="@string/condition_airplane_title"
|
||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginStart="@dimen/content_margin_left"
|
||||
android:layout_marginEnd="@dimen/content_margin_left"
|
||||
android:layout_marginTop="@dimen/view_airplane_mode_networks_button_margin_vertical"
|
||||
android:layout_marginBottom="@dimen/view_airplane_mode_networks_button_margin_vertical"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/view_airplane_mode_networks_button"
|
||||
android:text="@string/view_airplane_safe_networks"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginStart="@dimen/content_margin_left"
|
||||
android:layout_marginEnd="@dimen/content_margin_left"
|
||||
android:layout_marginBottom="@dimen/view_airplane_mode_networks_button_margin_vertical"
|
||||
style="@style/ActionPrimaryButton"/>
|
||||
</LinearLayout>
|
@@ -207,6 +207,9 @@
|
||||
<dimen name="wifi_ap_band_checkbox_padding">16dp</dimen>
|
||||
<dimen name="wifi_detail_page_header_image_size">32dp</dimen>
|
||||
|
||||
<!-- Internet Preferences -->
|
||||
<dimen name="view_airplane_mode_networks_button_margin_vertical">24dp</dimen>
|
||||
|
||||
<!-- Color picker -->
|
||||
<dimen name="color_swatch_size">16dp</dimen>
|
||||
<dimen name="color_swatch_stroke_width">4dp</dimen>
|
||||
|
@@ -12532,6 +12532,8 @@
|
||||
<string name="keywords_internet">network connection, internet, wireless, data, wifi, wi-fi, wi fi, cellular, mobile, cell carrier, 4g, 3g, 2g, lte</string>
|
||||
<!-- Label text to view airplane-safe networks. [CHAR LIMIT=40] -->
|
||||
<string name="view_airplane_safe_networks">View airplane mode networks</string>
|
||||
<!-- Text of message for viewing the networks that are available in airplane mode. [CHAR LIMIT=60] -->
|
||||
<string name="viewing_airplane_mode_networks">Viewing airplane mode networks</string>
|
||||
<!-- Label text to turn off airplane mode. [CHAR LIMIT=40] -->
|
||||
<string name="turn_off_airplane_mode">Turn off airplane mode</string>
|
||||
|
||||
|
@@ -20,8 +20,13 @@
|
||||
android:title="@string/provider_internet_settings"
|
||||
settings:keywords="@string/keywords_wifi">
|
||||
|
||||
<com.android.settings.wifi.LinkablePreference
|
||||
android:key="wifi_status_message"/>
|
||||
<!-- View airplane mode networks button -->
|
||||
<com.android.settingslib.widget.LayoutPreference
|
||||
android:key="view_airplane_mode_netwokrs_button"
|
||||
android:title="@string/condition_airplane_title"
|
||||
android:selectable="false"
|
||||
android:layout="@layout/view_airplane_mode_networks_button"
|
||||
settings:allowDividerBelow="true"/>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="connected_access_point"
|
||||
|
@@ -50,20 +50,17 @@ import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.android.settings.LinkifyUtils;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.RestrictedSettingsFragment;
|
||||
import com.android.settings.core.FeatureFlags;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.datausage.DataUsagePreference;
|
||||
import com.android.settings.datausage.DataUsageUtils;
|
||||
import com.android.settings.location.ScanningSettings;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.wifi.AddNetworkFragment;
|
||||
import com.android.settings.wifi.AddWifiNetworkPreference;
|
||||
import com.android.settings.wifi.ConfigureWifiEntryFragment;
|
||||
import com.android.settings.wifi.ConnectedWifiEntryPreference;
|
||||
import com.android.settings.wifi.LinkablePreference;
|
||||
import com.android.settings.wifi.WifiConfigUiBase2;
|
||||
import com.android.settings.wifi.WifiConnectListener;
|
||||
import com.android.settings.wifi.WifiDialog2;
|
||||
@@ -118,7 +115,6 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
|
||||
private static final String PREF_KEY_ACCESS_POINTS = "access_points";
|
||||
private static final String PREF_KEY_CONFIGURE_WIFI_SETTINGS = "configure_wifi_settings";
|
||||
private static final String PREF_KEY_SAVED_NETWORKS = "saved_networks";
|
||||
private static final String PREF_KEY_STATUS_MESSAGE = "wifi_status_message";
|
||||
@VisibleForTesting
|
||||
static final String PREF_KEY_DATA_USAGE = "non_carrier_data_usage";
|
||||
|
||||
@@ -192,7 +188,8 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
|
||||
Preference mSavedNetworksPreference;
|
||||
@VisibleForTesting
|
||||
DataUsagePreference mDataUsagePreference;
|
||||
private LinkablePreference mStatusMessagePreference;
|
||||
private ViewAirplaneModeNetworksLayoutPreferenceController
|
||||
mViewAirplaneModeNetworksButtonPreference;
|
||||
|
||||
/**
|
||||
* Mobile networks list for provider model
|
||||
@@ -242,13 +239,13 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
|
||||
mConfigureWifiSettingsPreference = findPreference(PREF_KEY_CONFIGURE_WIFI_SETTINGS);
|
||||
mSavedNetworksPreference = findPreference(PREF_KEY_SAVED_NETWORKS);
|
||||
mAddWifiNetworkPreference = new AddWifiNetworkPreference(getPrefContext());
|
||||
mStatusMessagePreference = findPreference(PREF_KEY_STATUS_MESSAGE);
|
||||
mDataUsagePreference = findPreference(PREF_KEY_DATA_USAGE);
|
||||
mDataUsagePreference.setVisible(DataUsageUtils.hasWifiRadio(getContext()));
|
||||
mDataUsagePreference.setTemplate(NetworkTemplate.buildTemplateWifiWildcard(),
|
||||
0 /*subId*/,
|
||||
null /*service*/);
|
||||
addNetworkMobileProviderController();
|
||||
addViewAirplaneModeNetworksButtonController();
|
||||
}
|
||||
|
||||
private void addNetworkMobileProviderController() {
|
||||
@@ -260,6 +257,15 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
|
||||
mNetworkMobileProviderController.displayPreference(getPreferenceScreen());
|
||||
}
|
||||
|
||||
private void addViewAirplaneModeNetworksButtonController() {
|
||||
if (mViewAirplaneModeNetworksButtonPreference == null) {
|
||||
mViewAirplaneModeNetworksButtonPreference =
|
||||
new ViewAirplaneModeNetworksLayoutPreferenceController(
|
||||
getContext(), getSettingsLifecycle());
|
||||
}
|
||||
mViewAirplaneModeNetworksButtonPreference.displayPreference(getPreferenceScreen());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
@@ -602,18 +608,17 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
|
||||
case WifiManager.WIFI_STATE_ENABLING:
|
||||
removeConnectedWifiEntryPreference();
|
||||
removeWifiEntryPreference();
|
||||
addMessagePreference(R.string.wifi_starting);
|
||||
setProgressBarVisible(true);
|
||||
break;
|
||||
|
||||
case WifiManager.WIFI_STATE_DISABLING:
|
||||
removeConnectedWifiEntryPreference();
|
||||
removeWifiEntryPreference();
|
||||
addMessagePreference(R.string.wifi_stopping);
|
||||
break;
|
||||
|
||||
case WifiManager.WIFI_STATE_DISABLED:
|
||||
setOffMessage();
|
||||
removeConnectedWifiEntryPreference();
|
||||
removeWifiEntryPreference();
|
||||
setAdditionalSettingsSummaries();
|
||||
setProgressBarVisible(false);
|
||||
mClickedConnect = false;
|
||||
@@ -683,7 +688,6 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
|
||||
}
|
||||
|
||||
boolean hasAvailableWifiEntries = false;
|
||||
mStatusMessagePreference.setVisible(false);
|
||||
mWifiEntryPreferenceCategory.setVisible(true);
|
||||
|
||||
final WifiEntry connectedEntry = mWifiPickerTracker.getConnectedWifiEntry();
|
||||
@@ -861,33 +865,6 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
|
||||
&& !powerManager.isPowerSaveMode();
|
||||
}
|
||||
|
||||
private void setOffMessage() {
|
||||
final CharSequence title = getText(R.string.wifi_empty_list_wifi_off);
|
||||
// Don't use WifiManager.isScanAlwaysAvailable() to check the Wi-Fi scanning mode. Instead,
|
||||
// read the system settings directly. Because when the device is in Airplane mode, even if
|
||||
// Wi-Fi scanning mode is on, WifiManager.isScanAlwaysAvailable() still returns "off".
|
||||
// TODO(b/149421497): Fix this?
|
||||
final boolean wifiScanningMode = mWifiManager.isScanAlwaysAvailable();
|
||||
final CharSequence description = wifiScanningMode ? getText(R.string.wifi_scan_notify_text)
|
||||
: getText(R.string.wifi_scan_notify_text_scanning_off);
|
||||
final LinkifyUtils.OnClickListener clickListener =
|
||||
() -> 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);
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user