Merge "Fix missing summaries" into nyc-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ac7200f..2350e55 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5177,6 +5177,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 already have a VPN connected to this profile. If you connected to 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>
 
     <!-- Preference title for VPN settings. [CHAR LIMIT=40] -->
     <string name="vpn_title">VPN</string>
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index af95056..e47a36e 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -953,7 +953,7 @@
             }
         } else if (pref == mEmergencyInfoPreference) {
             Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO);
-            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
+            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             startActivity(intent);
         }
         return false;
diff --git a/src/com/android/settings/vpn2/AppManagementFragment.java b/src/com/android/settings/vpn2/AppManagementFragment.java
index 0707d12..1a48954 100644
--- a/src/com/android/settings/vpn2/AppManagementFragment.java
+++ b/src/com/android/settings/vpn2/AppManagementFragment.java
@@ -15,19 +15,26 @@
  */
 package com.android.settings.vpn2;
 
+import android.annotation.NonNull;
 import android.app.AlertDialog;
 import android.app.AppOpsManager;
 import android.app.Dialog;
 import android.app.DialogFragment;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.Resources;
 import android.net.ConnectivityManager;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.provider.Settings;
 import android.support.v7.preference.Preference;
 import android.util.Log;
 
@@ -77,7 +84,7 @@
         public void onForget() {
             // Unset always-on-vpn when forgetting the VPN
             if (isVpnAlwaysOn()) {
-                setAlwaysOnVpn(false);
+                setAlwaysOnVpnByUI(false);
             }
             // Also dismiss and go back to VPN list
             finish();
@@ -159,18 +166,19 @@
             mPreferenceAlwaysOn.setChecked(false);
             ReplaceExistingVpnFragment.show(this);
         } else {
-            setAlwaysOnVpn(isChecked);
+            setAlwaysOnVpnByUI(isChecked);
         }
         return true;
     }
 
-    private void setAlwaysOnVpn(boolean isEnabled) {
+    private void setAlwaysOnVpnByUI(boolean isEnabled) {
         // Only clear legacy lockdown vpn in system user.
         if (mUserId == UserHandle.USER_SYSTEM) {
             VpnUtils.clearLockdownVpn(getContext());
         }
         mConnectivityManager.setAlwaysOnVpnPackageForUser(mUserId, isEnabled ? mPackageName : null);
         updateUI();
+        showCantConnectNotificationIfNeeded(isEnabled);
     }
 
     private void updateUI() {
@@ -238,6 +246,45 @@
         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.
+            }
+            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);
+    }
+
     public static class ReplaceExistingVpnFragment extends DialogFragment
             implements DialogInterface.OnClickListener {
 
@@ -260,7 +307,7 @@
         @Override
         public void onClick(DialogInterface dialog, int which) {
             if (getTargetFragment() instanceof AppManagementFragment) {
-                ((AppManagementFragment) getTargetFragment()).setAlwaysOnVpn(true);
+                ((AppManagementFragment) getTargetFragment()).setAlwaysOnVpnByUI(true);
             }
         }
     }