VPN: implement status report for legacy VPN.

Change-Id: I81c28dafd9588572df76cbc303b0d6a0f41f9bc6
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index afc04bb..b98d2a2 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -64,6 +64,7 @@
 import android.util.Slog;
 import android.util.SparseIntArray;
 
+import com.android.internal.net.LegacyVpnInfo;
 import com.android.internal.net.VpnConfig;
 import com.android.internal.telephony.Phone;
 import com.android.server.connectivity.Tethering;
@@ -2469,8 +2470,8 @@
 
     /**
      * Protect a socket from VPN routing rules. This method is used by
-     * VpnBuilder and not available in ConnectivityManager. Permission
-     * checks are done in Vpn class.
+     * VpnBuilder and not available in ConnectivityManager. Permissions
+     * are checked in Vpn class.
      * @hide
      */
     @Override
@@ -2480,8 +2481,8 @@
 
     /**
      * Prepare for a VPN application. This method is used by VpnDialogs
-     * and not available in ConnectivityManager. Permission checks are
-     * done in Vpn class.
+     * and not available in ConnectivityManager. Permissions are checked
+     * in Vpn class.
      * @hide
      */
     @Override
@@ -2492,8 +2493,8 @@
     /**
      * Configure a TUN interface and return its file descriptor. Parameters
      * are encoded and opaque to this class. This method is used by VpnBuilder
-     * and not available in ConnectivityManager. Permission checks are done
-     * in Vpn class.
+     * and not available in ConnectivityManager. Permissions are checked in
+     * Vpn class.
      * @hide
      */
     @Override
@@ -2502,12 +2503,25 @@
     }
 
     /**
-     * Handle a legacy VPN request.
+     * Start legacy VPN and return an intent to VpnDialogs. This method is
+     * used by VpnSettings and not available in ConnectivityManager.
+     * Permissions are checked in Vpn class.
      * @hide
      */
     @Override
-    public void doLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
-        mVpn.doLegacyVpn(config, racoon, mtpd);
+    public void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
+        mVpn.startLegacyVpn(config, racoon, mtpd);
+    }
+
+    /**
+     * Return the information of the ongoing legacy VPN. This method is used
+     * by VpnSettings and not available in ConnectivityManager. Permissions
+     * are checked in Vpn class.
+     * @hide
+     */
+    @Override
+    public LegacyVpnInfo getLegacyVpnInfo() {
+        return mVpn.getLegacyVpnInfo();
     }
 
     private String getDefaultInterface() {
diff --git a/services/java/com/android/server/connectivity/Vpn.java b/services/java/com/android/server/connectivity/Vpn.java
index bb3ce28..c185012 100644
--- a/services/java/com/android/server/connectivity/Vpn.java
+++ b/services/java/com/android/server/connectivity/Vpn.java
@@ -37,6 +37,7 @@
 import android.util.Log;
 
 import com.android.internal.R;
+import com.android.internal.net.LegacyVpnInfo;
 import com.android.internal.net.VpnConfig;
 import com.android.server.ConnectivityService.VpnCallback;
 
@@ -250,6 +251,7 @@
                     mContext.getString(R.string.vpn_title_long, label);
             String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
                     mContext.getString(R.string.vpn_text_long, config.session);
+            config.startTime = SystemClock.elapsedRealtime();
 
             long identity = Binder.clearCallingIdentity();
             Notification notification = new Notification.Builder(mContext)
@@ -257,7 +259,7 @@
                     .setLargeIcon(icon)
                     .setContentTitle(title)
                     .setContentText(text)
-                    .setContentIntent(VpnConfig.getIntentForNotification(mContext, config))
+                    .setContentIntent(VpnConfig.getIntentForStatusPanel(mContext, config))
                     .setDefaults(Notification.DEFAULT_ALL)
                     .setOngoing(true)
                     .getNotification();
@@ -284,24 +286,35 @@
     private native void jniProtect(int socket, String interfaze);
 
     /**
-     * Handle a legacy VPN request. This method stops the daemons and restart
-     * them if arguments are not null. Heavy things are offloaded to another
+     * Start legacy VPN. This method stops the daemons and restart them
+     * if arguments are not null. Heavy things are offloaded to another
      * thread, so callers will not be blocked for a long time.
      *
      * @param config The parameters to configure the network.
      * @param raoocn The arguments to be passed to racoon.
      * @param mtpd The arguments to be passed to mtpd.
      */
-    public synchronized void doLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
+    public synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
         // Prepare for the new request. This also checks the caller.
         prepare(null, VpnConfig.LEGACY_VPN);
 
-        // Start a new runner and we are done!
+        // Start a new LegacyVpnRunner and we are done!
         mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
         mLegacyVpnRunner.start();
     }
 
     /**
+     * Return the information of the current ongoing legacy VPN.
+     */
+    public synchronized LegacyVpnInfo getLegacyVpnInfo() {
+        // Only system user can call this method.
+        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
+            throw new SecurityException("Unauthorized Caller");
+        }
+        return (mLegacyVpnRunner == null) ? null : mLegacyVpnRunner.getInfo();
+    }
+
+    /**
      * Bringing up a VPN connection takes time, and that is all this thread
      * does. Here we have plenty of time. The only thing we need to take
      * care of is responding to interruptions as soon as possible. Otherwise
@@ -315,6 +328,8 @@
         private final VpnConfig mConfig;
         private final String[] mDaemons;
         private final String[][] mArguments;
+        private final LegacyVpnInfo mInfo;
+
         private long mTimer = -1;
 
         public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
@@ -322,7 +337,10 @@
             mConfig = config;
             mDaemons = new String[] {"racoon", "mtpd"};
             mArguments = new String[][] {racoon, mtpd};
+            mInfo = new LegacyVpnInfo();
 
+            // Legacy VPN is not a real package, so we use it to carry the key.
+            mInfo.key = mConfig.packagz;
             mConfig.packagz = VpnConfig.LEGACY_VPN;
         }
 
@@ -334,14 +352,22 @@
             interrupt();
         }
 
+        public LegacyVpnInfo getInfo() {
+            // Update the info when VPN is disconnected.
+            if (mInfo.state == LegacyVpnInfo.STATE_CONNECTED && mInterface == null) {
+                mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
+                mInfo.intent = null;
+            }
+            return mInfo;
+        }
+
         @Override
         public void run() {
             // Wait for the previous thread since it has been interrupted.
-            Log.v(TAG, "wait");
+            Log.v(TAG, "Waiting");
             synchronized (TAG) {
-                Log.v(TAG, "begin");
+                Log.v(TAG, "Executing");
                 execute();
-                Log.v(TAG, "end");
             }
         }
 
@@ -353,7 +379,8 @@
             } else if (now - mTimer <= 30000) {
                 Thread.sleep(yield ? 200 : 1);
             } else {
-                throw new InterruptedException("time is up");
+                mInfo.state = LegacyVpnInfo.STATE_TIMEOUT;
+                throw new IllegalStateException("time is up");
             }
         }
 
@@ -362,6 +389,7 @@
             try {
                 // Initialize the timer.
                 checkpoint(false);
+                mInfo.state = LegacyVpnInfo.STATE_INITIALIZING;
 
                 // First stop the daemons.
                 for (String daemon : mDaemons) {
@@ -390,8 +418,10 @@
                     restart = restart || (arguments != null);
                 }
                 if (!restart) {
+                    mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
                     return;
                 }
+                mInfo.state = LegacyVpnInfo.STATE_CONNECTING;
 
                 // Start the daemon with arguments.
                 for (int i = 0; i < mDaemons.length; ++i) {
@@ -459,7 +489,7 @@
                         String daemon = mDaemons[i];
                         if (mArguments[i] != null && !"running".equals(
                                 SystemProperties.get("init.svc." + daemon))) {
-                            throw new IllegalArgumentException(daemon + " is dead");
+                            throw new IllegalStateException(daemon + " is dead");
                         }
                     }
                     checkpoint(true);
@@ -492,11 +522,20 @@
                     mInterface = mConfig.interfaze;
                     mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
                     showNotification(mConfig, null, null);
+
+                    Log.i(TAG, "Connected!");
+                    mInfo.state = LegacyVpnInfo.STATE_CONNECTED;
+                    mInfo.intent = VpnConfig.getIntentForStatusPanel(mContext, null);
                 }
-                Log.i(TAG, "Connected!");
             } catch (Exception e) {
-                Log.i(TAG, "Abort because " + e.getMessage());
+                Log.i(TAG, "Aborting", e);
                 exit();
+            } finally {
+                // Do not leave an unstable state.
+                if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING ||
+                        mInfo.state == LegacyVpnInfo.STATE_CONNECTING) {
+                    mInfo.state = LegacyVpnInfo.STATE_FAILED;
+                }
             }
         }
     }