Refactor NetworkNotificationManager. am: c5391028ec am: a6dcb89e6d
am: 4e0916b416 Change-Id: I548b311ab8dc1d7e08f17a6b31573262c8a4518e
This commit is contained in:
@@ -38,6 +38,7 @@ import static android.net.NetworkPolicyManager.uidRulesToString;
|
|||||||
|
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
import android.app.BroadcastOptions;
|
import android.app.BroadcastOptions;
|
||||||
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
@@ -833,7 +834,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||||
|
|
||||||
mKeepaliveTracker = new KeepaliveTracker(mHandler);
|
mKeepaliveTracker = new KeepaliveTracker(mHandler);
|
||||||
mNotifier = new NetworkNotificationManager(mContext, mTelephonyManager);
|
mNotifier = new NetworkNotificationManager(mContext, mTelephonyManager,
|
||||||
|
mContext.getSystemService(NotificationManager.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
private NetworkRequest createInternetRequestForTransport(int transportType) {
|
private NetworkRequest createInternetRequestForTransport(int transportType) {
|
||||||
@@ -2232,18 +2234,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
updateCapabilities(nai, nai.networkCapabilities);
|
updateCapabilities(nai, nai.networkCapabilities);
|
||||||
}
|
}
|
||||||
if (!visible) {
|
if (!visible) {
|
||||||
mNotifier.setProvNotificationVisibleIntent(false, netId, null, 0, null,
|
mNotifier.clearNotification(netId);
|
||||||
null, false);
|
|
||||||
} else {
|
} else {
|
||||||
if (nai == null) {
|
if (nai == null) {
|
||||||
loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor");
|
loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!nai.networkMisc.provisioningNotificationDisabled) {
|
if (!nai.networkMisc.provisioningNotificationDisabled) {
|
||||||
mNotifier.setProvNotificationVisibleIntent(true, netId,
|
mNotifier.showNotification(netId, NotificationType.SIGN_IN, nai,
|
||||||
NotificationType.SIGN_IN,
|
(PendingIntent) msg.obj, nai.networkMisc.explicitlySelected);
|
||||||
nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(),
|
|
||||||
(PendingIntent)msg.obj, nai.networkMisc.explicitlySelected);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -2714,9 +2713,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
PendingIntent pendingIntent = PendingIntent.getActivityAsUser(
|
PendingIntent pendingIntent = PendingIntent.getActivityAsUser(
|
||||||
mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
|
mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
|
||||||
|
|
||||||
mNotifier.setProvNotificationVisibleIntent(true, nai.network.netId,
|
mNotifier.showNotification(nai.network.netId, NotificationType.NO_INTERNET, nai,
|
||||||
NotificationType.NO_INTERNET, nai.networkInfo.getType(),
|
pendingIntent, true);
|
||||||
nai.networkInfo.getExtraInfo(), pendingIntent, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class InternalHandler extends Handler {
|
private class InternalHandler extends Handler {
|
||||||
@@ -3725,7 +3723,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
enforceConnectivityInternalPermission();
|
enforceConnectivityInternalPermission();
|
||||||
final long ident = Binder.clearCallingIdentity();
|
final long ident = Binder.clearCallingIdentity();
|
||||||
try {
|
try {
|
||||||
mNotifier.setProvNotificationVisible(visible, networkType, action);
|
// Concatenate the range of types onto the range of NetIDs.
|
||||||
|
int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE);
|
||||||
|
mNotifier.setProvNotificationVisible(visible, id, action);
|
||||||
} finally {
|
} finally {
|
||||||
Binder.restoreCallingIdentity(ident);
|
Binder.restoreCallingIdentity(ident);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,15 +20,16 @@ import android.app.Notification;
|
|||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.NetworkCapabilities;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.util.Slog;
|
import android.util.Slog;
|
||||||
|
|
||||||
import com.android.internal.R;
|
import com.android.internal.R;
|
||||||
|
|
||||||
import static android.net.ConnectivityManager.getNetworkTypeName;
|
import static android.net.NetworkCapabilities.*;
|
||||||
|
|
||||||
|
|
||||||
public class NetworkNotificationManager {
|
public class NetworkNotificationManager {
|
||||||
@@ -43,10 +44,36 @@ public class NetworkNotificationManager {
|
|||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final TelephonyManager mTelephonyManager;
|
private final TelephonyManager mTelephonyManager;
|
||||||
|
private final NotificationManager mNotificationManager;
|
||||||
|
|
||||||
public NetworkNotificationManager(Context context, TelephonyManager telephonyManager) {
|
public NetworkNotificationManager(Context c, TelephonyManager t, NotificationManager n) {
|
||||||
mContext = context;
|
mContext = c;
|
||||||
mTelephonyManager = telephonyManager;
|
mTelephonyManager = t;
|
||||||
|
mNotificationManager = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: deal more gracefully with multi-transport networks.
|
||||||
|
private static int getFirstTransportType(NetworkAgentInfo nai) {
|
||||||
|
for (int i = 0; i < 64; i++) {
|
||||||
|
if (nai.networkCapabilities.hasTransport(i)) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getTransportName(int transportType) {
|
||||||
|
Resources r = Resources.getSystem();
|
||||||
|
String[] networkTypes = r.getStringArray(R.array.network_switch_type_name);
|
||||||
|
try {
|
||||||
|
return networkTypes[transportType];
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
return r.getString(R.string.network_switch_type_name_unknown);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getIcon(int transportType) {
|
||||||
|
return (transportType == TRANSPORT_WIFI) ?
|
||||||
|
R.drawable.stat_notify_wifi_in_range : // TODO: Distinguish ! from ?.
|
||||||
|
R.drawable.stat_notify_rssi_in_range;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,98 +91,103 @@ public class NetworkNotificationManager {
|
|||||||
* between show and hide calls. We use the NetID value but for legacy callers
|
* between show and hide calls. We use the NetID value but for legacy callers
|
||||||
* we concatenate the range of types with the range of NetIDs.
|
* we concatenate the range of types with the range of NetIDs.
|
||||||
*/
|
*/
|
||||||
public void setProvNotificationVisibleIntent(boolean visible, int id,
|
public void showNotification(int id, NotificationType notifyType,
|
||||||
NotificationType notifyType, int networkType, String extraInfo, PendingIntent intent,
|
NetworkAgentInfo nai, PendingIntent intent, boolean highPriority) {
|
||||||
boolean highPriority) {
|
int transportType;
|
||||||
if (VDBG || (DBG && visible)) {
|
String extraInfo;
|
||||||
Slog.d(TAG, "setProvNotificationVisibleIntent " + notifyType + " visible=" + visible
|
if (nai != null) {
|
||||||
+ " networkType=" + getNetworkTypeName(networkType)
|
transportType = getFirstTransportType(nai);
|
||||||
|
extraInfo = nai.networkInfo.getExtraInfo();
|
||||||
|
// Only notify for Internet-capable networks.
|
||||||
|
if (!nai.networkCapabilities.hasCapability(NET_CAPABILITY_INTERNET)) return;
|
||||||
|
} else {
|
||||||
|
// Legacy notifications.
|
||||||
|
transportType = TRANSPORT_CELLULAR;
|
||||||
|
extraInfo = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "showNotification " + notifyType
|
||||||
|
+ " transportType=" + getTransportName(transportType)
|
||||||
+ " extraInfo=" + extraInfo + " highPriority=" + highPriority);
|
+ " extraInfo=" + extraInfo + " highPriority=" + highPriority);
|
||||||
}
|
}
|
||||||
|
|
||||||
Resources r = Resources.getSystem();
|
Resources r = Resources.getSystem();
|
||||||
NotificationManager notificationManager = (NotificationManager) mContext
|
CharSequence title;
|
||||||
.getSystemService(Context.NOTIFICATION_SERVICE);
|
CharSequence details;
|
||||||
|
int icon = getIcon(transportType);
|
||||||
if (visible) {
|
if (notifyType == NotificationType.NO_INTERNET && transportType == TRANSPORT_WIFI) {
|
||||||
CharSequence title;
|
title = r.getString(R.string.wifi_no_internet, 0);
|
||||||
CharSequence details;
|
details = r.getString(R.string.wifi_no_internet_detailed);
|
||||||
int icon;
|
} else if (notifyType == NotificationType.SIGN_IN) {
|
||||||
if (notifyType == NotificationType.NO_INTERNET &&
|
switch (transportType) {
|
||||||
networkType == ConnectivityManager.TYPE_WIFI) {
|
case TRANSPORT_WIFI:
|
||||||
title = r.getString(R.string.wifi_no_internet, 0);
|
title = r.getString(R.string.wifi_available_sign_in, 0);
|
||||||
details = r.getString(R.string.wifi_no_internet_detailed);
|
details = r.getString(R.string.network_available_sign_in_detailed, extraInfo);
|
||||||
icon = R.drawable.stat_notify_wifi_in_range; // TODO: Need new icon.
|
break;
|
||||||
} else if (notifyType == NotificationType.SIGN_IN) {
|
case TRANSPORT_CELLULAR:
|
||||||
switch (networkType) {
|
title = r.getString(R.string.network_available_sign_in, 0);
|
||||||
case ConnectivityManager.TYPE_WIFI:
|
// TODO: Change this to pull from NetworkInfo once a printable
|
||||||
title = r.getString(R.string.wifi_available_sign_in, 0);
|
// name has been added to it
|
||||||
details = r.getString(R.string.network_available_sign_in_detailed,
|
details = mTelephonyManager.getNetworkOperatorName();
|
||||||
extraInfo);
|
break;
|
||||||
icon = R.drawable.stat_notify_wifi_in_range;
|
default:
|
||||||
break;
|
title = r.getString(R.string.network_available_sign_in, 0);
|
||||||
case ConnectivityManager.TYPE_MOBILE:
|
details = r.getString(R.string.network_available_sign_in_detailed, extraInfo);
|
||||||
case ConnectivityManager.TYPE_MOBILE_HIPRI:
|
break;
|
||||||
title = r.getString(R.string.network_available_sign_in, 0);
|
|
||||||
// TODO: Change this to pull from NetworkInfo once a printable
|
|
||||||
// name has been added to it
|
|
||||||
details = mTelephonyManager.getNetworkOperatorName();
|
|
||||||
icon = R.drawable.stat_notify_rssi_in_range;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
title = r.getString(R.string.network_available_sign_in, 0);
|
|
||||||
details = r.getString(R.string.network_available_sign_in_detailed,
|
|
||||||
extraInfo);
|
|
||||||
icon = R.drawable.stat_notify_rssi_in_range;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network type "
|
|
||||||
+ getNetworkTypeName(networkType));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Notification notification = new Notification.Builder(mContext)
|
|
||||||
.setWhen(0)
|
|
||||||
.setSmallIcon(icon)
|
|
||||||
.setAutoCancel(true)
|
|
||||||
.setTicker(title)
|
|
||||||
.setColor(mContext.getColor(
|
|
||||||
com.android.internal.R.color.system_notification_accent_color))
|
|
||||||
.setContentTitle(title)
|
|
||||||
.setContentText(details)
|
|
||||||
.setContentIntent(intent)
|
|
||||||
.setLocalOnly(true)
|
|
||||||
.setPriority(highPriority ?
|
|
||||||
Notification.PRIORITY_HIGH :
|
|
||||||
Notification.PRIORITY_DEFAULT)
|
|
||||||
.setDefaults(highPriority ? Notification.DEFAULT_ALL : 0)
|
|
||||||
.setOnlyAlertOnce(true)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try {
|
|
||||||
notificationManager.notifyAsUser(NOTIFICATION_ID, id, notification, UserHandle.ALL);
|
|
||||||
} catch (NullPointerException npe) {
|
|
||||||
Slog.d(TAG, "setNotificationVisible: visible notificationManager npe=" + npe);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network transport "
|
||||||
notificationManager.cancelAsUser(NOTIFICATION_ID, id, UserHandle.ALL);
|
+ getTransportName(transportType));
|
||||||
} catch (NullPointerException npe) {
|
return;
|
||||||
Slog.d(TAG, "setNotificationVisible: cancel notificationManager npe=" + npe);
|
}
|
||||||
}
|
|
||||||
|
Notification notification = new Notification.Builder(mContext)
|
||||||
|
.setWhen(0)
|
||||||
|
.setSmallIcon(icon)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.setTicker(title)
|
||||||
|
.setColor(mContext.getColor(
|
||||||
|
com.android.internal.R.color.system_notification_accent_color))
|
||||||
|
.setContentTitle(title)
|
||||||
|
.setContentText(details)
|
||||||
|
.setContentIntent(intent)
|
||||||
|
.setLocalOnly(true)
|
||||||
|
.setPriority(highPriority ?
|
||||||
|
Notification.PRIORITY_HIGH :
|
||||||
|
Notification.PRIORITY_DEFAULT)
|
||||||
|
.setDefaults(highPriority ? Notification.DEFAULT_ALL : 0)
|
||||||
|
.setOnlyAlertOnce(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
mNotificationManager.notifyAsUser(NOTIFICATION_ID, id, notification, UserHandle.ALL);
|
||||||
|
} catch (NullPointerException npe) {
|
||||||
|
Slog.d(TAG, "setNotificationVisible: visible notificationManager npe=" + npe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearNotification(int id) {
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "clearNotification id=" + id);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
mNotificationManager.cancelAsUser(NOTIFICATION_ID, id, UserHandle.ALL);
|
||||||
|
} catch (NullPointerException npe) {
|
||||||
|
Slog.d(TAG, "setNotificationVisible: cancel notificationManager npe=" + npe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Legacy provisioning notifications coming directly from DcTracker.
|
* Legacy provisioning notifications coming directly from DcTracker.
|
||||||
*/
|
*/
|
||||||
public void setProvNotificationVisible(boolean visible, int networkType, String action) {
|
public void setProvNotificationVisible(boolean visible, int id, String action) {
|
||||||
Intent intent = new Intent(action);
|
if (visible) {
|
||||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
|
Intent intent = new Intent(action);
|
||||||
// Concatenate the range of types onto the range of NetIDs.
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
|
||||||
int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE);
|
showNotification(id, NotificationType.SIGN_IN, null, pendingIntent, false);
|
||||||
mNotifier.setProvNotificationVisibleIntent(visible, id, NotificationType.SIGN_IN,
|
} else {
|
||||||
networkType, null, pendingIntent, false);
|
clearNotification(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user