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 36 /** 37 * Converts Python dict to {@link WifiP2pConfig}. 38 * 39 * @param jsonObject Python dict representing {@link WifiP2pConfig}. 40 * @return {@link WifiP2pConfig}. 41 * @throws JSONException if the JSON object is malformed. 42 */ jsonToWifiP2pConfig(JSONObject jsonObject)43 public static WifiP2pConfig jsonToWifiP2pConfig(JSONObject jsonObject) throws JSONException { 44 if (jsonObject.has("wps_setup")) { 45 // Create WifiP2pConfig directly. 46 WifiP2pConfig config = new WifiP2pConfig(); 47 config.wps.setup = jsonObject.getInt("wps_setup"); 48 if (jsonObject.has(DEVICE_ADDRESS)) { 49 config.deviceAddress = jsonObject.getString(DEVICE_ADDRESS); 50 } 51 return config; 52 } 53 54 // Create WifiP2pConfig through builder. 55 WifiP2pConfig.Builder builder = new WifiP2pConfig.Builder(); 56 if (jsonObject.has(PERSISTENT_MODE)) { 57 builder.enablePersistentMode(jsonObject.getBoolean(PERSISTENT_MODE)); 58 } 59 if (jsonObject.has(DEVICE_ADDRESS)) { 60 builder.setDeviceAddress(MacAddress.fromString(jsonObject.getString(DEVICE_ADDRESS))); 61 } 62 if (jsonObject.has(GROUP_CLIENT_IP_PROVISIONING_MODE)) { 63 builder.setGroupClientIpProvisioningMode( 64 jsonObject.getInt(GROUP_CLIENT_IP_PROVISIONING_MODE) 65 ); 66 } 67 if (jsonObject.has(GROUP_OPERATING_BAND)) { 68 builder.setGroupOperatingBand(jsonObject.getInt(GROUP_OPERATING_BAND)); 69 } 70 if (jsonObject.has(GROUP_OPERATING_FREQUENCY)) { 71 builder.setGroupOperatingFrequency(jsonObject.getInt(GROUP_OPERATING_FREQUENCY)); 72 } 73 if (jsonObject.has(NETWORK_NAME)) { 74 builder.setNetworkName(jsonObject.getString(NETWORK_NAME)); 75 } 76 if (jsonObject.has(PASSPHRASE)) { 77 builder.setPassphrase(jsonObject.getString(PASSPHRASE)); 78 } 79 return builder.build(); 80 } 81 82 } 83