• 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;
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         private long mCreateTimestamp;
68         private long mCurrentTimestamp;
69 
NetworkCallbackEventBase(String id, String event, long createTimestamp)70         public NetworkCallbackEventBase(String id, String event, long createTimestamp) {
71             mId = id;
72             mNetworkCallbackEvent = event;
73             mCreateTimestamp = createTimestamp;
74             mCurrentTimestamp = System.currentTimeMillis();
75         }
76 
77         /**
78          * Create a JSON data-structure.
79          */
toJSON()80         public JSONObject toJSON() throws JSONException {
81             JSONObject networkCallback = new JSONObject();
82 
83             networkCallback.put(ConnectivityConstants.NetworkCallbackContainer.ID, mId);
84             networkCallback.put(
85                     ConnectivityConstants.NetworkCallbackContainer.NETWORK_CALLBACK_EVENT,
86                     mNetworkCallbackEvent);
87             networkCallback.put(ConnectivityConstants.NetworkCallbackContainer.CREATE_TIMESTAMP,
88                     mCreateTimestamp);
89             networkCallback.put(ConnectivityConstants.NetworkCallbackContainer.CURRENT_TIMESTAMP,
90                     mCurrentTimestamp);
91 
92             return networkCallback;
93         }
94     }
95 
96     /**
97      * Specializes NetworkCallbackEventBase to add information for the onLosing() callback.
98      */
99     public static class NetworkCallbackEventOnLosing extends NetworkCallbackEventBase {
100         private int mMaxMsToLive;
101 
NetworkCallbackEventOnLosing(String id, String event, long createTimestamp, int maxMsToLive)102         public NetworkCallbackEventOnLosing(String id, String event, long createTimestamp,
103                 int maxMsToLive) {
104             super(id, event, createTimestamp);
105             mMaxMsToLive = maxMsToLive;
106         }
107 
108         /**
109          * Create a JSON data-structure.
110          */
toJSON()111         public JSONObject toJSON() throws JSONException {
112             JSONObject json = super.toJSON();
113             json.put(ConnectivityConstants.NetworkCallbackContainer.MAX_MS_TO_LIVE, mMaxMsToLive);
114             return json;
115         }
116     }
117 
118     /**
119      * Specializes NetworkCallbackEventBase to add information for the onCapabilitiesChanged()
120      * callback.
121      */
122     public static class NetworkCallbackEventOnCapabilitiesChanged extends NetworkCallbackEventBase {
123         private int mRssi;
124 
NetworkCallbackEventOnCapabilitiesChanged(String id, String event, long createTimestamp, int rssi)125         public NetworkCallbackEventOnCapabilitiesChanged(String id, String event,
126                 long createTimestamp, int rssi) {
127             super(id, event, createTimestamp);
128             mRssi = rssi;
129         }
130 
131         /**
132          * Create a JSON data-structure.
133          */
toJSON()134         public JSONObject toJSON() throws JSONException {
135             JSONObject json = super.toJSON();
136             json.put(ConnectivityConstants.NetworkCallbackContainer.RSSI, mRssi);
137             return json;
138         }
139     }
140 
141     /**
142      * Specializes NetworkCallbackEventBase to add information for the onCapabilitiesChanged()
143      * callback.
144      */
145     public static class NetworkCallbackEventOnLinkPropertiesChanged extends
146             NetworkCallbackEventBase {
147         private String mInterfaceName;
148 
NetworkCallbackEventOnLinkPropertiesChanged(String id, String event, long createTimestamp, String interfaceName)149         public NetworkCallbackEventOnLinkPropertiesChanged(String id, String event,
150                 long createTimestamp, String interfaceName) {
151             super(id, event, createTimestamp);
152             mInterfaceName = interfaceName;
153         }
154 
155         /**
156          * Create a JSON data-structure.
157          */
toJSON()158         public JSONObject toJSON() throws JSONException {
159             JSONObject json = super.toJSON();
160             json.put(ConnectivityConstants.NetworkCallbackContainer.INTERFACE_NAME, mInterfaceName);
161             return json;
162         }
163     }
164 }
165