// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you 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 org.openqa.selenium.remote.server.handler.mobile;

import java.util.Map;

import org.openqa.selenium.mobile.NetworkConnection.ConnectionType;
import org.openqa.selenium.remote.server.JsonParametersAware;
import org.openqa.selenium.remote.server.Session;
import org.openqa.selenium.remote.server.handler.WebDriverHandler;
import org.openqa.selenium.remote.server.handler.html5.Utils;

public class SetNetworkConnection extends WebDriverHandler<Number> implements JsonParametersAware {
  private volatile ConnectionType type;

  public SetNetworkConnection(Session session) {
    super(session);
  }

  @SuppressWarnings("unchecked")
  @Override
  public void setJsonParameters(Map<String, Object> allParameters) throws Exception {
	  Map<String, Map<String, Object>> parameters = (Map<String, Map<String, Object>>)allParameters.get("parameters");
	  Map<String, Object> typeMap = parameters.get("type");
	  
	  type = new ConnectionType(Boolean.parseBoolean(typeMap.get("wifiEnabled").toString()), 
			  					Boolean.parseBoolean(typeMap.get("dataEnabled").toString()),
			  					Boolean.parseBoolean(typeMap.get("airplaneMode").toString()));
  }

  @Override
  public Number call() throws Exception {
    return Integer.parseInt(Utils.getNetworkConnection(getUnwrappedDriver()).setNetworkConnection(type).toString());
  }

  @Override
  public String toString() {
    return String.format("[set network connection : %s]", type.toString());
  }
}

@@ -1535,30 +1535,19 @@
 ption %7B%0A
-%09
   
-Map%3CString,
+ 
  Map%3CStr
@@ -1558,17 +1558,16 @@
  Object%3E
-%3E
  paramet
@@ -1584,36 +1584,24 @@
 %3CString,
- Map%3CString,
  Object%3E
 %3E)allPar
@@ -1592,17 +1592,16 @@
  Object%3E
-%3E
 )allPara
@@ -1630,40 +1630,37 @@
 %22);%0A
-%09
   
-Map%3CString, Object%3E typeMap =
+  Number bitmask = (Number)
  par
@@ -1684,13 +1684,10 @@
 %22);%0A
-%09
   
-%0A%09
   ty
@@ -1714,210 +1714,25 @@
 ype(
-Boolean.parseBoolean(typeMap.get(%22wifiEnabled%22).toString()), %0A%09%09%09  %09%09%09%09%09Boolean.parseBoolean(typeMap.get(%22dataEnabled%22).toString()),%0A%09%09%09  %09%09%09%09%09Boolean.parseBoolean(typeMap.get(%22airplaneMode%22).toString()
+bitmask.intValue(
 ));%0A

Fix network connection (#3222)