1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.snippet.wifi.direct; 18 19 import android.net.MacAddress; 20 import android.net.wifi.p2p.WifiP2pConfig; 21 22 import org.json.JSONException; 23 import org.json.JSONObject; 24 25 /** Deserializes JSONObject into data objects defined in Android API. */ 26 public class JsonDeserializer { 27 private static final String PERSISTENT_MODE = "persistent_mode"; 28 private static final String DEVICE_ADDRESS = "device_address"; 29 private static final String GROUP_CLIENT_IP_PROVISIONING_MODE = 30 "group_client_ip_provisioning_mode"; 31 private static final String GROUP_OPERATING_BAND = "group_operating_band"; 32 private static final String GROUP_OPERATING_FREQUENCY = "group_operating_frequency"; 33 private static final String NETWORK_NAME = "network_name"; 34 private static final String PASSPHRASE = "passphrase"; 35 JsonDeserializer()36 private JsonDeserializer() { 37 } 38 39 /** Converts Python dict to android.net.wifi.p2p.WifiP2pConfig. */ jsonToWifiP2pConfig(JSONObject jsonObject)40 public static WifiP2pConfig jsonToWifiP2pConfig(JSONObject jsonObject) throws JSONException { 41 WifiP2pConfig.Builder builder = new WifiP2pConfig.Builder(); 42 if (jsonObject.has(PERSISTENT_MODE)) { 43 builder.enablePersistentMode(jsonObject.getBoolean(PERSISTENT_MODE)); 44 } 45 if (jsonObject.has(DEVICE_ADDRESS)) { 46 builder.setDeviceAddress(MacAddress.fromString(jsonObject.getString(DEVICE_ADDRESS))); 47 } 48 if (jsonObject.has(GROUP_CLIENT_IP_PROVISIONING_MODE)) { 49 builder.setGroupClientIpProvisioningMode( 50 jsonObject.getInt(GROUP_CLIENT_IP_PROVISIONING_MODE)); 51 } 52 if (jsonObject.has(GROUP_OPERATING_BAND)) { 53 builder.setGroupOperatingBand(jsonObject.getInt(GROUP_OPERATING_BAND)); 54 } 55 if (jsonObject.has(GROUP_OPERATING_FREQUENCY)) { 56 builder.setGroupOperatingFrequency(jsonObject.getInt(GROUP_OPERATING_FREQUENCY)); 57 } 58 if (jsonObject.has(NETWORK_NAME)) { 59 builder.setNetworkName(jsonObject.getString(NETWORK_NAME)); 60 } 61 if (jsonObject.has(PASSPHRASE)) { 62 builder.setPassphrase(jsonObject.getString(PASSPHRASE)); 63 } 64 return builder.build(); 65 } 66 } 67 68