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; 18 19 import com.googlecode.android_scripting.jsonrpc.JsonSerializable; 20 21 import org.json.JSONException; 22 import org.json.JSONObject; 23 24 /** 25 * Utility class for ConnectivityManager/Service events. Encapsulates the data in a JSON format 26 * to be used in the test script. 27 * 28 * Note that not all information is encapsulated. Add to the *Event classes as more information 29 * is needed. 30 */ 31 public class ConnectivityEvents { 32 /** 33 * Translates a packet keep-alive event to JSON. 34 */ 35 public static class PacketKeepaliveEvent implements JsonSerializable { 36 private String mId; 37 private String mPacketKeepaliveEvent; 38 PacketKeepaliveEvent(String id, String event)39 public PacketKeepaliveEvent(String id, String event) { 40 mId = id; 41 mPacketKeepaliveEvent = event; 42 } 43 44 /** 45 * Create a JSON data-structure. 46 */ toJSON()47 public JSONObject toJSON() throws JSONException { 48 JSONObject packetKeepalive = new JSONObject(); 49 50 packetKeepalive.put( 51 ConnectivityConstants.PacketKeepaliveContainer.ID, 52 mId); 53 packetKeepalive.put( 54 ConnectivityConstants.PacketKeepaliveContainer.PACKET_KEEPALIVE_EVENT, 55 mPacketKeepaliveEvent); 56 57 return packetKeepalive; 58 } 59 } 60 61 /** 62 * Translates a ConnectivityManager.NetworkCallback to JSON. 63 */ 64 public static class NetworkCallbackEventBase implements JsonSerializable { 65 private String mId; 66 private String mNetworkCallbackEvent; 67 NetworkCallbackEventBase(String id, String event)68 public NetworkCallbackEventBase(String id, String event) { 69 mId = id; 70 mNetworkCallbackEvent = event; 71 } 72 73 /** 74 * Create a JSON data-structure. 75 */ toJSON()76 public JSONObject toJSON() throws JSONException { 77 JSONObject networkCallback = new JSONObject(); 78 79 networkCallback.put(ConnectivityConstants.NetworkCallbackContainer.ID, mId); 80 networkCallback.put( 81 ConnectivityConstants.NetworkCallbackContainer.NETWORK_CALLBACK_EVENT, 82 mNetworkCallbackEvent); 83 84 return networkCallback; 85 } 86 } 87 88 /** 89 * Specializes NetworkCallbackEventBase to add information for the onLosing() callback. 90 */ 91 public static class NetworkCallbackEventOnLosing extends NetworkCallbackEventBase { 92 private int mMaxMsToLive; 93 NetworkCallbackEventOnLosing(String id, String event, int maxMsToLive)94 public NetworkCallbackEventOnLosing(String id, String event, int maxMsToLive) { 95 super(id, event); 96 mMaxMsToLive = maxMsToLive; 97 } 98 99 /** 100 * Create a JSON data-structure. 101 */ toJSON()102 public JSONObject toJSON() throws JSONException { 103 JSONObject json = super.toJSON(); 104 json.put(ConnectivityConstants.NetworkCallbackContainer.MAX_MS_TO_LIVE, mMaxMsToLive); 105 return json; 106 } 107 } 108 109 /** 110 * Specializes NetworkCallbackEventBase to add information for the onCapabilitiesChanged() 111 * callback. 112 */ 113 public static class NetworkCallbackEventOnCapabilitiesChanged extends NetworkCallbackEventBase { 114 private int mRssi; 115 NetworkCallbackEventOnCapabilitiesChanged(String id, String event, int rssi)116 public NetworkCallbackEventOnCapabilitiesChanged(String id, String event, int rssi) { 117 super(id, event); 118 mRssi = rssi; 119 } 120 121 /** 122 * Create a JSON data-structure. 123 */ toJSON()124 public JSONObject toJSON() throws JSONException { 125 JSONObject json = super.toJSON(); 126 json.put(ConnectivityConstants.NetworkCallbackContainer.RSSI, mRssi); 127 return json; 128 } 129 } 130 131 /** 132 * Specializes NetworkCallbackEventBase to add information for the onCapabilitiesChanged() 133 * callback. 134 */ 135 public static class NetworkCallbackEventOnLinkPropertiesChanged extends 136 NetworkCallbackEventBase { 137 private String mInterfaceName; 138 NetworkCallbackEventOnLinkPropertiesChanged(String id, String event, String interfaceName)139 public NetworkCallbackEventOnLinkPropertiesChanged(String id, String event, 140 String interfaceName) { 141 super(id, event); 142 mInterfaceName = interfaceName; 143 } 144 145 /** 146 * Create a JSON data-structure. 147 */ toJSON()148 public JSONObject toJSON() throws JSONException { 149 JSONObject json = super.toJSON(); 150 json.put(ConnectivityConstants.NetworkCallbackContainer.INTERFACE_NAME, mInterfaceName); 151 return json; 152 } 153 } 154 } 155