Restore legacy VPN stats dialog.

Was originally removed in ag/522961, but restoring to keep legacy VPN
behavior the same from within VpnSettings. This dialog is only
accesible from VpnSettings and so should only ever be shown for legacy
VPNs.

Bug: 17164793
Change-Id: I06c4e136e1023b8f84edfd15a15264d2e41d325b
diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java
index 69caab9..94aa421 100644
--- a/services/core/java/com/android/server/connectivity/Vpn.java
+++ b/services/core/java/com/android/server/connectivity/Vpn.java
@@ -22,6 +22,7 @@
 
 import android.app.AppGlobals;
 import android.app.AppOpsManager;
+import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
@@ -105,6 +106,7 @@
     private boolean mAllowIPv6;
     private Connection mConnection;
     private LegacyVpnRunner mLegacyVpnRunner;
+    private PendingIntent mStatusIntent;
     private volatile boolean mEnableTeardown = true;
     private final IConnectivityManager mConnService;
     private final INetworkManagementService mNetd;
@@ -237,6 +239,7 @@
 
         // Reset the interface.
         if (mInterface != null) {
+            mStatusIntent = null;
             agentDisconnect();
             jniReset(mInterface);
             mInterface = null;
@@ -567,17 +570,20 @@
 
         // add the user
         mVpnUsers.add(UidRange.createForUser(user));
+
+        prepareStatusIntent();
     }
 
     private void removeVpnUserLocked(int user) {
-            if (!isRunningLocked()) {
-                throw new IllegalStateException("VPN is not active");
-            }
-            UidRange uidRange = UidRange.createForUser(user);
-            if (mNetworkAgent != null) {
-                mNetworkAgent.removeUidRanges(new UidRange[] { uidRange });
-            }
-            mVpnUsers.remove(uidRange);
+        if (!isRunningLocked()) {
+            throw new IllegalStateException("VPN is not active");
+        }
+        UidRange uidRange = UidRange.createForUser(user);
+        if (mNetworkAgent != null) {
+            mNetworkAgent.removeUidRanges(new UidRange[] { uidRange });
+        }
+        mVpnUsers.remove(uidRange);
+        mStatusIntent = null;
     }
 
     private void onUserAdded(int userId) {
@@ -645,6 +651,7 @@
         public void interfaceRemoved(String interfaze) {
             synchronized (Vpn.this) {
                 if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) {
+                    mStatusIntent = null;
                     mVpnUsers = null;
                     mInterface = null;
                     if (mConnection != null) {
@@ -702,6 +709,15 @@
         }
     }
 
+    private void prepareStatusIntent() {
+        final long token = Binder.clearCallingIdentity();
+        try {
+            mStatusIntent = VpnConfig.getIntentForStatusPanel(mContext);
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
     public synchronized boolean addAddress(String address, int prefixLength) {
         if (Binder.getCallingUid() != mOwnerUID || mInterface == null || mNetworkAgent == null) {
             return false;
@@ -911,6 +927,9 @@
         final LegacyVpnInfo info = new LegacyVpnInfo();
         info.key = mConfig.user;
         info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo);
+        if (mNetworkInfo.isConnected()) {
+            info.intent = mStatusIntent;
+        }
         return info;
     }