From f3957386eb230ab85fa7d727c96d9ca6fe122ee3 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Tue, 21 Jan 2014 20:37:05 +0100 Subject: Move to common code for selecting and embedding files --HG-- extra : rebase_source : 7e0aa4672ca93e4e3b6a7c056e1004edd12f2fbc --- .../openvpn/fragments/Settings_Authentication.java | 21 +++- .../blinkt/openvpn/fragments/Settings_Basic.java | 11 +- src/de/blinkt/openvpn/fragments/Utils.java | 122 +++++++++++++++++++++ .../blinkt/openvpn/fragments/VPNProfileList.java | 32 +----- src/de/blinkt/openvpn/views/FileSelectLayout.java | 108 ++---------------- 5 files changed, 155 insertions(+), 139 deletions(-) create mode 100644 src/de/blinkt/openvpn/fragments/Utils.java (limited to 'src/de/blinkt/openvpn') diff --git a/src/de/blinkt/openvpn/fragments/Settings_Authentication.java b/src/de/blinkt/openvpn/fragments/Settings_Authentication.java index d8c4ac6a..ac0d804a 100644 --- a/src/de/blinkt/openvpn/fragments/Settings_Authentication.java +++ b/src/de/blinkt/openvpn/fragments/Settings_Authentication.java @@ -2,6 +2,7 @@ package de.blinkt.openvpn.fragments; import android.app.Activity; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; @@ -19,7 +20,8 @@ import de.blinkt.openvpn.VpnProfile; public class Settings_Authentication extends OpenVpnPreferencesFragment implements OnPreferenceChangeListener, OnPreferenceClickListener { private static final int SELECT_TLS_FILE = 23223232; - private CheckBoxPreference mExpectTLSCert; + private static final int SELECT_TLS_FILE_KITKAT = SELECT_TLS_FILE +1; + private CheckBoxPreference mExpectTLSCert; private CheckBoxPreference mCheckRemoteCN; private RemoteCNPreference mRemoteCN; private ListPreference mTLSAuthDirection; @@ -159,10 +161,15 @@ public class Settings_Authentication extends OpenVpnPreferencesFragment implemen } void startFileDialog() { - Intent startFC = new Intent(getActivity(), FileSelect.class); - startFC.putExtra(FileSelect.START_DATA, mTlsAuthFileData); - startFC.putExtra(FileSelect.WINDOW_TITLE, R.string.tls_auth_file); - startActivityForResult(startFC, SELECT_TLS_FILE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + Intent startFC = Utils.getFilePickerIntent (Utils.FileType.TLS_AUTH_FILE); + startActivityForResult(startFC, SELECT_TLS_FILE_KITKAT); + } else { + Intent startFC = new Intent(getActivity(), FileSelect.class); + startFC.putExtra(FileSelect.START_DATA, mTlsAuthFileData); + startFC.putExtra(FileSelect.WINDOW_TITLE, R.string.tls_auth_file); + startActivityForResult(startFC, SELECT_TLS_FILE); + } } @Override @@ -180,7 +187,9 @@ public class Settings_Authentication extends OpenVpnPreferencesFragment implemen mTlsAuthFileData=result; setTlsAuthSummary(result); - } + } else if (requestCode == SELECT_TLS_FILE_KITKAT && requestCode == Activity.RESULT_OK) { + + } } private void setTlsAuthSummary(String result) { diff --git a/src/de/blinkt/openvpn/fragments/Settings_Basic.java b/src/de/blinkt/openvpn/fragments/Settings_Basic.java index cf4d22d1..f2fab505 100644 --- a/src/de/blinkt/openvpn/fragments/Settings_Basic.java +++ b/src/de/blinkt/openvpn/fragments/Settings_Basic.java @@ -26,7 +26,6 @@ import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import android.widget.ToggleButton; -import de.blinkt.openvpn.activities.FileSelect; import de.blinkt.openvpn.views.FileSelectLayout; import de.blinkt.openvpn.R; import de.blinkt.openvpn.VpnProfile; @@ -64,7 +63,7 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On - private void addFileSelectLayout (FileSelectLayout fsl, FileSelectLayout.FileType type) { + private void addFileSelectLayout (FileSelectLayout fsl, Utils.FileType type) { int i = fileselects.size() + CHOOSE_FILE_OFFSET; fileselects.put(i, fsl); fsl.setFragment(this,i,type); @@ -141,10 +140,10 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On mPassword = (EditText) mView.findViewById(R.id.auth_password); mKeyPassword = (EditText) mView.findViewById(R.id.key_password); - addFileSelectLayout(mCaCert, FileSelectLayout.FileType.CERTIFICATE); - addFileSelectLayout(mClientCert, FileSelectLayout.FileType.CERTIFICATE); - addFileSelectLayout(mClientKey, FileSelectLayout.FileType.KEYFILE); - addFileSelectLayout(mpkcs12, FileSelectLayout.FileType.PKCS12); + addFileSelectLayout(mCaCert, Utils.FileType.CERTIFICATE); + addFileSelectLayout(mClientCert, Utils.FileType.CERTIFICATE); + addFileSelectLayout(mClientKey, Utils.FileType.KEYFILE); + addFileSelectLayout(mpkcs12, Utils.FileType.PKCS12); mCaCert.setShowClear(); mType.setOnItemSelectedListener(this); diff --git a/src/de/blinkt/openvpn/fragments/Utils.java b/src/de/blinkt/openvpn/fragments/Utils.java new file mode 100644 index 00000000..d7a144ed --- /dev/null +++ b/src/de/blinkt/openvpn/fragments/Utils.java @@ -0,0 +1,122 @@ +package de.blinkt.openvpn.fragments; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.util.Base64; +import android.webkit.MimeTypeMap; + +import java.io.*; +import java.util.TreeSet; +import java.util.Vector; + +public class Utils { + + @TargetApi(Build.VERSION_CODES.KITKAT) + public static Intent getFilePickerIntent(FileType fileType) { + Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + TreeSet supportedMimeTypes = new TreeSet(); + Vector extensions = new Vector(); + + switch (fileType) { + case PKCS12: + i.setType("application/x-pkcs12"); + supportedMimeTypes.add("application/x-pkcs12"); + extensions.add("p12"); + extensions.add("pfx"); + break; + case CERTIFICATE: + i.setType("application/x-pem-file"); + supportedMimeTypes.add("application/x-x509-ca-cert"); + supportedMimeTypes.add("application/x-x509-user-cert"); + supportedMimeTypes.add("application/x-pem-file"); + supportedMimeTypes.add("text/plain"); + + extensions.add("pem"); + extensions.add("crt"); + break; + case KEYFILE: + i.setType("application/x-pem-file"); + supportedMimeTypes.add("application/x-pem-file"); + supportedMimeTypes.add("application/pkcs8"); + extensions.add("key"); + break; + + case OVPN_CONFIG: + i.setType("application/x-openvpn-profile"); + supportedMimeTypes.add("application/x-openvpn-profile"); + supportedMimeTypes.add("application/openvpn-profile"); + supportedMimeTypes.add("application/ovpn"); + extensions.add("ovpn"); + extensions.add("conf"); + break; + case TLS_AUTH_FILE: + i.setType("text/plain"); + + // Backup .... + supportedMimeTypes.add("application/pkcs8"); + extensions.add("txt"); + extensions.add("key"); + break; + } + + MimeTypeMap mtm = MimeTypeMap.getSingleton(); + + for (String ext : extensions) { + String mimeType = mtm.getMimeTypeFromExtension(ext); + if (mimeType != null) + supportedMimeTypes.add(mimeType); + else + supportedMimeTypes.add("application/octet-stream"); + } + + i.putExtra(Intent.EXTRA_MIME_TYPES, supportedMimeTypes.toArray(new String[supportedMimeTypes.size()])); + return i; + } + + public enum FileType { + PKCS12, + CERTIFICATE, + OVPN_CONFIG, + KEYFILE, + TLS_AUTH_FILE + } + + static private byte[] readBytesFromStream(InputStream input) throws IOException { + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + + int nRead; + byte[] data = new byte[16384]; + + while ((nRead = input.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + + buffer.flush(); + input.close(); + return buffer.toByteArray(); + } + + public static String getStringFromFilePickerResult(FileType ft, Intent result, Context c) throws IOException { + + Uri uri = result.getData(); + if (uri ==null) + return null; + + byte[] fileData = readBytesFromStream(c.getContentResolver().openInputStream(uri)); + String newData = null; + switch (ft) { + case PKCS12: + newData = Base64.encodeToString(fileData, Base64.DEFAULT); + break; + default: + newData = new String(fileData, "UTF-8"); + break; + } + return newData; + } +} diff --git a/src/de/blinkt/openvpn/fragments/VPNProfileList.java b/src/de/blinkt/openvpn/fragments/VPNProfileList.java index 1fd875b0..b26f3505 100644 --- a/src/de/blinkt/openvpn/fragments/VPNProfileList.java +++ b/src/de/blinkt/openvpn/fragments/VPNProfileList.java @@ -220,37 +220,7 @@ public class VPNProfileList extends ListFragment { @TargetApi(Build.VERSION_CODES.KITKAT) private void startFilePicker() { - - - - Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("application/x-openvpn-profile"); - - - TreeSet supportedMimeTypes = new TreeSet(); - supportedMimeTypes.add("application/x-openvpn-profile"); - supportedMimeTypes.add("application/openvpn-profile"); - supportedMimeTypes.add("application/ovpn"); - - - // Webservers unfortunately report this for .conf files sometimes - supportedMimeTypes.add("text/plain"); - supportedMimeTypes.add("application/conf"); - - MimeTypeMap mtm = MimeTypeMap.getSingleton(); - - for(String ext: new String[] {"ovpn", "conf"}) { - String mimeType = mtm.getMimeTypeFromExtension(ext); - if(mimeType!=null) - supportedMimeTypes.add(mimeType); - else - supportedMimeTypes.add("application/octet-stream"); - } - - - - i.putExtra(Intent.EXTRA_MIME_TYPES, supportedMimeTypes.toArray(new String[supportedMimeTypes.size()])); + Intent i = Utils.getFilePickerIntent(Utils.FileType.OVPN_CONFIG); startActivityForResult(i, FILE_PICKER_RESULT); } diff --git a/src/de/blinkt/openvpn/views/FileSelectLayout.java b/src/de/blinkt/openvpn/views/FileSelectLayout.java index 57ccd895..18a2a766 100644 --- a/src/de/blinkt/openvpn/views/FileSelectLayout.java +++ b/src/de/blinkt/openvpn/views/FileSelectLayout.java @@ -1,13 +1,7 @@ package de.blinkt.openvpn.views; -import android.annotation.TargetApi; -import android.app.Activity; import android.net.Uri; -import android.os.Build; -import android.text.TextUtils; -import android.text.style.CharacterStyle; import android.util.Base64; -import android.webkit.MimeTypeMap; import de.blinkt.openvpn.R; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.activities.FileSelect; @@ -23,10 +17,9 @@ import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; +import de.blinkt.openvpn.fragments.Utils; import java.io.*; -import java.util.TreeSet; -import java.util.Vector; import static android.os.Build.*; @@ -38,20 +31,10 @@ public class FileSelectLayout extends LinearLayout implements OnClickListener { String fileData = data.getStringExtra(FileSelect.RESULT_DATA); setData(fileData, c); } else if (data != null) { - Uri uri = data.getData(); try { - byte[] fileData = readBytesFromStream(c.getContentResolver().openInputStream(uri)); - String newData = null; - switch (fileType) { - case CERTIFICATE: - case KEYFILE: - newData = new String(fileData, "UTF-8"); - break; - case PKCS12: - newData = Base64.encodeToString(fileData, Base64.DEFAULT); - break; - } - setData(VpnProfile.INLINE_TAG + newData, c); + String newData = Utils.getStringFromFilePickerResult(fileType,data,c); + if (newData!=null) + setData(VpnProfile.INLINE_TAG + newData, c); } catch (IOException e) { VpnStatus.logException(e); @@ -61,35 +44,13 @@ public class FileSelectLayout extends LinearLayout implements OnClickListener { } } - static private byte[] readBytesFromStream(InputStream input) throws IOException { - - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - - int nRead; - byte[] data = new byte[16384]; - - while ((nRead = input.read(data, 0, data.length)) != -1) { - buffer.write(data, 0, nRead); - } - - buffer.flush(); - input.close(); - return buffer.toByteArray(); - } - - public enum FileType { - PKCS12, - CERTIFICATE, - KEYFILE - } - private final boolean mIsCertificate; private TextView mDataView; private String mData; private Fragment mFragment; private int mTaskId; private Button mSelectButton; - private FileType fileType; + private Utils.FileType fileType; private String mTitle; private boolean mShowClear; private TextView mDataDetails; @@ -114,7 +75,7 @@ public class FileSelectLayout extends LinearLayout implements OnClickListener { ta.recycle(); } - public void setFragment(Fragment fragment, int i, FileType ft) { + public void setFragment(Fragment fragment, int i, Utils.FileType ft) { mTaskId = i; mFragment = fragment; fileType = ft; @@ -124,7 +85,7 @@ public class FileSelectLayout extends LinearLayout implements OnClickListener { Intent startFC = new Intent(getContext(), FileSelect.class); startFC.putExtra(FileSelect.START_DATA, mData); startFC.putExtra(FileSelect.WINDOW_TITLE, mTitle); - if (fileType == FileType.PKCS12) + if (fileType == Utils.FileType.PKCS12) startFC.putExtra(FileSelect.DO_BASE64_ENCODE, true); if (mShowClear) startFC.putExtra(FileSelect.SHOW_CLEAR_BUTTON, true); @@ -156,62 +117,17 @@ public class FileSelectLayout extends LinearLayout implements OnClickListener { @Override public void onClick(View v) { if (v == mSelectButton) { - if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) - startFilePicker(); - else + if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { + Intent startFilePicker = Utils.getFilePickerIntent(fileType); + mFragment.startActivityForResult(startFilePicker, mTaskId); + } else { getCertificateFileDialog(); + } } } - @TargetApi(VERSION_CODES.KITKAT) - private void startFilePicker() { - - - Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - TreeSet supportedMimeTypes = new TreeSet(); - Vector extensions = new Vector(); - - switch (fileType) { - case PKCS12: - i.setType("application/x-pkcs12"); - supportedMimeTypes.add("application/x-pkcs12"); - extensions.add("p12"); - extensions.add("pfx"); - break; - case CERTIFICATE: - i.setType("application/x-pem-file"); - supportedMimeTypes.add("application/x-x509-ca-cert"); - supportedMimeTypes.add("application/x-x509-user-cert"); - supportedMimeTypes.add("application/x-pem-file"); - supportedMimeTypes.add("text/plain"); - - extensions.add("pem"); - extensions.add("crt"); - break; - case KEYFILE: - i.setType("application/x-pem-file"); - supportedMimeTypes.add("application/x-pem-file"); - supportedMimeTypes.add("application/pkcs8"); - extensions.add("key"); - break; - } - MimeTypeMap mtm = MimeTypeMap.getSingleton(); - - for (String ext : new String[]{"ovpn", "conf"}) { - String mimeType = mtm.getMimeTypeFromExtension(ext); - if (mimeType != null) - supportedMimeTypes.add(mimeType); - else - supportedMimeTypes.add("application/octet-stream"); - } - - - i.putExtra(Intent.EXTRA_MIME_TYPES, supportedMimeTypes.toArray(new String[supportedMimeTypes.size()])); - mFragment.startActivityForResult(i, mTaskId); - } public void setShowClear() { mShowClear = true; -- cgit v1.2.3