Merge "Add documentation to NetworkStateTracker and a small change to the API."
This commit is contained in:
@@ -67,15 +67,10 @@ public class MobileDataStateTracker implements NetworkStateTracker {
|
||||
|
||||
/**
|
||||
* Create a new MobileDataStateTracker
|
||||
* @param context the application context of the caller
|
||||
* @param target a message handler for getting callbacks about state changes
|
||||
* @param netType the ConnectivityManager network type
|
||||
* @param apnType the Phone apnType
|
||||
* @param tag the name of this network
|
||||
*/
|
||||
public MobileDataStateTracker(Context context, Handler target, int netType, String tag) {
|
||||
mTarget = target;
|
||||
mContext = context;
|
||||
public MobileDataStateTracker(int netType, String tag) {
|
||||
mNetworkInfo = new NetworkInfo(netType,
|
||||
TelephonyManager.getDefault().getNetworkType(), tag,
|
||||
TelephonyManager.getDefault().getNetworkTypeName());
|
||||
@@ -100,6 +95,25 @@ public class MobileDataStateTracker implements NetworkStateTracker {
|
||||
"net.ppp0.dns2"};
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin monitoring data connectivity.
|
||||
*
|
||||
* @param context is the current Android context
|
||||
* @param target is the Hander to which to return the events.
|
||||
*/
|
||||
public void startMonitoring(Context context, Handler target) {
|
||||
mTarget = target;
|
||||
mContext = context;
|
||||
|
||||
IntentFilter filter =
|
||||
new IntentFilter(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
|
||||
filter.addAction(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED);
|
||||
filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
|
||||
|
||||
mContext.registerReceiver(new MobileDataStateReceiver(), filter);
|
||||
mMobileDataState = Phone.DataState.DISCONNECTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the IP addresses of the DNS servers available for the mobile data
|
||||
* network interface.
|
||||
@@ -139,45 +153,6 @@ public class MobileDataStateTracker implements NetworkStateTracker {
|
||||
public void releaseWakeLock() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin monitoring mobile data connectivity.
|
||||
*/
|
||||
public void startMonitoring() {
|
||||
IntentFilter filter =
|
||||
new IntentFilter(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
|
||||
filter.addAction(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED);
|
||||
filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
|
||||
|
||||
mContext.registerReceiver(new MobileDataStateReceiver(), filter);
|
||||
mMobileDataState = Phone.DataState.DISCONNECTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record the roaming status of the device, and if it is a change from the previous
|
||||
* status, send a notification to any listeners.
|
||||
* @param isRoaming {@code true} if the device is now roaming, {@code false}
|
||||
* if it is no longer roaming.
|
||||
*/
|
||||
private void setRoamingStatus(boolean isRoaming) {
|
||||
if (isRoaming != mNetworkInfo.isRoaming()) {
|
||||
mNetworkInfo.setRoaming(isRoaming);
|
||||
Message msg = mTarget.obtainMessage(EVENT_ROAMING_CHANGED, mNetworkInfo);
|
||||
msg.sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private void setSubtype(int subtype, String subtypeName) {
|
||||
if (mNetworkInfo.isConnected()) {
|
||||
int oldSubtype = mNetworkInfo.getSubtype();
|
||||
if (subtype != oldSubtype) {
|
||||
mNetworkInfo.setSubtype(subtype, subtypeName);
|
||||
Message msg = mTarget.obtainMessage(
|
||||
EVENT_NETWORK_SUBTYPE_CHANGED, oldSubtype, 0, mNetworkInfo);
|
||||
msg.sendToTarget();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MobileDataStateReceiver extends BroadcastReceiver {
|
||||
IConnectivityManager mConnectivityManager;
|
||||
|
||||
@@ -279,8 +254,6 @@ public class MobileDataStateTracker implements NetworkStateTracker {
|
||||
setDetailedState(DetailedState.FAILED, reason, apnName);
|
||||
}
|
||||
TelephonyManager tm = TelephonyManager.getDefault();
|
||||
setRoamingStatus(tm.isNetworkRoaming());
|
||||
setSubtype(tm.getNetworkType(), tm.getNetworkTypeName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,29 +16,75 @@
|
||||
|
||||
package android.net;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
|
||||
/**
|
||||
* Interface for connectivity service to act on a network interface.
|
||||
* All state information for a network should be kept in a Tracker class.
|
||||
* This interface defines network-type-independent functions that should
|
||||
* be implemented by the Tracker class.
|
||||
* Interface provides the {@link com.android.server.ConnectivityService}
|
||||
* with three services. Events to the ConnectivityService when
|
||||
* changes occur, an API for controlling the network and storage
|
||||
* for network specific information.
|
||||
*
|
||||
* The Connectivity will call startMonitoring before any other
|
||||
* method is called.
|
||||
*
|
||||
* {@hide}
|
||||
*/
|
||||
public interface NetworkStateTracker {
|
||||
|
||||
public static final int EVENT_STATE_CHANGED = 1;
|
||||
/**
|
||||
* arg1: 1 to show, 0 to hide
|
||||
* arg2: ID of the notification
|
||||
* obj: Notification (if showing)
|
||||
* -------------------------------------------------------------
|
||||
* Event Interface back to ConnectivityService.
|
||||
*
|
||||
* The events that are to be sent back to the Handler passed
|
||||
* to startMonitoring when the particular event occurs.
|
||||
* -------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* The network state has changed and the NetworkInfo object
|
||||
* contains the new state.
|
||||
*
|
||||
* msg.what = EVENT_STATE_CHANGED
|
||||
* msg.obj = NetworkInfo object
|
||||
*/
|
||||
public static final int EVENT_STATE_CHANGED = 1;
|
||||
|
||||
/**
|
||||
* msg.what = EVENT_CONFIGURATION_CHANGED
|
||||
* msg.obj = NetworkInfo object
|
||||
*/
|
||||
public static final int EVENT_NOTIFICATION_CHANGED = 2;
|
||||
public static final int EVENT_CONFIGURATION_CHANGED = 3;
|
||||
public static final int EVENT_ROAMING_CHANGED = 4;
|
||||
public static final int EVENT_NETWORK_SUBTYPE_CHANGED = 5;
|
||||
|
||||
/**
|
||||
* msg.what = EVENT_RESTORE_DEFAULT_NETWORK
|
||||
* msg.obj = FeatureUser object
|
||||
*/
|
||||
public static final int EVENT_RESTORE_DEFAULT_NETWORK = 6;
|
||||
|
||||
/**
|
||||
* USED by ConnectivityService only
|
||||
*
|
||||
* msg.what = EVENT_CLEAR_NET_TRANSITION_WAKELOCK
|
||||
* msg.arg1 = mNetTransitionWakeLockSerialNumber
|
||||
*/
|
||||
public static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 7;
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------
|
||||
* Control Interface
|
||||
* -------------------------------------------------------------
|
||||
*/
|
||||
/**
|
||||
* Begin monitoring data connectivity.
|
||||
*
|
||||
* This is the first method called when this interface is used.
|
||||
*
|
||||
* @param context is the current Android context
|
||||
* @param target is the Hander to which to return the events.
|
||||
*/
|
||||
public void startMonitoring(Context context, Handler target);
|
||||
|
||||
/**
|
||||
* Fetch NetworkInfo for the network
|
||||
*/
|
||||
@@ -55,43 +101,6 @@ public interface NetworkStateTracker {
|
||||
*/
|
||||
public String getTcpBufferSizesPropName();
|
||||
|
||||
/**
|
||||
* Check if private DNS route is set for the network
|
||||
*/
|
||||
public boolean isPrivateDnsRouteSet();
|
||||
|
||||
/**
|
||||
* Set a flag indicating private DNS route is set
|
||||
*/
|
||||
public void privateDnsRouteSet(boolean enabled);
|
||||
|
||||
/**
|
||||
* Fetch default gateway address for the network
|
||||
*/
|
||||
public int getDefaultGatewayAddr();
|
||||
|
||||
/**
|
||||
* Check if default route is set
|
||||
*/
|
||||
public boolean isDefaultRouteSet();
|
||||
|
||||
/**
|
||||
* Set a flag indicating default route is set for the network
|
||||
*/
|
||||
public void defaultRouteSet(boolean enabled);
|
||||
|
||||
/**
|
||||
* Indicate tear down requested from connectivity
|
||||
*/
|
||||
public void setTeardownRequested(boolean isRequested);
|
||||
|
||||
/**
|
||||
* Check if tear down was requested
|
||||
*/
|
||||
public boolean isTeardownRequested();
|
||||
|
||||
public void startMonitoring();
|
||||
|
||||
/**
|
||||
* Disable connectivity to a network
|
||||
* @return {@code true} if a teardown occurred, {@code false} if the
|
||||
@@ -118,6 +127,11 @@ public interface NetworkStateTracker {
|
||||
*/
|
||||
public boolean isAvailable();
|
||||
|
||||
/**
|
||||
* Fetch default gateway address for the network
|
||||
*/
|
||||
public int getDefaultGatewayAddr();
|
||||
|
||||
/**
|
||||
* Tells the underlying networking system that the caller wants to
|
||||
* begin using the named feature. The interpretation of {@code feature}
|
||||
@@ -146,4 +160,41 @@ public interface NetworkStateTracker {
|
||||
*/
|
||||
public int stopUsingNetworkFeature(String feature, int callingPid, int callingUid);
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------
|
||||
* Storage API used by ConnectivityService for saving
|
||||
* Network specific information.
|
||||
* -------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if private DNS route is set for the network
|
||||
*/
|
||||
public boolean isPrivateDnsRouteSet();
|
||||
|
||||
/**
|
||||
* Set a flag indicating private DNS route is set
|
||||
*/
|
||||
public void privateDnsRouteSet(boolean enabled);
|
||||
|
||||
/**
|
||||
* Check if default route is set
|
||||
*/
|
||||
public boolean isDefaultRouteSet();
|
||||
|
||||
/**
|
||||
* Set a flag indicating default route is set for the network
|
||||
*/
|
||||
public void defaultRouteSet(boolean enabled);
|
||||
|
||||
/**
|
||||
* Check if tear down was requested
|
||||
*/
|
||||
public boolean isTeardownRequested();
|
||||
|
||||
/**
|
||||
* Indicate tear down requested from connectivity
|
||||
*/
|
||||
public void setTeardownRequested(boolean isRequested);
|
||||
|
||||
}
|
||||
|
||||
@@ -313,21 +313,21 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
||||
switch (mNetAttributes[netType].mRadio) {
|
||||
case ConnectivityManager.TYPE_WIFI:
|
||||
if (DBG) Slog.v(TAG, "Starting Wifi Service.");
|
||||
WifiStateTracker wst = new WifiStateTracker(context, mHandler);
|
||||
WifiStateTracker wst = new WifiStateTracker();
|
||||
WifiService wifiService = new WifiService(context);
|
||||
ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
|
||||
wifiService.checkAndStartWifi();
|
||||
mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
|
||||
wst.startMonitoring();
|
||||
wst.startMonitoring(context, mHandler);
|
||||
|
||||
//TODO: as part of WWS refactor, create only when needed
|
||||
mWifiWatchdogService = new WifiWatchdogService(context);
|
||||
|
||||
break;
|
||||
case ConnectivityManager.TYPE_MOBILE:
|
||||
mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
|
||||
netType, mNetAttributes[netType].mName);
|
||||
mNetTrackers[netType].startMonitoring();
|
||||
mNetTrackers[netType] = new MobileDataStateTracker(netType,
|
||||
mNetAttributes[netType].mName);
|
||||
mNetTrackers[netType].startMonitoring(context, mHandler);
|
||||
if (noMobileData) {
|
||||
if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
|
||||
mNetTrackers[netType].teardown();
|
||||
@@ -1205,18 +1205,6 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
||||
sendConnectedBroadcast(info);
|
||||
}
|
||||
|
||||
private void handleNotificationChange(boolean visible, int id,
|
||||
Notification notification) {
|
||||
NotificationManager notificationManager = (NotificationManager) mContext
|
||||
.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
if (visible) {
|
||||
notificationManager.notify(id, notification);
|
||||
} else {
|
||||
notificationManager.cancel(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* After a change in the connectivity state of a network. We're mainly
|
||||
* concerned with making sure that the list of DNS servers is set up
|
||||
@@ -1608,25 +1596,12 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
||||
handleConnect(info);
|
||||
}
|
||||
break;
|
||||
|
||||
case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
|
||||
handleNotificationChange(msg.arg1 == 1, msg.arg2,
|
||||
(Notification) msg.obj);
|
||||
break;
|
||||
|
||||
case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
|
||||
// TODO - make this handle ip/proxy/gateway/dns changes
|
||||
info = (NetworkInfo) msg.obj;
|
||||
type = info.getType();
|
||||
handleDnsConfigurationChange(type);
|
||||
break;
|
||||
case NetworkStateTracker.EVENT_ROAMING_CHANGED:
|
||||
// fill me in
|
||||
break;
|
||||
|
||||
case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
|
||||
// fill me in
|
||||
break;
|
||||
case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
|
||||
FeatureUser u = (FeatureUser)msg.obj;
|
||||
u.expire();
|
||||
|
||||
@@ -1387,9 +1387,7 @@ public class WifiService extends IWifiManager.Stub {
|
||||
}
|
||||
|
||||
if (mNotification == null) {
|
||||
// Cache the Notification mainly so we can remove the
|
||||
// EVENT_NOTIFICATION_CHANGED message with this Notification from
|
||||
// the queue later
|
||||
// Cache the Notification object.
|
||||
mNotification = new Notification();
|
||||
mNotification.when = 0;
|
||||
mNotification.icon = ICON_NETWORKS_AVAILABLE;
|
||||
@@ -1408,30 +1406,10 @@ public class WifiService extends IWifiManager.Stub {
|
||||
mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
|
||||
|
||||
notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
|
||||
/*
|
||||
* TODO: Clean up connectivity service & remove this
|
||||
*/
|
||||
/* message = mCsHandler.obtainMessage(EVENT_NOTIFICATION_CHANGED, 1,
|
||||
ICON_NETWORKS_AVAILABLE, mNotification); */
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
|
||||
/*
|
||||
* TODO: Clean up connectivity service & remove this
|
||||
*/
|
||||
/*
|
||||
// Remove any pending messages to show the notification
|
||||
mCsHandler.removeMessages(EVENT_NOTIFICATION_CHANGED, mNotification);
|
||||
|
||||
message = mCsHandler.obtainMessage(EVENT_NOTIFICATION_CHANGED, 0,
|
||||
ICON_NETWORKS_AVAILABLE);
|
||||
*/
|
||||
}
|
||||
|
||||
//mCsHandler.sendMessageDelayed(message, delay);
|
||||
|
||||
mNotificationShown = visible;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,10 +56,7 @@ public class WifiStateTracker implements NetworkStateTracker {
|
||||
private BroadcastReceiver mWifiStateReceiver;
|
||||
private WifiManager mWifiManager;
|
||||
|
||||
public WifiStateTracker(Context context, Handler target) {
|
||||
mCsHandler = target;
|
||||
mContext = context;
|
||||
|
||||
public WifiStateTracker() {
|
||||
mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, NETWORKTYPE, "");
|
||||
mNetworkProperties = new NetworkProperties();
|
||||
|
||||
@@ -80,7 +77,10 @@ public class WifiStateTracker implements NetworkStateTracker {
|
||||
/**
|
||||
* Begin monitoring wifi connectivity
|
||||
*/
|
||||
public void startMonitoring() {
|
||||
public void startMonitoring(Context context, Handler target) {
|
||||
mCsHandler = target;
|
||||
mContext = context;
|
||||
|
||||
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
|
||||
|
||||
Reference in New Issue
Block a user