android: Add menu item to import VPN profile via Storage Access Framework
authorTobias Brunner <tobias@strongswan.org>
Tue, 18 Apr 2017 15:07:57 +0000 (17:07 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 30 Jun 2017 07:32:27 +0000 (09:32 +0200)
This is useful in case the proper MIME type was not set for a downloaded
profile.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/MainActivity.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileImportActivity.java
src/frontends/android/app/src/main/res/menu/main.xml

index 8904342..5ba1061 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 Tobias Brunner
+ * Copyright (C) 2012-2017 Tobias Brunner
  * Copyright (C) 2012 Giuliano Grassi
  * Copyright (C) 2012 Ralf Sager
  * Hochschule fuer Technik Rapperswil
@@ -26,6 +26,7 @@ import android.content.Intent;
 import android.content.ServiceConnection;
 import android.net.VpnService;
 import android.os.AsyncTask;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.support.v4.app.Fragment;
@@ -141,10 +142,24 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
        }
 
        @Override
+       public boolean onPrepareOptionsMenu(Menu menu)
+       {
+               if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
+               {
+                       menu.removeItem(R.id.menu_import_profile);
+               }
+               return true;
+       }
+
+       @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
                switch (item.getItemId())
                {
+                       case R.id.menu_import_profile:
+                               Intent intent = new Intent(this, VpnProfileImportActivity.class);
+                               startActivity(intent);
+                               return true;
                        case R.id.menu_manage_certs:
                                Intent certIntent = new Intent(this, TrustedCertificatesActivity.class);
                                startActivity(certIntent);
index 1b6b6e8..8ab1a17 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Tobias Brunner
+ * Copyright (C) 2016-2017 Tobias Brunner
  * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@ import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.Loader;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Bundle;
 import android.security.KeyChain;
 import android.security.KeyChainAliasCallback;
@@ -75,7 +76,9 @@ import javax.net.ssl.SSLHandshakeException;
 public class VpnProfileImportActivity extends AppCompatActivity
 {
        private static final String PKCS12_INSTALLED = "PKCS12_INSTALLED";
+       private static final String PROFILE_URI = "PROFILE_URI";
        private static final int INSTALL_PKCS12 = 0;
+       private static final int OPEN_DOCUMENT = 1;
        private static final int PROFILE_LOADER = 0;
        private static final int USER_CERT_LOADER = 1;
 
@@ -107,7 +110,7 @@ public class VpnProfileImportActivity extends AppCompatActivity
                @Override
                public Loader<ProfileLoadResult> onCreateLoader(int id, Bundle args)
                {
-                       return new ProfileLoader(VpnProfileImportActivity.this, getIntent().getData());
+                       return new ProfileLoader(VpnProfileImportActivity.this, (Uri)args.getParcelable(PROFILE_URI));
                }
 
                @Override
@@ -197,16 +200,13 @@ public class VpnProfileImportActivity extends AppCompatActivity
                String action = intent.getAction();
                if (Intent.ACTION_VIEW.equals(action))
                {
-                       mProgress = ProgressDialog.show(this, null, getString(R.string.loading),
-                                                                                       true, true, new DialogInterface.OnCancelListener() {
-                               @Override
-                               public void onCancel(DialogInterface dialog)
-                               {
-                                       finish();
-                               }
-                       });
-
-                       getLoaderManager().initLoader(PROFILE_LOADER, null, mProfileLoaderCallbacks);
+                       loadProfile(getIntent().getData());
+               }
+               else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
+               {
+                       Intent openIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
+                       openIntent.setType("*/*");
+                       startActivityForResult(openIntent, OPEN_DOCUMENT);
                }
 
                if (savedInstanceState != null)
@@ -279,9 +279,34 @@ public class VpnProfileImportActivity extends AppCompatActivity
                                        mImportUserCert.setEnabled(false);
                                        mSelectUserCert.performClick();
                                }
+                               break;
+                       case OPEN_DOCUMENT:
+                               if (resultCode == Activity.RESULT_OK && data != null)
+                               {
+                                       loadProfile(data.getData());
+                                       return;
+                               }
+                               finish();
+                               break;
                }
        }
 
+       private void loadProfile(Uri uri)
+       {
+               mProgress = ProgressDialog.show(this, null, getString(R.string.loading),
+                               true, true, new DialogInterface.OnCancelListener() {
+                                       @Override
+                                       public void onCancel(DialogInterface dialog)
+                                       {
+                                               finish();
+                                       }
+                               });
+
+               Bundle args = new Bundle();
+               args.putParcelable(PROFILE_URI, uri);
+               getLoaderManager().initLoader(PROFILE_LOADER, args, mProfileLoaderCallbacks);
+       }
+
        public void handleProfile(ProfileLoadResult data)
        {
                mProgress.dismiss();
index 599254a..3c8da08 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-    Copyright (C) 2012-2014 Tobias Brunner
+    Copyright (C) 2012-2017 Tobias Brunner
     Hochschule fuer Technik Rapperswil
 
     This program is free software; you can redistribute it and/or modify it
       xmlns:app="http://schemas.android.com/apk/res-auto">
 
     <item
+        android:id="@+id/menu_import_profile"
+        android:title="@string/profile_import"
+        app:showAsAction="withText" />
+
+    <item
         android:id="@+id/menu_manage_certs"
         android:title="@string/trusted_certs_title"
         app:showAsAction="withText" />