Home | History | Annotate | Download | only in connectivity
      1 /*
      2  * Copyright (C) 2014 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.android.server.connectivity;
     18 
     19 import android.content.Context;
     20 import android.net.LinkProperties;
     21 import android.net.Network;
     22 import android.net.NetworkCapabilities;
     23 import android.net.NetworkInfo;
     24 import android.net.NetworkMisc;
     25 import android.net.NetworkRequest;
     26 import android.os.Handler;
     27 import android.os.Messenger;
     28 import android.util.SparseArray;
     29 
     30 import com.android.internal.util.AsyncChannel;
     31 import com.android.server.connectivity.NetworkMonitor;
     32 
     33 import java.util.ArrayList;
     34 
     35 /**
     36  * A bag class used by ConnectivityService for holding a collection of most recent
     37  * information published by a particular NetworkAgent as well as the
     38  * AsyncChannel/messenger for reaching that NetworkAgent and lists of NetworkRequests
     39  * interested in using it.
     40  */
     41 public class NetworkAgentInfo {
     42     public NetworkInfo networkInfo;
     43     public Network network;
     44     public LinkProperties linkProperties;
     45     public NetworkCapabilities networkCapabilities;
     46     public final NetworkMonitor networkMonitor;
     47     public final NetworkMisc networkMisc;
     48     public boolean created;
     49     public boolean validated;
     50 
     51     // This represents the last score received from the NetworkAgent.
     52     private int currentScore;
     53     // Penalty applied to scores of Networks that have not been validated.
     54     private static final int UNVALIDATED_SCORE_PENALTY = 40;
     55 
     56     // Score for explicitly connected network.
     57     private static final int EXPLICITLY_SELECTED_NETWORK_SCORE = 100;
     58 
     59     // The list of NetworkRequests being satisfied by this Network.
     60     public final SparseArray<NetworkRequest> networkRequests = new SparseArray<NetworkRequest>();
     61     public final ArrayList<NetworkRequest> networkLingered = new ArrayList<NetworkRequest>();
     62 
     63     public final Messenger messenger;
     64     public final AsyncChannel asyncChannel;
     65 
     66     public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, NetworkInfo info,
     67             LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler,
     68             NetworkMisc misc) {
     69         this.messenger = messenger;
     70         asyncChannel = ac;
     71         network = null;
     72         networkInfo = info;
     73         linkProperties = lp;
     74         networkCapabilities = nc;
     75         currentScore = score;
     76         networkMonitor = new NetworkMonitor(context, handler, this);
     77         networkMisc = misc;
     78         created = false;
     79         validated = false;
     80     }
     81 
     82     public void addRequest(NetworkRequest networkRequest) {
     83         networkRequests.put(networkRequest.requestId, networkRequest);
     84     }
     85 
     86     public boolean isVPN() {
     87         return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
     88     }
     89 
     90     // Get the current score for this Network.  This may be modified from what the
     91     // NetworkAgent sent, as it has modifiers applied to it.
     92     public int getCurrentScore() {
     93         // TODO: We may want to refactor this into a NetworkScore class that takes a base score from
     94         // the NetworkAgent and signals from the NetworkAgent and uses those signals to modify the
     95         // score.  The NetworkScore class would provide a nice place to centralize score constants
     96         // so they are not scattered about the transports.
     97 
     98         int score = currentScore;
     99 
    100         if (!validated) score -= UNVALIDATED_SCORE_PENALTY;
    101         if (score < 0) score = 0;
    102 
    103         if (networkMisc.explicitlySelected) score = EXPLICITLY_SELECTED_NETWORK_SCORE;
    104 
    105         return score;
    106     }
    107 
    108     public void setCurrentScore(int newScore) {
    109         currentScore = newScore;
    110     }
    111 
    112     public String toString() {
    113         return "NetworkAgentInfo{ ni{" + networkInfo + "}  network{" +
    114                 network + "}  lp{" +
    115                 linkProperties + "}  nc{" +
    116                 networkCapabilities + "}  Score{" + getCurrentScore() + "} " +
    117                 "validated{" + validated + "} created{" + created + "} " +
    118                 "explicitlySelected{" + networkMisc.explicitlySelected + "} }";
    119     }
    120 
    121     public String name() {
    122         return "NetworkAgentInfo [" + networkInfo.getTypeName() + " (" +
    123                 networkInfo.getSubtypeName() + ") - " +
    124                 (network == null ? "null" : network.toString()) + "]";
    125     }
    126 }
    127