Fixes how we read the current WiFi network IP address.
The old logic could read the current IP address only when the network has the Internet access. Bug: 31934577 Test: Manual inspection Test: make RunSettingsRoboTests -j40 Change-Id: I46fe6f6fb4322e8245d3ac66ac6530228c226d16
This commit is contained in:
@@ -47,7 +47,9 @@ import android.graphics.BitmapFactory;
|
|||||||
import android.hardware.fingerprint.FingerprintManager;
|
import android.hardware.fingerprint.FingerprintManager;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.LinkProperties;
|
import android.net.LinkProperties;
|
||||||
|
import android.net.Network;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
@@ -235,10 +237,15 @@ public final class Utils extends com.android.settingslib.Utils {
|
|||||||
* @return the formatted and newline-separated IP addresses, or null if none.
|
* @return the formatted and newline-separated IP addresses, or null if none.
|
||||||
*/
|
*/
|
||||||
public static String getWifiIpAddresses(Context context) {
|
public static String getWifiIpAddresses(Context context) {
|
||||||
ConnectivityManager cm = (ConnectivityManager)
|
WifiManager wifiManager = context.getSystemService(WifiManager.class);
|
||||||
|
Network currentNetwork = wifiManager.getCurrentNetwork();
|
||||||
|
if (currentNetwork != null) {
|
||||||
|
ConnectivityManager cm = (ConnectivityManager)
|
||||||
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
|
LinkProperties prop = cm.getLinkProperties(currentNetwork);
|
||||||
return formatIpAddresses(prop);
|
return formatIpAddresses(prop);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
69
tests/robotests/src/com/android/settings/UtilsTest.java
Normal file
69
tests/robotests/src/com/android/settings/UtilsTest.java
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package com.android.settings;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.LinkAddress;
|
||||||
|
import android.net.LinkProperties;
|
||||||
|
import android.net.Network;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class UtilsTest {
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
@Mock private WifiManager wifiManager;
|
||||||
|
@Mock private Network network;
|
||||||
|
@Mock private ConnectivityManager connectivityManager;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
when(mContext.getSystemService(WifiManager.class)).thenReturn(wifiManager);
|
||||||
|
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
|
||||||
|
.thenReturn(connectivityManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetWifiIpAddresses_succeeds() throws Exception {
|
||||||
|
when(wifiManager.getCurrentNetwork()).thenReturn(network);
|
||||||
|
LinkAddress address = new LinkAddress(InetAddress.getByName("127.0.0.1"), 0);
|
||||||
|
LinkProperties lp = new LinkProperties();
|
||||||
|
lp.addLinkAddress(address);
|
||||||
|
when(connectivityManager.getLinkProperties(network)).thenReturn(lp);
|
||||||
|
|
||||||
|
assertThat(Utils.getWifiIpAddresses(mContext)).isEqualTo("127.0.0.1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetWifiIpAddresses_nullLinkProperties() {
|
||||||
|
when(wifiManager.getCurrentNetwork()).thenReturn(network);
|
||||||
|
// Explicitly set the return value to null for readability sake.
|
||||||
|
when(connectivityManager.getLinkProperties(network)).thenReturn(null);
|
||||||
|
|
||||||
|
assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetWifiIpAddresses_nullNetwork() {
|
||||||
|
// Explicitly set the return value to null for readability sake.
|
||||||
|
when(wifiManager.getCurrentNetwork()).thenReturn(null);
|
||||||
|
|
||||||
|
assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user