First pass on multinetwork framework

Starting to switch netd to use NetId.
Adding the Network identifying class

bug:13550136
Change-Id: Ie0db4fb17c9300bfafb63329adfa02339911b33d
This commit is contained in:
Robert Greenwalt
2014-03-19 17:56:12 -07:00
parent c8b9171607
commit 42a0e1ee84
3 changed files with 121 additions and 11 deletions

View File

@@ -408,6 +408,11 @@ public class ConnectivityManager {
*/
public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
/**
* @hide
*/
public final static int INVALID_NET_ID = 0;
private final IConnectivityManager mService;
private final String mPackageName;

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import android.os.Parcelable;
import android.os.Parcel;
/**
* Identifies the Network.
* @hide
*/
public class Network implements Parcelable {
public final int netId;
public Network(int netId) {
this.netId = netId;
}
public Network(Network that) {
this.netId = that.netId;
}
// implement the Parcelable interface
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(netId);
}
public static final Creator<Network> CREATOR =
new Creator<Network>() {
public Network createFromParcel(Parcel in) {
int netId = in.readInt();
return new Network(netId);
}
public Network[] newArray(int size) {
return new Network[size];
}
};
}

View File

@@ -65,6 +65,7 @@ import android.net.LinkProperties;
import android.net.LinkProperties.CompareResult;
import android.net.LinkQualityInfo;
import android.net.MobileDataStateTracker;
import android.net.Network;
import android.net.NetworkConfig;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
@@ -165,6 +166,8 @@ import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import static android.net.ConnectivityManager.INVALID_NET_ID;
/**
* @hide
*/
@@ -442,6 +445,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
TelephonyManager mTelephonyManager;
private final static int MIN_NET_ID = 10; // some reserved marks
private final static int MAX_NET_ID = 65535;
private int mNextNetId = MIN_NET_ID;
public ConnectivityService(Context context, INetworkManagementService netd,
INetworkStatsService statsService, INetworkPolicyManager policyManager) {
// Currently, omitting a NetworkFactory will create one internally
@@ -706,6 +713,12 @@ public class ConnectivityService extends IConnectivityManager.Stub {
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
}
private synchronized int nextNetId() {
int netId = mNextNetId;
if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID;
return netId;
}
/**
* Factory that creates {@link NetworkStateTracker} instances using given
* {@link NetworkConfig}.
@@ -1984,6 +1997,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
int prevNetType = info.getType();
mNetTrackers[prevNetType].setTeardownRequested(false);
int thisNetId = mNetTrackers[prevNetType].getNetwork().netId;
// Remove idletimer previously setup in {@code handleConnect}
if (mNetConfigs[prevNetType].isDefault()) {
@@ -2069,6 +2083,13 @@ public class ConnectivityService extends IConnectivityManager.Stub {
sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(),
getConnectivityChangeDelay());
}
try {
mNetd.removeNetwork(thisNetId);
} catch (Exception e) {
loge("Exception removing network: " + e);
} finally {
mNetTrackers[prevNetType].setNetId(INVALID_NET_ID);
}
}
private void tryFailover(int prevNetType) {
@@ -2336,6 +2357,8 @@ public class ConnectivityService extends IConnectivityManager.Stub {
if (mNetConfigs[newNetType].isDefault()) {
if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) {
if (isNewNetTypePreferredOverCurrentNetType(newNetType)) {
String teardownPolicy = SystemProperties.get("net.teardownPolicy");
if (TextUtils.equals(teardownPolicy, "keep") == false) {
// tear down the other
NetworkStateTracker otherNet =
mNetTrackers[mActiveDefaultNetwork];
@@ -2348,6 +2371,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
teardown(thisNet);
return;
}
} else {
//TODO - remove
loge("network teardown skipped due to net.teardownPolicy setting");
}
} else {
// don't accept this one
if (VDBG) {
@@ -2358,6 +2385,15 @@ public class ConnectivityService extends IConnectivityManager.Stub {
return;
}
}
int thisNetId = nextNetId();
thisNet.setNetId(thisNetId);
try {
mNetd.createNetwork(thisNetId, thisIface);
} catch (Exception e) {
loge("Exception creating network :" + e);
teardown(thisNet);
return;
}
setupDataActivityTracking(newNetType);
synchronized (ConnectivityService.this) {
// have a new default network, release the transition wakelock in a second
@@ -2380,6 +2416,16 @@ public class ConnectivityService extends IConnectivityManager.Stub {
// Don't do this - if we never sign in stay, grey
//reportNetworkCondition(mActiveDefaultNetwork, 100);
updateNetworkSettings(thisNet);
} else {
int thisNetId = nextNetId();
thisNet.setNetId(thisNetId);
try {
mNetd.createNetwork(thisNetId, thisIface);
} catch (Exception e) {
loge("Exception creating network :" + e);
teardown(thisNet);
return;
}
}
thisNet.setTeardownRequested(false);
updateMtuSizeSettings(thisNet);