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
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ef6ae74..e6a9801 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5200,11 +5200,10 @@
     <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] -->
     <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] -->
-    <string name="vpn_cant_connect_notification_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] -->
-    <string name="vpn_tap_for_vpn_settings">Tap for VPN settings</string>
-
+    <!-- Dialog mesage title when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
+    <string name="vpn_cant_connect_title"><xliff:g id="vpn_name" example="OpenVPN">%1$s</xliff:g> can\'t connect</string>
+    <!-- Dialog message subtitle when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
+    <string name="vpn_cant_connect_message">This app doesn\'t support always-on VPN.</string>
     <!-- Preference title for VPN settings. [CHAR LIMIT=40] -->
     <string name="vpn_title">VPN</string>
     <!-- Preference title to create a new VPN profile. [CHAR LIMIT=40] -->
diff --git a/src/com/android/settings/vpn2/AppManagementFragment.java b/src/com/android/settings/vpn2/AppManagementFragment.java
index 2a12ade..f8e30f9 100644
--- a/src/com/android/settings/vpn2/AppManagementFragment.java
+++ b/src/com/android/settings/vpn2/AppManagementFragment.java
@@ -177,8 +177,10 @@
             VpnUtils.clearLockdownVpn(getContext());
         }
         mConnectivityManager.setAlwaysOnVpnPackageForUser(mUserId, isEnabled ? mPackageName : null);
+        if (isEnabled && !isVpnAlwaysOn()) {
+            CannotConnectFragment.show(this, mVpnLabel);
+        }
         updateUI();
-        showCantConnectNotificationIfNeeded(isEnabled);
     }
 
     private void updateUI() {
@@ -246,52 +248,42 @@
         return getAlwaysOnVpnPackage() != null && !isVpnAlwaysOn();
     }
 
-    private void showCantConnectNotificationIfNeeded(boolean isEnabledExpected) {
-        // Display notification only when user tries to turn on but system fails to turn it on.
-        if (isEnabledExpected && !isVpnAlwaysOn()) {
-            String appDisplayName = mPackageName;
-            try {
-                appDisplayName = VpnConfig.getVpnLabel(getContext(), mPackageName).toString();
-            } catch (NameNotFoundException e) {
-                // Use default package name as app name. Quietly fail.
+    public static class CannotConnectFragment extends DialogFragment {
+        private static final String TAG = "CannotConnect";
+        private static final String ARG_VPN_LABEL = "label";
+
+        public static void show(AppManagementFragment parent, String vpnLabel) {
+            if (parent.getFragmentManager().findFragmentByTag(TAG) == null) {
+                final Bundle args = new Bundle();
+                args.putString(ARG_VPN_LABEL, vpnLabel);
+
+                final DialogFragment frag = new CannotConnectFragment();
+                frag.setArguments(args);
+                frag.show(parent.getFragmentManager(), TAG);
             }
-            postCantConnectNotification(getContext(), appDisplayName,
-                    mPackageUid /* notificationId */);
         }
-    }
 
-    /**
-     * @param notificationId should be unique to the vpn app, e.g. uid, to keep one notification per
-     *                       vpn app per user
-     */
-    private static void postCantConnectNotification(Context context, @NonNull String vpnName,
-            int notificationId) {
-        final Resources res = context.getResources();
-        // Only action is specified to match cross-profile intent filter set by ManagedProfileSetup
-        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);
+        @Override
+        public Dialog onCreateDialog(Bundle savedInstanceState) {
+            final String vpnLabel = getArguments().getString(ARG_VPN_LABEL);
+            return new AlertDialog.Builder(getActivity())
+                    .setTitle(getActivity().getString(R.string.vpn_cant_connect_title, vpnLabel))
+                    .setMessage(getActivity().getString(R.string.vpn_cant_connect_message))
+                    .setPositiveButton(R.string.okay, null)
+                    .create();
+        }
     }
 
     public static class ReplaceExistingVpnFragment extends DialogFragment
             implements DialogInterface.OnClickListener {
+        private static final String TAG = "ReplaceExistingVpn";
 
         public static void show(AppManagementFragment parent) {
-            final ReplaceExistingVpnFragment frag = new ReplaceExistingVpnFragment();
-            frag.setTargetFragment(parent, 0);
-            frag.show(parent.getFragmentManager(), null);
+            if (parent.getFragmentManager().findFragmentByTag(TAG) == null) {
+                final ReplaceExistingVpnFragment frag = new ReplaceExistingVpnFragment();
+                frag.setTargetFragment(parent, 0);
+                frag.show(parent.getFragmentManager(), TAG);
+            }
         }
 
         @Override