Ensure location is enabled before getting SSID.
When location is disabled, Wi-Fi scan results and SSID are not available to apps. Fixes: 153850762 Fixes: 153396893 Test: atest hostsidetests/net/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java Change-Id: I05285811d7131e116d5e1d072137ed2cf9576d05
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
<option name="config-descriptor:metadata" key="parameter" value="not_multi_abi" />
|
<option name="config-descriptor:metadata" key="parameter" value="not_multi_abi" />
|
||||||
<option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
|
<option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
|
||||||
|
|
||||||
|
<target_preparer class="com.android.compatibility.common.tradefed.targetprep.LocationCheck" />
|
||||||
<target_preparer class="com.android.cts.net.NetworkPolicyTestsPreparer" />
|
<target_preparer class="com.android.cts.net.NetworkPolicyTestsPreparer" />
|
||||||
|
|
||||||
<target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
|
<target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
|
||||||
|
|||||||
@@ -126,12 +126,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
|
|||||||
protected Context mContext;
|
protected Context mContext;
|
||||||
protected Instrumentation mInstrumentation;
|
protected Instrumentation mInstrumentation;
|
||||||
protected ConnectivityManager mCm;
|
protected ConnectivityManager mCm;
|
||||||
protected WifiManager mWfm;
|
|
||||||
protected int mUid;
|
protected int mUid;
|
||||||
private int mMyUid;
|
private int mMyUid;
|
||||||
private MyServiceClient mServiceClient;
|
private MyServiceClient mServiceClient;
|
||||||
private String mDeviceIdleConstantsSetting;
|
private String mDeviceIdleConstantsSetting;
|
||||||
private boolean mIsLocationOn;
|
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final RuleChain mRuleChain = RuleChain.outerRule(new RequiredPropertiesRule())
|
public final RuleChain mRuleChain = RuleChain.outerRule(new RequiredPropertiesRule())
|
||||||
@@ -144,16 +142,11 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
|
|||||||
mInstrumentation = getInstrumentation();
|
mInstrumentation = getInstrumentation();
|
||||||
mContext = getContext();
|
mContext = getContext();
|
||||||
mCm = getConnectivityManager();
|
mCm = getConnectivityManager();
|
||||||
mWfm = getWifiManager();
|
|
||||||
mUid = getUid(TEST_APP2_PKG);
|
mUid = getUid(TEST_APP2_PKG);
|
||||||
mMyUid = getUid(mContext.getPackageName());
|
mMyUid = getUid(mContext.getPackageName());
|
||||||
mServiceClient = new MyServiceClient(mContext);
|
mServiceClient = new MyServiceClient(mContext);
|
||||||
mServiceClient.bind();
|
mServiceClient.bind();
|
||||||
mDeviceIdleConstantsSetting = "device_idle_constants";
|
mDeviceIdleConstantsSetting = "device_idle_constants";
|
||||||
mIsLocationOn = isLocationOn();
|
|
||||||
if (!mIsLocationOn) {
|
|
||||||
enableLocation();
|
|
||||||
}
|
|
||||||
executeShellCommand("cmd netpolicy start-watching " + mUid);
|
executeShellCommand("cmd netpolicy start-watching " + mUid);
|
||||||
setAppIdle(false);
|
setAppIdle(false);
|
||||||
|
|
||||||
@@ -164,33 +157,9 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
|
|||||||
|
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
executeShellCommand("cmd netpolicy stop-watching");
|
executeShellCommand("cmd netpolicy stop-watching");
|
||||||
if (!mIsLocationOn) {
|
|
||||||
disableLocation();
|
|
||||||
}
|
|
||||||
mServiceClient.unbind();
|
mServiceClient.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableLocation() throws Exception {
|
|
||||||
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE,
|
|
||||||
Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
|
|
||||||
assertEquals(Settings.Secure.LOCATION_MODE_SENSORS_ONLY,
|
|
||||||
Settings.Secure.getInt(mContext.getContentResolver(),
|
|
||||||
Settings.Secure.LOCATION_MODE));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void disableLocation() throws Exception {
|
|
||||||
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE,
|
|
||||||
Settings.Secure.LOCATION_MODE_OFF);
|
|
||||||
assertEquals(Settings.Secure.LOCATION_MODE_OFF,
|
|
||||||
Settings.Secure.getInt(mContext.getContentResolver(),
|
|
||||||
Settings.Secure.LOCATION_MODE));
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isLocationOn() throws Exception {
|
|
||||||
return Settings.Secure.getInt(mContext.getContentResolver(),
|
|
||||||
Settings.Secure.LOCATION_MODE) != Settings.Secure.LOCATION_MODE_OFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected int getUid(String packageName) throws Exception {
|
protected int getUid(String packageName) throws Exception {
|
||||||
return mContext.getPackageManager().getPackageUid(packageName, 0);
|
return mContext.getPackageManager().getPackageUid(packageName, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,17 +27,20 @@ import static com.android.cts.net.hostside.AbstractRestrictBackgroundNetworkTest
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
import android.app.Instrumentation;
|
import android.app.Instrumentation;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.location.LocationManager;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.ConnectivityManager.NetworkCallback;
|
import android.net.ConnectivityManager.NetworkCallback;
|
||||||
import android.net.Network;
|
import android.net.Network;
|
||||||
import android.net.NetworkCapabilities;
|
import android.net.NetworkCapabilities;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.os.Process;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
@@ -113,6 +116,20 @@ public class NetworkPolicyTestUtils {
|
|||||||
return am.isLowRamDevice();
|
return am.isLowRamDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isLocationEnabled() {
|
||||||
|
final LocationManager lm = (LocationManager) getContext().getSystemService(
|
||||||
|
Context.LOCATION_SERVICE);
|
||||||
|
return lm.isLocationEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setLocationEnabled(boolean enabled) {
|
||||||
|
final LocationManager lm = (LocationManager) getContext().getSystemService(
|
||||||
|
Context.LOCATION_SERVICE);
|
||||||
|
lm.setLocationEnabledForUser(enabled, Process.myUserHandle());
|
||||||
|
assertEquals("Couldn't change location enabled state", lm.isLocationEnabled(), enabled);
|
||||||
|
Log.d(TAG, "Changed location enabled state to " + enabled);
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isActiveNetworkMetered(boolean metered) {
|
public static boolean isActiveNetworkMetered(boolean metered) {
|
||||||
return getConnectivityManager().isActiveNetworkMetered() == metered;
|
return getConnectivityManager().isActiveNetworkMetered() == metered;
|
||||||
}
|
}
|
||||||
@@ -128,9 +145,21 @@ public class NetworkPolicyTestUtils {
|
|||||||
if (isActiveNetworkMetered(metered)) {
|
if (isActiveNetworkMetered(metered)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final String ssid = unquoteSSID(getWifiManager().getConnectionInfo().getSSID());
|
final boolean isLocationEnabled = isLocationEnabled();
|
||||||
setWifiMeteredStatus(ssid, metered);
|
try {
|
||||||
return Pair.create(ssid, !metered);
|
if (!isLocationEnabled) {
|
||||||
|
setLocationEnabled(true);
|
||||||
|
}
|
||||||
|
final String ssid = unquoteSSID(getWifiManager().getConnectionInfo().getSSID());
|
||||||
|
assertNotEquals(WifiManager.UNKNOWN_SSID, ssid);
|
||||||
|
setWifiMeteredStatus(ssid, metered);
|
||||||
|
return Pair.create(ssid, !metered);
|
||||||
|
} finally {
|
||||||
|
// Reset the location enabled state
|
||||||
|
if (!isLocationEnabled) {
|
||||||
|
setLocationEnabled(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void resetMeteredNetwork(String ssid, boolean metered) throws Exception {
|
public static void resetMeteredNetwork(String ssid, boolean metered) throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user