android: Start a specific VPN profile based on special Intents
authorTobias Brunner <tobias@strongswan.org>
Wed, 7 Nov 2012 15:06:30 +0000 (16:06 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 21 Nov 2012 17:57:40 +0000 (18:57 +0100)
src/frontends/android/AndroidManifest.xml
src/frontends/android/res/values-de/strings.xml
src/frontends/android/res/values-pl/strings.xml
src/frontends/android/res/values/strings.xml
src/frontends/android/src/org/strongswan/android/ui/MainActivity.java

index 971b307..c191a9e 100644 (file)
             android:launchMode="singleTop" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
-
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
+            <intent-filter>
+                <action android:name="org.strongswan.android.action.START_PROFILE" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
         </activity>
         <activity
             android:name=".ui.VpnProfileDetailActivity" >
index bb4cf5d..5c922ad 100644 (file)
@@ -27,6 +27,7 @@
     <string name="vpn_not_supported">Ihr Gerät unterstützt keine VPN Anwendungen.\nBitte kontaktieren Sie den Hersteller.</string>
     <string name="vpn_not_supported_during_lockdown">VPN Verbindungen sind nicht möglich im abgeriegelten Modus.</string>
     <string name="loading">Laden&#8230;</string>
+    <string name="profile_not_found">Profil nicht gefunden</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
index 58d158b..80fab63 100644 (file)
@@ -29,6 +29,7 @@
     <string name="vpn_not_supported">Urządzenie nie obsługuje aplikacji VPN.\nProszę skontaktować się z producentem.</string>
     <string name="vpn_not_supported_during_lockdown">Polączenia nie sa możliwe w trybie zamkniętym</string>
     <string name="loading">Wczytywanie&#8230;</string>
+    <string name="profile_not_found">Nie znaleziono profilu</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
index 4b33234..84fdf38 100644 (file)
@@ -27,6 +27,7 @@
     <string name="vpn_not_supported">Your device does not support VPN applications.\nPlease contact the manufacturer.</string>
     <string name="vpn_not_supported_during_lockdown">VPN connections are not supported in lockdown mode.</string>
     <string name="loading">Loading&#8230;</string>
+    <string name="profile_not_found">Profile not found</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
index 4ccf7d3..8a5d4fe 100644 (file)
@@ -42,10 +42,13 @@ import android.view.MenuItem;
 import android.view.View;
 import android.view.Window;
 import android.widget.EditText;
+import android.widget.Toast;
 
 public class MainActivity extends Activity implements OnVpnProfileSelectedListener
 {
        public static final String CONTACT_EMAIL = "android@strongswan.org";
+       public static final String START_PROFILE = "org.strongswan.android.action.START_PROFILE";
+       public static final String EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID";
        private static final int PREPARE_VPN_SERVICE = 0;
 
        private Bundle mProfileInfo;
@@ -62,6 +65,25 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
 
                /* load CA certificates in a background task */
                new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, false);
+
+               if (START_PROFILE.equals(getIntent().getAction()))
+               {
+                       startVpnProfile(getIntent());
+               }
+       }
+
+       /**
+        * Due to launchMode=singleTop this is called if the Activity already exists
+        */
+       @Override
+       protected void onNewIntent(Intent intent)
+       {
+               super.onNewIntent(intent);
+
+               if (START_PROFILE.equals(intent.getAction()))
+               {
+                       startVpnProfile(intent);
+               }
        }
 
        @Override
@@ -168,6 +190,33 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
        }
 
        /**
+        * Start the VPN profile referred to by the given intent. Displays an error
+        * if the profile doesn't exist.
+        * @param intent Intent that caused us to start this
+        */
+       private void startVpnProfile(Intent intent)
+       {
+               long profileId = intent.getLongExtra(EXTRA_VPN_PROFILE_ID, 0);
+               if (profileId <= 0)
+               {       /* invalid invocation */
+                       return;
+               }
+               VpnProfileDataSource dataSource = new VpnProfileDataSource(this);
+               dataSource.open();
+               VpnProfile profile = dataSource.getVpnProfile(profileId);
+               dataSource.close();
+
+               if (profile != null)
+               {
+                       onVpnProfileSelected(profile);
+               }
+               else
+               {
+                       Toast.makeText(this, R.string.profile_not_found, Toast.LENGTH_LONG).show();
+               }
+       }
+
+       /**
         * Class that loads or reloads the cached CA certificates.
         */
        private class CertificateLoadTask extends AsyncTask<Boolean, Void, TrustedCertificateManager>