Merge "Refactor add network page"
This commit is contained in:
committed by
Android (Google) Code Review
commit
c748fee5d8
159
src/com/android/settings/wifi/AddNetworkFragment.java
Normal file
159
src/com/android/settings/wifi/AddNetworkFragment.java
Normal 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();
|
||||
}
|
||||
}
|
@@ -50,14 +50,6 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase,
|
||||
private WifiConfigController mController;
|
||||
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
|
||||
* view.
|
||||
|
@@ -100,6 +100,9 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
public static final int WIFI_DIALOG_ID = 1;
|
||||
private static final int WRITE_NFC_DIALOG_ID = 6;
|
||||
|
||||
@VisibleForTesting
|
||||
static final int ADD_NETWORK_REQUEST = 2;
|
||||
|
||||
// Instance state keys
|
||||
private static final String SAVE_DIALOG_MODE = "dialog_mode";
|
||||
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) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
// Only handle request comes from AddNetworkFragment
|
||||
if (requestCode == ADD_NETWORK_REQUEST) {
|
||||
handleAddNetworkRequest(resultCode, data);
|
||||
return;
|
||||
}
|
||||
|
||||
final boolean formerlyRestricted = mIsRestricted;
|
||||
mIsRestricted = isUiRestricted();
|
||||
if (formerlyRestricted && !mIsRestricted
|
||||
@@ -590,22 +599,15 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
public Dialog onCreateDialog(int dialogId) {
|
||||
switch (dialogId) {
|
||||
case WIFI_DIALOG_ID:
|
||||
if (mDlgAccessPoint == null && mAccessPointSavedState == null) {
|
||||
// add new network
|
||||
mDialog = WifiDialog
|
||||
.createFullscreen(getActivity(), this, mDlgAccessPoint, mDialogMode);
|
||||
} else {
|
||||
// modify network
|
||||
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);
|
||||
// modify network
|
||||
if (mDlgAccessPoint == null && mAccessPointSavedState != 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);
|
||||
mSelectedAccessPoint = mDlgAccessPoint;
|
||||
return mDialog;
|
||||
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) {
|
||||
new SubSettingLauncher(getContext())
|
||||
.setTitleRes(R.string.pref_title_network_details)
|
||||
@@ -1096,14 +1107,29 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
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.
|
||||
*/
|
||||
/* package */ void onAddNetworkPressed() {
|
||||
mMetricsFeatureProvider.action(getActivity(), MetricsEvent.ACTION_WIFI_ADD_NETWORK);
|
||||
private void onAddNetworkPressed() {
|
||||
// No exact access point is selected.
|
||||
mSelectedAccessPoint = null;
|
||||
showDialog(null, WifiConfigUiBase.MODE_CONNECT);
|
||||
launchAddNetworkFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user