Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bef3c6dd authored by Amin Shaikh's avatar Amin Shaikh
Browse files

Update WriteWifiConfigToNfcDialog.

- Use the new WifiManager API to retrieve the WPS NFC token for the
current network.
- Add WifiManagerWrapper class to support testing because Robolectric
does not yet support testing Android O.

Bug: 35725168
Test: m RunSettingsRoboTests
Change-Id: I7805bdcbe02dc262083bcd371d4ad88d256f8089
parent 510a1a69
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
package com.android.settings.wifi;

import android.net.wifi.WifiManager;

/**
 * Wrapper around {@link WifiManager} to facilitate unit testing.
 *
 * TODO: delete this class once robolectric supports Android O
 */
public class WifiManagerWrapper {
    private final WifiManager mWifiManager;

    public WifiManagerWrapper(WifiManager wifiManager) {
        mWifiManager = wifiManager;
    }

    /**
     * {@link WifiManager#getCurrentNetworkWpsNfcConfigurationToken}
     */
    public String getCurrentNetworkWpsNfcConfigurationToken() {
        return mWifiManager.getCurrentNetworkWpsNfcConfigurationToken();
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -577,12 +577,12 @@ public class WifiSettings extends RestrictedSettingsFragment
            case WRITE_NFC_DIALOG_ID:
                if (mSelectedAccessPoint != null) {
                    mWifiToNfcDialog = new WriteWifiConfigToNfcDialog(
                            getActivity(), mSelectedAccessPoint.getConfig().networkId,
                            getActivity(),
                            mSelectedAccessPoint.getSecurity(),
                            mWifiManager);
                            new WifiManagerWrapper(mWifiManager));
                } else if (mWifiNfcDialogSavedState != null) {
                    mWifiToNfcDialog = new WriteWifiConfigToNfcDialog(
                            getActivity(), mWifiNfcDialogSavedState, mWifiManager);
                    mWifiToNfcDialog = new WriteWifiConfigToNfcDialog(getActivity(),
                            mWifiNfcDialogSavedState, new WifiManagerWrapper(mWifiManager));
                }

                return mWifiToNfcDialog;
+8 −15
Original line number Diff line number Diff line
@@ -56,7 +56,6 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
    private static final String PASSWORD_FORMAT = "102700%s%s";
    private static final int HEX_RADIX = 16;
    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
    private static final String NETWORK_ID = "network_id";
    private static final String SECURITY = "security";

    private final PowerManager.WakeLock mWakeLock;
@@ -69,33 +68,29 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
    private TextView mLabelView;
    private CheckBox mPasswordCheckBox;
    private ProgressBar mProgressBar;
    private WifiManager mWifiManager;
    private WifiManagerWrapper mWifiManager;
    private String mWpsNfcConfigurationToken;
    private Context mContext;
    private int mNetworkId;
    private int mSecurity;

    WriteWifiConfigToNfcDialog(Context context, int networkId, int security,
            WifiManager wifiManager) {
    WriteWifiConfigToNfcDialog(Context context, int security, WifiManagerWrapper wifiManager) {
        super(context);

        mContext = context;
        mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE))
                .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WriteWifiConfigToNfcDialog:wakeLock");
        mOnTextChangedHandler = new Handler();
        mNetworkId = networkId;
        mSecurity = security;
        mWifiManager = wifiManager;
    }

    WriteWifiConfigToNfcDialog(Context context, Bundle savedState, WifiManager wifiManager) {
    WriteWifiConfigToNfcDialog(Context context, Bundle savedState, WifiManagerWrapper wifiManager) {
        super(context);

        mContext = context;
        mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE))
                .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WriteWifiConfigToNfcDialog:wakeLock");
        mOnTextChangedHandler = new Handler();
        mNetworkId = savedState.getInt(NETWORK_ID);
        mSecurity = savedState.getInt(SECURITY);
        mWifiManager = wifiManager;
    }
@@ -114,12 +109,12 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
                mContext.getResources().getString(com.android.internal.R.string.cancel),
                (OnClickListener) null);

        mPasswordView = (TextView) mView.findViewById(R.id.password);
        mLabelView = (TextView) mView.findViewById(R.id.password_label);
        mPasswordView = mView.findViewById(R.id.password);
        mLabelView = mView.findViewById(R.id.password_label);
        mPasswordView.addTextChangedListener(this);
        mPasswordCheckBox = (CheckBox) mView.findViewById(R.id.show_password);
        mPasswordCheckBox = mView.findViewById(R.id.show_password);
        mPasswordCheckBox.setOnCheckedChangeListener(this);
        mProgressBar = (ProgressBar) mView.findViewById(R.id.progress_bar);
        mProgressBar = mView.findViewById(R.id.progress_bar);

        super.onCreate(savedInstanceState);

@@ -135,8 +130,7 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
        mWakeLock.acquire();

        String password = mPasswordView.getText().toString();
        String wpsNfcConfigurationToken
                = mWifiManager.getWpsNfcConfigurationToken(mNetworkId);
        String wpsNfcConfigurationToken = mWifiManager.getCurrentNetworkWpsNfcConfigurationToken();
        String passwordHex = byteArrayToHexString(password.getBytes());

        String passwordLength = password.length() >= HEX_RADIX
@@ -180,7 +174,6 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
    }

    public void saveState(Bundle state) {
        state.putInt(NETWORK_ID, mNetworkId);
        state.putInt(SECURITY, mSecurity);
    }

+5 −7
Original line number Diff line number Diff line
@@ -46,10 +46,8 @@ import org.robolectric.util.ReflectionHelpers;
        shadows = ShadowNfcAdapter.class
)
public class WriteWifiConfigToNfcDialogTest {
    private static final int NETWORK_ID = 17;

    @Mock Activity mActivity;
    @Mock WifiManager mWifiManager;
    @Mock WifiManagerWrapper mWifiManager;

    private WriteWifiConfigToNfcDialog mWriteWifiConfigToNfcDialog;

@@ -61,7 +59,7 @@ public class WriteWifiConfigToNfcDialogTest {
                .thenReturn(ReflectionHelpers.newInstance(InputMethodManager.class));

        mWriteWifiConfigToNfcDialog = new WriteWifiConfigToNfcDialog(RuntimeEnvironment.application,
                NETWORK_ID, 0 /* security */, mWifiManager);
                0 /* security */, mWifiManager);
        mWriteWifiConfigToNfcDialog.setOwnerActivity(mActivity);
        mWriteWifiConfigToNfcDialog.onCreate(null /* savedInstanceState */);
    }
@@ -73,7 +71,7 @@ public class WriteWifiConfigToNfcDialogTest {

    @Test
    public void testOnClick_nfcConfigurationTokenDoesNotContainPasswordHex() {
        when(mWifiManager.getWpsNfcConfigurationToken(NETWORK_ID)).thenReturn("blah");
        when(mWifiManager.getCurrentNetworkWpsNfcConfigurationToken()).thenReturn("blah");

        mWriteWifiConfigToNfcDialog.onClick(null);

@@ -82,7 +80,7 @@ public class WriteWifiConfigToNfcDialogTest {

    @Test
    public void testOnClick_nfcConfigurationTokenIsNull() {
        when(mWifiManager.getWpsNfcConfigurationToken(NETWORK_ID)).thenReturn(null);
        when(mWifiManager.getCurrentNetworkWpsNfcConfigurationToken()).thenReturn(null);

        mWriteWifiConfigToNfcDialog.onClick(null);

@@ -92,7 +90,7 @@ public class WriteWifiConfigToNfcDialogTest {
    @Test
    public void testOnClick_nfcConfigurationTokenContainsPasswordHex() {
        // This is the corresponding passwordHex for an empty string password.
        when(mWifiManager.getWpsNfcConfigurationToken(NETWORK_ID)).thenReturn("10270000");
        when(mWifiManager.getCurrentNetworkWpsNfcConfigurationToken()).thenReturn("10270000");

        mWriteWifiConfigToNfcDialog.onClick(null);