Change VPN failure notification to dialog

It's pointless, verging on antipattern, to pop up a notification that
points to the screen we're already on, especially when that notification
is telling us something really high-priority ("your connection failed").

Change-Id: Idf0c219adcefd64b235960f3239a70b059213f7d
Fix: 27374485
This commit is contained in:
Robin Lee
2016-04-21 21:50:41 +01:00
parent c9bf2ac95e
commit 2821067709
2 changed files with 34 additions and 43 deletions

View File

@@ -5200,11 +5200,10 @@
<string name="vpn_replace_always_on_vpn_title">Replace existing VPN?</string> <string name="vpn_replace_always_on_vpn_title">Replace existing VPN?</string>
<!-- Dialog message body to set another VPN app to be always-on [CHAR LIMIT=NONE] --> <!-- Dialog message body to set another VPN app to be always-on [CHAR LIMIT=NONE] -->
<string name="vpn_replace_always_on_vpn_message">You\'re already connected to a VPN. If you connect to a different one, your existing VPN will be replaced.</string> <string name="vpn_replace_always_on_vpn_message">You\'re already connected to a VPN. If you connect to a different one, your existing VPN will be replaced.</string>
<!-- Notification title when the user can't connect an always-on vpn [CHAR LIMIT=NONE] --> <!-- Dialog mesage title when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
<string name="vpn_cant_connect_notification_title"><xliff:g id="vpn_name" example="OpenVPN">%1$s</xliff:g> can\'t connect</string> <string name="vpn_cant_connect_title"><xliff:g id="vpn_name" example="OpenVPN">%1$s</xliff:g> can\'t connect</string>
<!-- Notification subtitle when the user can't connect an always-on vpn [CHAR LIMIT=NONE] --> <!-- Dialog message subtitle when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
<string name="vpn_tap_for_vpn_settings">Tap for VPN settings</string> <string name="vpn_cant_connect_message">This app doesn\'t support always-on VPN.</string>
<!-- Preference title for VPN settings. [CHAR LIMIT=40] --> <!-- Preference title for VPN settings. [CHAR LIMIT=40] -->
<string name="vpn_title">VPN</string> <string name="vpn_title">VPN</string>
<!-- Preference title to create a new VPN profile. [CHAR LIMIT=40] --> <!-- Preference title to create a new VPN profile. [CHAR LIMIT=40] -->

View File

@@ -177,8 +177,10 @@ public class AppManagementFragment extends SettingsPreferenceFragment
VpnUtils.clearLockdownVpn(getContext()); VpnUtils.clearLockdownVpn(getContext());
} }
mConnectivityManager.setAlwaysOnVpnPackageForUser(mUserId, isEnabled ? mPackageName : null); mConnectivityManager.setAlwaysOnVpnPackageForUser(mUserId, isEnabled ? mPackageName : null);
if (isEnabled && !isVpnAlwaysOn()) {
CannotConnectFragment.show(this, mVpnLabel);
}
updateUI(); updateUI();
showCantConnectNotificationIfNeeded(isEnabled);
} }
private void updateUI() { private void updateUI() {
@@ -246,52 +248,42 @@ public class AppManagementFragment extends SettingsPreferenceFragment
return getAlwaysOnVpnPackage() != null && !isVpnAlwaysOn(); return getAlwaysOnVpnPackage() != null && !isVpnAlwaysOn();
} }
private void showCantConnectNotificationIfNeeded(boolean isEnabledExpected) { public static class CannotConnectFragment extends DialogFragment {
// Display notification only when user tries to turn on but system fails to turn it on. private static final String TAG = "CannotConnect";
if (isEnabledExpected && !isVpnAlwaysOn()) { private static final String ARG_VPN_LABEL = "label";
String appDisplayName = mPackageName;
try { public static void show(AppManagementFragment parent, String vpnLabel) {
appDisplayName = VpnConfig.getVpnLabel(getContext(), mPackageName).toString(); if (parent.getFragmentManager().findFragmentByTag(TAG) == null) {
} catch (NameNotFoundException e) { final Bundle args = new Bundle();
// Use default package name as app name. Quietly fail. args.putString(ARG_VPN_LABEL, vpnLabel);
final DialogFragment frag = new CannotConnectFragment();
frag.setArguments(args);
frag.show(parent.getFragmentManager(), TAG);
} }
postCantConnectNotification(getContext(), appDisplayName,
mPackageUid /* notificationId */);
} }
}
/** @Override
* @param notificationId should be unique to the vpn app, e.g. uid, to keep one notification per public Dialog onCreateDialog(Bundle savedInstanceState) {
* vpn app per user final String vpnLabel = getArguments().getString(ARG_VPN_LABEL);
*/ return new AlertDialog.Builder(getActivity())
private static void postCantConnectNotification(Context context, @NonNull String vpnName, .setTitle(getActivity().getString(R.string.vpn_cant_connect_title, vpnLabel))
int notificationId) { .setMessage(getActivity().getString(R.string.vpn_cant_connect_message))
final Resources res = context.getResources(); .setPositiveButton(R.string.okay, null)
// Only action is specified to match cross-profile intent filter set by ManagedProfileSetup .create();
final Intent intent = new Intent(Settings.ACTION_VPN_SETTINGS); }
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
final Notification notification = new Notification.Builder(context)
.setContentTitle(res.getString(R.string.vpn_cant_connect_notification_title,
vpnName))
.setContentText(res.getString(R.string.vpn_tap_for_vpn_settings))
.setSmallIcon(R.drawable.ic_settings_wireless)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManager nm = context.getSystemService(NotificationManager.class);
nm.notify(notificationId, notification);
} }
public static class ReplaceExistingVpnFragment extends DialogFragment public static class ReplaceExistingVpnFragment extends DialogFragment
implements DialogInterface.OnClickListener { implements DialogInterface.OnClickListener {
private static final String TAG = "ReplaceExistingVpn";
public static void show(AppManagementFragment parent) { public static void show(AppManagementFragment parent) {
final ReplaceExistingVpnFragment frag = new ReplaceExistingVpnFragment(); if (parent.getFragmentManager().findFragmentByTag(TAG) == null) {
frag.setTargetFragment(parent, 0); final ReplaceExistingVpnFragment frag = new ReplaceExistingVpnFragment();
frag.show(parent.getFragmentManager(), null); frag.setTargetFragment(parent, 0);
frag.show(parent.getFragmentManager(), TAG);
}
} }
@Override @Override