Refactor NetworkNotificationManager. am: c5391028ec am: a6dcb89e6d am: 4e0916b416

am: 2f063e3065

Change-Id: I1249d019ce449f8721541c138f89b98230a67b57
This commit is contained in:
Lorenzo Colitti
2016-08-23 18:34:58 +00:00
committed by android-build-merger
2 changed files with 129 additions and 97 deletions

View File

@@ -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) {
@@ -2235,17 +2237,14 @@ 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,
nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(),
(PendingIntent) msg.obj, nai.networkMisc.explicitlySelected); (PendingIntent) msg.obj, nai.networkMisc.explicitlySelected);
} }
} }
@@ -2731,9 +2730,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 {
@@ -3747,7 +3745,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);
} }

View File

@@ -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,54 +91,54 @@ 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
.getSystemService(Context.NOTIFICATION_SERVICE);
if (visible) {
CharSequence title; CharSequence title;
CharSequence details; CharSequence details;
int icon; int icon = getIcon(transportType);
if (notifyType == NotificationType.NO_INTERNET && if (notifyType == NotificationType.NO_INTERNET && transportType == TRANSPORT_WIFI) {
networkType == ConnectivityManager.TYPE_WIFI) {
title = r.getString(R.string.wifi_no_internet, 0); title = r.getString(R.string.wifi_no_internet, 0);
details = r.getString(R.string.wifi_no_internet_detailed); details = r.getString(R.string.wifi_no_internet_detailed);
icon = R.drawable.stat_notify_wifi_in_range; // TODO: Need new icon.
} else if (notifyType == NotificationType.SIGN_IN) { } else if (notifyType == NotificationType.SIGN_IN) {
switch (networkType) { switch (transportType) {
case ConnectivityManager.TYPE_WIFI: case TRANSPORT_WIFI:
title = r.getString(R.string.wifi_available_sign_in, 0); title = r.getString(R.string.wifi_available_sign_in, 0);
details = r.getString(R.string.network_available_sign_in_detailed, details = r.getString(R.string.network_available_sign_in_detailed, extraInfo);
extraInfo);
icon = R.drawable.stat_notify_wifi_in_range;
break; break;
case ConnectivityManager.TYPE_MOBILE: case TRANSPORT_CELLULAR:
case ConnectivityManager.TYPE_MOBILE_HIPRI:
title = r.getString(R.string.network_available_sign_in, 0); title = r.getString(R.string.network_available_sign_in, 0);
// TODO: Change this to pull from NetworkInfo once a printable // TODO: Change this to pull from NetworkInfo once a printable
// name has been added to it // name has been added to it
details = mTelephonyManager.getNetworkOperatorName(); details = mTelephonyManager.getNetworkOperatorName();
icon = R.drawable.stat_notify_rssi_in_range;
break; break;
default: default:
title = r.getString(R.string.network_available_sign_in, 0); title = r.getString(R.string.network_available_sign_in, 0);
details = r.getString(R.string.network_available_sign_in_detailed, details = r.getString(R.string.network_available_sign_in_detailed, extraInfo);
extraInfo);
icon = R.drawable.stat_notify_rssi_in_range;
break; break;
} }
} else { } else {
Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network type " Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network transport "
+ getNetworkTypeName(networkType)); + getTransportName(transportType));
return; return;
} }
@@ -134,28 +161,33 @@ public class NetworkNotificationManager {
.build(); .build();
try { try {
notificationManager.notifyAsUser(NOTIFICATION_ID, id, notification, UserHandle.ALL); mNotificationManager.notifyAsUser(NOTIFICATION_ID, id, notification, UserHandle.ALL);
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
Slog.d(TAG, "setNotificationVisible: visible notificationManager npe=" + npe); Slog.d(TAG, "setNotificationVisible: visible notificationManager npe=" + npe);
} }
} else { }
public void clearNotification(int id) {
if (DBG) {
Slog.d(TAG, "clearNotification id=" + id);
}
try { try {
notificationManager.cancelAsUser(NOTIFICATION_ID, id, UserHandle.ALL); mNotificationManager.cancelAsUser(NOTIFICATION_ID, id, UserHandle.ALL);
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
Slog.d(TAG, "setNotificationVisible: cancel notificationManager npe=" + 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) {
if (visible) {
Intent intent = new Intent(action); Intent intent = new Intent(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
// Concatenate the range of types onto the range of NetIDs. showNotification(id, NotificationType.SIGN_IN, null, pendingIntent, false);
int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); } else {
mNotifier.setProvNotificationVisibleIntent(visible, id, NotificationType.SIGN_IN, clearNotification(id);
networkType, null, pendingIntent, false); }
} }
} }