• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.googlecode.android_scripting.facade.wifi;
18 
19 import static android.net.wifi.ScanResult.FLAG_80211mc_RESPONDER;
20 import static android.net.wifi.ScanResult.FLAG_PASSPOINT_NETWORK;
21 
22 import android.net.wifi.ScanResult;
23 
24 import org.apache.commons.codec.binary.Base64Codec;
25 import org.json.JSONArray;
26 import org.json.JSONException;
27 import org.json.JSONObject;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Set of utility methods to parse JSON constructs back into classes. Usually counterparts to
34  * utility methods which automatically do the conversion from classes to JSON constructs and
35  * are in JsonBuilder.java.
36  */
37 public class WifiJsonParser {
38     /**
39      * Converts a JSON representation of a ScanResult to an actual ScanResult object. Mirror of
40      * the code in
41      * {@link com.googlecode.android_scripting.jsonrpc.JsonBuilder#buildJsonScanResult(ScanResult)}.
42      *
43      * @param j JSON object representing a ScanResult.
44      * @return a ScanResult object
45      * @throws JSONException on any JSON errors
46      */
getScanResult(JSONObject j)47     public static ScanResult getScanResult(JSONObject j) throws JSONException {
48         if (j == null) {
49             return null;
50         }
51 
52         ScanResult scanResult = new ScanResult();
53 
54         if (j.has("BSSID")) {
55             scanResult.BSSID = j.getString("BSSID");
56         }
57         if (j.has("SSID")) {
58             scanResult.SSID = j.getString("SSID");
59         }
60         if (j.has("frequency")) {
61             scanResult.frequency = j.getInt("frequency");
62         }
63         if (j.has("level")) {
64             scanResult.level = j.getInt("level");
65         }
66         if (j.has("capabilities")) {
67             scanResult.capabilities = j.getString("capabilities");
68         }
69         if (j.has("timestamp")) {
70             scanResult.timestamp = j.getLong("timestamp");
71         }
72         if (j.has("centerFreq0")) {
73             scanResult.centerFreq0 = j.getInt("centerFreq0");
74         }
75         if (j.has("centerFreq1")) {
76             scanResult.centerFreq1 = j.getInt("centerFreq1");
77         }
78         if (j.has("channelWidth")) {
79             scanResult.channelWidth = j.getInt("channelWidth");
80         }
81         if (j.has("distanceCm")) {
82             scanResult.distanceCm = j.getInt("distanceCm");
83         }
84         if (j.has("distanceSdCm")) {
85             scanResult.distanceSdCm = j.getInt("distanceSdCm");
86         }
87         if (j.has("is80211McRTTResponder") && j.getBoolean("is80211McRTTResponder")) {
88             scanResult.setFlag(FLAG_80211mc_RESPONDER);
89         }
90         if (j.has("passpointNetwork") && j.getBoolean("passpointNetwork")) {
91             scanResult.setFlag(FLAG_PASSPOINT_NETWORK);
92         }
93         if (j.has("numUsage")) {
94             scanResult.numUsage = j.getInt("numUsage");
95         }
96         if (j.has("seen")) {
97             scanResult.seen = j.getLong("seen");
98         }
99         if (j.has("untrusted")) {
100             scanResult.untrusted = j.getBoolean("untrusted");
101         }
102         if (j.has("operatorFriendlyName")) {
103             scanResult.operatorFriendlyName = j.getString("operatorFriendlyName");
104         }
105         if (j.has("venueName")) {
106             scanResult.venueName = j.getString("venueName");
107         }
108         if (j.has("InformationElements")) {
109             JSONArray jIes = j.getJSONArray("InformationElements");
110             if (jIes.length() > 0) {
111                 scanResult.informationElements = new ScanResult.InformationElement[jIes.length()];
112                 for (int i = 0; i < jIes.length(); ++i) {
113                     ScanResult.InformationElement scanIe = new ScanResult.InformationElement();
114                     JSONObject jIe = jIes.getJSONObject(i);
115                     scanIe.id = jIe.getInt("id");
116                     scanIe.bytes = Base64Codec.decodeBase64(jIe.getString("bytes"));
117                     scanResult.informationElements[i] = scanIe;
118                 }
119             }
120         }
121         if (j.has("radioChainInfos")) {
122             JSONArray jRinfos = j.getJSONArray("radioChainInfos");
123             if (jRinfos.length() > 0) {
124                 scanResult.radioChainInfos = new ScanResult.RadioChainInfo[jRinfos.length()];
125                 for (int i = 0; i < jRinfos.length(); i++) {
126                     ScanResult.RadioChainInfo item = new ScanResult.RadioChainInfo();
127                     JSONObject jRinfo = jRinfos.getJSONObject(i);
128                     item.id = jRinfo.getInt("id");
129                     item.level = jRinfo.getInt("level");
130                     scanResult.radioChainInfos[i] = item;
131                 }
132             }
133         }
134 
135         return scanResult;
136     }
137 
138     /**
139      * Converts a JSONArray toa a list of ScanResult.
140      *
141      * @param j JSONArray representing a collection of ScanResult objects
142      * @return a list of ScanResult objects
143      * @throws JSONException on any JSON error
144      */
getScanResults(JSONArray j)145     public static List<ScanResult> getScanResults(JSONArray j) throws JSONException {
146         if (j == null || j.length() == 0) {
147             return null;
148         }
149 
150         ArrayList<ScanResult> scanResults = new ArrayList<>(j.length());
151         for (int i = 0; i < j.length(); ++i) {
152             scanResults.add(getScanResult(j.getJSONObject(i)));
153         }
154 
155         return scanResults;
156     }
157 }
158