16547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen/*
2f04335f899f2cce69f843692a3cb9cec229683c2tturney * Copyright (C) 2017 The Android Open Source Project
36547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen *
46547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * Licensed under the Apache License, Version 2.0 (the "License");
56547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * you may not use this file except in compliance with the License.
66547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * You may obtain a copy of the License at
76547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen *
86547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen *      http://www.apache.org/licenses/LICENSE-2.0
96547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen *
106547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * Unless required by applicable law or agreed to in writing, software
116547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * distributed under the License is distributed on an "AS IS" BASIS,
126547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * See the License for the specific language governing permissions and
146547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * limitations under the License.
156547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen */
166547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
176547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohenpackage com.googlecode.android_scripting.facade;
186547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
196547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohenimport com.googlecode.android_scripting.jsonrpc.JsonSerializable;
206547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
216547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohenimport org.json.JSONException;
226547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohenimport org.json.JSONObject;
236547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
246547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen/**
256547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * Utility class for ConnectivityManager/Service events. Encapsulates the data in a JSON format
266547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * to be used in the test script.
276547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen *
286547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * Note that not all information is encapsulated. Add to the *Event classes as more information
296547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen * is needed.
306547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen */
316547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohenpublic class ConnectivityEvents {
326547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    /**
336547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     * Translates a packet keep-alive event to JSON.
346547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     */
356547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    public static class PacketKeepaliveEvent implements JsonSerializable {
366547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        private String mId;
376547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        private String mPacketKeepaliveEvent;
386547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
396547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        public PacketKeepaliveEvent(String id, String event) {
406547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            mId = id;
416547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            mPacketKeepaliveEvent = event;
426547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
436547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
446547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        /**
456547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         * Create a JSON data-structure.
466547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         */
476547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        public JSONObject toJSON() throws JSONException {
486547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            JSONObject packetKeepalive = new JSONObject();
496547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
506547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            packetKeepalive.put(
516547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen                    ConnectivityConstants.PacketKeepaliveContainer.ID,
526547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen                    mId);
536547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            packetKeepalive.put(
546547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen                    ConnectivityConstants.PacketKeepaliveContainer.PACKET_KEEPALIVE_EVENT,
556547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen                    mPacketKeepaliveEvent);
566547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
576547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            return packetKeepalive;
586547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
596547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    }
606547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
616547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    /**
626547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     * Translates a ConnectivityManager.NetworkCallback to JSON.
636547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     */
646547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    public static class NetworkCallbackEventBase implements JsonSerializable {
656547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        private String mId;
666547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        private String mNetworkCallbackEvent;
67a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen        private long mCreateTimestamp;
68a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen        private long mCurrentTimestamp;
696547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
70a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen        public NetworkCallbackEventBase(String id, String event, long createTimestamp) {
716547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            mId = id;
726547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            mNetworkCallbackEvent = event;
73a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen            mCreateTimestamp = createTimestamp;
74a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen            mCurrentTimestamp = System.currentTimeMillis();
756547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
766547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
776547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        /**
786547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         * Create a JSON data-structure.
796547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         */
806547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        public JSONObject toJSON() throws JSONException {
816547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            JSONObject networkCallback = new JSONObject();
826547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
836547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            networkCallback.put(ConnectivityConstants.NetworkCallbackContainer.ID, mId);
846547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            networkCallback.put(
856547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen                    ConnectivityConstants.NetworkCallbackContainer.NETWORK_CALLBACK_EVENT,
866547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen                    mNetworkCallbackEvent);
87a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen            networkCallback.put(ConnectivityConstants.NetworkCallbackContainer.CREATE_TIMESTAMP,
88a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen                    mCreateTimestamp);
89a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen            networkCallback.put(ConnectivityConstants.NetworkCallbackContainer.CURRENT_TIMESTAMP,
90a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen                    mCurrentTimestamp);
916547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
926547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            return networkCallback;
936547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
946547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    }
956547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
966547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    /**
976547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     * Specializes NetworkCallbackEventBase to add information for the onLosing() callback.
986547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     */
996547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    public static class NetworkCallbackEventOnLosing extends NetworkCallbackEventBase {
1006547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        private int mMaxMsToLive;
1016547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
102a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen        public NetworkCallbackEventOnLosing(String id, String event, long createTimestamp,
103a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen                int maxMsToLive) {
104a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen            super(id, event, createTimestamp);
1056547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            mMaxMsToLive = maxMsToLive;
1066547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
1076547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
1086547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        /**
1096547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         * Create a JSON data-structure.
1106547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         */
1116547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        public JSONObject toJSON() throws JSONException {
1126547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            JSONObject json = super.toJSON();
1136547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            json.put(ConnectivityConstants.NetworkCallbackContainer.MAX_MS_TO_LIVE, mMaxMsToLive);
1146547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            return json;
1156547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
1166547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    }
1176547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
1186547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    /**
1196547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     * Specializes NetworkCallbackEventBase to add information for the onCapabilitiesChanged()
1206547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     * callback.
1216547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen     */
1226547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    public static class NetworkCallbackEventOnCapabilitiesChanged extends NetworkCallbackEventBase {
1236547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        private int mRssi;
1246547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
125a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen        public NetworkCallbackEventOnCapabilitiesChanged(String id, String event,
126a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen                long createTimestamp, int rssi) {
127a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen            super(id, event, createTimestamp);
1286547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            mRssi = rssi;
1296547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
1306547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen
1316547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        /**
1326547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         * Create a JSON data-structure.
1336547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen         */
1346547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        public JSONObject toJSON() throws JSONException {
1356547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            JSONObject json = super.toJSON();
1366547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            json.put(ConnectivityConstants.NetworkCallbackContainer.RSSI, mRssi);
1376547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen            return json;
1386547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen        }
1396547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen    }
1403b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen
1413b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen    /**
1423b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen     * Specializes NetworkCallbackEventBase to add information for the onCapabilitiesChanged()
1433b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen     * callback.
1443b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen     */
1453b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen    public static class NetworkCallbackEventOnLinkPropertiesChanged extends
1463b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen            NetworkCallbackEventBase {
1473b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen        private String mInterfaceName;
1483b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen
1493b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen        public NetworkCallbackEventOnLinkPropertiesChanged(String id, String event,
150a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen                long createTimestamp, String interfaceName) {
151a08f945cbf88779d2d7f2e1e1b8d1b712f399032Etan Cohen            super(id, event, createTimestamp);
1523b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen            mInterfaceName = interfaceName;
1533b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen        }
1543b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen
1553b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen        /**
1563b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen         * Create a JSON data-structure.
1573b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen         */
1583b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen        public JSONObject toJSON() throws JSONException {
1593b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen            JSONObject json = super.toJSON();
1603b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen            json.put(ConnectivityConstants.NetworkCallbackContainer.INTERFACE_NAME, mInterfaceName);
1613b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen            return json;
1623b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen        }
1633b854061a05abc6c4d5c8755dd6c7bd8bef9e353Etan Cohen    }
1646547a98275d9b6d93de6bf2d9eab397920911d57Etan Cohen}
165