NetworkAgentInfo.java revision 2161a8ea123134ee3f9a10c0f8f56aabd8289f69
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 17package com.android.server.connectivity; 18 19import android.content.Context; 20import android.net.LinkProperties; 21import android.net.Network; 22import android.net.NetworkCapabilities; 23import android.net.NetworkInfo; 24import android.net.NetworkMisc; 25import android.net.NetworkRequest; 26import android.os.Handler; 27import android.os.Messenger; 28import android.util.SparseArray; 29 30import com.android.internal.util.AsyncChannel; 31import com.android.server.connectivity.NetworkMonitor; 32 33import 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 */ 41public 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 // The list of NetworkRequests being satisfied by this Network. 57 public final SparseArray<NetworkRequest> networkRequests = new SparseArray<NetworkRequest>(); 58 public final ArrayList<NetworkRequest> networkLingered = new ArrayList<NetworkRequest>(); 59 60 public final Messenger messenger; 61 public final AsyncChannel asyncChannel; 62 63 public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, NetworkInfo info, 64 LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler, 65 NetworkMisc misc) { 66 this.messenger = messenger; 67 asyncChannel = ac; 68 network = null; 69 networkInfo = info; 70 linkProperties = lp; 71 networkCapabilities = nc; 72 currentScore = score; 73 networkMonitor = new NetworkMonitor(context, handler, this); 74 networkMisc = misc; 75 created = false; 76 validated = false; 77 } 78 79 public void addRequest(NetworkRequest networkRequest) { 80 networkRequests.put(networkRequest.requestId, networkRequest); 81 } 82 83 public boolean isVPN() { 84 return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN); 85 } 86 87 // Get the current score for this Network. This may be modified from what the 88 // NetworkAgent sent, as it has modifiers applied to it. 89 public int getCurrentScore() { 90 // TODO: We may want to refactor this into a NetworkScore class that takes a base score from 91 // the NetworkAgent and signals from the NetworkAgent and uses those signals to modify the 92 // score. The NetworkScore class would provide a nice place to centralize score constants 93 // so they are not scattered about the transports. 94 95 int score = currentScore; 96 97 if (!validated) score -= UNVALIDATED_SCORE_PENALTY; 98 99 if (score < 0) score = 0; 100 101 return score; 102 } 103 104 public void setCurrentScore(int newScore) { 105 currentScore = newScore; 106 } 107 108 public String toString() { 109 return "NetworkAgentInfo{ ni{" + networkInfo + "} network{" + 110 network + "} lp{" + 111 linkProperties + "} nc{" + 112 networkCapabilities + "} Score{" + getCurrentScore() + "} " + 113 "validated{" + validated + "} created{" + created + "} }"; 114 } 115 116 public String name() { 117 return "NetworkAgentInfo [" + networkInfo.getTypeName() + " (" + 118 networkInfo.getSubtypeName() + ") - " + 119 (network == null ? "null" : network.toString()) + "]"; 120 } 121} 122