Skip to content

Commit d5fc86f

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Begin moving VPN to NetworkStateTracker pattern." into jb-mr1-dev
2 parents e7485cb + 899223b commit d5fc86f

File tree

7 files changed

+396
-101
lines changed

7 files changed

+396
-101
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (C) 2012 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 android.net;
18+
19+
import android.content.Context;
20+
import android.os.Handler;
21+
22+
import com.android.internal.util.Preconditions;
23+
24+
import java.util.concurrent.atomic.AtomicBoolean;
25+
26+
/**
27+
* Interface to control and observe state of a specific network, hiding
28+
* network-specific details from {@link ConnectivityManager}. Surfaces events
29+
* through the registered {@link Handler} to enable {@link ConnectivityManager}
30+
* to respond to state changes over time.
31+
*
32+
* @hide
33+
*/
34+
public abstract class BaseNetworkStateTracker implements NetworkStateTracker {
35+
// TODO: better document threading expectations
36+
// TODO: migrate to make NetworkStateTracker abstract class
37+
38+
public static final String PROP_TCP_BUFFER_UNKNOWN = "net.tcp.buffersize.unknown";
39+
public static final String PROP_TCP_BUFFER_WIFI = "net.tcp.buffersize.wifi";
40+
41+
protected Context mContext;
42+
private Handler mTarget;
43+
44+
protected NetworkInfo mNetworkInfo;
45+
protected LinkProperties mLinkProperties;
46+
protected LinkCapabilities mLinkCapabilities;
47+
48+
private AtomicBoolean mTeardownRequested = new AtomicBoolean(false);
49+
private AtomicBoolean mPrivateDnsRouteSet = new AtomicBoolean(false);
50+
private AtomicBoolean mDefaultRouteSet = new AtomicBoolean(false);
51+
52+
public BaseNetworkStateTracker(int networkType) {
53+
mNetworkInfo = new NetworkInfo(
54+
networkType, -1, ConnectivityManager.getNetworkTypeName(networkType), null);
55+
mLinkProperties = new LinkProperties();
56+
mLinkCapabilities = new LinkCapabilities();
57+
}
58+
59+
@Deprecated
60+
protected Handler getTargetHandler() {
61+
return mTarget;
62+
}
63+
64+
protected final void dispatchStateChanged() {
65+
// TODO: include snapshot of other fields when sending
66+
mTarget.obtainMessage(EVENT_STATE_CHANGED, getNetworkInfo()).sendToTarget();
67+
}
68+
69+
protected final void dispatchConfigurationChanged() {
70+
// TODO: include snapshot of other fields when sending
71+
mTarget.obtainMessage(EVENT_CONFIGURATION_CHANGED, getNetworkInfo()).sendToTarget();
72+
}
73+
74+
@Override
75+
public final void startMonitoring(Context context, Handler target) {
76+
mContext = Preconditions.checkNotNull(context);
77+
mTarget = Preconditions.checkNotNull(target);
78+
startMonitoringInternal();
79+
}
80+
81+
protected abstract void startMonitoringInternal();
82+
83+
@Override
84+
public final NetworkInfo getNetworkInfo() {
85+
return new NetworkInfo(mNetworkInfo);
86+
}
87+
88+
@Override
89+
public final LinkProperties getLinkProperties() {
90+
return new LinkProperties(mLinkProperties);
91+
}
92+
93+
@Override
94+
public final LinkCapabilities getLinkCapabilities() {
95+
return new LinkCapabilities(mLinkCapabilities);
96+
}
97+
98+
@Override
99+
public boolean setRadio(boolean turnOn) {
100+
// Base tracker doesn't handle radios
101+
return true;
102+
}
103+
104+
@Override
105+
public boolean isAvailable() {
106+
return mNetworkInfo.isAvailable();
107+
}
108+
109+
@Override
110+
public void setUserDataEnable(boolean enabled) {
111+
// Base tracker doesn't handle enabled flags
112+
}
113+
114+
@Override
115+
public void setPolicyDataEnable(boolean enabled) {
116+
// Base tracker doesn't handle enabled flags
117+
}
118+
119+
@Override
120+
public boolean isPrivateDnsRouteSet() {
121+
return mPrivateDnsRouteSet.get();
122+
}
123+
124+
@Override
125+
public void privateDnsRouteSet(boolean enabled) {
126+
mPrivateDnsRouteSet.set(enabled);
127+
}
128+
129+
@Override
130+
public boolean isDefaultRouteSet() {
131+
return mDefaultRouteSet.get();
132+
}
133+
134+
@Override
135+
public void defaultRouteSet(boolean enabled) {
136+
mDefaultRouteSet.set(enabled);
137+
}
138+
139+
@Override
140+
public boolean isTeardownRequested() {
141+
return mTeardownRequested.get();
142+
}
143+
144+
@Override
145+
public void setTeardownRequested(boolean isRequested) {
146+
mTeardownRequested.set(isRequested);
147+
}
148+
149+
@Override
150+
public void setDependencyMet(boolean met) {
151+
// Base tracker doesn't handle dependencies
152+
}
153+
}

core/java/android/os/SystemService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package android.os;
1818

19+
import android.util.Slog;
20+
1921
import com.google.android.collect.Maps;
2022

2123
import java.util.HashMap;
@@ -81,7 +83,7 @@ public static State getState(String service) {
8183
if (state != null) {
8284
return state;
8385
} else {
84-
throw new IllegalStateException("Service " + service + " in unknown state " + rawState);
86+
return State.STOPPED;
8587
}
8688
}
8789

core/java/com/android/internal/net/LegacyVpnInfo.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package com.android.internal.net;
1818

1919
import android.app.PendingIntent;
20+
import android.net.NetworkInfo;
2021
import android.os.Parcel;
2122
import android.os.Parcelable;
23+
import android.util.Log;
2224

2325
/**
2426
* A simple container used to carry information of the ongoing legacy VPN.
@@ -27,6 +29,8 @@
2729
* @hide
2830
*/
2931
public class LegacyVpnInfo implements Parcelable {
32+
private static final String TAG = "LegacyVpnInfo";
33+
3034
public static final int STATE_DISCONNECTED = 0;
3135
public static final int STATE_INITIALIZING = 1;
3236
public static final int STATE_CONNECTING = 2;
@@ -66,4 +70,25 @@ public LegacyVpnInfo[] newArray(int size) {
6670
return new LegacyVpnInfo[size];
6771
}
6872
};
73+
74+
/**
75+
* Return best matching {@link LegacyVpnInfo} state based on given
76+
* {@link NetworkInfo}.
77+
*/
78+
public static int stateFromNetworkInfo(NetworkInfo info) {
79+
switch (info.getDetailedState()) {
80+
case CONNECTING:
81+
return STATE_CONNECTING;
82+
case CONNECTED:
83+
return STATE_CONNECTED;
84+
case DISCONNECTED:
85+
return STATE_DISCONNECTED;
86+
case FAILED:
87+
return STATE_FAILED;
88+
default:
89+
Log.w(TAG, "Unhandled state " + info.getDetailedState()
90+
+ " ; treating as disconnected");
91+
return STATE_DISCONNECTED;
92+
}
93+
}
6994
}

core/java/com/android/internal/net/VpnConfig.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import android.os.Parcel;
2323
import android.os.Parcelable;
2424

25+
import com.android.internal.util.Preconditions;
26+
2527
import java.util.List;
2628

2729
/**
@@ -45,13 +47,14 @@ public static Intent getIntentForConfirmation() {
4547
}
4648

4749
public static PendingIntent getIntentForStatusPanel(Context context, VpnConfig config) {
50+
Preconditions.checkNotNull(config);
51+
4852
Intent intent = new Intent();
4953
intent.setClassName(DIALOGS_PACKAGE, DIALOGS_PACKAGE + ".ManageDialog");
5054
intent.putExtra("config", config);
5155
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY |
5256
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
53-
return PendingIntent.getActivity(context, 0, intent, (config == null) ?
54-
PendingIntent.FLAG_NO_CREATE : PendingIntent.FLAG_CANCEL_CURRENT);
57+
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
5558
}
5659

5760
public String user;
@@ -64,6 +67,7 @@ public static PendingIntent getIntentForStatusPanel(Context context, VpnConfig c
6467
public List<String> searchDomains;
6568
public PendingIntent configureIntent;
6669
public long startTime = -1;
70+
public boolean legacy;
6771

6872
@Override
6973
public int describeContents() {
@@ -82,6 +86,7 @@ public void writeToParcel(Parcel out, int flags) {
8286
out.writeStringList(searchDomains);
8387
out.writeParcelable(configureIntent, flags);
8488
out.writeLong(startTime);
89+
out.writeInt(legacy ? 1 : 0);
8590
}
8691

8792
public static final Parcelable.Creator<VpnConfig> CREATOR =
@@ -99,6 +104,7 @@ public VpnConfig createFromParcel(Parcel in) {
99104
config.searchDomains = in.createStringArrayList();
100105
config.configureIntent = in.readParcelable(null);
101106
config.startTime = in.readLong();
107+
config.legacy = in.readInt() != 0;
102108
return config;
103109
}
104110

packages/VpnDialogs/src/com/android/vpndialogs/ManageDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected void onResume() {
7979
mDataReceived = (TextView) view.findViewById(R.id.data_received);
8080
mDataRowsHidden = true;
8181

82-
if (mConfig.user.equals(VpnConfig.LEGACY_VPN)) {
82+
if (mConfig.legacy) {
8383
mAlertParams.mIconId = android.R.drawable.ic_dialog_info;
8484
mAlertParams.mTitle = getText(R.string.legacy_title);
8585
} else {

services/java/com/android/server/ConnectivityService.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@
115115
* @hide
116116
*/
117117
public class ConnectivityService extends IConnectivityManager.Stub {
118+
private static final String TAG = "ConnectivityService";
118119

119120
private static final boolean DBG = true;
120121
private static final boolean VDBG = false;
121-
private static final String TAG = "ConnectivityService";
122122

123123
private static final boolean LOGD_RULES = false;
124124

125+
// TODO: create better separation between radio types and network types
126+
125127
// how long to wait before switching back to a radio's default network
126128
private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
127129
// system property that can override the above value
@@ -136,6 +138,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
136138
private boolean mTetheringConfigValid = false;
137139

138140
private Vpn mVpn;
141+
private VpnCallback mVpnCallback = new VpnCallback();
139142

140143
/** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */
141144
private Object mRulesLock = new Object();
@@ -328,7 +331,7 @@ public ConnectivityService(Context context, INetworkManagementService netd,
328331
this(context, netd, statsService, policyManager, null);
329332
}
330333

331-
public ConnectivityService(Context context, INetworkManagementService netd,
334+
public ConnectivityService(Context context, INetworkManagementService netManager,
332335
INetworkStatsService statsService, INetworkPolicyManager policyManager,
333336
NetworkFactory netFactory) {
334337
if (DBG) log("ConnectivityService starting up");
@@ -366,7 +369,7 @@ public ConnectivityService(Context context, INetworkManagementService netd,
366369
}
367370

368371
mContext = checkNotNull(context, "missing Context");
369-
mNetd = checkNotNull(netd, "missing INetworkManagementService");
372+
mNetd = checkNotNull(netManager, "missing INetworkManagementService");
370373
mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
371374

372375
try {
@@ -506,11 +509,11 @@ public ConnectivityService(Context context, INetworkManagementService netd,
506509
mTethering.getTetherableBluetoothRegexs().length != 0) &&
507510
mTethering.getUpstreamIfaceTypes().length != 0);
508511

509-
mVpn = new Vpn(mContext, new VpnCallback());
512+
mVpn = new Vpn(mContext, mVpnCallback, mNetd);
513+
mVpn.startMonitoring(mContext, mTrackerHandler);
510514

511515
try {
512516
mNetd.registerObserver(mTethering);
513-
mNetd.registerObserver(mVpn);
514517
mNetd.registerObserver(mDataActivityObserver);
515518
} catch (RemoteException e) {
516519
loge("Error registering observer :" + e);
@@ -2238,9 +2241,9 @@ private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp,
22382241
*/
22392242
public void updateNetworkSettings(NetworkStateTracker nt) {
22402243
String key = nt.getTcpBufferSizesPropName();
2241-
String bufferSizes = SystemProperties.get(key);
2244+
String bufferSizes = key == null ? null : SystemProperties.get(key);
22422245

2243-
if (bufferSizes.length() == 0) {
2246+
if (TextUtils.isEmpty(bufferSizes)) {
22442247
if (VDBG) log(key + " not found in system properties. Using defaults");
22452248

22462249
// Setting to default values so we won't be stuck to previous values
@@ -3153,10 +3156,14 @@ public LegacyVpnInfo getLegacyVpnInfo() {
31533156
* be done whenever a better abstraction is developed.
31543157
*/
31553158
public class VpnCallback {
3156-
31573159
private VpnCallback() {
31583160
}
31593161

3162+
public void onStateChanged(NetworkInfo info) {
3163+
// TODO: if connected, release delayed broadcast
3164+
// TODO: if disconnected, consider kicking off reconnect
3165+
}
3166+
31603167
public void override(List<String> dnsServers, List<String> searchDomains) {
31613168
if (dnsServers == null) {
31623169
restore();

0 commit comments

Comments
 (0)