aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java
diff options
context:
space:
mode:
authorAng Li <angli@google.com>2017-04-26 15:21:10 -0700
committerGitHub <noreply@github.com>2017-04-26 15:21:10 -0700
commit4c0c5d68410dda9845e457a37fbb2098e743b550 (patch)
treee48f79d92eeb48f4c138c9d7bd05eb5a7322e4e0 /src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java
parent3c4740c21204e35f4ff4ae2d3e120dc26bb1b038 (diff)
downloadmobly-bundled-snippets-4c0c5d68410dda9845e457a37fbb2098e743b550.tar.gz
Add Wi-Fi soft ap related Rpc methods. (#35)
* Add Wi-Fi soft ap related Rpc methods. * Change wait util to accomodate lambda functions that throw exceptions, which interrupt the wait.
Diffstat (limited to 'src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java')
-rw-r--r--src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java
index f4fa0c0..ba9569c 100644
--- a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java
+++ b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java
@@ -23,14 +23,17 @@ import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
+import android.os.Build;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import com.google.android.mobly.snippet.Snippet;
+import com.google.android.mobly.snippet.bundled.utils.ApiVersionException;
import com.google.android.mobly.snippet.bundled.utils.JsonDeserializer;
import com.google.android.mobly.snippet.bundled.utils.JsonSerializer;
import com.google.android.mobly.snippet.bundled.utils.Utils;
import com.google.android.mobly.snippet.rpc.Rpc;
import com.google.android.mobly.snippet.util.Log;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
@@ -240,6 +243,106 @@ public class WifiManagerSnippet implements Snippet {
return mJsonSerializer.toJson(mWifiManager.getDhcpInfo());
}
+ private void verifyApiVersionForSoftAp() throws ApiVersionException {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
+ throw new ApiVersionException(
+ "Soft AP APIs are not supported in Android versions >= N.");
+ }
+ }
+
+ @Rpc(description = "Check whether Wi-Fi Soft AP (hotspot) is enabled.")
+ public boolean wifiIsApEnabled() throws Throwable {
+ verifyApiVersionForSoftAp();
+ try {
+ return (boolean)
+ mWifiManager
+ .getClass()
+ .getDeclaredMethod("isWifiApEnabled")
+ .invoke(mWifiManager);
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ }
+
+ /**
+ * Enable Wi-Fi Soft AP (hotspot).
+ *
+ * <p>Does not work for release N.
+ *
+ * @param configuration The same format as the param wifiNetworkConfig param for wifiConnect.
+ * @throws Throwable
+ */
+ @Rpc(description = "Enable Wi-Fi Soft AP (hotspot).")
+ public void wifiEnableSoftAp(@Nullable JSONObject configuration) throws Throwable {
+ verifyApiVersionForSoftAp();
+ // If no configuration is provided, the existing configuration would be used.
+ WifiConfiguration wifiConfiguration = null;
+ if (configuration != null) {
+ wifiConfiguration = JsonDeserializer.jsonToWifiConfig(configuration);
+ // Have to trim off the extra quotation marks since Soft AP logic interprets
+ // WifiConfiguration.SSID literally, unlike the WifiManager connection logic.
+ wifiConfiguration.SSID = JsonSerializer.trimQuotationMarks(wifiConfiguration.SSID);
+ }
+ boolean success;
+ try {
+ success =
+ (boolean)
+ mWifiManager
+ .getClass()
+ .getDeclaredMethod(
+ "setWifiApEnabled",
+ WifiConfiguration.class,
+ boolean.class)
+ .invoke(mWifiManager, wifiConfiguration, true);
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ if (!success) {
+ throw new WifiManagerSnippetException("Failed to initiate turning on Wi-Fi Soft AP.");
+ }
+ if (!Utils.waitUntil(() -> wifiIsApEnabled() == true, 60)) {
+ throw new WifiManagerSnippetException(
+ "Timed out after 60s waiting for Wi-Fi Soft AP state to turn on with configuration: "
+ + configuration);
+ }
+ }
+
+ /**
+ * Disables Wi-Fi Soft AP (hotspot).
+ *
+ * <p>Does not work for release N.
+ *
+ * @throws Throwable
+ */
+ @Rpc(description = "Disable Wi-Fi Soft AP (hotspot).")
+ public void wifiDisableSoftAp() throws Throwable {
+ verifyApiVersionForSoftAp();
+ boolean success;
+ try {
+ success =
+ (boolean)
+ mWifiManager
+ .getClass()
+ .getDeclaredMethod(
+ "setWifiApEnabled",
+ WifiConfiguration.class,
+ boolean.class)
+ .invoke(
+ mWifiManager,
+ null, /* No configuration needed for disabling */
+ false);
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ if (!success) {
+ throw new WifiManagerSnippetException("Failed to initiate turning off Wi-Fi Soft AP.");
+ }
+ if (!Utils.waitUntil(() -> wifiIsApEnabled() == false, 60)) {
+ throw new WifiManagerSnippetException(
+ "Timed out after 60s waiting for Wi-Fi Soft AP state to turn off.");
+ }
+ }
+
@Override
public void shutdown() {}