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