Refactor add network page

WifiDialog.java can be launched as "full screen mode"
in some use cases. However the way it's done is that
it uses theme to fake the full screen appearance.
It just feels hacky to use a dialog for full screen UI.
So, we created an "AddNetworkFragment" to make this
page can look like a normal fragment.

Also, clean up some useless code about "full screen mode"
of WifiDialog.

Change-Id: Iedd04c6a8e403cbceb872313314e1cba0d514246
Fixes: 111875856
Test: robo test, manual test
This commit is contained in:
tmfang
2018-08-10 00:25:47 +08:00
parent a439fec1c8
commit db00a3177a
9 changed files with 402 additions and 53 deletions

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 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.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/add_network_button_bar"
android:layout_alignParentTop="true"
layout="@layout/wifi_dialog"/>
<include
android:id="@+id/add_network_button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="@*android:layout/alert_dialog_button_bar_material"/>
</RelativeLayout>

View File

@@ -174,21 +174,4 @@
<item name="android:windowLightStatusBar">true</item> <item name="android:windowLightStatusBar">true</item>
</style> </style>
<!--TODO(b/111875856) This theme will be useless, when we add real activity/fragment to handle the full screen for WifiDialog -->
<style name="Theme.Settings.WifiDialogFullScreen" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Color names copied from frameworks/base/core/res/res/values/themes_device_defaults.xml -->
<item name="colorPrimary">@*android:color/primary_device_default_settings_light</item>
<item name="colorPrimaryDark">@*android:color/primary_dark_device_default_settings_light</item>
<item name="colorAccent">@*android:color/accent_device_default_light</item>
<!-- Add white nav bar with divider that matches material -->
<item name="android:navigationBarDividerColor">@color/ripple_material_light</item>
<item name="android:navigationBarColor">@android:color/white</item>
<item name="android:windowLightNavigationBar">true</item>
<item name="android:windowLightStatusBar">true</item>
<!-- For AndroidX AlertDialog -->
<item name="alertDialogTheme">@style/Theme.AlertDialog</item>
</style>
</resources> </resources>

View File

@@ -0,0 +1,159 @@
/*
* Copyright (C) 2018 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.wifi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.core.InstrumentedFragment;
public class AddNetworkFragment extends InstrumentedFragment implements WifiConfigUiBase,
View.OnClickListener {
final static String WIFI_CONFIG_KEY = "wifi_config_key";
@VisibleForTesting
final static int SUBMIT_BUTTON_ID = android.R.id.button1;
@VisibleForTesting
final static int CANCEL_BUTTON_ID = android.R.id.button2;
private WifiConfigController mUIController;
private Button mSubmitBtn;
private Button mCancelBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public int getMetricsCategory() {
return MetricsEvent.SETTINGS_WIFI_ADD_NETWORK;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.wifi_add_network_view, container, false);
mSubmitBtn = rootView.findViewById(SUBMIT_BUTTON_ID);
mCancelBtn = rootView.findViewById(CANCEL_BUTTON_ID);
mSubmitBtn.setOnClickListener(this);
mCancelBtn.setOnClickListener(this);
mUIController = new WifiConfigController(this, rootView, null, getMode());
return rootView;
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
mUIController.updatePassword();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case SUBMIT_BUTTON_ID:
handleSubmitAction();
break;
case CANCEL_BUTTON_ID:
handleCancelAction();
break;
}
}
@Override
public int getMode() {
return WifiConfigUiBase.MODE_CONNECT;
}
@Override
public WifiConfigController getController() {
return mUIController;
}
@Override
public void dispatchSubmit() {
handleSubmitAction();
}
@Override
public void setTitle(int id) {
getActivity().setTitle(id);
}
@Override
public void setTitle(CharSequence title) {
getActivity().setTitle(title);
}
@Override
public void setSubmitButton(CharSequence text) {
mSubmitBtn.setText(text);
}
@Override
public void setCancelButton(CharSequence text) {
mCancelBtn.setText(text);
}
@Override
public void setForgetButton(CharSequence text) {
// AddNetwork doesn't need forget button.
}
@Override
public Button getSubmitButton() {
return mSubmitBtn;
}
@Override
public Button getCancelButton() {
return mCancelBtn;
}
@Override
public Button getForgetButton() {
// AddNetwork doesn't need forget button.
return null;
}
@VisibleForTesting
void handleSubmitAction() {
final Intent intent = new Intent();
final Activity activity = getActivity();
intent.putExtra(WIFI_CONFIG_KEY, mUIController.getConfig());
activity.setResult(Activity.RESULT_OK, intent);
activity.finish();
}
@VisibleForTesting
void handleCancelAction() {
final Activity activity = getActivity();
activity.setResult(Activity.RESULT_CANCELED);
activity.finish();
}
}

View File

@@ -50,14 +50,6 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase,
private WifiConfigController mController; private WifiConfigController mController;
private boolean mHideSubmitButton; private boolean mHideSubmitButton;
// TODO(b/111875856) WifiDialog should not mimic full screen UI.
/** Creates a WifiDialog with fullscreen style. It displays in fullscreen mode. */
public static WifiDialog createFullscreen(Context context, WifiDialogListener listener,
AccessPoint accessPoint, int mode) {
return new WifiDialog(context, listener, accessPoint, mode,
R.style.Theme_Settings_WifiDialogFullScreen, false /* hideSubmitButton */);
}
/** /**
* Creates a WifiDialog with no additional style. It displays as a dialog above the current * Creates a WifiDialog with no additional style. It displays as a dialog above the current
* view. * view.

View File

@@ -100,6 +100,9 @@ public class WifiSettings extends RestrictedSettingsFragment
public static final int WIFI_DIALOG_ID = 1; public static final int WIFI_DIALOG_ID = 1;
private static final int WRITE_NFC_DIALOG_ID = 6; private static final int WRITE_NFC_DIALOG_ID = 6;
@VisibleForTesting
static final int ADD_NETWORK_REQUEST = 2;
// Instance state keys // Instance state keys
private static final String SAVE_DIALOG_MODE = "dialog_mode"; private static final String SAVE_DIALOG_MODE = "dialog_mode";
private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state"; private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state";
@@ -416,6 +419,12 @@ public class WifiSettings extends RestrictedSettingsFragment
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
// Only handle request comes from AddNetworkFragment
if (requestCode == ADD_NETWORK_REQUEST) {
handleAddNetworkRequest(resultCode, data);
return;
}
final boolean formerlyRestricted = mIsRestricted; final boolean formerlyRestricted = mIsRestricted;
mIsRestricted = isUiRestricted(); mIsRestricted = isUiRestricted();
if (formerlyRestricted && !mIsRestricted if (formerlyRestricted && !mIsRestricted
@@ -590,22 +599,15 @@ public class WifiSettings extends RestrictedSettingsFragment
public Dialog onCreateDialog(int dialogId) { public Dialog onCreateDialog(int dialogId) {
switch (dialogId) { switch (dialogId) {
case WIFI_DIALOG_ID: case WIFI_DIALOG_ID:
if (mDlgAccessPoint == null && mAccessPointSavedState == null) { // modify network
// add new network if (mDlgAccessPoint == null && mAccessPointSavedState != null) {
mDialog = WifiDialog // restore AP from save state
.createFullscreen(getActivity(), this, mDlgAccessPoint, mDialogMode); mDlgAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState);
} else { // Reset the saved access point data
// modify network mAccessPointSavedState = null;
if (mDlgAccessPoint == null) {
// restore AP from save state
mDlgAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState);
// Reset the saved access point data
mAccessPointSavedState = null;
}
mDialog = WifiDialog
.createModal(getActivity(), this, mDlgAccessPoint, mDialogMode);
} }
mDialog = WifiDialog
.createModal(getActivity(), this, mDlgAccessPoint, mDialogMode);
mSelectedAccessPoint = mDlgAccessPoint; mSelectedAccessPoint = mDlgAccessPoint;
return mDialog; return mDialog;
case WRITE_NFC_DIALOG_ID: case WRITE_NFC_DIALOG_ID:
@@ -928,6 +930,15 @@ public class WifiSettings extends RestrictedSettingsFragment
} }
} }
private void launchAddNetworkFragment() {
new SubSettingLauncher(getContext())
.setTitleRes(R.string.wifi_add_network)
.setDestination(AddNetworkFragment.class.getName())
.setSourceMetricsCategory(getMetricsCategory())
.setResultListener(this, ADD_NETWORK_REQUEST)
.launch();
}
private void launchNetworkDetailsFragment(ConnectedAccessPointPreference pref) { private void launchNetworkDetailsFragment(ConnectedAccessPointPreference pref) {
new SubSettingLauncher(getContext()) new SubSettingLauncher(getContext())
.setTitleRes(R.string.pref_title_network_details) .setTitleRes(R.string.pref_title_network_details)
@@ -1096,14 +1107,29 @@ public class WifiSettings extends RestrictedSettingsFragment
mWifiManager.connect(networkId, mConnectListener); mWifiManager.connect(networkId, mConnectListener);
} }
@VisibleForTesting
void handleAddNetworkRequest(int result, Intent data) {
if(result == Activity.RESULT_OK) {
handleAddNetworkSubmitEvent(data);
}
mWifiTracker.resumeScanning();
}
private void handleAddNetworkSubmitEvent(Intent data) {
final WifiConfiguration wifiConfiguration = data.getParcelableExtra(
AddNetworkFragment.WIFI_CONFIG_KEY);
if (wifiConfiguration != null) {
mWifiManager.save(wifiConfiguration, mSaveListener);
}
}
/** /**
* Called when "add network" button is pressed. * Called when "add network" button is pressed.
*/ */
/* package */ void onAddNetworkPressed() { private void onAddNetworkPressed() {
mMetricsFeatureProvider.action(getActivity(), MetricsEvent.ACTION_WIFI_ADD_NETWORK);
// No exact access point is selected. // No exact access point is selected.
mSelectedAccessPoint = null; mSelectedAccessPoint = null;
showDialog(null, WifiConfigUiBase.MODE_CONNECT); launchAddNetworkFragment();
} }
@Override @Override

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 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.
-->
<!-- Since Robolectric can't inflate @*android:layout/alert_dialog_button_bar_material in
../res/layout/wifi_add_network_view.xml, so this Layout overrides button bar part. -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="@layout/wifi_dialog"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@android:id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@android:id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2018 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.wifi;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.view.View;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import com.android.settingslib.testutils.FragmentTestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = ShadowConnectivityManager.class)
public class AddNetworkFragmentTest {
private AddNetworkFragment mAddNetworkFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mAddNetworkFragment = spy(new AddNetworkFragment());
FragmentTestUtils.startFragment(mAddNetworkFragment);
}
@Test
public void getMetricsCategory_shouldReturnAddNetwork() {
assertThat(mAddNetworkFragment.getMetricsCategory()).isEqualTo(
MetricsEvent.SETTINGS_WIFI_ADD_NETWORK);
}
@Test
public void getMode_shouldBeModeConnected() {
assertThat(mAddNetworkFragment.getMode()).isEqualTo(WifiConfigUiBase.MODE_CONNECT);
}
@Test
public void launchFragment_shouldShowSubmitButton() {
assertThat(mAddNetworkFragment.getSubmitButton()).isNotNull();
}
@Test
public void launchFragment_shouldShowCancelButton() {
assertThat(mAddNetworkFragment.getCancelButton()).isNotNull();
}
@Test
public void onClickSubmitButton_shouldHandleSubmitAction() {
View submitButton = mAddNetworkFragment.getView().findViewById(
AddNetworkFragment.SUBMIT_BUTTON_ID);
mAddNetworkFragment.onClick(submitButton);
verify(mAddNetworkFragment).handleSubmitAction();
}
@Test
public void onClickCancelButton_shouldHandleCancelAction() {
View cancelButton = mAddNetworkFragment.getView().findViewById(
AddNetworkFragment.CANCEL_BUTTON_ID);
mAddNetworkFragment.onClick(cancelButton);
verify(mAddNetworkFragment).handleCancelAction();
}
@Test
public void dispatchSubmit_shouldHandleSubmitAction() {
mAddNetworkFragment.dispatchSubmit();
verify(mAddNetworkFragment).handleSubmitAction();
}
}

View File

@@ -4,7 +4,6 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController; import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.wifi.WifiDialog.WifiDialogListener; import com.android.settings.wifi.WifiDialog.WifiDialogListener;
@@ -32,14 +31,6 @@ public class WifiDialogTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
} }
@Test
public void createFullscreen_setsFullscreenTheme() {
WifiDialog fullscreen = WifiDialog.createFullscreen(mContext, mListener, mockAccessPoint,
WifiConfigUiBase.MODE_CONNECT);
assertThat(fullscreen.getContext().getThemeResId())
.isEqualTo(R.style.Theme_Settings_WifiDialogFullScreen);
}
@Test @Test
public void createModal_usesDefaultTheme() { public void createModal_usesDefaultTheme() {
WifiDialog modal = WifiDialog WifiDialog modal = WifiDialog

View File

@@ -16,9 +16,17 @@
package com.android.settings.wifi; package com.android.settings.wifi;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import com.android.settings.search.SearchIndexableRaw; import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -60,4 +68,15 @@ public class WifiSettingsTest {
assertThat(indexRes).isEmpty(); assertThat(indexRes).isEmpty();
} }
@Test
public void addNetworkFragmentSendResult_onActivityResult_shouldHandleEvent() {
final WifiSettings wifiSettings = spy(new WifiSettings());
final Intent intent = new Intent();
doNothing().when(wifiSettings).handleAddNetworkRequest(anyInt(), any(Intent.class));
wifiSettings.onActivityResult(WifiSettings.ADD_NETWORK_REQUEST, Activity.RESULT_OK, intent);
verify(wifiSettings).handleAddNetworkRequest(anyInt(), any(Intent.class));
}
} }