Home | History | Annotate | Download | only in facade
      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 import android.net.wifi.aware.WifiAwareNetworkInfo;
     21 
     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 packet keep-alive event to JSON.
     37      */
     38     public static class PacketKeepaliveEvent implements JsonSerializable {
     39         private String mId;
     40         private String mPacketKeepaliveEvent;
     41 
     42         public PacketKeepaliveEvent(String id, String event) {
     43             mId = id;
     44             mPacketKeepaliveEvent = event;
     45         }
     46 
     47         /**
     48          * Create a JSON data-structure.
     49          */
     50         public JSONObject toJSON() throws JSONException {
     51             JSONObject packetKeepalive = new JSONObject();
     52 
     53             packetKeepalive.put(
     54                     ConnectivityConstants.PacketKeepaliveContainer.ID,
     55                     mId);
     56             packetKeepalive.put(
     57                     ConnectivityConstants.PacketKeepaliveContainer.PACKET_KEEPALIVE_EVENT,
     58                     mPacketKeepaliveEvent);
     59 
     60             return packetKeepalive;
     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 
     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          */
     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 
    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          */
    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 
    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          */
    137         public JSONObject toJSON() throws JSONException {
    138             JSONObject json = super.toJSON();
    139             json.put(ConnectivityConstants.NetworkCallbackContainer.RSSI,
    140                     mNetworkCapabilities.getSignalStrength());
    141             if (mNetworkCapabilities.getNetworkSpecifier() != null) {
    142                 json.put("network_specifier",
    143                         mNetworkCapabilities.getNetworkSpecifier().toString());
    144             }
    145             if (mNetworkCapabilities.getTransportInfo() instanceof WifiAwareNetworkInfo) {
    146                 WifiAwareNetworkInfo anc =
    147                         (WifiAwareNetworkInfo) mNetworkCapabilities.getTransportInfo();
    148 
    149                 String ipv6 = anc.getPeerIpv6Addr().toString();
    150                 if (ipv6.charAt(0) == '/') {
    151                     ipv6 = ipv6.substring(1);
    152                 }
    153                 json.put("aware_ipv6", ipv6);
    154                 if (anc.getPort() != 0) {
    155                     json.put("aware_port", anc.getPort());
    156                 }
    157                 if (anc.getTransportProtocol() != -1) {
    158                     json.put("aware_transport_protocol", anc.getTransportProtocol());
    159                 }
    160             }
    161             return json;
    162         }
    163     }
    164 
    165     /**
    166      * Specializes NetworkCallbackEventBase to add information for the onCapabilitiesChanged()
    167      * callback.
    168      */
    169     public static class NetworkCallbackEventOnLinkPropertiesChanged extends
    170             NetworkCallbackEventBase {
    171         private String mInterfaceName;
    172 
    173         public NetworkCallbackEventOnLinkPropertiesChanged(String id, String event,
    174                 long createTimestamp, String interfaceName) {
    175             super(id, event, createTimestamp);
    176             mInterfaceName = interfaceName;
    177         }
    178 
    179         /**
    180          * Create a JSON data-structure.
    181          */
    182         public JSONObject toJSON() throws JSONException {
    183             JSONObject json = super.toJSON();
    184             json.put(ConnectivityConstants.NetworkCallbackContainer.INTERFACE_NAME, mInterfaceName);
    185             return json;
    186         }
    187     }
    188 }
    189