From d07fe5bb87b3a68e20484042d3c15fba3c6ccc86 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 5 Jun 2015 05:39:12 +0530 Subject: added proxy/tor preferences --- .../org/sufficientlysecure/keychain/Constants.java | 11 + .../keychain/ui/SettingsActivity.java | 285 ++++++++++++++++++++- .../keychain/util/Preferences.java | 47 ++++ .../keychain/util/orbot/OrbotHelper.java | 124 +++++++++ .../keychain/util/orbot/TorServiceUtils.java | 234 +++++++++++++++++ OpenKeychain/src/main/res/values/strings.xml | 20 ++ .../src/main/res/xml/preference_headers.xml | 3 + OpenKeychain/src/main/res/xml/proxy_prefs.xml | 27 ++ 8 files changed, 750 insertions(+), 1 deletion(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java create mode 100644 OpenKeychain/src/main/res/xml/proxy_prefs.xml (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index d1b37aed2..048fbf327 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -93,6 +93,17 @@ public final class Constants { public static final String FILE_USE_COMPRESSION = "useFileCompression"; public static final String TEXT_USE_COMPRESSION = "useTextCompression"; public static final String USE_ARMOR = "useArmor"; + // proxy settings + public static final String USE_NORMAL_PROXY = "useNormalProxy"; + public static final String USE_TOR_PROXY = "useTorProxy"; + public static final String PROXY_HOST = "proxyHost"; + public static final String PROXY_PORT = "proxyPort"; + } + + public static final class ProxyOrbot { + public static final String PROXY_HOST = "127.0.0.1"; + public static final int PROXY_HTTP_PORT = 8118; + public static final int PROXY_SOCKS_PORT = 9050; } public static final class Defaults { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 6fc0aaac6..a49eb731d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; @@ -33,9 +34,11 @@ import android.widget.LinearLayout; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity; +import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.List; @@ -209,6 +212,285 @@ public class SettingsActivity extends AppCompatPreferenceActivity { } } + public static class ProxyPrefsFragment extends PreferenceFragment { + private CheckBoxPreference mUseTor; + private CheckBoxPreference mUseNormalProxy; + private EditTextPreference mProxyHost; + private EditTextPreference mProxyPort; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.proxy_prefs); + + mUseTor = (CheckBoxPreference) findPreference(Constants.Pref.USE_TOR_PROXY); + mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); + mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); + mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); + + initializeUseTorPref(); + initializeUseNormalProxyPref(); + initialiseEditTextPreferences(); + + if (mUseTor.isChecked()) disableNormalProxyPrefs(); + else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); + } + + private void initializeUseTorPref() { + mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if ((Boolean)newValue) { + OrbotHelper orbotHelper = new OrbotHelper(ProxyPrefsFragment.this.getActivity()); + boolean installed = orbotHelper.isOrbotInstalled(); + if (!installed) { + Log.d(Constants.TAG, "Prompting to install Tor"); + orbotHelper.promptToInstall(ProxyPrefsFragment.this.getActivity()); + // don't let the user check the box until he's installed orbot + return false; + } else { + disableNormalProxyPrefs(); + // let the enable tor box be checked + return true; + } + } + else { + // we're unchecking Tor, so enable other proxy + enableNormalProxyPrefs(); + return true; + } + } + }); + } + + private void initializeUseNormalProxyPref() { + mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if ((Boolean) newValue) { + disableUseTorPrefs(); + } else { + enableUseTorPrefs(); + } + return true; + } + }); + } + + private void initialiseEditTextPreferences() { + mProxyHost.setSummary(mProxyHost.getText()); + mProxyPort.setSummary(mProxyPort.getText()); + + mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (newValue.equals("")) { + Notify.create( + ProxyPrefsFragment.this.getActivity(), + R.string.pref_proxy_host_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } else { + mProxyHost.setSummary((CharSequence) newValue); + return true; + } + } + }); + + mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + try { + int port = Integer.parseInt((String) newValue); + if(port < 0 || port > 65535) { + Notify.create( + ProxyPrefsFragment.this.getActivity(), + R.string.pref_proxy_port_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } + // no issues, save port + mProxyPort.setSummary("" + port); + return true; + } catch (NumberFormatException e) { + Notify.create( + ProxyPrefsFragment.this.getActivity(), + R.string.pref_proxy_port_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } + } + }); + } + + private void disableNormalProxyPrefs() { + mUseNormalProxy.setChecked(false); + mUseNormalProxy.setEnabled(false); + mProxyHost.setEnabled(false); + mProxyPort.setEnabled(false); + } + + private void enableNormalProxyPrefs() { + mUseNormalProxy.setEnabled(true); + mProxyHost.setEnabled(true); + mProxyPort.setEnabled(true); + } + + private void disableUseTorPrefs() { + mUseTor.setChecked(false); + mUseTor.setEnabled(false); + } + + private void enableUseTorPrefs() { + mUseTor.setEnabled(true); + } + } + + @TargetApi(Build.VERSION_CODES.KITKAT) + public static class ProxyPrefsFragment extends PreferenceFragment { + private CheckBoxPreference mUseTor; + private CheckBoxPreference mUseNormalProxy; + private EditTextPreference mProxyHost; + private EditTextPreference mProxyPort; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.proxy_prefs); + + mUseTor = (CheckBoxPreference) findPreference(Constants.Pref.USE_TOR_PROXY); + mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); + mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); + mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); + + initializeUseTorPref(); + initializeUseNormalProxyPref(); + initialiseEditTextPreferences(); + + if (mUseTor.isChecked()) disableNormalProxyPrefs(); + else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); + } + + private void initializeUseTorPref() { + mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if ((Boolean)newValue) { + OrbotHelper orbotHelper = new OrbotHelper(ProxyPrefsFragment.this.getActivity()); + boolean installed = orbotHelper.isOrbotInstalled(); + if (!installed) { + Log.d(Constants.TAG, "Prompting to install Tor"); + orbotHelper.promptToInstall(ProxyPrefsFragment.this.getActivity()); + // don't let the user check the box until he's installed orbot + return false; + } else { + disableNormalProxyPrefs(); + // let the enable tor box be checked + return true; + } + } + else { + // we're unchecking Tor, so enable other proxy + enableNormalProxyPrefs(); + return true; + } + } + }); + } + + private void initializeUseNormalProxyPref() { + mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if ((Boolean) newValue) { + disableUseTorPrefs(); + } else { + enableUseTorPrefs(); + } + return true; + } + }); + } + + private void initialiseEditTextPreferences() { + mProxyHost.setSummary(mProxyHost.getText()); + mProxyPort.setSummary(mProxyPort.getText()); + + mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (newValue.equals("")) { + Notify.create( + ProxyPrefsFragment.this.getActivity(), + R.string.pref_proxy_host_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } else { + mProxyHost.setSummary((CharSequence) newValue); + return true; + } + } + }); + + mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + try { + int port = Integer.parseInt((String) newValue); + if(port < 0 || port > 65535) { + Notify.create( + ProxyPrefsFragment.this.getActivity(), + R.string.pref_proxy_port_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } + // no issues, save port + mProxyPort.setSummary("" + port); + return true; + } catch (NumberFormatException e) { + Notify.create( + ProxyPrefsFragment.this.getActivity(), + R.string.pref_proxy_port_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } + } + }); + } + + private void disableNormalProxyPrefs() { + mUseNormalProxy.setChecked(false); + mUseNormalProxy.setEnabled(false); + mProxyHost.setEnabled(false); + mProxyPort.setEnabled(false); + } + + private void enableNormalProxyPrefs() { + mUseNormalProxy.setEnabled(true); + mProxyHost.setEnabled(true); + mProxyPort.setEnabled(true); + } + + private void disableUseTorPrefs() { + mUseTor.setChecked(false); + mUseTor.setEnabled(false); + } + + private void enableUseTorPrefs() { + mUseTor.setEnabled(true); + } + } + protected boolean isValidFragment(String fragmentName) { return AdvancedPrefsFragment.class.getName().equals(fragmentName) || CloudSearchPrefsFragment.class.getName().equals(fragmentName) @@ -270,7 +552,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity { String[] servers = sPreferences.getKeyServers(); String serverSummary = context.getResources().getQuantityString( R.plurals.n_keyservers, servers.length, servers.length); - return serverSummary + "; " + context.getString(R.string.label_preferred) + ": " + sPreferences.getPreferredKeyserver(); + return serverSummary + "; " + context.getString(R.string.label_preferred) + ": " + sPreferences + .getPreferredKeyserver(); } private static void initializeUseDefaultYubiKeyPin(final CheckBoxPreference mUseDefaultYubiKeyPin) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 713d5f5ea..80f0500c6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -228,6 +228,53 @@ public class Preferences { return mSharedPreferences.getBoolean(Pref.ENCRYPT_FILENAMES, true); } + public boolean getUseNormalProxy() { + return mSharedPreferences.getBoolean(Constants.Pref.USE_NORMAL_PROXY, false); + } + + public void setUseNormalProxy(boolean use) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putBoolean(Constants.Pref.USE_NORMAL_PROXY, use); + editor.commit(); + } + + public boolean getUseTorProxy() { + return mSharedPreferences.getBoolean(Constants.Pref.USE_TOR_PROXY, false); + } + + public void setUseTorProxy(boolean use) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putBoolean(Constants.Pref.USE_TOR_PROXY, use); + editor.commit(); + } + + public String getProxyHost() { + return mSharedPreferences.getString(Constants.Pref.PROXY_HOST, null); + } + + public void setProxyHost(String host) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putString(Constants.Pref.PROXY_HOST, host); + editor.commit(); + } + + /** + * we store port as String for easy interfacing with EditTextPreference, but return it as an integer + * @return port number of proxy + */ + public int getProxyPort() { + return Integer.parseInt(mSharedPreferences.getString(Pref.PROXY_PORT, "-1")); + } + /** + * we store port as String for easy interfacing with EditTextPreference, but return it as an integer + * @param port proxy port + */ + public void setProxyPort(String port) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putString(Pref.PROXY_PORT, port); + editor.commit(); + } + public CloudSearchPrefs getCloudSearchPrefs() { return new CloudSearchPrefs(mSharedPreferences.getBoolean(Pref.SEARCH_KEYSERVER, true), mSharedPreferences.getBoolean(Pref.SEARCH_KEYBASE, true), diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java new file mode 100644 index 000000000..44a12e188 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -0,0 +1,124 @@ + +package org.sufficientlysecure.keychain.util.orbot; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import org.sufficientlysecure.keychain.R; + + +public class OrbotHelper { + + private final static int REQUEST_CODE_STATUS = 100; + + public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; + public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; + + public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; + public final static String ACTION_REQUEST_HS = "org.torproject.android.REQUEST_HS_PORT"; + public final static int HS_REQUEST_CODE = 9999; + + private Context mContext = null; + + public OrbotHelper(Context context) + { + mContext = context; + } + + public boolean isOrbotRunning() + { + int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); + + return (procId != -1); + } + + public boolean isOrbotInstalled() + { + return isAppInstalled(ORBOT_PACKAGE_NAME); + } + + private boolean isAppInstalled(String uri) { + PackageManager pm = mContext.getPackageManager(); + boolean installed = false; + try { + pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); + installed = true; + } catch (PackageManager.NameNotFoundException e) { + installed = false; + } + return installed; + } + + public void promptToInstall(Activity activity) + { + String uriMarket = activity.getString(R.string.market_orbot); + // show dialog - install from market, f-droid or direct APK + showDownloadDialog(activity, activity.getString(R.string.install_orbot_), + activity.getString(R.string.you_must_have_orbot), + activity.getString(R.string.orbot_yes), activity.getString(R.string.orbot_no), uriMarket); + } + + private static AlertDialog showDownloadDialog(final Activity activity, + CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes, + CharSequence stringButtonNo, final String uriString) { + AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); + downloadDialog.setTitle(stringTitle); + downloadDialog.setMessage(stringMessage); + downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + Uri uri = Uri.parse(uriString); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + activity.startActivity(intent); + } + }); + downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + } + }); + return downloadDialog.show(); + } + + public void requestOrbotStart(final Activity activity) + { + + AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); + downloadDialog.setTitle(R.string.start_orbot_); + downloadDialog + .setMessage(R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_); + downloadDialog.setPositiveButton(R.string.orbot_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + activity.startActivityForResult(getOrbotStartIntent(), 1); + } + }); + downloadDialog.setNegativeButton(R.string.orbot_no, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + } + }); + downloadDialog.show(); + + } + + public void requestHiddenServiceOnPort(Activity activity, int port) + { + Intent intent = new Intent(ACTION_REQUEST_HS); + intent.setPackage(ORBOT_PACKAGE_NAME); + intent.putExtra("hs_port", port); + + activity.startActivityForResult(intent, HS_REQUEST_CODE); + } + + public static Intent getOrbotStartIntent() { + Intent intent = new Intent(ACTION_START_TOR); + intent.setPackage(ORBOT_PACKAGE_NAME); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + return intent; + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java new file mode 100644 index 000000000..ea77582fc --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java @@ -0,0 +1,234 @@ +/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */ +/* See LICENSE for licensing information */ + +package org.sufficientlysecure.keychain.util.orbot; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.URLEncoder; +import java.util.StringTokenizer; + +import android.util.Log; + +public class TorServiceUtils { + + private final static String TAG = "TorUtils"; + // various console cmds + public final static String SHELL_CMD_CHMOD = "chmod"; + public final static String SHELL_CMD_KILL = "kill -9"; + public final static String SHELL_CMD_RM = "rm"; + public final static String SHELL_CMD_PS = "ps"; + public final static String SHELL_CMD_PIDOF = "pidof"; + + public final static String CHMOD_EXE_VALUE = "700"; + + public static boolean isRootPossible() + { + + StringBuilder log = new StringBuilder(); + + try { + + // Check if Superuser.apk exists + File fileSU = new File("/system/app/Superuser.apk"); + if (fileSU.exists()) + return true; + + fileSU = new File("/system/app/superuser.apk"); + if (fileSU.exists()) + return true; + + fileSU = new File("/system/bin/su"); + if (fileSU.exists()) + { + String[] cmd = { + "su" + }; + int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); + if (exitCode != 0) + return false; + else + return true; + } + + // Check for 'su' binary + String[] cmd = { + "which su" + }; + int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); + + if (exitCode == 0) { + Log.d(TAG, "root exists, but not sure about permissions"); + return true; + + } + + } catch (IOException e) { + // this means that there is no root to be had (normally) so we won't + // log anything + Log.e(TAG, "Error checking for root access", e); + + } catch (Exception e) { + Log.e(TAG, "Error checking for root access", e); + // this means that there is no root to be had (normally) + } + + Log.e(TAG, "Could not acquire root permissions"); + + return false; + } + + public static int findProcessId(String command) + { + int procId = -1; + + try + { + procId = findProcessIdWithPidOf(command); + + if (procId == -1) + procId = findProcessIdWithPS(command); + } catch (Exception e) + { + try + { + procId = findProcessIdWithPS(command); + } catch (Exception e2) + { + Log.e(TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); + } + } + + return procId; + } + + // use 'pidof' command + public static int findProcessIdWithPidOf(String command) throws Exception + { + + int procId = -1; + + Runtime r = Runtime.getRuntime(); + + Process procPs = null; + + String baseName = new File(command).getName(); + // fix contributed my mikos on 2010.12.10 + procPs = r.exec(new String[] { + SHELL_CMD_PIDOF, baseName + }); + // procPs = r.exec(SHELL_CMD_PIDOF); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line = null; + + while ((line = reader.readLine()) != null) + { + + try + { + // this line should just be the process id + procId = Integer.parseInt(line.trim()); + break; + } catch (NumberFormatException e) + { + Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); + } + } + + return procId; + + } + + // use 'ps' command + public static int findProcessIdWithPS(String command) throws Exception + { + + int procId = -1; + + Runtime r = Runtime.getRuntime(); + + Process procPs = null; + + procPs = r.exec(SHELL_CMD_PS); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line = null; + + while ((line = reader.readLine()) != null) + { + if (line.indexOf(' ' + command) != -1) + { + + StringTokenizer st = new StringTokenizer(line, " "); + st.nextToken(); // proc owner + + procId = Integer.parseInt(st.nextToken().trim()); + + break; + } + } + + return procId; + + } + + public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, + boolean waitFor) throws Exception + { + + Process proc = null; + int exitCode = -1; + + if (runAsRoot) + proc = Runtime.getRuntime().exec("su"); + else + proc = Runtime.getRuntime().exec("sh"); + + OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); + + for (int i = 0; i < cmds.length; i++) + { + // TorService.logMessage("executing shell cmd: " + cmds[i] + + // "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); + + out.write(cmds[i]); + out.write("\n"); + } + + out.flush(); + out.write("exit\n"); + out.flush(); + + if (waitFor) + { + + final char buf[] = new char[10]; + + // Consume the "stdout" + InputStreamReader reader = new InputStreamReader(proc.getInputStream()); + int read = 0; + while ((read = reader.read(buf)) != -1) { + if (log != null) + log.append(buf, 0, read); + } + + // Consume the "stderr" + reader = new InputStreamReader(proc.getErrorStream()); + read = 0; + while ((read = reader.read(buf)) != -1) { + if (log != null) + log.append(buf, 0, read); + } + + exitCode = proc.waitFor(); + + } + + return exitCode; + + } +} diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 5a43fda19..defa7d2a5 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -49,6 +49,7 @@ "Subkeys" "Cloud search" "Password/PIN Handling" + "Proxy Settings" "Confirm" "Actions" "Key" @@ -172,6 +173,16 @@ "keybase.io" "Search keys on keybase.io" + + "Don't use a proxy" + "Enable Tor" + "Requires Orbot to be installed" + "Enable other proxy" + "Proxy Host" + "Proxy host cannot be empty" + "Proxy Port" + "Invalid port number entered" + "<no name>" "<none>" @@ -187,6 +198,15 @@ "Secret Key:" + + "Install Orbot?" + "market://search?q=pname:org.torproject.android" + "You must have Orbot installed and activated to proxy traffic through it. Would you like to install it from Google Play?" + "Yes" + "No" + "Start Orbot?" + "Orbot doesn\'t appear to be running. Would you like to start it up and connect to Tor?" + "None" "15 secs" diff --git a/OpenKeychain/src/main/res/xml/preference_headers.xml b/OpenKeychain/src/main/res/xml/preference_headers.xml index e3447ff48..70e400567 100644 --- a/OpenKeychain/src/main/res/xml/preference_headers.xml +++ b/OpenKeychain/src/main/res/xml/preference_headers.xml @@ -5,4 +5,7 @@
+
diff --git a/OpenKeychain/src/main/res/xml/proxy_prefs.xml b/OpenKeychain/src/main/res/xml/proxy_prefs.xml new file mode 100644 index 000000000..e77ac6d71 --- /dev/null +++ b/OpenKeychain/src/main/res/xml/proxy_prefs.xml @@ -0,0 +1,27 @@ + + + + + + + -- cgit v1.2.3 From 020fad77e5e5780325dc2a4a50edd2c1529715c0 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 5 Jun 2015 17:36:54 +0530 Subject: added license --- .../keychain/util/orbot/OrbotHelper.java | 34 ++++++++++++++++++++- .../keychain/util/orbot/TorServiceUtils.java | 35 ++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java index 44a12e188..aa2052a26 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -1,4 +1,36 @@ - +/* +Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ package org.sufficientlysecure.keychain.util.orbot; import android.app.Activity; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java index ea77582fc..60611d251 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java @@ -1,5 +1,36 @@ -/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */ -/* See LICENSE for licensing information */ +/* +Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ package org.sufficientlysecure.keychain.util.orbot; -- cgit v1.2.3 From f4f3a66d8c1d6a3b863eb7a7a6cb47a995609872 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 5 Jun 2015 18:09:24 +0530 Subject: added class sources --- .../org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java | 6 +++++- .../org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java index aa2052a26..c0aa95201 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -40,9 +40,13 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; -import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.R; +/** + * This class has been taken from the NetCipher library + * https://github.com/guardianproject/NetCipher + */ public class OrbotHelper { private final static int REQUEST_CODE_STATUS = 100; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java index 60611d251..76eff25d7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java @@ -44,6 +44,10 @@ import java.util.StringTokenizer; import android.util.Log; +/** + * This class has been taken from the NetCipher library + * https://github.com/guardianproject/NetCipher + */ public class TorServiceUtils { private final static String TAG = "TorUtils"; -- cgit v1.2.3 From 3034db0f71f9dd8e2f4d8838c1803952702f7520 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sat, 6 Jun 2015 15:26:22 +0530 Subject: added proxy type --- .../org/sufficientlysecure/keychain/Constants.java | 7 +----- .../keychain/ui/SettingsActivity.java | 22 ++++++++++++++++-- .../keychain/util/Preferences.java | 26 ++++++++++++++++++++++ OpenKeychain/src/main/res/values/arrays.xml | 8 +++++++ OpenKeychain/src/main/res/values/strings.xml | 14 ++++++++---- OpenKeychain/src/main/res/xml/proxy_prefs.xml | 13 ++++++++--- 6 files changed, 75 insertions(+), 15 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index 048fbf327..045ef347c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -98,12 +98,7 @@ public final class Constants { public static final String USE_TOR_PROXY = "useTorProxy"; public static final String PROXY_HOST = "proxyHost"; public static final String PROXY_PORT = "proxyPort"; - } - - public static final class ProxyOrbot { - public static final String PROXY_HOST = "127.0.0.1"; - public static final int PROXY_HTTP_PORT = 8118; - public static final int PROXY_SOCKS_PORT = 9050; + public static final String PROXY_TYPE = "proxyType"; } public static final class Defaults { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index a49eb731d..383a542ff 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -217,6 +217,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { private CheckBoxPreference mUseNormalProxy; private EditTextPreference mProxyHost; private EditTextPreference mProxyPort; + private ListPreference mProxyType; @Override public void onCreate(Bundle savedInstanceState) { @@ -229,10 +230,12 @@ public class SettingsActivity extends AppCompatPreferenceActivity { mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); + mProxyType = (ListPreference) findPreference(Constants.Pref.PROXY_TYPE); initializeUseTorPref(); initializeUseNormalProxyPref(); - initialiseEditTextPreferences(); + initializeEditTextPreferences(); + initializeProxyTypePreference(); if (mUseTor.isChecked()) disableNormalProxyPrefs(); else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); @@ -279,7 +282,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { }); } - private void initialiseEditTextPreferences() { + private void initializeEditTextPreferences() { mProxyHost.setSummary(mProxyHost.getText()); mProxyPort.setSummary(mProxyPort.getText()); @@ -328,17 +331,32 @@ public class SettingsActivity extends AppCompatPreferenceActivity { }); } + private void initializeProxyTypePreference() { + mProxyType.setSummary(mProxyType.getEntry()); + + mProxyType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + CharSequence entry = mProxyType.getEntries()[mProxyType.findIndexOfValue((String) newValue)]; + mProxyType.setSummary(entry); + return true; + } + }); + } + private void disableNormalProxyPrefs() { mUseNormalProxy.setChecked(false); mUseNormalProxy.setEnabled(false); mProxyHost.setEnabled(false); mProxyPort.setEnabled(false); + mProxyType.setEnabled(false); } private void enableNormalProxyPrefs() { mUseNormalProxy.setEnabled(true); mProxyHost.setEnabled(true); mProxyPort.setEnabled(true); + mProxyType.setEnabled(true); } private void disableUseTorPrefs() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 80f0500c6..6a7d6d3eb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -21,9 +21,12 @@ package org.sufficientlysecure.keychain.util; import android.content.Context; import android.content.SharedPreferences; +import android.content.res.Resources; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.Pref; +import org.sufficientlysecure.keychain.R; +import java.net.Proxy; import java.util.ArrayList; import java.util.Arrays; import java.util.ListIterator; @@ -35,6 +38,7 @@ import java.util.Vector; public class Preferences { private static Preferences sPreferences; private SharedPreferences mSharedPreferences; + private Resources mResources; public static synchronized Preferences getPreferences(Context context) { return getPreferences(context, false); @@ -51,6 +55,7 @@ public class Preferences { } private Preferences(Context context) { + mResources = context.getResources(); updateSharedPreferences(context); } @@ -228,6 +233,8 @@ public class Preferences { return mSharedPreferences.getBoolean(Pref.ENCRYPT_FILENAMES, true); } + // proxy preference functions start here + public boolean getUseNormalProxy() { return mSharedPreferences.getBoolean(Constants.Pref.USE_NORMAL_PROXY, false); } @@ -260,13 +267,16 @@ public class Preferences { /** * we store port as String for easy interfacing with EditTextPreference, but return it as an integer + * * @return port number of proxy */ public int getProxyPort() { return Integer.parseInt(mSharedPreferences.getString(Pref.PROXY_PORT, "-1")); } + /** * we store port as String for easy interfacing with EditTextPreference, but return it as an integer + * * @param port proxy port */ public void setProxyPort(String port) { @@ -275,6 +285,22 @@ public class Preferences { editor.commit(); } + public Proxy.Type getProxyType() { + final String typeHttp = mResources.getString(R.string.pref_proxy_type_value_http); + final String typeSocks = mResources.getString(R.string.pref_proxy_type_value_socks); + + String type = mSharedPreferences.getString(Pref.PROXY_TYPE, typeHttp); + + if(type.equals(typeHttp)) return Proxy.Type.HTTP; + else if(type.equals(typeSocks)) return Proxy.Type.SOCKS; + else { // shouldn't happen + Log.e(Constants.TAG, "Invalid Proxy Type in preferences"); + return null; + } + } + + // proxy preference functions ends here + public CloudSearchPrefs getCloudSearchPrefs() { return new CloudSearchPrefs(mSharedPreferences.getBoolean(Pref.SEARCH_KEYSERVER, true), mSharedPreferences.getBoolean(Pref.SEARCH_KEYBASE, true), diff --git a/OpenKeychain/src/main/res/values/arrays.xml b/OpenKeychain/src/main/res/values/arrays.xml index 44bbe00cc..241f530d8 100644 --- a/OpenKeychain/src/main/res/values/arrays.xml +++ b/OpenKeychain/src/main/res/values/arrays.xml @@ -29,6 +29,14 @@ 28800 -1 + + @string/pref_proxy_type_choice_http + @string/pref_proxy_type_choice_socks + + + @string/pref_proxy_type_value_http + @string/pref_proxy_type_value_socks + @string/key_size_2048 @string/key_size_4096 diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index defa7d2a5..d8499ed06 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -174,14 +174,20 @@ "Search keys on keybase.io" - "Don't use a proxy" - "Enable Tor" + "Enable Tor" "Requires Orbot to be installed" "Enable other proxy" - "Proxy Host" + "Proxy Host" "Proxy host cannot be empty" - "Proxy Port" + "Proxy Port" "Invalid port number entered" + "Proxy Type" + + + "HTTP" + "SOCKS" + "proxyHttp" + "proxySocks" "<no name>" "<none>" diff --git a/OpenKeychain/src/main/res/xml/proxy_prefs.xml b/OpenKeychain/src/main/res/xml/proxy_prefs.xml index e77ac6d71..ab9c5a3e3 100644 --- a/OpenKeychain/src/main/res/xml/proxy_prefs.xml +++ b/OpenKeychain/src/main/res/xml/proxy_prefs.xml @@ -3,7 +3,7 @@ @@ -21,7 +21,14 @@ android:key="proxyPort" android:defaultValue="8118" android:persistent="true" - android:title="@string/pref_proxy_port" + android:title="@string/pref_proxy_port_label" android:textCursorDrawable="@null" android:inputType="number" /> + -- cgit v1.2.3 From 0883784ce1a2f85100ada0a84c1f604ad458dd96 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sat, 6 Jun 2015 19:13:16 +0530 Subject: added NetCipher as submodule --- .../keychain/ui/SettingsActivity.java | 2 +- .../keychain/util/orbot/OrbotHelper.java | 160 ------------ .../keychain/util/orbot/TorServiceUtils.java | 269 --------------------- OpenKeychain/src/main/res/values/strings.xml | 9 - 4 files changed, 1 insertion(+), 439 deletions(-) delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 383a542ff..4dbfe1a97 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -31,6 +31,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; +import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity; @@ -38,7 +39,6 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.List; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java deleted file mode 100644 index c0aa95201..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ /dev/null @@ -1,160 +0,0 @@ -/* -Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -package org.sufficientlysecure.keychain.util.orbot; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; - -import org.sufficientlysecure.keychain.R; - -/** - * This class has been taken from the NetCipher library - * https://github.com/guardianproject/NetCipher - */ -public class OrbotHelper { - - private final static int REQUEST_CODE_STATUS = 100; - - public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; - public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; - - public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; - public final static String ACTION_REQUEST_HS = "org.torproject.android.REQUEST_HS_PORT"; - public final static int HS_REQUEST_CODE = 9999; - - private Context mContext = null; - - public OrbotHelper(Context context) - { - mContext = context; - } - - public boolean isOrbotRunning() - { - int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); - - return (procId != -1); - } - - public boolean isOrbotInstalled() - { - return isAppInstalled(ORBOT_PACKAGE_NAME); - } - - private boolean isAppInstalled(String uri) { - PackageManager pm = mContext.getPackageManager(); - boolean installed = false; - try { - pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); - installed = true; - } catch (PackageManager.NameNotFoundException e) { - installed = false; - } - return installed; - } - - public void promptToInstall(Activity activity) - { - String uriMarket = activity.getString(R.string.market_orbot); - // show dialog - install from market, f-droid or direct APK - showDownloadDialog(activity, activity.getString(R.string.install_orbot_), - activity.getString(R.string.you_must_have_orbot), - activity.getString(R.string.orbot_yes), activity.getString(R.string.orbot_no), uriMarket); - } - - private static AlertDialog showDownloadDialog(final Activity activity, - CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes, - CharSequence stringButtonNo, final String uriString) { - AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); - downloadDialog.setTitle(stringTitle); - downloadDialog.setMessage(stringMessage); - downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Uri uri = Uri.parse(uriString); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - activity.startActivity(intent); - } - }); - downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - } - }); - return downloadDialog.show(); - } - - public void requestOrbotStart(final Activity activity) - { - - AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); - downloadDialog.setTitle(R.string.start_orbot_); - downloadDialog - .setMessage(R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_); - downloadDialog.setPositiveButton(R.string.orbot_yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - activity.startActivityForResult(getOrbotStartIntent(), 1); - } - }); - downloadDialog.setNegativeButton(R.string.orbot_no, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - } - }); - downloadDialog.show(); - - } - - public void requestHiddenServiceOnPort(Activity activity, int port) - { - Intent intent = new Intent(ACTION_REQUEST_HS); - intent.setPackage(ORBOT_PACKAGE_NAME); - intent.putExtra("hs_port", port); - - activity.startActivityForResult(intent, HS_REQUEST_CODE); - } - - public static Intent getOrbotStartIntent() { - Intent intent = new Intent(ACTION_START_TOR); - intent.setPackage(ORBOT_PACKAGE_NAME); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return intent; - } -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java deleted file mode 100644 index 76eff25d7..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ /dev/null @@ -1,269 +0,0 @@ -/* -Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -package org.sufficientlysecure.keychain.util.orbot; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.URLEncoder; -import java.util.StringTokenizer; - -import android.util.Log; - -/** - * This class has been taken from the NetCipher library - * https://github.com/guardianproject/NetCipher - */ -public class TorServiceUtils { - - private final static String TAG = "TorUtils"; - // various console cmds - public final static String SHELL_CMD_CHMOD = "chmod"; - public final static String SHELL_CMD_KILL = "kill -9"; - public final static String SHELL_CMD_RM = "rm"; - public final static String SHELL_CMD_PS = "ps"; - public final static String SHELL_CMD_PIDOF = "pidof"; - - public final static String CHMOD_EXE_VALUE = "700"; - - public static boolean isRootPossible() - { - - StringBuilder log = new StringBuilder(); - - try { - - // Check if Superuser.apk exists - File fileSU = new File("/system/app/Superuser.apk"); - if (fileSU.exists()) - return true; - - fileSU = new File("/system/app/superuser.apk"); - if (fileSU.exists()) - return true; - - fileSU = new File("/system/bin/su"); - if (fileSU.exists()) - { - String[] cmd = { - "su" - }; - int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); - if (exitCode != 0) - return false; - else - return true; - } - - // Check for 'su' binary - String[] cmd = { - "which su" - }; - int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); - - if (exitCode == 0) { - Log.d(TAG, "root exists, but not sure about permissions"); - return true; - - } - - } catch (IOException e) { - // this means that there is no root to be had (normally) so we won't - // log anything - Log.e(TAG, "Error checking for root access", e); - - } catch (Exception e) { - Log.e(TAG, "Error checking for root access", e); - // this means that there is no root to be had (normally) - } - - Log.e(TAG, "Could not acquire root permissions"); - - return false; - } - - public static int findProcessId(String command) - { - int procId = -1; - - try - { - procId = findProcessIdWithPidOf(command); - - if (procId == -1) - procId = findProcessIdWithPS(command); - } catch (Exception e) - { - try - { - procId = findProcessIdWithPS(command); - } catch (Exception e2) - { - Log.e(TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); - } - } - - return procId; - } - - // use 'pidof' command - public static int findProcessIdWithPidOf(String command) throws Exception - { - - int procId = -1; - - Runtime r = Runtime.getRuntime(); - - Process procPs = null; - - String baseName = new File(command).getName(); - // fix contributed my mikos on 2010.12.10 - procPs = r.exec(new String[] { - SHELL_CMD_PIDOF, baseName - }); - // procPs = r.exec(SHELL_CMD_PIDOF); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; - - while ((line = reader.readLine()) != null) - { - - try - { - // this line should just be the process id - procId = Integer.parseInt(line.trim()); - break; - } catch (NumberFormatException e) - { - Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); - } - } - - return procId; - - } - - // use 'ps' command - public static int findProcessIdWithPS(String command) throws Exception - { - - int procId = -1; - - Runtime r = Runtime.getRuntime(); - - Process procPs = null; - - procPs = r.exec(SHELL_CMD_PS); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; - - while ((line = reader.readLine()) != null) - { - if (line.indexOf(' ' + command) != -1) - { - - StringTokenizer st = new StringTokenizer(line, " "); - st.nextToken(); // proc owner - - procId = Integer.parseInt(st.nextToken().trim()); - - break; - } - } - - return procId; - - } - - public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, - boolean waitFor) throws Exception - { - - Process proc = null; - int exitCode = -1; - - if (runAsRoot) - proc = Runtime.getRuntime().exec("su"); - else - proc = Runtime.getRuntime().exec("sh"); - - OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); - - for (int i = 0; i < cmds.length; i++) - { - // TorService.logMessage("executing shell cmd: " + cmds[i] + - // "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); - - out.write(cmds[i]); - out.write("\n"); - } - - out.flush(); - out.write("exit\n"); - out.flush(); - - if (waitFor) - { - - final char buf[] = new char[10]; - - // Consume the "stdout" - InputStreamReader reader = new InputStreamReader(proc.getInputStream()); - int read = 0; - while ((read = reader.read(buf)) != -1) { - if (log != null) - log.append(buf, 0, read); - } - - // Consume the "stderr" - reader = new InputStreamReader(proc.getErrorStream()); - read = 0; - while ((read = reader.read(buf)) != -1) { - if (log != null) - log.append(buf, 0, read); - } - - exitCode = proc.waitFor(); - - } - - return exitCode; - - } -} diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index d8499ed06..d1380c4fe 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -204,15 +204,6 @@ "Secret Key:" - - "Install Orbot?" - "market://search?q=pname:org.torproject.android" - "You must have Orbot installed and activated to proxy traffic through it. Would you like to install it from Google Play?" - "Yes" - "No" - "Start Orbot?" - "Orbot doesn\'t appear to be running. Would you like to start it up and connect to Tor?" - "None" "15 secs" -- cgit v1.2.3 From a6cb330dafa5fccdd92376502cb6624b9dc72df6 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sat, 6 Jun 2015 19:56:07 +0530 Subject: added ProxyPrefs --- .../org/sufficientlysecure/keychain/Constants.java | 12 +++++++ .../keychain/util/Preferences.java | 41 ++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index 045ef347c..9492f35ce 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -26,6 +26,8 @@ import org.spongycastle.jce.provider.BouncyCastleProvider; import org.sufficientlysecure.keychain.BuildConfig; import java.io.File; +import java.net.InetSocketAddress; +import java.net.Proxy; public final class Constants { @@ -101,6 +103,16 @@ public final class Constants { public static final String PROXY_TYPE = "proxyType"; } + /** + * information to connect to Orbot's localhost HTTP proxy + */ + public static final class Orbot { + public static final String PROXY_HOST = "127.0.0.1"; + public static final int PROXY_PORT = 8118; + public static final Proxy.Type PROXY_TYPE = Proxy.Type.HTTP; + public static final Proxy PROXY = new Proxy(PROXY_TYPE, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); + } + public static final class Defaults { public static final String KEY_SERVERS = "hkps://hkps.pool.sks-keyservers.net, hkps://pgp.mit.edu"; public static final int PREF_VERSION = 4; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 6a7d6d3eb..7c8e5f1ce 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -22,10 +22,12 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; +import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.Pref; import org.sufficientlysecure.keychain.R; +import java.net.InetSocketAddress; import java.net.Proxy; import java.util.ArrayList; import java.util.Arrays; @@ -212,7 +214,6 @@ public class Preferences { } - public void setUseArmor(boolean useArmor) { SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putBoolean(Pref.USE_ARMOR, useArmor); @@ -291,14 +292,48 @@ public class Preferences { String type = mSharedPreferences.getString(Pref.PROXY_TYPE, typeHttp); - if(type.equals(typeHttp)) return Proxy.Type.HTTP; - else if(type.equals(typeSocks)) return Proxy.Type.SOCKS; + if (type.equals(typeHttp)) return Proxy.Type.HTTP; + else if (type.equals(typeSocks)) return Proxy.Type.SOCKS; else { // shouldn't happen Log.e(Constants.TAG, "Invalid Proxy Type in preferences"); return null; } } + public ProxyPrefs getProxyPrefs() { + Proxy proxy = null; + boolean useTor = getUseTorProxy(); + boolean useNormalProxy = getUseNormalProxy(); + + if (useTor) { + proxy = Constants.Orbot.PROXY; + } + else if (useNormalProxy) { + proxy = new Proxy(getProxyType(), new InetSocketAddress(getProxyHost(), getProxyPort())); + } + + return new ProxyPrefs(getUseTorProxy(), getUseNormalProxy(), proxy); + } + + public static class ProxyPrefs { + public final Proxy proxy; + public final boolean torEnabled; + public final boolean normalPorxyEnabled; + + /** + * torEnabled and normalProxyEnabled are not expected to both be true + * + * @param torEnabled if Tor is to be used + * @param normalPorxyEnabled if user-specified proxy is to be used + * @param proxy proxy to use, leave null if none + */ + public ProxyPrefs(boolean torEnabled, boolean normalPorxyEnabled, Proxy proxy) { + this.torEnabled = torEnabled; + this.normalPorxyEnabled = normalPorxyEnabled; + this.proxy = proxy; + } + } + // proxy preference functions ends here public CloudSearchPrefs getCloudSearchPrefs() { -- cgit v1.2.3 From 007d02f01b1381d218a248a377e186b4549a5e0e Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sun, 7 Jun 2015 02:19:03 +0530 Subject: added proxy support, silent right now --- .../org/sufficientlysecure/keychain/Constants.java | 1 - .../keychain/keyimport/CloudSearch.java | 6 +- .../keychain/keyimport/HkpKeyserver.java | 94 +++++++++++++-------- .../keychain/keyimport/KeybaseKeyserver.java | 9 +- .../keychain/keyimport/Keyserver.java | 7 +- .../keychain/operations/CertifyOperation.java | 1 + .../keychain/operations/ImportOperation.java | 2 +- .../keychain/ui/CreateYubiKeyImportFragment.java | 3 +- .../keychain/ui/ImportKeysActivity.java | 31 ++++++- .../keychain/ui/ImportKeysListFragment.java | 12 ++- .../ui/adapter/ImportKeysListCloudLoader.java | 8 +- .../keychain/util/ParcelableProxy.java | 98 ++++++++++++++++++++++ .../keychain/util/Preferences.java | 3 +- .../keychain/util/TlsHelper.java | 27 ++++++ 14 files changed, 248 insertions(+), 54 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index 9492f35ce..f45c4baa3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -110,7 +110,6 @@ public final class Constants { public static final String PROXY_HOST = "127.0.0.1"; public static final int PROXY_PORT = 8118; public static final Proxy.Type PROXY_TYPE = Proxy.Type.HTTP; - public static final Proxy PROXY = new Proxy(PROXY_TYPE, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); } public static final class Defaults { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java index 649cede10..fc5490fd4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java @@ -20,6 +20,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; +import java.net.Proxy; import java.util.ArrayList; import java.util.Vector; @@ -30,7 +31,8 @@ public class CloudSearch { private final static long SECONDS = 1000; - public static ArrayList search(final String query, Preferences.CloudSearchPrefs cloudPrefs) + public static ArrayList search(final String query, Preferences.CloudSearchPrefs cloudPrefs, + final Proxy proxy) throws Keyserver.CloudSearchFailureException { final ArrayList servers = new ArrayList<>(); @@ -51,7 +53,7 @@ public class CloudSearch { @Override public void run() { try { - results.addAll(keyserver.search(query)); + results.addAll(keyserver.search(query, proxy)); } catch (Keyserver.CloudSearchFailureException e) { problems.add(e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index cb8a53e25..f18878bb5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -18,6 +18,8 @@ package org.sufficientlysecure.keychain.keyimport; +import com.squareup.okhttp.*; +import okio.BufferedSink; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; @@ -29,16 +31,14 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLDecoder; -import java.net.URLEncoder; +import java.net.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -190,7 +190,7 @@ public class HkpKeyserver extends Keyserver { return mSecure ? "https://" : "http://"; } - private HttpURLConnection openConnection(URL url) throws IOException { + private HttpURLConnection openConnectioan(URL url) throws IOException { HttpURLConnection conn = null; try { conn = (HttpURLConnection) TlsHelper.openConnection(url); @@ -205,18 +205,43 @@ public class HkpKeyserver extends Keyserver { return conn; } - private String query(String request) throws QueryFailedException, HttpError { + /** + * returns a client with pinned certificate if necessary + * + * @param url + * @param proxy + * @return + */ + private OkHttpClient getClient(URL url, Proxy proxy) { + OkHttpClient client = new OkHttpClient(); + + try { + TlsHelper.pinCertificateIfNecessary(client, url); + } catch (TlsHelper.TlsHelperException e) { + Log.w(Constants.TAG, e); + } + + client.setProxy(proxy); + // TODO: if proxy !=null increase timeout? + client.setConnectTimeout(5000, TimeUnit.MILLISECONDS); + client.setReadTimeout(25000, TimeUnit.MILLISECONDS); + + return client; + } + + private String query(String request, Proxy proxy) throws QueryFailedException, HttpError { try { URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + request); Log.d(Constants.TAG, "hkp keyserver query: " + url); - HttpURLConnection conn = openConnection(url); - conn.connect(); - int response = conn.getResponseCode(); - if (response >= 200 && response < 300) { - return readAll(conn.getInputStream(), conn.getContentEncoding()); + OkHttpClient client = getClient(url, proxy); + Response response = client.newCall(new Request.Builder().url(url).build()).execute(); + + String responseBody = response.body().string();// contains body both in case of success or failure + + if (response.isSuccessful()) { + return responseBody; } else { - String data = readAll(conn.getErrorStream(), conn.getContentEncoding()); - throw new HttpError(response, data); + throw new HttpError(response.code(), responseBody); } } catch (IOException e) { throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!"); @@ -232,7 +257,7 @@ public class HkpKeyserver extends Keyserver { * @throws QueryNeedsRepairException */ @Override - public ArrayList search(String query) throws QueryFailedException, + public ArrayList search(String query, Proxy proxy) throws QueryFailedException, QueryNeedsRepairException { ArrayList results = new ArrayList<>(); @@ -250,7 +275,7 @@ public class HkpKeyserver extends Keyserver { String data; try { - data = query(request); + data = query(request, proxy); } catch (HttpError e) { if (e.getData() != null) { Log.d(Constants.TAG, "returned error data: " + e.getData().toLowerCase(Locale.ENGLISH)); @@ -334,13 +359,14 @@ public class HkpKeyserver extends Keyserver { } @Override - public String get(String keyIdHex) throws QueryFailedException { + public String get(String keyIdHex, Proxy proxy) throws QueryFailedException { String request = "/pks/lookup?op=get&options=mr&search=" + keyIdHex; Log.d(Constants.TAG, "hkp keyserver get: " + request); String data; try { - data = query(request); + data = query(request, proxy); } catch (HttpError httpError) { + httpError.printStackTrace(); throw new QueryFailedException("not found"); } Matcher matcher = PgpHelper.PGP_PUBLIC_KEY.matcher(data); @@ -351,38 +377,35 @@ public class HkpKeyserver extends Keyserver { } @Override - public void add(String armoredKey) throws AddKeyException { + public void add(String armoredKey, Proxy proxy) throws AddKeyException { try { - String request = "/pks/add"; + String path = "/pks/add"; String params; try { params = "keytext=" + URLEncoder.encode(armoredKey, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new AddKeyException(); } - URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + request); + URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + path); Log.d(Constants.TAG, "hkp keyserver add: " + url.toString()); Log.d(Constants.TAG, "params: " + params); - HttpURLConnection conn = openConnection(url); - conn.setRequestMethod("POST"); - conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - conn.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length)); - conn.setDoInput(true); - conn.setDoOutput(true); + RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params); + + Log.e("PHILIP", "Media Type charset: "+body.contentType().charset()); - OutputStream os = conn.getOutputStream(); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); - writer.write(params); - writer.flush(); - writer.close(); - os.close(); + Request request = new Request.Builder() + .url(url) + .addHeader("Content-Type", "application/x-www-form-urlencoded") + .addHeader("Content-Length", Integer.toString(params.getBytes().length)) + .post(body) + .build(); - conn.connect(); + Response response = new OkHttpClient().setProxy(proxy).newCall(request).execute(); - Log.d(Constants.TAG, "response code: " + conn.getResponseCode()); - Log.d(Constants.TAG, "answer: " + readAll(conn.getInputStream(), conn.getContentEncoding())); + Log.d(Constants.TAG, "response code: " + response.code()); + Log.d(Constants.TAG, "answer: " + response.body().string()); } catch (IOException e) { Log.e(Constants.TAG, "IOException", e); throw new AddKeyException(); @@ -398,6 +421,7 @@ public class HkpKeyserver extends Keyserver { * Tries to find a server responsible for a given domain * * @return A responsible Keyserver or null if not found. + * TODO: Add proxy functionality */ public static HkpKeyserver resolve(String domain) { try { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java index e310e9a3f..7bbe42993 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java @@ -26,6 +26,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; +import java.net.Proxy; import java.util.ArrayList; import java.util.List; @@ -34,8 +35,9 @@ public class KeybaseKeyserver extends Keyserver { private String mQuery; @Override - public ArrayList search(String query) throws QueryFailedException, + public ArrayList search(String query, Proxy proxy) throws QueryFailedException, QueryNeedsRepairException { + // TODO: implement proxy ArrayList results = new ArrayList<>(); if (query.startsWith("0x")) { @@ -98,7 +100,8 @@ public class KeybaseKeyserver extends Keyserver { } @Override - public String get(String id) throws QueryFailedException { + public String get(String id, Proxy proxy) throws QueryFailedException { + // TODO: implement proxy try { return User.keyForUsername(id); } catch (KeybaseException e) { @@ -107,7 +110,7 @@ public class KeybaseKeyserver extends Keyserver { } @Override - public void add(String armoredKey) throws AddKeyException { + public void add(String armoredKey, Proxy proxy) throws AddKeyException { throw new AddKeyException(); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java index 5e4bd0b70..260e2af40 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java @@ -21,6 +21,7 @@ package org.sufficientlysecure.keychain.keyimport; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.Proxy; import java.util.List; public abstract class Keyserver { @@ -67,12 +68,12 @@ public abstract class Keyserver { private static final long serialVersionUID = -507574859137295530L; } - public abstract List search(String query) throws QueryFailedException, + public abstract List search(String query, Proxy proxy) throws QueryFailedException, QueryNeedsRepairException; - public abstract String get(String keyIdHex) throws QueryFailedException; + public abstract String get(String keyIdHex, Proxy proxy) throws QueryFailedException; - public abstract void add(String armoredKey) throws AddKeyException; + public abstract void add(String armoredKey, Proxy proxy) throws AddKeyException; public static String readAll(InputStream in, String encoding) throws IOException { ByteArrayOutputStream raw = new ByteArrayOutputStream(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java index 0806e6a16..44427348d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java @@ -46,6 +46,7 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Passphrase; +import java.net.Proxy; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java index ace059dac..44a67086f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java @@ -562,4 +562,4 @@ public class ImportOperation extends BaseOperation { } } -} \ No newline at end of file +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index c64f05687..945d42a24 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -171,8 +171,9 @@ public class CreateYubiKeyImportFragment } public void refreshSearch() { + // TODO: PHILIP implement proxy in YubiKey parts mListFragment.loadNew(new ImportKeysListFragment.CloudLoaderState("0x" + mNfcFingerprint, - Preferences.getPreferences(getActivity()).getCloudSearchPrefs())); + Preferences.getPreferences(getActivity()).getCloudSearchPrefs()), null); } public void importKey() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index bc83b05b0..b2b6c224a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -26,6 +26,7 @@ import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; +import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.intents.OpenKeychainIntents; @@ -42,6 +43,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; +import org.sufficientlysecure.keychain.util.Preferences; import java.io.IOException; import java.util.ArrayList; @@ -87,11 +89,14 @@ public class ImportKeysActivity extends BaseNfcActivity private ArrayList mKeyList; private CryptoOperationHelper mOperationHelper; + private Preferences.ProxyPrefs mProxyPrefs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + mProxyPrefs = Preferences.getPreferences(this).getProxyPrefs(); + mImportButton = findViewById(R.id.import_import); mImportButton.setOnClickListener(new OnClickListener() { @Override @@ -224,7 +229,7 @@ public class ImportKeysActivity extends BaseNfcActivity Notify.Style.WARN).show(mTopFragment); // we just set the keyserver startCloudFragment(savedInstanceState, null, false, keyserver); - // it's not necessary to set the keyserver for ImportKeysListFragment since + // we don't set the keyserver for ImportKeysListFragment since // it'll be taken care of by ImportKeysCloudFragment when the user clicks // the search button startListFragment(savedInstanceState, null, null, null, null); @@ -347,7 +352,29 @@ public class ImportKeysActivity extends BaseNfcActivity } public void loadCallback(ImportKeysListFragment.LoaderState loaderState) { - mListFragment.loadNew(loaderState); + if (loaderState instanceof ImportKeysListFragment.CloudLoaderState) { + // do the tor check + OrbotHelper helper = new OrbotHelper(this); + // TODO: Add callbacks by modifying OrbotHelper so we know if the user wants to not use Tor + + if(mProxyPrefs.torEnabled && !helper.isOrbotInstalled()) { + helper.promptToInstall(this); + return; + } + if(mProxyPrefs.torEnabled && !helper.isOrbotRunning()) { + helper.requestOrbotStart(this); + return; + } + } + + mListFragment.loadNew(loaderState, mProxyPrefs.proxy); + } + + /** + * disables use of Tor as proxy for this session + */ + private void disableTorForSession() { + mProxyPrefs = new Preferences.ProxyPrefs(false, false, null); } private void handleMessage(Message message) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index bf7e41045..a5f661fb7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -47,6 +47,7 @@ import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.net.Proxy; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -64,6 +65,7 @@ public class ImportKeysListFragment extends ListFragment implements private ImportKeysAdapter mAdapter; private LoaderState mLoaderState; + private Proxy mProxy; private static final int LOADER_ID_BYTES = 0; private static final int LOADER_ID_CLOUD = 1; @@ -126,6 +128,7 @@ public class ImportKeysListFragment extends ListFragment implements /** * Creates an interactive ImportKeyListFragment which reads keyrings from bytes, or file specified * by dataUri, or searches a keyserver for serverQuery, if parameter is not null, in that order + * Will immediately load data if non-null bytes/dataUri/serverQuery * * @param bytes byte data containing list of keyrings to be imported * @param dataUri file from which keyrings are to be imported @@ -141,7 +144,7 @@ public class ImportKeysListFragment extends ListFragment implements /** * Visually consists of a list of keyrings with checkboxes to specify which are to be imported - * Can immediately load keyrings specified by any of its parameters + * Will immediately load data if non-null bytes/dataUri/serverQuery is supplied * * @param bytes byte data containing list of keyrings to be imported * @param dataUri file from which keyrings are to be imported @@ -183,6 +186,7 @@ public class ImportKeysListFragment extends ListFragment implements static public class CloudLoaderState extends LoaderState { Preferences.CloudSearchPrefs mCloudPrefs; String mServerQuery; + Proxy proxy; CloudLoaderState(String serverQuery, Preferences.CloudSearchPrefs cloudPrefs) { mServerQuery = serverQuery; @@ -258,7 +262,9 @@ public class ImportKeysListFragment extends ListFragment implements mAdapter.notifyDataSetChanged(); } - public void loadNew(LoaderState loaderState) { + public void loadNew(LoaderState loaderState, Proxy proxy) { + mProxy = proxy; + mLoaderState = loaderState; restartLoaders(); @@ -301,7 +307,7 @@ public class ImportKeysListFragment extends ListFragment implements } case LOADER_ID_CLOUD: { CloudLoaderState ls = (CloudLoaderState) mLoaderState; - return new ImportKeysListCloudLoader(getActivity(), ls.mServerQuery, ls.mCloudPrefs); + return new ImportKeysListCloudLoader(getActivity(), ls.mServerQuery, ls.mCloudPrefs, mProxy); } default: diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java index af919f3b6..05d5a19ee 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java @@ -29,6 +29,7 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; +import java.net.Proxy; import java.util.ArrayList; public class ImportKeysListCloudLoader @@ -38,15 +39,18 @@ public class ImportKeysListCloudLoader Preferences.CloudSearchPrefs mCloudPrefs; String mServerQuery; + private Proxy mProxy; private ArrayList mEntryList = new ArrayList<>(); private AsyncTaskResultWrapper> mEntryListWrapper; - public ImportKeysListCloudLoader(Context context, String serverQuery, Preferences.CloudSearchPrefs cloudPrefs) { + public ImportKeysListCloudLoader(Context context, String serverQuery, Preferences.CloudSearchPrefs cloudPrefs, + Proxy proxy) { super(context); mContext = context; mServerQuery = serverQuery; mCloudPrefs = cloudPrefs; + mProxy = proxy; } @Override @@ -97,7 +101,7 @@ public class ImportKeysListCloudLoader private void queryServer(boolean enforceFingerprint) { try { ArrayList searchResult - = CloudSearch.search(mServerQuery, mCloudPrefs); + = CloudSearch.search(mServerQuery, mCloudPrefs, mProxy); mEntryList.clear(); // add result to data diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java new file mode 100644 index 000000000..a24141a69 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.util; + +import android.os.Parcel; +import android.os.Parcelable; + +import java.net.InetSocketAddress; +import java.net.Proxy; + +/** + * used to simply transport java.net.Proxy objects created using InetSockets between services/activities + */ +public class ParcelableProxy implements Parcelable { + private String mProxyHost; + private int mProxyPort; + private int mProxyType; + + private final int TYPE_HTTP = 1; + private final int TYPE_SOCKS = 2; + + public ParcelableProxy(Proxy proxy) { + InetSocketAddress address = (InetSocketAddress) proxy.address(); + + mProxyHost = address.getHostName(); + mProxyPort = address.getPort(); + + switch (proxy.type()) { + case HTTP: { + mProxyType = TYPE_HTTP; + break; + } + case SOCKS: { + mProxyType = TYPE_SOCKS; + break; + } + } + } + + public Proxy getProxy() { + Proxy.Type type = null; + switch (mProxyType) { + case TYPE_HTTP: + type = Proxy.Type.HTTP; + break; + case TYPE_SOCKS: + type = Proxy.Type.SOCKS; + break; + } + return new Proxy(type, new InetSocketAddress(mProxyHost, mProxyPort)); + } + + protected ParcelableProxy(Parcel in) { + mProxyHost = in.readString(); + mProxyPort = in.readInt(); + mProxyType = in.readInt(); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(mProxyHost); + dest.writeInt(mProxyPort); + dest.writeInt(mProxyType); + } + + @SuppressWarnings("unused") + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + @Override + public ParcelableProxy createFromParcel(Parcel in) { + return new ParcelableProxy(in); + } + + @Override + public ParcelableProxy[] newArray(int size) { + return new ParcelableProxy[size]; + } + }; +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 7c8e5f1ce..ab33f30bf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -306,7 +306,8 @@ public class Preferences { boolean useNormalProxy = getUseNormalProxy(); if (useTor) { - proxy = Constants.Orbot.PROXY; + proxy = new Proxy(Constants.Orbot.PROXY_TYPE, + new InetSocketAddress(Constants.Orbot.PROXY_HOST, Constants.Orbot.PROXY_PORT)); } else if (useNormalProxy) { proxy = new Proxy(getProxyType(), new InetSocketAddress(getProxyHost(), getProxyPort())); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java index 4ff14e3bb..b116524ef 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java @@ -19,6 +19,8 @@ package org.sufficientlysecure.keychain.util; import android.content.res.AssetManager; +import com.squareup.okhttp.CertificatePinner; +import com.squareup.okhttp.OkHttpClient; import org.sufficientlysecure.keychain.Constants; import java.io.ByteArrayInputStream; @@ -85,6 +87,31 @@ public class TlsHelper { return url.openConnection(); } + public static void pinCertificateIfNecessary(OkHttpClient client, URL url) throws TlsHelperException { + if (url.getProtocol().equals("https")) { + for (String domain : sStaticCA.keySet()) { + if (url.getHost().endsWith(domain)) { + pinCertificate(sStaticCA.get(domain), domain, client); + } + } + } + } + + public static void pinCertificate(byte[] certificate, String hostName, OkHttpClient client) + throws TlsHelperException { + try { + // Load CA + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + Certificate ca = cf.generateCertificate(new ByteArrayInputStream(certificate)); + String pin = CertificatePinner.pin(ca); + Log.e("PHILIP", "" + ca.getPublicKey() + ":" + pin); + + client.setCertificatePinner(new CertificatePinner.Builder().add(hostName, pin).build()); + } catch (CertificateException e) { + throw new TlsHelperException(e); + } + } + /** * Opens a Connection that will only accept certificates signed with a specific CA and skips common name check. * This is required for some distributed Keyserver networks like sks-keyservers.net -- cgit v1.2.3 From 25d07d173c9b4d8da9e1596ea6286047fa8f111e Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sun, 7 Jun 2015 21:40:27 +0530 Subject: fixed prefs --- .../org/sufficientlysecure/keychain/ui/SettingsActivity.java | 3 +++ .../org/sufficientlysecure/keychain/util/Preferences.java | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 4dbfe1a97..94b854b73 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -222,6 +222,9 @@ public class SettingsActivity extends AppCompatPreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // makes android's preference framework write to our file instead of default + // This allows us to use the "persistent" attribute to simplify code + sPreferences.setPreferenceManagerFileAndMode(getPreferenceManager()); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.proxy_prefs); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index ab33f30bf..536f956cd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; +import android.preference.PreferenceManager; import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.Pref; @@ -42,6 +43,9 @@ public class Preferences { private SharedPreferences mSharedPreferences; private Resources mResources; + private static String PREF_FILE_NAME = "APG.main"; + private static int PREF_FILE_MODE = Context.MODE_MULTI_PROCESS; + public static synchronized Preferences getPreferences(Context context) { return getPreferences(context, false); } @@ -61,9 +65,14 @@ public class Preferences { updateSharedPreferences(context); } + public void setPreferenceManagerFileAndMode(PreferenceManager manager) { + manager.setSharedPreferencesName(PREF_FILE_NAME); + manager.setSharedPreferencesMode(PREF_FILE_MODE); + } + public void updateSharedPreferences(Context context) { // multi-process safe preferences - mSharedPreferences = context.getSharedPreferences("APG.main", Context.MODE_MULTI_PROCESS); + mSharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, PREF_FILE_MODE); } public String getLanguage() { -- cgit v1.2.3 From 731ec3382ef8df566e56061c25ffc0b292521e20 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sun, 7 Jun 2015 21:44:15 +0530 Subject: fixed certificate pinning --- .../keychain/keyimport/HkpKeyserver.java | 9 +++-- .../keychain/util/TlsHelper.java | 46 +++++++++++++++++----- 2 files changed, 41 insertions(+), 14 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index f18878bb5..da1494ce3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -193,7 +193,7 @@ public class HkpKeyserver extends Keyserver { private HttpURLConnection openConnectioan(URL url) throws IOException { HttpURLConnection conn = null; try { - conn = (HttpURLConnection) TlsHelper.openConnection(url); + conn = (HttpURLConnection) TlsHelper.opeanConnection(url); } catch (TlsHelper.TlsHelperException e) { Log.w(Constants.TAG, e); } @@ -212,7 +212,7 @@ public class HkpKeyserver extends Keyserver { * @param proxy * @return */ - private OkHttpClient getClient(URL url, Proxy proxy) { + public static OkHttpClient getClient(URL url, Proxy proxy) throws IOException { OkHttpClient client = new OkHttpClient(); try { @@ -222,7 +222,7 @@ public class HkpKeyserver extends Keyserver { } client.setProxy(proxy); - // TODO: if proxy !=null increase timeout? + // TODO: PHILIP if proxy !=null increase timeout? client.setConnectTimeout(5000, TimeUnit.MILLISECONDS); client.setReadTimeout(25000, TimeUnit.MILLISECONDS); @@ -244,6 +244,7 @@ public class HkpKeyserver extends Keyserver { throw new HttpError(response.code(), responseBody); } } catch (IOException e) { + e.printStackTrace(); throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!"); } } @@ -421,7 +422,7 @@ public class HkpKeyserver extends Keyserver { * Tries to find a server responsible for a given domain * * @return A responsible Keyserver or null if not found. - * TODO: Add proxy functionality + * TODO: PHILIP Add proxy functionality */ public static HkpKeyserver resolve(String domain) { try { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java index b116524ef..58c250cab 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java @@ -19,7 +19,6 @@ package org.sufficientlysecure.keychain.util; import android.content.res.AssetManager; -import com.squareup.okhttp.CertificatePinner; import com.squareup.okhttp.OkHttpClient; import org.sufficientlysecure.keychain.Constants; @@ -76,7 +75,7 @@ public class TlsHelper { } } - public static URLConnection openConnection(URL url) throws IOException, TlsHelperException { + public static URLConnection opeanConnection(URL url) throws IOException, TlsHelperException { if (url.getProtocol().equals("https")) { for (String domain : sStaticCA.keySet()) { if (url.getHost().endsWith(domain)) { @@ -87,27 +86,54 @@ public class TlsHelper { return url.openConnection(); } - public static void pinCertificateIfNecessary(OkHttpClient client, URL url) throws TlsHelperException { + public static void pinCertificateIfNecessary(OkHttpClient client, URL url) throws TlsHelperException, IOException { if (url.getProtocol().equals("https")) { for (String domain : sStaticCA.keySet()) { if (url.getHost().endsWith(domain)) { - pinCertificate(sStaticCA.get(domain), domain, client); + pinCertificate(sStaticCA.get(domain), client); } } } } - public static void pinCertificate(byte[] certificate, String hostName, OkHttpClient client) - throws TlsHelperException { + /** + * Modifies the client to accept only requests with a given certificate. Applies to all URLs requested by the + * client. + * Therefore a client that is pinned this way should be used to only make requests to URLs with passed certificate. + * TODO: Refactor - More like SSH StrictHostKeyChecking than pinning? + * + * @param certificate certificate to pin + * @param client OkHttpClient to enforce pinning on + * @throws TlsHelperException + * @throws IOException + */ + private static void pinCertificate(byte[] certificate, OkHttpClient client) + throws TlsHelperException, IOException { + // We don't use OkHttp's CertificatePinner since it depends on a TrustManager to verify it too. Refer to + // note at end of description: http://square.github.io/okhttp/javadoc/com/squareup/okhttp/CertificatePinner.html + // Creating our own TrustManager that trusts only our certificate eliminates the need for certificate pinning try { // Load CA CertificateFactory cf = CertificateFactory.getInstance("X.509"); Certificate ca = cf.generateCertificate(new ByteArrayInputStream(certificate)); - String pin = CertificatePinner.pin(ca); - Log.e("PHILIP", "" + ca.getPublicKey() + ":" + pin); - client.setCertificatePinner(new CertificatePinner.Builder().add(hostName, pin).build()); - } catch (CertificateException e) { + // Create a KeyStore containing our trusted CAs + String keyStoreType = KeyStore.getDefaultType(); + KeyStore keyStore = KeyStore.getInstance(keyStoreType); + keyStore.load(null, null); + keyStore.setCertificateEntry("ca", ca); + + // Create a TrustManager that trusts the CAs in our KeyStore + String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); + tmf.init(keyStore); + + // Create an SSLContext that uses our TrustManager + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, tmf.getTrustManagers(), null); + + client.setSslSocketFactory(context.getSocketFactory()); + } catch (CertificateException | KeyStoreException | KeyManagementException | NoSuchAlgorithmException e) { throw new TlsHelperException(e); } } -- cgit v1.2.3 From 413536c62b39f9c583cf86b4cd9750b088df6563 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sun, 7 Jun 2015 22:08:08 +0530 Subject: added placeholder proxy to AddKeyServer --- .../ui/adapter/ImportKeysListCloudLoader.java | 3 +-- .../ui/dialog/AddEditKeyserverDialogFragment.java | 27 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java index 05d5a19ee..3fd517043 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java @@ -100,8 +100,7 @@ public class ImportKeysListCloudLoader */ private void queryServer(boolean enforceFingerprint) { try { - ArrayList searchResult - = CloudSearch.search(mServerQuery, mCloudPrefs, mProxy); + ArrayList searchResult = CloudSearch.search(mServerQuery, mCloudPrefs, mProxy); mEntryList.clear(); // add result to data diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java index 321242b2f..7377c4060 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java @@ -48,11 +48,26 @@ import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; +import com.squareup.okhttp.OkHttpClient; +import com.squareup.okhttp.Request; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.TlsHelper; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +import javax.net.ssl.HttpsURLConnection; +import java.io.IOException; +import java.net.*; +import javax.net.ssl.HttpsURLConnection; + public class AddEditKeyserverDialogFragment extends DialogFragment implements OnEditorActionListener { private static final String ARG_MESSENGER = "arg_messenger"; private static final String ARG_ACTION = "arg_dialog_action"; @@ -207,7 +222,8 @@ public class AddEditKeyserverDialogFragment extends DialogFragment implements On // behaviour same for edit and add String keyserverUrl = mKeyserverEditText.getText().toString(); if (mVerifyKeyserverCheckBox.isChecked()) { - verifyConnection(keyserverUrl); + // TODO: PHILIP Implement proxy + verifyConnection(keyserverUrl, null); } else { dismiss(); // return unverified keyserver back to activity @@ -249,7 +265,7 @@ public class AddEditKeyserverDialogFragment extends DialogFragment implements On sendMessageToHandler(MESSAGE_VERIFICATION_FAILED, data); } - public void verifyConnection(String keyserver) { + public void verifyConnection(String keyserver, final Proxy proxy) { new AsyncTask() { ProgressDialog mProgressDialog; @@ -283,10 +299,11 @@ public class AddEditKeyserverDialogFragment extends DialogFragment implements On } URI newKeyserver = new URI(scheme, schemeSpecificPart, fragment); - Log.d(Constants.TAG, "Converted URL" + newKeyserver); + Log.d("Converted URL", newKeyserver.toString()); - // just see if we can get a connection, then immediately close - TlsHelper.openConnection(newKeyserver.toURL()).getInputStream().close(); + OkHttpClient client = HkpKeyserver.getClient(newKeyserver.toURL(), proxy); + TlsHelper.pinCertificateIfNecessary(client, newKeyserver.toURL()); + client.newCall(new Request.Builder().url(newKeyserver.toURL()).build()).execute(); } catch (TlsHelper.TlsHelperException e) { reason = FailureReason.CONNECTION_FAILED; } catch (MalformedURLException | URISyntaxException e) { -- cgit v1.2.3 From dbc3f90360e56538ca9f599241edce9ac81e063c Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Mon, 8 Jun 2015 01:56:35 +0530 Subject: fixed Proxy generation, proxy working for for cloud import --- .../keychain/keyimport/HkpKeyserver.java | 28 +++++++++------------- .../keychain/ui/ImportKeysActivity.java | 8 +++++-- .../keychain/ui/ImportKeysListFragment.java | 5 ++-- .../keychain/ui/SettingsActivity.java | 5 +++- .../ui/adapter/ImportKeysListCloudLoader.java | 13 ++++++---- .../keychain/util/ParcelableProxy.java | 10 ++++---- .../keychain/util/Preferences.java | 19 +++++++-------- 7 files changed, 46 insertions(+), 42 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index da1494ce3..26f4da4e5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -190,21 +190,6 @@ public class HkpKeyserver extends Keyserver { return mSecure ? "https://" : "http://"; } - private HttpURLConnection openConnectioan(URL url) throws IOException { - HttpURLConnection conn = null; - try { - conn = (HttpURLConnection) TlsHelper.opeanConnection(url); - } catch (TlsHelper.TlsHelperException e) { - Log.w(Constants.TAG, e); - } - if (conn == null) { - conn = (HttpURLConnection) url.openConnection(); - } - conn.setConnectTimeout(5000); - conn.setReadTimeout(25000); - return conn; - } - /** * returns a client with pinned certificate if necessary * @@ -223,8 +208,8 @@ public class HkpKeyserver extends Keyserver { client.setProxy(proxy); // TODO: PHILIP if proxy !=null increase timeout? - client.setConnectTimeout(5000, TimeUnit.MILLISECONDS); - client.setReadTimeout(25000, TimeUnit.MILLISECONDS); + client.setConnectTimeout(proxy != null ? 30000 : 5000, TimeUnit.MILLISECONDS); + client.setReadTimeout(45000, TimeUnit.MILLISECONDS); return client; } @@ -233,9 +218,12 @@ public class HkpKeyserver extends Keyserver { try { URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + request); Log.d(Constants.TAG, "hkp keyserver query: " + url); + Log.d("PHILIP", "hkpKeyserver query(): " + proxy); OkHttpClient client = getClient(url, proxy); Response response = client.newCall(new Request.Builder().url(url).build()).execute(); + tempIpTest(proxy); + String responseBody = response.body().string();// contains body both in case of success or failure if (response.isSuccessful()) { @@ -249,6 +237,12 @@ public class HkpKeyserver extends Keyserver { } } + private void tempIpTest(Proxy proxy) throws IOException { + URL url = new URL("https://wtfismyip.com/text"); + Response response = getClient(url, proxy).newCall(new Request.Builder().url(url).build()).execute(); + Log.e("PHILIP", "proxy Test: " + response.body().string()); + } + /** * Results are sorted by creation date of key! * diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index b2b6c224a..f78c19d13 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -43,9 +43,11 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; import java.io.IOException; +import java.net.Proxy; import java.util.ArrayList; public class ImportKeysActivity extends BaseNfcActivity @@ -367,14 +369,14 @@ public class ImportKeysActivity extends BaseNfcActivity } } - mListFragment.loadNew(loaderState, mProxyPrefs.proxy); + mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } /** * disables use of Tor as proxy for this session */ private void disableTorForSession() { - mProxyPrefs = new Preferences.ProxyPrefs(false, false, null); + mProxyPrefs = new Preferences.ProxyPrefs(false, false, null, -1, null); } private void handleMessage(Message message) { @@ -455,6 +457,8 @@ public class ImportKeysActivity extends BaseNfcActivity ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls; + data.putParcelable(KeychainService.EXTRA_PARCELABLE_PROXY, mProxyPrefs.parcelableProxy); + // get selected key entries ArrayList keys = new ArrayList<>(); { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index a5f661fb7..23368504b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -41,6 +41,7 @@ import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListLoader; import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; import java.io.ByteArrayInputStream; @@ -65,7 +66,7 @@ public class ImportKeysListFragment extends ListFragment implements private ImportKeysAdapter mAdapter; private LoaderState mLoaderState; - private Proxy mProxy; + private ParcelableProxy mProxy; private static final int LOADER_ID_BYTES = 0; private static final int LOADER_ID_CLOUD = 1; @@ -262,7 +263,7 @@ public class ImportKeysListFragment extends ListFragment implements mAdapter.notifyDataSetChanged(); } - public void loadNew(LoaderState loaderState, Proxy proxy) { + public void loadNew(LoaderState loaderState, ParcelableProxy proxy) { mProxy = proxy; mLoaderState = loaderState; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 94b854b73..17df91ba6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -23,10 +23,13 @@ import android.content.Intent; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; +import android.preference.ListPreference; import android.preference.Preference; +import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; import android.support.v7.widget.Toolbar; +import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; @@ -292,7 +295,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - if (newValue.equals("")) { + if (TextUtils.isEmpty((String) newValue)) { Notify.create( ProxyPrefsFragment.this.getActivity(), R.string.pref_proxy_host_err_invalid, diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java index 3fd517043..a4f8f22e0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java @@ -27,6 +27,7 @@ import org.sufficientlysecure.keychain.keyimport.Keyserver; import org.sufficientlysecure.keychain.operations.results.GetKeyResult; import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; import java.net.Proxy; @@ -39,18 +40,18 @@ public class ImportKeysListCloudLoader Preferences.CloudSearchPrefs mCloudPrefs; String mServerQuery; - private Proxy mProxy; + private ParcelableProxy mParcelableProxy; private ArrayList mEntryList = new ArrayList<>(); private AsyncTaskResultWrapper> mEntryListWrapper; public ImportKeysListCloudLoader(Context context, String serverQuery, Preferences.CloudSearchPrefs cloudPrefs, - Proxy proxy) { + ParcelableProxy proxy) { super(context); mContext = context; mServerQuery = serverQuery; mCloudPrefs = cloudPrefs; - mProxy = proxy; + mParcelableProxy = proxy; } @Override @@ -100,7 +101,11 @@ public class ImportKeysListCloudLoader */ private void queryServer(boolean enforceFingerprint) { try { - ArrayList searchResult = CloudSearch.search(mServerQuery, mCloudPrefs, mProxy); + ArrayList searchResult = CloudSearch.search( + mServerQuery, + mCloudPrefs, + mParcelableProxy.getProxy() + ); mEntryList.clear(); // add result to data diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java index a24141a69..e5bb1deb4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java @@ -34,13 +34,11 @@ public class ParcelableProxy implements Parcelable { private final int TYPE_HTTP = 1; private final int TYPE_SOCKS = 2; - public ParcelableProxy(Proxy proxy) { - InetSocketAddress address = (InetSocketAddress) proxy.address(); + public ParcelableProxy(String hostName, int port, Proxy.Type type) { + mProxyHost = hostName; + mProxyPort = port; - mProxyHost = address.getHostName(); - mProxyPort = address.getPort(); - - switch (proxy.type()) { + switch (type) { case HTTP: { mProxyType = TYPE_HTTP; break; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 536f956cd..5650a7deb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -310,23 +310,22 @@ public class Preferences { } public ProxyPrefs getProxyPrefs() { - Proxy proxy = null; boolean useTor = getUseTorProxy(); boolean useNormalProxy = getUseNormalProxy(); if (useTor) { - proxy = new Proxy(Constants.Orbot.PROXY_TYPE, - new InetSocketAddress(Constants.Orbot.PROXY_HOST, Constants.Orbot.PROXY_PORT)); + return new ProxyPrefs(true, false, Constants.Orbot.PROXY_HOST, Constants.Orbot.PROXY_PORT, + Constants.Orbot.PROXY_TYPE); } else if (useNormalProxy) { - proxy = new Proxy(getProxyType(), new InetSocketAddress(getProxyHost(), getProxyPort())); + return new ProxyPrefs(useTor, useNormalProxy, getProxyHost(), getProxyPort(), getProxyType()); + } else { + return new ProxyPrefs(false, false, null, -1, null); } - - return new ProxyPrefs(getUseTorProxy(), getUseNormalProxy(), proxy); } public static class ProxyPrefs { - public final Proxy proxy; + public final ParcelableProxy parcelableProxy; public final boolean torEnabled; public final boolean normalPorxyEnabled; @@ -335,12 +334,12 @@ public class Preferences { * * @param torEnabled if Tor is to be used * @param normalPorxyEnabled if user-specified proxy is to be used - * @param proxy proxy to use, leave null if none */ - public ProxyPrefs(boolean torEnabled, boolean normalPorxyEnabled, Proxy proxy) { + public ProxyPrefs(boolean torEnabled, boolean normalPorxyEnabled, String hostName, int port, Proxy.Type type) { this.torEnabled = torEnabled; this.normalPorxyEnabled = normalPorxyEnabled; - this.proxy = proxy; + if(!torEnabled && !normalPorxyEnabled) this.parcelableProxy = null; + else this.parcelableProxy = new ParcelableProxy(hostName, port, type); } } -- cgit v1.2.3 From 31fac3080e13ef1bd0cf380b3d04c13293259e5f Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Mon, 8 Jun 2015 13:33:11 +0530 Subject: removed NetCipher submodule, copied classes --- .../keychain/util/orbot/OrbotHelper.java | 124 +++++++++++ .../keychain/util/orbot/TorServiceUtils.java | 234 +++++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java new file mode 100644 index 000000000..d9566be4a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -0,0 +1,124 @@ + +package info.guardianproject.onionkit.ui; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; + +import info.guardianproject.onionkit.R; + +public class OrbotHelper { + + private final static int REQUEST_CODE_STATUS = 100; + + public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; + public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; + + public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; + public final static String ACTION_REQUEST_HS = "org.torproject.android.REQUEST_HS_PORT"; + public final static int HS_REQUEST_CODE = 9999; + + private Context mContext = null; + + public OrbotHelper(Context context) + { + mContext = context; + } + + public boolean isOrbotRunning() + { + int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); + + return (procId != -1); + } + + public boolean isOrbotInstalled() + { + return isAppInstalled(ORBOT_PACKAGE_NAME); + } + + private boolean isAppInstalled(String uri) { + PackageManager pm = mContext.getPackageManager(); + boolean installed = false; + try { + pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); + installed = true; + } catch (PackageManager.NameNotFoundException e) { + installed = false; + } + return installed; + } + + public void promptToInstall(Activity activity) + { + String uriMarket = activity.getString(R.string.market_orbot); + // show dialog - install from market, f-droid or direct APK + showDownloadDialog(activity, activity.getString(R.string.install_orbot_), + activity.getString(R.string.you_must_have_orbot), + activity.getString(R.string.yes), activity.getString(R.string.no), uriMarket); + } + + private static AlertDialog showDownloadDialog(final Activity activity, + CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes, + CharSequence stringButtonNo, final String uriString) { + AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); + downloadDialog.setTitle(stringTitle); + downloadDialog.setMessage(stringMessage); + downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + Uri uri = Uri.parse(uriString); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + activity.startActivity(intent); + } + }); + downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + } + }); + return downloadDialog.show(); + } + + public void requestOrbotStart(final Activity activity) + { + + AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); + downloadDialog.setTitle(R.string.start_orbot_); + downloadDialog + .setMessage(R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_); + downloadDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + activity.startActivityForResult(getOrbotStartIntent(), 1); + } + }); + downloadDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + } + }); + downloadDialog.show(); + + } + + public void requestHiddenServiceOnPort(Activity activity, int port) + { + Intent intent = new Intent(ACTION_REQUEST_HS); + intent.setPackage(ORBOT_PACKAGE_NAME); + intent.putExtra("hs_port", port); + + activity.startActivityForResult(intent, HS_REQUEST_CODE); + } + + public static Intent getOrbotStartIntent() { + Intent intent = new Intent(ACTION_START_TOR); + intent.setPackage(ORBOT_PACKAGE_NAME); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + return intent; + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java new file mode 100644 index 000000000..80a812344 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java @@ -0,0 +1,234 @@ +/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */ +/* See LICENSE for licensing information */ + +package info.guardianproject.onionkit.ui; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.URLEncoder; +import java.util.StringTokenizer; + +import android.util.Log; + +public class TorServiceUtils { + + private final static String TAG = "TorUtils"; + // various console cmds + public final static String SHELL_CMD_CHMOD = "chmod"; + public final static String SHELL_CMD_KILL = "kill -9"; + public final static String SHELL_CMD_RM = "rm"; + public final static String SHELL_CMD_PS = "ps"; + public final static String SHELL_CMD_PIDOF = "pidof"; + + public final static String CHMOD_EXE_VALUE = "700"; + + public static boolean isRootPossible() + { + + StringBuilder log = new StringBuilder(); + + try { + + // Check if Superuser.apk exists + File fileSU = new File("/system/app/Superuser.apk"); + if (fileSU.exists()) + return true; + + fileSU = new File("/system/app/superuser.apk"); + if (fileSU.exists()) + return true; + + fileSU = new File("/system/bin/su"); + if (fileSU.exists()) + { + String[] cmd = { + "su" + }; + int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); + if (exitCode != 0) + return false; + else + return true; + } + + // Check for 'su' binary + String[] cmd = { + "which su" + }; + int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); + + if (exitCode == 0) { + Log.d(TAG, "root exists, but not sure about permissions"); + return true; + + } + + } catch (IOException e) { + // this means that there is no root to be had (normally) so we won't + // log anything + Log.e(TAG, "Error checking for root access", e); + + } catch (Exception e) { + Log.e(TAG, "Error checking for root access", e); + // this means that there is no root to be had (normally) + } + + Log.e(TAG, "Could not acquire root permissions"); + + return false; + } + + public static int findProcessId(String command) + { + int procId = -1; + + try + { + procId = findProcessIdWithPidOf(command); + + if (procId == -1) + procId = findProcessIdWithPS(command); + } catch (Exception e) + { + try + { + procId = findProcessIdWithPS(command); + } catch (Exception e2) + { + Log.e(TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); + } + } + + return procId; + } + + // use 'pidof' command + public static int findProcessIdWithPidOf(String command) throws Exception + { + + int procId = -1; + + Runtime r = Runtime.getRuntime(); + + Process procPs = null; + + String baseName = new File(command).getName(); + // fix contributed my mikos on 2010.12.10 + procPs = r.exec(new String[] { + SHELL_CMD_PIDOF, baseName + }); + // procPs = r.exec(SHELL_CMD_PIDOF); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line = null; + + while ((line = reader.readLine()) != null) + { + + try + { + // this line should just be the process id + procId = Integer.parseInt(line.trim()); + break; + } catch (NumberFormatException e) + { + Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); + } + } + + return procId; + + } + + // use 'ps' command + public static int findProcessIdWithPS(String command) throws Exception + { + + int procId = -1; + + Runtime r = Runtime.getRuntime(); + + Process procPs = null; + + procPs = r.exec(SHELL_CMD_PS); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line = null; + + while ((line = reader.readLine()) != null) + { + if (line.indexOf(' ' + command) != -1) + { + + StringTokenizer st = new StringTokenizer(line, " "); + st.nextToken(); // proc owner + + procId = Integer.parseInt(st.nextToken().trim()); + + break; + } + } + + return procId; + + } + + public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, + boolean waitFor) throws Exception + { + + Process proc = null; + int exitCode = -1; + + if (runAsRoot) + proc = Runtime.getRuntime().exec("su"); + else + proc = Runtime.getRuntime().exec("sh"); + + OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); + + for (int i = 0; i < cmds.length; i++) + { + // TorService.logMessage("executing shell cmd: " + cmds[i] + + // "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); + + out.write(cmds[i]); + out.write("\n"); + } + + out.flush(); + out.write("exit\n"); + out.flush(); + + if (waitFor) + { + + final char buf[] = new char[10]; + + // Consume the "stdout" + InputStreamReader reader = new InputStreamReader(proc.getInputStream()); + int read = 0; + while ((read = reader.read(buf)) != -1) { + if (log != null) + log.append(buf, 0, read); + } + + // Consume the "stderr" + reader = new InputStreamReader(proc.getErrorStream()); + read = 0; + while ((read = reader.read(buf)) != -1) { + if (log != null) + log.append(buf, 0, read); + } + + exitCode = proc.waitFor(); + + } + + return exitCode; + + } +} -- cgit v1.2.3 From 65ca77c3cbb5b8bece96e16ff4cd5040ad7884a3 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 11 Jun 2015 01:34:35 +0530 Subject: orbot dialog fragments added --- .../keychain/ui/ImportKeysActivity.java | 40 +- .../keychain/ui/SettingsActivity.java | 419 ++++++++------------- .../keychain/ui/dialog/InstallDialogFragment.java | 143 +++++++ .../ui/dialog/OrbotStartDialogFragment.java | 99 +++++ .../ui/dialog/PreferenceInstallDialogFragment.java | 143 +++++++ .../keychain/util/ParcelableProxy.java | 5 + .../keychain/util/Preferences.java | 24 +- .../keychain/util/orbot/OrbotHelper.java | 155 ++++---- .../keychain/util/orbot/TorServiceUtils.java | 175 +++------ OpenKeychain/src/main/res/values/strings.xml | 29 +- OpenKeychain/src/main/res/xml/proxy_prefs.xml | 10 +- 11 files changed, 738 insertions(+), 504 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index f78c19d13..b41ca58b4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -20,13 +20,13 @@ package org.sufficientlysecure.keychain.ui; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; -import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.intents.OpenKeychainIntents; @@ -43,8 +43,8 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; -import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.io.IOException; import java.net.Proxy; @@ -353,18 +353,33 @@ public class ImportKeysActivity extends BaseNfcActivity } } - public void loadCallback(ImportKeysListFragment.LoaderState loaderState) { + public void loadCallback(final ImportKeysListFragment.LoaderState loaderState) { if (loaderState instanceof ImportKeysListFragment.CloudLoaderState) { // do the tor check - OrbotHelper helper = new OrbotHelper(this); - // TODO: Add callbacks by modifying OrbotHelper so we know if the user wants to not use Tor + // this handle will set tor to be ignored whenever a message is received + Handler ignoreTorHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + // disables Tor until Activity is recreated + mProxyPrefs = new Preferences.ProxyPrefs(false, false, null, -1, null); + mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); + } + }; + + if(mProxyPrefs.torEnabled && !OrbotHelper.isOrbotInstalled(this)) { + + OrbotHelper.getInstallDialogFragmentWithThirdButton( + new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor + ).show(getSupportFragmentManager(), "orbotInstallDialog"); - if(mProxyPrefs.torEnabled && !helper.isOrbotInstalled()) { - helper.promptToInstall(this); return; } - if(mProxyPrefs.torEnabled && !helper.isOrbotRunning()) { - helper.requestOrbotStart(this); + + if(mProxyPrefs.torEnabled && !OrbotHelper.isOrbotRunning()) { + OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor) + .show(getSupportFragmentManager(), "orbotStartDialog"); return; } } @@ -372,13 +387,6 @@ public class ImportKeysActivity extends BaseNfcActivity mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } - /** - * disables use of Tor as proxy for this session - */ - private void disableTorForSession() { - mProxyPrefs = new Preferences.ProxyPrefs(false, false, null, -1, null); - } - private void handleMessage(Message message) { if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) { // get returned data bundle diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 17df91ba6..6605995eb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -18,6 +18,7 @@ package org.sufficientlysecure.keychain.ui; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; @@ -34,7 +35,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; -import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity; @@ -42,6 +42,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.List; @@ -49,6 +50,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { public static final String ACTION_PREFS_CLOUD = "org.sufficientlysecure.keychain.ui.PREFS_CLOUD"; public static final String ACTION_PREFS_ADV = "org.sufficientlysecure.keychain.ui.PREFS_ADV"; + public static final String ACTION_PREFS_PROXY = "org.sufficientlysecure.keychain.ui.PREFS_PROXY"; public static final int REQUEST_CODE_KEYSERVER_PREF = 0x00007005; @@ -216,305 +218,202 @@ public class SettingsActivity extends AppCompatPreferenceActivity { } public static class ProxyPrefsFragment extends PreferenceFragment { - private CheckBoxPreference mUseTor; - private CheckBoxPreference mUseNormalProxy; - private EditTextPreference mProxyHost; - private EditTextPreference mProxyPort; - private ListPreference mProxyType; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // makes android's preference framework write to our file instead of default - // This allows us to use the "persistent" attribute to simplify code - sPreferences.setPreferenceManagerFileAndMode(getPreferenceManager()); - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.proxy_prefs); + new Initializer(this).initialize(); - mUseTor = (CheckBoxPreference) findPreference(Constants.Pref.USE_TOR_PROXY); - mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); - mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); - mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); - mProxyType = (ListPreference) findPreference(Constants.Pref.PROXY_TYPE); + } - initializeUseTorPref(); - initializeUseNormalProxyPref(); - initializeEditTextPreferences(); - initializeProxyTypePreference(); + public static class Initializer { + private CheckBoxPreference mUseTor; + private CheckBoxPreference mUseNormalProxy; + private EditTextPreference mProxyHost; + private EditTextPreference mProxyPort; + private ListPreference mProxyType; + private PreferenceActivity mActivity; + private PreferenceFragment mFragment; + + public Initializer(PreferenceFragment fragment) { + mFragment = fragment; + } - if (mUseTor.isChecked()) disableNormalProxyPrefs(); - else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); - } + public Initializer(PreferenceActivity activity) { + mActivity = activity; + } - private void initializeUseTorPref() { - mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean)newValue) { - OrbotHelper orbotHelper = new OrbotHelper(ProxyPrefsFragment.this.getActivity()); - boolean installed = orbotHelper.isOrbotInstalled(); - if (!installed) { - Log.d(Constants.TAG, "Prompting to install Tor"); - orbotHelper.promptToInstall(ProxyPrefsFragment.this.getActivity()); - // don't let the user check the box until he's installed orbot - return false; - } else { - disableNormalProxyPrefs(); - // let the enable tor box be checked - return true; - } - } - else { - // we're unchecking Tor, so enable other proxy - enableNormalProxyPrefs(); - return true; - } + public Preference automaticallyFindPreference(String key) { + if(mFragment != null) { + return mFragment.findPreference(key); + } else { + return mActivity.findPreference(key); } - }); - } + } - private void initializeUseNormalProxyPref() { - mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean) newValue) { - disableUseTorPrefs(); - } else { - enableUseTorPrefs(); - } - return true; + public void initialize() { + // makes android's preference framework write to our file instead of default + // This allows us to use the "persistent" attribute to simplify code + if (mFragment != null) { + Preferences.setPreferenceManagerFileAndMode(mFragment.getPreferenceManager()); + // Load the preferences from an XML resource + mFragment.addPreferencesFromResource(R.xml.proxy_prefs); + } + else { + Preferences.setPreferenceManagerFileAndMode(mActivity.getPreferenceManager()); + // Load the preferences from an XML resource + mActivity.addPreferencesFromResource(R.xml.proxy_prefs); } - }); - } - private void initializeEditTextPreferences() { - mProxyHost.setSummary(mProxyHost.getText()); - mProxyPort.setSummary(mProxyPort.getText()); - - mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (TextUtils.isEmpty((String) newValue)) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_host_err_invalid, - Notify.Style.ERROR - ).show(); - return false; - } else { - mProxyHost.setSummary((CharSequence) newValue); - return true; + mUseTor = (CheckBoxPreference) automaticallyFindPreference(Constants.Pref.USE_TOR_PROXY); + mUseNormalProxy = (CheckBoxPreference) automaticallyFindPreference(Constants.Pref.USE_NORMAL_PROXY); + mProxyHost = (EditTextPreference) automaticallyFindPreference(Constants.Pref.PROXY_HOST); + mProxyPort = (EditTextPreference) automaticallyFindPreference(Constants.Pref.PROXY_PORT); + mProxyType = (ListPreference) automaticallyFindPreference(Constants.Pref.PROXY_TYPE); + initializeUseTorPref(); + initializeUseNormalProxyPref(); + initializeEditTextPreferences(); + initializeProxyTypePreference(); + + if (mUseTor.isChecked()) disableNormalProxyPrefs(); + else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); + } + + private void initializeUseTorPref() { + mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; + if ((Boolean)newValue) { + boolean installed = OrbotHelper.isOrbotInstalled(activity); + if (!installed) { + Log.d(Constants.TAG, "Prompting to install Tor"); + OrbotHelper.getPreferenceInstallDialogFragment().show(activity.getFragmentManager(), + "installDialog"); + // don't let the user check the box until he's installed orbot + return false; + } else { + disableNormalProxyPrefs(); + // let the enable tor box be checked + return true; + } + } + else { + // we're unchecking Tor, so enable other proxy + enableNormalProxyPrefs(); + return true; + } } - } - }); - - mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - try { - int port = Integer.parseInt((String) newValue); - if(port < 0 || port > 65535) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_port_err_invalid, - Notify.Style.ERROR - ).show(); - return false; + }); + } + + private void initializeUseNormalProxyPref() { + mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if ((Boolean) newValue) { + disableUseTorPrefs(); + } else { + enableUseTorPrefs(); } - // no issues, save port - mProxyPort.setSummary("" + port); return true; - } catch (NumberFormatException e) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_port_err_invalid, - Notify.Style.ERROR - ).show(); - return false; } - } - }); - } - - private void initializeProxyTypePreference() { - mProxyType.setSummary(mProxyType.getEntry()); - - mProxyType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - CharSequence entry = mProxyType.getEntries()[mProxyType.findIndexOfValue((String) newValue)]; - mProxyType.setSummary(entry); - return true; - } - }); - } - - private void disableNormalProxyPrefs() { - mUseNormalProxy.setChecked(false); - mUseNormalProxy.setEnabled(false); - mProxyHost.setEnabled(false); - mProxyPort.setEnabled(false); - mProxyType.setEnabled(false); - } - - private void enableNormalProxyPrefs() { - mUseNormalProxy.setEnabled(true); - mProxyHost.setEnabled(true); - mProxyPort.setEnabled(true); - mProxyType.setEnabled(true); - } - - private void disableUseTorPrefs() { - mUseTor.setChecked(false); - mUseTor.setEnabled(false); - } - - private void enableUseTorPrefs() { - mUseTor.setEnabled(true); - } - } - - @TargetApi(Build.VERSION_CODES.KITKAT) - public static class ProxyPrefsFragment extends PreferenceFragment { - private CheckBoxPreference mUseTor; - private CheckBoxPreference mUseNormalProxy; - private EditTextPreference mProxyHost; - private EditTextPreference mProxyPort; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.proxy_prefs); - - mUseTor = (CheckBoxPreference) findPreference(Constants.Pref.USE_TOR_PROXY); - mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); - mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); - mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); - - initializeUseTorPref(); - initializeUseNormalProxyPref(); - initialiseEditTextPreferences(); + }); + } - if (mUseTor.isChecked()) disableNormalProxyPrefs(); - else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); - } + private void initializeEditTextPreferences() { + mProxyHost.setSummary(mProxyHost.getText()); + mProxyPort.setSummary(mProxyPort.getText()); - private void initializeUseTorPref() { - mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean)newValue) { - OrbotHelper orbotHelper = new OrbotHelper(ProxyPrefsFragment.this.getActivity()); - boolean installed = orbotHelper.isOrbotInstalled(); - if (!installed) { - Log.d(Constants.TAG, "Prompting to install Tor"); - orbotHelper.promptToInstall(ProxyPrefsFragment.this.getActivity()); - // don't let the user check the box until he's installed orbot + mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; + if (TextUtils.isEmpty((String) newValue)) { + Notify.create( + activity, + R.string.pref_proxy_host_err_invalid, + Notify.Style.ERROR + ).show(); return false; } else { - disableNormalProxyPrefs(); - // let the enable tor box be checked + mProxyHost.setSummary((CharSequence) newValue); return true; } } - else { - // we're unchecking Tor, so enable other proxy - enableNormalProxyPrefs(); - return true; - } - } - }); - } - - private void initializeUseNormalProxyPref() { - mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean) newValue) { - disableUseTorPrefs(); - } else { - enableUseTorPrefs(); - } - return true; - } - }); - } + }); - private void initialiseEditTextPreferences() { - mProxyHost.setSummary(mProxyHost.getText()); - mProxyPort.setSummary(mProxyPort.getText()); - - mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (newValue.equals("")) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_host_err_invalid, - Notify.Style.ERROR - ).show(); - return false; - } else { - mProxyHost.setSummary((CharSequence) newValue); - return true; - } - } - }); - - mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - try { - int port = Integer.parseInt((String) newValue); - if(port < 0 || port > 65535) { + mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; + try { + int port = Integer.parseInt((String) newValue); + if(port < 0 || port > 65535) { + Notify.create( + activity, + R.string.pref_proxy_port_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } + // no issues, save port + mProxyPort.setSummary("" + port); + return true; + } catch (NumberFormatException e) { Notify.create( - ProxyPrefsFragment.this.getActivity(), + activity, R.string.pref_proxy_port_err_invalid, Notify.Style.ERROR ).show(); return false; } - // no issues, save port - mProxyPort.setSummary("" + port); + } + }); + } + + private void initializeProxyTypePreference() { + mProxyType.setSummary(mProxyType.getEntry()); + + mProxyType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + CharSequence entry = mProxyType.getEntries()[mProxyType.findIndexOfValue((String) newValue)]; + mProxyType.setSummary(entry); return true; - } catch (NumberFormatException e) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_port_err_invalid, - Notify.Style.ERROR - ).show(); - return false; } - } - }); - } + }); + } - private void disableNormalProxyPrefs() { - mUseNormalProxy.setChecked(false); - mUseNormalProxy.setEnabled(false); - mProxyHost.setEnabled(false); - mProxyPort.setEnabled(false); - } + private void disableNormalProxyPrefs() { + mUseNormalProxy.setChecked(false); + mUseNormalProxy.setEnabled(false); + mProxyHost.setEnabled(false); + mProxyPort.setEnabled(false); + mProxyType.setEnabled(false); + } - private void enableNormalProxyPrefs() { - mUseNormalProxy.setEnabled(true); - mProxyHost.setEnabled(true); - mProxyPort.setEnabled(true); - } + private void enableNormalProxyPrefs() { + mUseNormalProxy.setEnabled(true); + mProxyHost.setEnabled(true); + mProxyPort.setEnabled(true); + mProxyType.setEnabled(true); + } - private void disableUseTorPrefs() { - mUseTor.setChecked(false); - mUseTor.setEnabled(false); - } + private void disableUseTorPrefs() { + mUseTor.setChecked(false); + mUseTor.setEnabled(false); + } - private void enableUseTorPrefs() { - mUseTor.setEnabled(true); + private void enableUseTorPrefs() { + mUseTor.setEnabled(true); + } } + } + @TargetApi(Build.VERSION_CODES.KITKAT) protected boolean isValidFragment(String fragmentName) { return AdvancedPrefsFragment.class.getName().equals(fragmentName) || CloudSearchPrefsFragment.class.getName().equals(fragmentName) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java new file mode 100644 index 000000000..01ff4715d --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Activity; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v4.app.DialogFragment; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +public class InstallDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_TITLE = "title"; + private static final String ARG_MESSAGE = "message"; + private static final String ARG_MIDDLE_BUTTON = "middleButton"; + private static final String ARG_INSTALL_PATH = "installPath"; + private static final String ARG_USE_MIDDLE_BUTTON = "useMiddleButton"; + + public static final String PLAY_STORE_PATH = "market://search?q=pname:"; + + public static final int MESSAGE_MIDDLE_CLICKED = 1; + + /** + * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" + * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. + * + * @param messenger required only for callback from middle button if it has been set + * @param title + * @param message content of dialog + * @param packageToInstall package name of application to install + * @param middleButton if not null, adds a third button to the app with a call back + * @return The dialog to display + */ + public static InstallDialogFragment newInstance(Messenger messenger, int title, int message, + String packageToInstall, int middleButton, boolean useMiddleButton) { + InstallDialogFragment frag = new InstallDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + + args.putInt(ARG_TITLE, title); + args.putInt(ARG_MESSAGE, message); + args.putInt(ARG_MIDDLE_BUTTON, middleButton); + args.putString(ARG_INSTALL_PATH, PLAY_STORE_PATH + packageToInstall); + args.putBoolean(ARG_USE_MIDDLE_BUTTON, useMiddleButton); + + frag.setArguments(args); + + return frag; + } + + /** + * To create a DialogFragment with only two buttons + * + * @param title + * @param message + * @param packageToInstall + * @return + */ + public static InstallDialogFragment newInstance(int title, int message, + String packageToInstall) { + return newInstance(null, title, message, packageToInstall, -1, false); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final Activity activity = getActivity(); + + final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); + + final int title = getArguments().getInt(ARG_TITLE); + final int message = getArguments().getInt(ARG_MESSAGE); + final int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON); + final String installPath = getArguments().getString(ARG_INSTALL_PATH); + final boolean useMiddleButton = getArguments().getBoolean(ARG_USE_MIDDLE_BUTTON); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); + + builder.setTitle(title).setMessage(message); + + builder.setNegativeButton(R.string.orbot_install_dialog_cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + + builder.setPositiveButton(R.string.orbot_install_dialog_install, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Uri uri = Uri.parse(installPath); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + activity.startActivity(intent); + } + } + ); + + if (useMiddleButton) { + builder.setNeutralButton(middleButton, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Message msg = new Message(); + msg.what=MESSAGE_MIDDLE_CLICKED; + try { + messenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + } + ); + } + + return builder.show(); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java new file mode 100644 index 000000000..e9092b14a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v4.app.DialogFragment; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; + +/** + * displays a dialog asking the user to enable Tor + */ +public class OrbotStartDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_TITLE = "title"; + private static final String ARG_MESSAGE = "message"; + private static final String ARG_MIDDLE_BUTTON = "middleButton"; + + public static final int MESSAGE_MIDDLE_BUTTON = 1; + + public static OrbotStartDialogFragment newInstance(Messenger messenger, int title, int message, int middleButton) { + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + args.putInt(ARG_TITLE, title); + args.putInt(ARG_MESSAGE, message); + args.putInt(ARG_MIDDLE_BUTTON, middleButton); + + OrbotStartDialogFragment fragment = new OrbotStartDialogFragment(); + fragment.setArguments(args); + + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + + final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); + int title = getArguments().getInt(ARG_TITLE); + final int message = getArguments().getInt(ARG_MESSAGE); + int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(getActivity()); + builder.setTitle(title).setMessage(message); + + builder.setNegativeButton(R.string.orbot_start_dialog_cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + + builder.setPositiveButton(R.string.orbot_start_dialog_start, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + getActivity().startActivityForResult(OrbotHelper.getOrbotStartIntent(), 1); + } + }); + + builder.setNeutralButton(middleButton, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Message msg = new Message(); + msg.what = MESSAGE_MIDDLE_BUTTON; + try { + messenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + }); + + return builder.show(); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java new file mode 100644 index 000000000..8236ff9d7 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Activity; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.app.DialogFragment; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +public class PreferenceInstallDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_TITLE = "title"; + private static final String ARG_MESSAGE = "message"; + private static final String ARG_MIDDLE_BUTTON = "middleButton"; + private static final String ARG_INSTALL_PATH = "installPath"; + private static final String ARG_USE_MIDDLE_BUTTON = "installPath"; + + public static final String PLAY_STORE_PATH = "market://search?q=pname:"; + + public static final int MESSAGE_MIDDLE_CLICKED = 1; + + /** + * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" + * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. + * + * @param messenger required only for callback from middle button if it has been set + * @param title + * @param message content of dialog + * @param packageToInstall package name of application to install + * @param middleButton if not null, adds a third button to the app with a call back + * @return The dialog to display + */ + public static PreferenceInstallDialogFragment newInstance(Messenger messenger, int title, int message, + String packageToInstall, int middleButton, boolean useMiddleButton) { + PreferenceInstallDialogFragment frag = new PreferenceInstallDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + + args.putInt(ARG_TITLE, title); + args.putInt(ARG_MESSAGE, message); + args.putInt(ARG_MIDDLE_BUTTON, middleButton); + args.putString(ARG_INSTALL_PATH, PLAY_STORE_PATH + packageToInstall); + args.putBoolean(ARG_USE_MIDDLE_BUTTON, useMiddleButton); + + frag.setArguments(args); + + return frag; + } + + /** + * To create a DialogFragment with only two buttons + * + * @param title + * @param message + * @param packageToInstall + * @return + */ + public static PreferenceInstallDialogFragment newInstance(int title, int message, + String packageToInstall) { + return newInstance(null, title, message, packageToInstall, -1, false); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final Activity activity = getActivity(); + + final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); + + final String title = getArguments().getString(ARG_TITLE); + final String message = getArguments().getString(ARG_MESSAGE); + final String installPath = getArguments().getString(ARG_INSTALL_PATH); + final String middleButton = getArguments().getString(ARG_MIDDLE_BUTTON); + final boolean useMiddleButton = getArguments().getBoolean(ARG_USE_MIDDLE_BUTTON); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); + + builder.setTitle(title).setMessage(message); + + builder.setNegativeButton(R.string.orbot_install_dialog_cancel, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + + builder.setPositiveButton(R.string.orbot_install_dialog_install, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Uri uri = Uri.parse(installPath); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + activity.startActivity(intent); + } + } + ); + + if (useMiddleButton) { + builder.setNeutralButton(middleButton, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Message msg = new Message(); + msg.what=MESSAGE_MIDDLE_CLICKED; + try { + messenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + } + ); + } + + return builder.show(); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java index e5bb1deb4..601547fa9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java @@ -36,6 +36,9 @@ public class ParcelableProxy implements Parcelable { public ParcelableProxy(String hostName, int port, Proxy.Type type) { mProxyHost = hostName; + + if (hostName == null) return; // represents a null proxy + mProxyPort = port; switch (type) { @@ -51,6 +54,8 @@ public class ParcelableProxy implements Parcelable { } public Proxy getProxy() { + if(mProxyHost == null) return null; + Proxy.Type type = null; switch (mProxyType) { case TYPE_HTTP: diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 5650a7deb..f54eac867 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -23,12 +23,10 @@ import android.content.SharedPreferences; import android.content.res.Resources; import android.preference.PreferenceManager; -import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.Pref; import org.sufficientlysecure.keychain.R; -import java.net.InetSocketAddress; import java.net.Proxy; import java.util.ArrayList; import java.util.Arrays; @@ -65,7 +63,7 @@ public class Preferences { updateSharedPreferences(context); } - public void setPreferenceManagerFileAndMode(PreferenceManager manager) { + public static void setPreferenceManagerFileAndMode(PreferenceManager manager) { manager.setSharedPreferencesName(PREF_FILE_NAME); manager.setSharedPreferencesMode(PREF_FILE_MODE); } @@ -249,32 +247,14 @@ public class Preferences { return mSharedPreferences.getBoolean(Constants.Pref.USE_NORMAL_PROXY, false); } - public void setUseNormalProxy(boolean use) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putBoolean(Constants.Pref.USE_NORMAL_PROXY, use); - editor.commit(); - } - public boolean getUseTorProxy() { return mSharedPreferences.getBoolean(Constants.Pref.USE_TOR_PROXY, false); } - public void setUseTorProxy(boolean use) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putBoolean(Constants.Pref.USE_TOR_PROXY, use); - editor.commit(); - } - public String getProxyHost() { return mSharedPreferences.getString(Constants.Pref.PROXY_HOST, null); } - public void setProxyHost(String host) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putString(Constants.Pref.PROXY_HOST, host); - editor.commit(); - } - /** * we store port as String for easy interfacing with EditTextPreference, but return it as an integer * @@ -338,7 +318,7 @@ public class Preferences { public ProxyPrefs(boolean torEnabled, boolean normalPorxyEnabled, String hostName, int port, Proxy.Type type) { this.torEnabled = torEnabled; this.normalPorxyEnabled = normalPorxyEnabled; - if(!torEnabled && !normalPorxyEnabled) this.parcelableProxy = null; + if(!torEnabled && !normalPorxyEnabled) this.parcelableProxy = new ParcelableProxy(null, -1, null); else this.parcelableProxy = new ParcelableProxy(hostName, port, type); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java index d9566be4a..fb016f9e6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -1,5 +1,53 @@ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. -package info.guardianproject.onionkit.ui; + For more information about Orlib, see https://guardianproject.info/ + + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html + + ***** +*/ + +package org.sufficientlysecure.keychain.util.orbot; import android.app.Activity; import android.app.AlertDialog; @@ -7,42 +55,37 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; -import android.net.Uri; - -import info.guardianproject.onionkit.R; - +import android.os.Messenger; +import android.support.v4.app.DialogFragment; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; + +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ public class OrbotHelper { - private final static int REQUEST_CODE_STATUS = 100; - public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; - public final static String ACTION_REQUEST_HS = "org.torproject.android.REQUEST_HS_PORT"; - public final static int HS_REQUEST_CODE = 9999; - - private Context mContext = null; - - public OrbotHelper(Context context) - { - mContext = context; - } - public boolean isOrbotRunning() + public static boolean isOrbotRunning() { int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); return (procId != -1); } - public boolean isOrbotInstalled() + public static boolean isOrbotInstalled(Context context) { - return isAppInstalled(ORBOT_PACKAGE_NAME); + return isAppInstalled(ORBOT_PACKAGE_NAME, context); } - private boolean isAppInstalled(String uri) { - PackageManager pm = mContext.getPackageManager(); + private static boolean isAppInstalled(String uri, Context context) { + PackageManager pm = context.getPackageManager(); boolean installed = false; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); @@ -53,66 +96,32 @@ public class OrbotHelper { return installed; } - public void promptToInstall(Activity activity) + /** + * hack to get around teh fact that PreferenceActivity still supports only android.app.DialogFragment + * + * @return + */ + public static android.app.DialogFragment getPreferenceInstallDialogFragment() { - String uriMarket = activity.getString(R.string.market_orbot); - // show dialog - install from market, f-droid or direct APK - showDownloadDialog(activity, activity.getString(R.string.install_orbot_), - activity.getString(R.string.you_must_have_orbot), - activity.getString(R.string.yes), activity.getString(R.string.no), uriMarket); + return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); } - private static AlertDialog showDownloadDialog(final Activity activity, - CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes, - CharSequence stringButtonNo, final String uriString) { - AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); - downloadDialog.setTitle(stringTitle); - downloadDialog.setMessage(stringMessage); - downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Uri uri = Uri.parse(uriString); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - activity.startActivity(intent); - } - }); - downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - } - }); - return downloadDialog.show(); - } - - public void requestOrbotStart(final Activity activity) + public static DialogFragment getInstallDialogFragment() { - - AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); - downloadDialog.setTitle(R.string.start_orbot_); - downloadDialog - .setMessage(R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_); - downloadDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - activity.startActivityForResult(getOrbotStartIntent(), 1); - } - }); - downloadDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - } - }); - downloadDialog.show(); - + return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); } - public void requestHiddenServiceOnPort(Activity activity, int port) + public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { - Intent intent = new Intent(ACTION_REQUEST_HS); - intent.setPackage(ORBOT_PACKAGE_NAME); - intent.putExtra("hs_port", port); + return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); + } - activity.startActivityForResult(intent, HS_REQUEST_CODE); + public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { + return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string.orbot_start_dialog_content, + middleButton); } public static Intent getOrbotStartIntent() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java index 80a812344..127e9d43f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java @@ -1,85 +1,72 @@ -/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */ -/* See LICENSE for licensing information */ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. -package info.guardianproject.onionkit.ui; + For more information about Orlib, see https://guardianproject.info/ -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.URLEncoder; -import java.util.StringTokenizer; + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) -import android.util.Log; + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project -public class TorServiceUtils { + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: - private final static String TAG = "TorUtils"; - // various console cmds - public final static String SHELL_CMD_CHMOD = "chmod"; - public final static String SHELL_CMD_KILL = "kill -9"; - public final static String SHELL_CMD_RM = "rm"; - public final static String SHELL_CMD_PS = "ps"; - public final static String SHELL_CMD_PIDOF = "pidof"; + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. - public final static String CHMOD_EXE_VALUE = "700"; + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. - public static boolean isRootPossible() - { + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. - StringBuilder log = new StringBuilder(); + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - try { + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html - // Check if Superuser.apk exists - File fileSU = new File("/system/app/Superuser.apk"); - if (fileSU.exists()) - return true; + ***** +*/ - fileSU = new File("/system/app/superuser.apk"); - if (fileSU.exists()) - return true; +package org.sufficientlysecure.keychain.util.orbot; - fileSU = new File("/system/bin/su"); - if (fileSU.exists()) - { - String[] cmd = { - "su" - }; - int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); - if (exitCode != 0) - return false; - else - return true; - } - - // Check for 'su' binary - String[] cmd = { - "which su" - }; - int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true); - - if (exitCode == 0) { - Log.d(TAG, "root exists, but not sure about permissions"); - return true; - - } +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.net.URLEncoder; +import java.util.StringTokenizer; - } catch (IOException e) { - // this means that there is no root to be had (normally) so we won't - // log anything - Log.e(TAG, "Error checking for root access", e); +import android.util.Log; - } catch (Exception e) { - Log.e(TAG, "Error checking for root access", e); - // this means that there is no root to be had (normally) - } +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ - Log.e(TAG, "Could not acquire root permissions"); +public class TorServiceUtils { - return false; - } + private final static String TAG = "TorUtils"; + // various console cmds + public final static String SHELL_CMD_PS = "ps"; + public final static String SHELL_CMD_PIDOF = "pidof"; public static int findProcessId(String command) { @@ -175,60 +162,4 @@ public class TorServiceUtils { return procId; } - - public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, - boolean waitFor) throws Exception - { - - Process proc = null; - int exitCode = -1; - - if (runAsRoot) - proc = Runtime.getRuntime().exec("su"); - else - proc = Runtime.getRuntime().exec("sh"); - - OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); - - for (int i = 0; i < cmds.length; i++) - { - // TorService.logMessage("executing shell cmd: " + cmds[i] + - // "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); - - out.write(cmds[i]); - out.write("\n"); - } - - out.flush(); - out.write("exit\n"); - out.flush(); - - if (waitFor) - { - - final char buf[] = new char[10]; - - // Consume the "stdout" - InputStreamReader reader = new InputStreamReader(proc.getInputStream()); - int read = 0; - while ((read = reader.read(buf)) != -1) { - if (log != null) - log.append(buf, 0, read); - } - - // Consume the "stderr" - reader = new InputStreamReader(proc.getErrorStream()); - read = 0; - while ((read = reader.read(buf)) != -1) { - if (log != null) - log.append(buf, 0, read); - } - - exitCode = proc.waitFor(); - - } - - return exitCode; - - } } diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index d1380c4fe..12d222e6a 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -174,21 +174,38 @@ "Search keys on keybase.io" - "Enable Tor" + "Enable Tor" "Requires Orbot to be installed" - "Enable other proxy" - "Proxy Host" + "Enable other proxy" + "Proxy Host" "Proxy host cannot be empty" - "Proxy Port" + "Proxy Port" "Invalid port number entered" - "Proxy Type" + "Proxy Type" - + "HTTP" "SOCKS" "proxyHttp" "proxySocks" + + + Install Orbot to use Tor? + "Install" + You must have Orbot installed and activated to proxy traffic through it. Would you like to install it? + "Cancel" + "Don\'t use Tor" + + + Start Orbot? + "Orbot doesn\'t appear to be running. Would you like to start it up and connect to Tor?" + "Start Orbot" + "Start Orbot" + "Cancel" + "Don\'t use Tor" + + "<no name>" "<none>" diff --git a/OpenKeychain/src/main/res/xml/proxy_prefs.xml b/OpenKeychain/src/main/res/xml/proxy_prefs.xml index ab9c5a3e3..94e101cb6 100644 --- a/OpenKeychain/src/main/res/xml/proxy_prefs.xml +++ b/OpenKeychain/src/main/res/xml/proxy_prefs.xml @@ -3,17 +3,17 @@ + android:title="@string/pref_proxy_normal_title" /> @@ -21,7 +21,7 @@ android:key="proxyPort" android:defaultValue="8118" android:persistent="true" - android:title="@string/pref_proxy_port_label" + android:title="@string/pref_proxy_port_title" android:textCursorDrawable="@null" android:inputType="number" /> + android:title="@string/pref_proxy_type_title" /> -- cgit v1.2.3 From aa7ad4ac634123db73dbbd59930df49086c9395c Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 11 Jun 2015 03:40:20 +0530 Subject: added OperationResult support to upload to keyserver --- .../keychain/operations/CertifyOperation.java | 24 ++++++++++++++-------- .../operations/results/OperationResult.java | 4 ++++ OpenKeychain/src/main/res/values/strings.xml | 4 ++++ 3 files changed, 23 insertions(+), 9 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java index 44427348d..ef08964a3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java @@ -23,6 +23,8 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.keyimport.Keyserver.AddKeyException; import org.sufficientlysecure.keychain.operations.results.CertifyResult; +import org.sufficientlysecure.keychain.operations.results.ExportResult; +import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType; import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog; import org.sufficientlysecure.keychain.operations.results.SaveKeyringResult; @@ -211,12 +213,12 @@ public class CertifyOperation extends BaseOperation { SaveKeyringResult result = mProviderHelper.savePublicKeyRing(certifiedKey); if (exportOperation != null) { - // TODO use subresult, get rid of try/catch! - try { - exportOperation.uploadKeyRingToServer(keyServer, certifiedKey); + ExportResult uploadResult = importExportOperation.uploadKeyRingToServer(keyServer, certifiedKey, proxy); + log.add(uploadResult, 2); + + if (uploadResult.success()) { uploadOk += 1; - } catch (AddKeyException e) { - Log.e(Constants.TAG, "error uploading key", e); + } else { uploadError += 1; } } @@ -236,11 +238,15 @@ public class CertifyOperation extends BaseOperation { return new CertifyResult(CertifyResult.RESULT_ERROR, log, certifyOk, certifyError, uploadOk, uploadError); } - log.add(LogType.MSG_CRT_SUCCESS, 0); - //since only verified keys are synced to contacts, we need to initiate a sync now + // since only verified keys are synced to contacts, we need to initiate a sync now ContactSyncAdapterService.requestSync(); - - return new CertifyResult(CertifyResult.RESULT_OK, log, certifyOk, certifyError, uploadOk, uploadError); + + log.add(LogType.MSG_CRT_SUCCESS, 0); + if (uploadError != 0) { + return new CertifyResult(CertifyResult.RESULT_WARNINGS, log, certifyOk, certifyError, uploadOk, uploadError); + } else { + return new CertifyResult(CertifyResult.RESULT_OK, log, certifyOk, certifyError, uploadOk, uploadError); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java index 245623762..2dd493c81 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java @@ -692,6 +692,7 @@ public abstract class OperationResult implements Parcelable { MSG_CRT_WARN_NOT_FOUND (LogLevel.WARN, R.string.msg_crt_warn_not_found), MSG_CRT_WARN_CERT_FAILED (LogLevel.WARN, R.string.msg_crt_warn_cert_failed), MSG_CRT_WARN_SAVE_FAILED (LogLevel.WARN, R.string.msg_crt_warn_save_failed), + MSG_CRT_WARN_UPLOAD_FAILED (LogLevel.WARN, R.string.msg_crt_warn_upload_failed), MSG_IMPORT (LogLevel.START, R.plurals.msg_import), @@ -712,6 +713,7 @@ public abstract class OperationResult implements Parcelable { MSG_IMPORT_SUCCESS (LogLevel.OK, R.string.msg_import_success), MSG_EXPORT (LogLevel.START, R.plurals.msg_export), + MSG_EXPORT_UPLOAD_PUBLIC(LogLevel.START, R.string.msg_export_upload_public), MSG_EXPORT_PUBLIC (LogLevel.DEBUG, R.string.msg_export_public), MSG_EXPORT_SECRET (LogLevel.DEBUG, R.string.msg_export_secret), MSG_EXPORT_ALL (LogLevel.START, R.string.msg_export_all), @@ -723,7 +725,9 @@ public abstract class OperationResult implements Parcelable { MSG_EXPORT_ERROR_DB (LogLevel.ERROR, R.string.msg_export_error_db), MSG_EXPORT_ERROR_IO (LogLevel.ERROR, R.string.msg_export_error_io), MSG_EXPORT_ERROR_KEY (LogLevel.ERROR, R.string.msg_export_error_key), + MSG_EXPORT_ERROR_UPLOAD (LogLevel.ERROR, R.string.msg_export_error_upload), MSG_EXPORT_SUCCESS (LogLevel.OK, R.string.msg_export_success), + MSG_EXPORT_UPLOAD_SUCCESS (LogLevel.OK, R.string.msg_export_upload_success), MSG_CRT_UPLOAD_SUCCESS (LogLevel.OK, R.string.msg_crt_upload_success), diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 12d222e6a..fc392d6f8 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -1197,6 +1197,7 @@ "Key not found!" "Certificate generation failed!" "Save operation failed!" + "Upload operation failed!" "Successfully uploaded key to server" @@ -1226,6 +1227,7 @@ "Exporting all keys" "Exporting public key %s" + "Uploading public key %s" "Exporting secret key %s" "No filename specified!" "Error opening file!" @@ -1235,7 +1237,9 @@ "Database error!" "Input/output error!" "Error preprocessing key data!" + "Error uploading key to server! Please check your internet connection" "Export operation successful" + "Upload to keyserver successful" "Nothing to delete!" "Secret keys can only be deleted individually!" -- cgit v1.2.3 From 79fc0f97eafd6a70bd40bb8cddd50de52ef84f7c Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 11 Jun 2015 05:33:31 +0530 Subject: added proxy to all import operations using KeychainService --- .../keychain/ui/CreateYubiKeyImportFragment.java | 18 +++++++- .../keychain/ui/DecryptFragment.java | 18 +++++++- .../keychain/ui/ImportKeysProxyActivity.java | 30 ++++++++++---- .../keychain/ui/KeyListFragment.java | 5 ++- .../keychain/ui/UploadKeyActivity.java | 16 +++++++- .../keychain/ui/ViewKeyActivity.java | 6 +-- .../keychain/util/orbot/OrbotHelper.java | 48 ++++++++++++++++++++++ OpenKeychain/src/main/res/values/strings.xml | 2 + 8 files changed, 122 insertions(+), 21 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index 945d42a24..4d85255ca 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -41,7 +41,9 @@ import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction; import org.sufficientlysecure.keychain.ui.CreateKeyActivity.NfcListenerFragment; import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; public class CreateYubiKeyImportFragment @@ -120,7 +122,19 @@ public class CreateYubiKeyImportFragment mNextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - importKey(); + + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()).getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + importKey(new ParcelableProxy(null, -1, null)); + } + }; + + if(OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + getActivity())) { + importKey(proxyPrefs.parcelableProxy); + } } }); } @@ -176,7 +190,7 @@ public class CreateYubiKeyImportFragment Preferences.getPreferences(getActivity()).getCloudSearchPrefs()), null); } - public void importKey() { + public void importKey(ParcelableProxy parcelableProxy) { ArrayList keyList = new ArrayList<>(); keyList.add(new ParcelableKeyRing(mNfcFingerprint, null, null)); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java index 4eb8cd5e8..adb15111d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -58,7 +58,9 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils.State; import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.Style; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; public abstract class DecryptFragment extends Fragment implements LoaderManager.LoaderCallbacks { @@ -136,7 +138,7 @@ public abstract class DecryptFragment extends Fragment implements LoaderManager. } } - private void lookupUnknownKey(long unknownKeyId) { + private void lookupUnknownKey(long unknownKeyId, ParcelableProxy parcelableProxy) { final ArrayList keyList; final String keyserver; @@ -427,7 +429,19 @@ public abstract class DecryptFragment extends Fragment implements LoaderManager. mSignatureLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - lookupUnknownKey(signatureKeyId); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()) + .getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + lookupUnknownKey(signatureKeyId, new ParcelableProxy(null, -1, null)); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + getActivity())) { + lookupUnknownKey(signatureKeyId, proxyPrefs.parcelableProxy); + } } }); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java index da713d0d8..1a03b8102 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java @@ -44,7 +44,9 @@ import org.sufficientlysecure.keychain.service.ImportKeyringParcel; import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.util.IntentIntegratorSupportV4; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.ArrayList; import java.util.Locale; @@ -157,8 +159,7 @@ public class ImportKeysProxyActivity extends FragmentActivity returnResult(intent); return; } - - String fingerprint = uri.getEncodedSchemeSpecificPart().toLowerCase(Locale.ENGLISH); + final String fingerprint = uri.getEncodedSchemeSpecificPart().toLowerCase(Locale.ENGLISH); if (!fingerprint.matches("[a-fA-F0-9]{40}")) { SingletonResult result = new SingletonResult( SingletonResult.RESULT_ERROR, LogType.MSG_WRONG_QR_CODE_FP); @@ -194,23 +195,23 @@ public class ImportKeysProxyActivity extends FragmentActivity } } - public void importKeys(byte[] keyringData) { + public void importKeys(byte[] keyringData, ParcelableProxy parcelableProxy) { ParcelableKeyRing keyEntry = new ParcelableKeyRing(keyringData); ArrayList selectedEntries = new ArrayList<>(); selectedEntries.add(keyEntry); - startImportService(selectedEntries); + startImportService(selectedEntries, parcelableProxy); } - public void importKeys(String fingerprint) { + public void importKeys(String fingerprint, ParcelableProxy parcelableProxy) { ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); ArrayList selectedEntries = new ArrayList<>(); selectedEntries.add(keyEntry); - startImportService(selectedEntries); + startImportService(selectedEntries, parcelableProxy); } - private void startImportService(ArrayList keyRings) { + private void startImportService(ArrayList keyRings, ParcelableProxy parcelableProxy) { // search config { @@ -273,8 +274,19 @@ public class ImportKeysProxyActivity extends FragmentActivity // only one message sent during the beam NdefMessage msg = (NdefMessage) rawMsgs[0]; // record 0 contains the MIME type, record 1 is the AAR, if present - byte[] receivedKeyringBytes = msg.getRecords()[0].getPayload(); - importKeys(receivedKeyringBytes); + final byte[] receivedKeyringBytes = msg.getRecords()[0].getPayload(); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(this) + .getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + importKeys(receivedKeyringBytes, new ParcelableProxy(null, -1, null)); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, this)) { + importKeys(receivedKeyringBytes, proxyPrefs.parcelableProxy); + } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index e038cf948..76ae147a2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -71,16 +71,17 @@ import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter; import org.sufficientlysecure.keychain.ui.util.Notify; -import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.util.ExportHelper; import org.sufficientlysecure.keychain.util.FabContainer; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.ui.util.Notify.Style; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter; import se.emilsjolander.stickylistheaders.StickyListHeadersListView; @@ -562,7 +563,7 @@ public class KeyListFragment extends LoaderFragment startActivityForResult(intent, REQUEST_ACTION); } - private void updateAllKeys() { + private void updateAllKeys(ParcelableProxy parcelableProxy) { Activity activity = getActivity(); if (activity == null) { return; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java index 8b49f3b96..ee37b4b98 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java @@ -37,7 +37,9 @@ import org.sufficientlysecure.keychain.service.ExportKeyringParcel; import org.sufficientlysecure.keychain.ui.base.BaseActivity; import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; /** * Sends the selected public key to a keyserver @@ -76,7 +78,19 @@ public class UploadKeyActivity extends BaseActivity mUploadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - uploadKey(); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(UploadKeyActivity.this) + .getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + uploadKey(proxyPrefs.parcelableProxy); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + UploadKeyActivity.this)) { + uploadKey(proxyPrefs.parcelableProxy); + } } }); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 27832b665..da00748e4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -53,6 +53,7 @@ import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.getbase.floatingactionbutton.FloatingActionButton; +import edu.cmu.cylab.starslinger.exchange.ExchangeActivity; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; @@ -80,11 +81,6 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.ActionListener; import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.ui.util.QrCodeUtils; -import org.sufficientlysecure.keychain.util.ContactHelper; -import org.sufficientlysecure.keychain.util.ExportHelper; -import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.NfcHelper; -import org.sufficientlysecure.keychain.util.Preferences; import java.io.IOException; import java.util.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java index fb016f9e6..5e0e393c5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -55,12 +55,17 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; +import android.os.Handler; +import android.os.Message; import android.os.Messenger; import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; +import org.sufficientlysecure.keychain.util.Preferences; /** * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ @@ -130,4 +135,47 @@ public class OrbotHelper { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); return intent; } + + /** + * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. + * + * @param middleButton resourceId of string to display as the middle button of install and enable dialogs + * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button + * @param proxyPrefs + * @param fragmentActivity + * @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false + */ + public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable, + Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) { + Handler ignoreTorHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + // every message received by this handler will mean the middle button was pressed + middleButtonRunnable.run(); + } + }; + + if (!proxyPrefs.torEnabled) { + return true; + } + + if(!OrbotHelper.isOrbotInstalled(fragmentActivity)) { + + OrbotHelper.getInstallDialogFragmentWithThirdButton( + new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor + ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); + + return false; + } else if(!OrbotHelper.isOrbotRunning()) { + + OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor) + .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); + + return false; + } else { + return true; + } + } } diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index fc392d6f8..f6892c664 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -190,6 +190,8 @@ "proxySocks" + "Don\'t use Tor" + Install Orbot to use Tor? "Install" -- cgit v1.2.3 From 389fd7d46dcf9e2496fddfd785665a7381ecfca5 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 11 Jun 2015 05:42:10 +0530 Subject: modify ImportKeysActivity to match new pattern --- .../keychain/ui/ImportKeysActivity.java | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index b41ca58b4..4b6af6072 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -357,30 +357,16 @@ public class ImportKeysActivity extends BaseNfcActivity if (loaderState instanceof ImportKeysListFragment.CloudLoaderState) { // do the tor check // this handle will set tor to be ignored whenever a message is received - Handler ignoreTorHandler = new Handler() { + Runnable ignoreTor = new Runnable() { @Override - public void handleMessage(Message msg) { + public void run() { // disables Tor until Activity is recreated mProxyPrefs = new Preferences.ProxyPrefs(false, false, null, -1, null); mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } }; - - if(mProxyPrefs.torEnabled && !OrbotHelper.isOrbotInstalled(this)) { - - OrbotHelper.getInstallDialogFragmentWithThirdButton( - new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor - ).show(getSupportFragmentManager(), "orbotInstallDialog"); - - return; - } - - if(mProxyPrefs.torEnabled && !OrbotHelper.isOrbotRunning()) { - OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor) - .show(getSupportFragmentManager(), "orbotStartDialog"); - return; + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, mProxyPrefs, this)) { + mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } } -- cgit v1.2.3 From 592b1285963a90e5b4e47022c5d7dc29c7b951d9 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 11 Jun 2015 05:58:37 +0530 Subject: added proxy support to keyserver verification --- .../keychain/ui/dialog/AddEditKeyserverDialogFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java index 7377c4060..056d82fd3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java @@ -223,7 +223,7 @@ public class AddEditKeyserverDialogFragment extends DialogFragment implements On String keyserverUrl = mKeyserverEditText.getText().toString(); if (mVerifyKeyserverCheckBox.isChecked()) { // TODO: PHILIP Implement proxy - verifyConnection(keyserverUrl, null); + verifyConnection(keyserverUrl); } else { dismiss(); // return unverified keyserver back to activity -- cgit v1.2.3 From ec8c57b7803cf2f4435778cc455d0acc30482c24 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 11 Jun 2015 08:24:33 +0530 Subject: added proxy support to KeybaseLib --- .../keychain/keyimport/CloudSearch.java | 12 ++++++---- .../keychain/keyimport/KeybaseKeyserver.java | 6 ++--- .../keychain/ui/ImportKeysActivity.java | 5 ++-- .../keychain/ui/ViewKeyTrustFragment.java | 28 +++++++++++++++++++--- 4 files changed, 37 insertions(+), 14 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java index fc5490fd4..c089db17f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java @@ -65,20 +65,24 @@ public class CloudSearch { searchThread.start(); } - // wait for either all the searches to come back, or 10 seconds + // wait for either all the searches to come back, or 10 seconds. If using proxy, wait 30 seconds. synchronized (results) { try { - results.wait(10 * SECONDS); + if (proxy != null) { + results.wait(30 * SECONDS); + } else{ + results.wait(10 * SECONDS); + } for (Thread thread : searchThreads) { // kill threads that haven't returned yet thread.interrupt(); - } + } } catch (InterruptedException e) { } } if (results.outstandingSuppliers() > 0) { - String message = "Launched " + servers.size() + " cloud searchers, but" + + String message = "Launched " + servers.size() + " cloud searchers, but " + results.outstandingSuppliers() + "failed to complete."; problems.add(new Keyserver.QueryFailedException(message)); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java index 7bbe42993..c2865410e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java @@ -37,7 +37,6 @@ public class KeybaseKeyserver extends Keyserver { @Override public ArrayList search(String query, Proxy proxy) throws QueryFailedException, QueryNeedsRepairException { - // TODO: implement proxy ArrayList results = new ArrayList<>(); if (query.startsWith("0x")) { @@ -50,7 +49,7 @@ public class KeybaseKeyserver extends Keyserver { mQuery = query; try { - Iterable matches = Search.search(query); + Iterable matches = Search.search(query, proxy); for (Match match : matches) { results.add(makeEntry(match)); } @@ -101,9 +100,8 @@ public class KeybaseKeyserver extends Keyserver { @Override public String get(String id, Proxy proxy) throws QueryFailedException { - // TODO: implement proxy try { - return User.keyForUsername(id); + return User.keyForUsername(id, proxy); } catch (KeybaseException e) { throw new QueryFailedException(e.getMessage()); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 4b6af6072..b61e94c6d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -20,7 +20,6 @@ package org.sufficientlysecure.keychain.ui; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.view.View; @@ -368,9 +367,9 @@ public class ImportKeysActivity extends BaseNfcActivity if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, mProxyPrefs, this)) { mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } + } else if (loaderState instanceof ImportKeysListFragment.BytesLoaderState) { // must always be true + mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } - - mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } private void handleMessage(Message message) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java index 6052eec16..32e8b4fc1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java @@ -51,7 +51,11 @@ import org.sufficientlysecure.keychain.service.KeybaseVerificationParcel; import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.ParcelableProxy; +import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import java.net.Proxy; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -197,8 +201,21 @@ public class ViewKeyTrustFragment extends LoaderFragment implements mStartSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - mStartSearch.setEnabled(false); - new DescribeKey().execute(fingerprint); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()).getProxyPrefs(); + + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + mStartSearch.setEnabled(false); + new DescribeKey(proxyPrefs.parcelableProxy).execute(fingerprint); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + getActivity())) { + mStartSearch.setEnabled(false); + new DescribeKey(proxyPrefs.parcelableProxy).execute(fingerprint); + } } }); } @@ -229,6 +246,11 @@ public class ViewKeyTrustFragment extends LoaderFragment implements // look for evidence from keybase in the background, make tabular version of result // private class DescribeKey extends AsyncTask { + ParcelableProxy mParcelableProxy; + + public DescribeKey(ParcelableProxy parcelableProxy) { + mParcelableProxy = parcelableProxy; + } @Override protected ResultPage doInBackground(String... args) { @@ -237,7 +259,7 @@ public class ViewKeyTrustFragment extends LoaderFragment implements final ArrayList proofList = new ArrayList(); final Hashtable> proofs = new Hashtable>(); try { - User keybaseUser = User.findByFingerprint(fingerprint); + User keybaseUser = User.findByFingerprint(fingerprint, mParcelableProxy.getProxy()); for (Proof proof : keybaseUser.getProofs()) { Integer proofType = proof.getType(); appendIfOK(proofs, proofType, proof); -- cgit v1.2.3 From f8da3d784b5f314f8ab58deab7c0a496dc8b11f3 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 12 Jun 2015 23:06:35 +0530 Subject: added keyserver/proxy support to certify operation --- .../sufficientlysecure/keychain/operations/CertifyOperation.java | 4 +++- .../sufficientlysecure/keychain/service/CertifyActionsParcel.java | 6 ++++++ .../java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java index ef08964a3..9092a7354 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java @@ -46,6 +46,7 @@ import org.sufficientlysecure.keychain.service.input.RequiredInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.NfcSignOperationsBuilder; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Passphrase; import java.net.Proxy; @@ -213,7 +214,8 @@ public class CertifyOperation extends BaseOperation { SaveKeyringResult result = mProviderHelper.savePublicKeyRing(certifiedKey); if (exportOperation != null) { - ExportResult uploadResult = importExportOperation.uploadKeyRingToServer(keyServer, certifiedKey, proxy); + ExportResult uploadResult = importExportOperation.uploadKeyRingToServer(keyServer, certifiedKey, + parcelableProxy.getProxy()); log.add(uploadResult, 2); if (uploadResult.success()) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java index a11f81658..05d5546f6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java @@ -29,6 +29,7 @@ import java.util.Map; import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; +import org.sufficientlysecure.keychain.util.ParcelableProxy; /** @@ -44,6 +45,7 @@ public class CertifyActionsParcel implements Parcelable { public ArrayList mCertifyActions = new ArrayList<>(); public String keyServerUri; + public ParcelableProxy parcelableProxy; public CertifyActionsParcel(long masterKeyId) { mMasterKeyId = masterKeyId; @@ -54,6 +56,8 @@ public class CertifyActionsParcel implements Parcelable { mMasterKeyId = source.readLong(); // just like parcelables, this is meant for ad-hoc IPC only and is NOT portable! mLevel = CertifyLevel.values()[source.readInt()]; + keyServerUri = source.readString(); + parcelableProxy = source.readParcelable(ParcelableProxy.class.getClassLoader()); mCertifyActions = (ArrayList) source.readSerializable(); } @@ -66,6 +70,8 @@ public class CertifyActionsParcel implements Parcelable { public void writeToParcel(Parcel destination, int flags) { destination.writeLong(mMasterKeyId); destination.writeInt(mLevel.ordinal()); + destination.writeString(keyServerUri); + destination.writeParcelable(parcelableProxy, flags); destination.writeSerializable(mCertifyActions); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java index 891c2268c..6c57074a7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java @@ -311,6 +311,11 @@ public class CertifyKeyFragment CertifyActionsParcel actionsParcel = new CertifyActionsParcel(selectedKeyId); actionsParcel.mCertifyActions.addAll(certifyActions); + if (mUploadKeyCheckbox.isChecked()) { + actionsParcel.keyServerUri = Preferences.getPreferences(getActivity()).getPreferredKeyserver(); + actionsParcel.parcelableProxy = mProxyPrefs.parcelableProxy; + } + // cached for next cryptoOperation loop cacheActionsParcel(actionsParcel); -- cgit v1.2.3 From 2402c6d3c72b19b8eded017ff3fbeb83c30ae0a8 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Tue, 16 Jun 2015 14:36:18 +0530 Subject: ensuring code style is preserved --- .../keychain/keyimport/CloudSearch.java | 2 +- .../keychain/keyimport/HkpKeyserver.java | 31 +++++++--------- .../keychain/keyimport/Keyserver.java | 1 + .../keychain/operations/CertifyOperation.java | 22 ++++++------ .../operations/results/OperationResult.java | 2 +- .../keychain/provider/ProviderHelper.java | 10 +++--- .../keychain/service/CertifyActionsParcel.java | 6 +--- .../keychain/ui/CreateYubiKeyImportFragment.java | 2 +- .../keychain/ui/ImportKeysActivity.java | 4 +-- .../keychain/ui/KeyListFragment.java | 18 +++++----- .../keychain/ui/SettingsActivity.java | 12 +++---- .../keychain/ui/ViewKeyActivity.java | 23 +++++++----- .../keychain/ui/ViewKeyTrustFragment.java | 4 +-- .../ui/adapter/ImportKeysListCloudLoader.java | 1 - .../ui/dialog/AddEditKeyserverDialogFragment.java | 3 +- .../keychain/ui/dialog/InstallDialogFragment.java | 11 +++--- .../ui/dialog/OrbotStartDialogFragment.java | 1 + .../ui/dialog/PreferenceInstallDialogFragment.java | 14 ++++---- .../keychain/util/ParcelableProxy.java | 2 +- .../keychain/util/Preferences.java | 5 ++- .../keychain/util/TlsHelper.java | 16 ++------- .../keychain/util/orbot/OrbotHelper.java | 26 ++++++-------- .../keychain/util/orbot/TorServiceUtils.java | 42 ++++++++-------------- 23 files changed, 112 insertions(+), 146 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java index c089db17f..d91dd28bc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/CloudSearch.java @@ -70,7 +70,7 @@ public class CloudSearch { try { if (proxy != null) { results.wait(30 * SECONDS); - } else{ + } else { results.wait(10 * SECONDS); } for (Thread thread : searchThreads) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 26f4da4e5..33565803b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -18,20 +18,23 @@ package org.sufficientlysecure.keychain.keyimport; -import com.squareup.okhttp.*; -import okio.BufferedSink; +import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.OkHttpClient; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.RequestBody; +import com.squareup.okhttp.Response; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.TlsHelper; -import java.io.BufferedWriter; import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; -import java.net.*; +import java.net.Proxy; +import java.net.URL; +import java.net.URLDecoder; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -197,7 +200,7 @@ public class HkpKeyserver extends Keyserver { * @param proxy * @return */ - public static OkHttpClient getClient(URL url, Proxy proxy) throws IOException { + public static OkHttpClient getClient(URL url, Proxy proxy) throws IOException { OkHttpClient client = new OkHttpClient(); try { @@ -207,7 +210,6 @@ public class HkpKeyserver extends Keyserver { } client.setProxy(proxy); - // TODO: PHILIP if proxy !=null increase timeout? client.setConnectTimeout(proxy != null ? 30000 : 5000, TimeUnit.MILLISECONDS); client.setReadTimeout(45000, TimeUnit.MILLISECONDS); @@ -222,8 +224,6 @@ public class HkpKeyserver extends Keyserver { OkHttpClient client = getClient(url, proxy); Response response = client.newCall(new Request.Builder().url(url).build()).execute(); - tempIpTest(proxy); - String responseBody = response.body().string();// contains body both in case of success or failure if (response.isSuccessful()) { @@ -237,12 +237,6 @@ public class HkpKeyserver extends Keyserver { } } - private void tempIpTest(Proxy proxy) throws IOException { - URL url = new URL("https://wtfismyip.com/text"); - Response response = getClient(url, proxy).newCall(new Request.Builder().url(url).build()).execute(); - Log.e("PHILIP", "proxy Test: " + response.body().string()); - } - /** * Results are sorted by creation date of key! * @@ -388,8 +382,6 @@ public class HkpKeyserver extends Keyserver { RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params); - Log.e("PHILIP", "Media Type charset: "+body.contentType().charset()); - Request request = new Request.Builder() .url(url) .addHeader("Content-Type", "application/x-www-form-urlencoded") @@ -397,10 +389,11 @@ public class HkpKeyserver extends Keyserver { .post(body) .build(); - Response response = new OkHttpClient().setProxy(proxy).newCall(request).execute(); + Response response = getClient(url, proxy).newCall(request).execute(); Log.d(Constants.TAG, "response code: " + response.code()); Log.d(Constants.TAG, "answer: " + response.body().string()); + } catch (IOException e) { Log.e(Constants.TAG, "IOException", e); throw new AddKeyException(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java index 260e2af40..640b39f44 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java @@ -32,6 +32,7 @@ public abstract class Keyserver { public CloudSearchFailureException(String message) { super(message); } + public CloudSearchFailureException() { super(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java index 9092a7354..07aeeaff6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/CertifyOperation.java @@ -19,12 +19,9 @@ package org.sufficientlysecure.keychain.operations; import android.content.Context; -import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; -import org.sufficientlysecure.keychain.keyimport.Keyserver.AddKeyException; import org.sufficientlysecure.keychain.operations.results.CertifyResult; import org.sufficientlysecure.keychain.operations.results.ExportResult; -import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType; import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog; import org.sufficientlysecure.keychain.operations.results.SaveKeyringResult; @@ -45,26 +42,25 @@ import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.NfcSignOperationsBuilder; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; -import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Passphrase; -import java.net.Proxy; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicBoolean; -/** An operation which implements a high level user id certification operation. - * +/** + * An operation which implements a high level user id certification operation. + *

* This operation takes a specific CertifyActionsParcel as its input. These * contain a masterKeyId to be used for certification, and a list of * masterKeyIds and related user ids to certify. * * @see CertifyActionsParcel - * */ public class CertifyOperation extends BaseOperation { - public CertifyOperation(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) { + public CertifyOperation(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean + cancelled) { super(context, providerHelper, progressable, cancelled); } @@ -178,7 +174,7 @@ public class CertifyOperation extends BaseOperation { } - if ( ! allRequiredInput.isEmpty()) { + if (!allRequiredInput.isEmpty()) { log.add(LogType.MSG_CRT_NFC_RETURN, 1); return new CertifyResult(log, allRequiredInput.build()); } @@ -204,7 +200,8 @@ public class CertifyOperation extends BaseOperation { // Check if we were cancelled if (checkCancelled()) { log.add(LogType.MSG_OPERATION_CANCELLED, 0); - return new CertifyResult(CertifyResult.RESULT_CANCELLED, log, certifyOk, certifyError, uploadOk, uploadError); + return new CertifyResult(CertifyResult.RESULT_CANCELLED, log, certifyOk, certifyError, uploadOk, + uploadError); } log.add(LogType.MSG_CRT_SAVE, 2, @@ -245,7 +242,8 @@ public class CertifyOperation extends BaseOperation { log.add(LogType.MSG_CRT_SUCCESS, 0); if (uploadError != 0) { - return new CertifyResult(CertifyResult.RESULT_WARNINGS, log, certifyOk, certifyError, uploadOk, uploadError); + return new CertifyResult(CertifyResult.RESULT_WARNINGS, log, certifyOk, certifyError, uploadOk, + uploadError); } else { return new CertifyResult(CertifyResult.RESULT_OK, log, certifyOk, certifyError, uploadOk, uploadError); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java index 2dd493c81..1ae5aaefb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java @@ -713,7 +713,7 @@ public abstract class OperationResult implements Parcelable { MSG_IMPORT_SUCCESS (LogLevel.OK, R.string.msg_import_success), MSG_EXPORT (LogLevel.START, R.plurals.msg_export), - MSG_EXPORT_UPLOAD_PUBLIC(LogLevel.START, R.string.msg_export_upload_public), + MSG_EXPORT_UPLOAD_PUBLIC (LogLevel.START, R.string.msg_export_upload_public), MSG_EXPORT_PUBLIC (LogLevel.DEBUG, R.string.msg_export_public), MSG_EXPORT_SECRET (LogLevel.DEBUG, R.string.msg_export_secret), MSG_EXPORT_ALL (LogLevel.START, R.string.msg_export_all), diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 590c58f97..353c3fcca 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -906,7 +906,8 @@ public class ProviderHelper { // If there is a secret key, merge new data (if any) and save the key for later CanonicalizedSecretKeyRing canSecretRing; try { - UncachedKeyRing secretRing = getCanonicalizedSecretKeyRing(publicRing.getMasterKeyId()).getUncachedKeyRing(); + UncachedKeyRing secretRing = getCanonicalizedSecretKeyRing(publicRing.getMasterKeyId()) + .getUncachedKeyRing(); // Merge data from new public ring into secret one log(LogType.MSG_IP_MERGE_SECRET); @@ -1031,7 +1032,8 @@ public class ProviderHelper { publicRing = secretRing.extractPublicKeyRing(); } - CanonicalizedPublicKeyRing canPublicRing = (CanonicalizedPublicKeyRing) publicRing.canonicalize(mLog, mIndent); + CanonicalizedPublicKeyRing canPublicRing = (CanonicalizedPublicKeyRing) publicRing.canonicalize(mLog, + mIndent); if (canPublicRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog, null); } @@ -1082,7 +1084,7 @@ public class ProviderHelper { indent += 1; final Cursor cursor = mContentResolver.query(KeyRingData.buildSecretKeyRingUri(), - new String[]{ KeyRingData.KEY_RING_DATA }, null, null, null); + new String[]{KeyRingData.KEY_RING_DATA}, null, null, null); if (cursor == null) { log.add(LogType.MSG_CON_ERROR_DB, indent); @@ -1143,7 +1145,7 @@ public class ProviderHelper { final Cursor cursor = mContentResolver.query( KeyRingData.buildPublicKeyRingUri(), - new String[]{ KeyRingData.KEY_RING_DATA }, null, null, null); + new String[]{KeyRingData.KEY_RING_DATA}, null, null, null); if (cursor == null) { log.add(LogType.MSG_CON_ERROR_DB, indent); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java index 05d5546f6..d32d75279 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java @@ -22,13 +22,9 @@ import android.os.Parcel; import android.os.Parcelable; import java.io.Serializable; -import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.Date; -import java.util.Map; import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute; -import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.util.ParcelableProxy; @@ -100,7 +96,7 @@ public class CertifyActionsParcel implements Parcelable { } public CertifyAction(long masterKeyId, ArrayList userIds, - ArrayList attributes) { + ArrayList attributes) { mMasterKeyId = masterKeyId; mUserIds = userIds; mUserAttributes = attributes; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index 4d85255ca..39dea6f0d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -131,7 +131,7 @@ public class CreateYubiKeyImportFragment } }; - if(OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, getActivity())) { importKey(proxyPrefs.parcelableProxy); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index b61e94c6d..e2b90056d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -46,7 +46,6 @@ import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.io.IOException; -import java.net.Proxy; import java.util.ArrayList; public class ImportKeysActivity extends BaseNfcActivity @@ -322,7 +321,8 @@ public class ImportKeysActivity extends BaseNfcActivity * specified in user preferences */ - private void startCloudFragment(Bundle savedInstanceState, String query, boolean disableQueryEdit, String keyserver) { + private void startCloudFragment(Bundle savedInstanceState, String query, boolean disableQueryEdit, String + keyserver) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 76ae147a2..da181711e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -51,6 +51,8 @@ import android.widget.TextView; import com.getbase.floatingactionbutton.FloatingActionButton; import com.getbase.floatingactionbutton.FloatingActionsMenu; +import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter; +import se.emilsjolander.stickylistheaders.StickyListHeadersListView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; @@ -81,10 +83,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; -import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter; -import se.emilsjolander.stickylistheaders.StickyListHeadersListView; - +/** + * Public key list with sticky list headers. It does _not_ exte /** * Public key list with sticky list headers. It does _not_ extend ListFragment because it uses * StickyListHeaders library which does not extend upon ListView. @@ -472,15 +472,13 @@ public class KeyListFragment extends LoaderFragment return true; case R.id.menu_key_list_export: - mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true); + ); return true; - case R.id.menu_key_list_update_all_keys: - updateAllKeys(); - return true; + case R.id.menu_key_list_debug +_cons return true; - case R.id.menu_key_list_debug_cons: - consolidate(); + consolidate(case R.id.menu_key_list_debug_cons:); return true; case R.id.menu_key_list_debug_read: diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 6605995eb..1b8d71d23 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -245,7 +245,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { } public Preference automaticallyFindPreference(String key) { - if(mFragment != null) { + if (mFragment != null) { return mFragment.findPreference(key); } else { return mActivity.findPreference(key); @@ -259,8 +259,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { Preferences.setPreferenceManagerFileAndMode(mFragment.getPreferenceManager()); // Load the preferences from an XML resource mFragment.addPreferencesFromResource(R.xml.proxy_prefs); - } - else { + } else { Preferences.setPreferenceManagerFileAndMode(mActivity.getPreferenceManager()); // Load the preferences from an XML resource mActivity.addPreferencesFromResource(R.xml.proxy_prefs); @@ -285,7 +284,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; - if ((Boolean)newValue) { + if ((Boolean) newValue) { boolean installed = OrbotHelper.isOrbotInstalled(activity); if (!installed) { Log.d(Constants.TAG, "Prompting to install Tor"); @@ -298,8 +297,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { // let the enable tor box be checked return true; } - } - else { + } else { // we're unchecking Tor, so enable other proxy enableNormalProxyPrefs(); return true; @@ -350,7 +348,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; try { int port = Integer.parseInt((String) newValue); - if(port < 0 || port > 65535) { + if (port < 0 || port > 65535) { Notify.create( activity, R.string.pref_proxy_port_err_invalid, diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index da00748e4..ce8935bce 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -53,7 +53,6 @@ import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.getbase.floatingactionbutton.FloatingActionButton; -import edu.cmu.cylab.starslinger.exchange.ExchangeActivity; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; @@ -81,6 +80,13 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.ActionListener; import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.ui.util.QrCodeUtils; +import org.sufficientlysecure.keychain.util.ContactHelper; +import org.sufficientlysecure.keychain.util.ExportHelper; +import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.NfcHelper; +import org.sufficientlysecure.keychain.util.ParcelableProxy; +import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.io.IOException; import java.util.ArrayList; @@ -460,11 +466,11 @@ public class ViewKeyActivity extends BaseNfcActivity implements HashMap data = providerHelper.getGenericData( baseUri, - new String[] {KeychainContract.Keys.MASTER_KEY_ID, KeychainContract.KeyRings.HAS_SECRET}, - new int[] {ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_INTEGER}); + new String[]{KeychainContract.Keys.MASTER_KEY_ID, KeychainContract.KeyRings.HAS_SECRET}, + new int[]{ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_INTEGER}); exportHelper.showExportKeysDialog( - new long[] {(Long) data.get(KeychainContract.KeyRings.MASTER_KEY_ID)}, + new long[]{(Long) data.get(KeychainContract.KeyRings.MASTER_KEY_ID)}, Constants.Path.APP_DIR_FILE, ((Long) data.get(KeychainContract.KeyRings.HAS_SECRET) != 0) ); } catch (ProviderHelper.NotFoundException e) { @@ -488,7 +494,7 @@ public class ViewKeyActivity extends BaseNfcActivity implements // Create a new Messenger for the communication back Messenger messenger = new Messenger(returnHandler); DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger, - new long[] {mMasterKeyId}); + new long[]{mMasterKeyId}); deleteKeyDialog.show(getSupportFragmentManager(), "deleteKeyDialog"); } @@ -632,7 +638,7 @@ public class ViewKeyActivity extends BaseNfcActivity implements long keyId = new ProviderHelper(this) .getCachedPublicKeyRing(dataUri) .extractOrGetMasterKeyId(); - long[] encryptionKeyIds = new long[] {keyId}; + long[] encryptionKeyIds = new long[]{keyId}; Intent intent; if (text) { intent = new Intent(this, EncryptTextActivity.class); @@ -738,7 +744,7 @@ public class ViewKeyActivity extends BaseNfcActivity implements // These are the rows that we will retrieve. - static final String[] PROJECTION = new String[] { + static final String[] PROJECTION = new String[]{ KeychainContract.KeyRings._ID, KeychainContract.KeyRings.MASTER_KEY_ID, KeychainContract.KeyRings.USER_ID, @@ -826,7 +832,8 @@ public class ViewKeyActivity extends BaseNfcActivity implements AsyncTask photoTask = new AsyncTask() { protected Bitmap doInBackground(Long... mMasterKeyId) { - return ContactHelper.loadPhotoByMasterKeyId(getContentResolver(), mMasterKeyId[0], true); + return ContactHelper.loadPhotoByMasterKeyId(getContentResolver(), + mMasterKeyId[0], true); } protected void onPostExecute(Bitmap photo) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java index 32e8b4fc1..f6b580b01 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java @@ -55,7 +55,6 @@ import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; -import java.net.Proxy; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -201,7 +200,8 @@ public class ViewKeyTrustFragment extends LoaderFragment implements mStartSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()).getProxyPrefs(); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()) + .getProxyPrefs(); Runnable ignoreTor = new Runnable() { @Override diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java index a4f8f22e0..c7f69207c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListCloudLoader.java @@ -30,7 +30,6 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import java.net.Proxy; import java.util.ArrayList; public class ImportKeysListCloudLoader diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java index 056d82fd3..e5ab3a228 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java @@ -59,14 +59,13 @@ import org.sufficientlysecure.keychain.util.TlsHelper; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.Proxy; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import java.io.IOException; -import java.net.*; -import javax.net.ssl.HttpsURLConnection; public class AddEditKeyserverDialogFragment extends DialogFragment implements OnEditorActionListener { private static final String ARG_MESSENGER = "arg_messenger"; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java index 01ff4715d..7b3f9ad28 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java @@ -48,15 +48,16 @@ public class InstallDialogFragment extends DialogFragment { * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. * - * @param messenger required only for callback from middle button if it has been set + * @param messenger required only for callback from middle button if it has been set * @param title - * @param message content of dialog + * @param message content of dialog * @param packageToInstall package name of application to install - * @param middleButton if not null, adds a third button to the app with a call back + * @param middleButton if not null, adds a third button to the app with a call back * @return The dialog to display */ public static InstallDialogFragment newInstance(Messenger messenger, int title, int message, - String packageToInstall, int middleButton, boolean useMiddleButton) { + String packageToInstall, int middleButton, boolean + useMiddleButton) { InstallDialogFragment frag = new InstallDialogFragment(); Bundle args = new Bundle(); args.putParcelable(ARG_MESSENGER, messenger); @@ -125,7 +126,7 @@ public class InstallDialogFragment extends DialogFragment { @Override public void onClick(DialogInterface dialog, int which) { Message msg = new Message(); - msg.what=MESSAGE_MIDDLE_CLICKED; + msg.what = MESSAGE_MIDDLE_CLICKED; try { messenger.send(msg); } catch (RemoteException e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java index e9092b14a..4736eddca 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java @@ -25,6 +25,7 @@ import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.support.v4.app.DialogFragment; + import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.util.Log; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java index 8236ff9d7..b9b4da83a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java @@ -27,6 +27,7 @@ import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.app.DialogFragment; + import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.util.Log; @@ -47,15 +48,16 @@ public class PreferenceInstallDialogFragment extends DialogFragment { * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. * - * @param messenger required only for callback from middle button if it has been set + * @param messenger required only for callback from middle button if it has been set * @param title - * @param message content of dialog + * @param message content of dialog * @param packageToInstall package name of application to install - * @param middleButton if not null, adds a third button to the app with a call back + * @param middleButton if not null, adds a third button to the app with a call back * @return The dialog to display */ public static PreferenceInstallDialogFragment newInstance(Messenger messenger, int title, int message, - String packageToInstall, int middleButton, boolean useMiddleButton) { + String packageToInstall, int middleButton, boolean + useMiddleButton) { PreferenceInstallDialogFragment frag = new PreferenceInstallDialogFragment(); Bundle args = new Bundle(); args.putParcelable(ARG_MESSENGER, messenger); @@ -80,7 +82,7 @@ public class PreferenceInstallDialogFragment extends DialogFragment { * @return */ public static PreferenceInstallDialogFragment newInstance(int title, int message, - String packageToInstall) { + String packageToInstall) { return newInstance(null, title, message, packageToInstall, -1, false); } @@ -125,7 +127,7 @@ public class PreferenceInstallDialogFragment extends DialogFragment { @Override public void onClick(DialogInterface dialog, int which) { Message msg = new Message(); - msg.what=MESSAGE_MIDDLE_CLICKED; + msg.what = MESSAGE_MIDDLE_CLICKED; try { messenger.send(msg); } catch (RemoteException e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java index 601547fa9..4898e7f1a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableProxy.java @@ -54,7 +54,7 @@ public class ParcelableProxy implements Parcelable { } public Proxy getProxy() { - if(mProxyHost == null) return null; + if (mProxyHost == null) return null; Proxy.Type type = null; switch (mProxyType) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index f54eac867..c13c07503 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -296,8 +296,7 @@ public class Preferences { if (useTor) { return new ProxyPrefs(true, false, Constants.Orbot.PROXY_HOST, Constants.Orbot.PROXY_PORT, Constants.Orbot.PROXY_TYPE); - } - else if (useNormalProxy) { + } else if (useNormalProxy) { return new ProxyPrefs(useTor, useNormalProxy, getProxyHost(), getProxyPort(), getProxyType()); } else { return new ProxyPrefs(false, false, null, -1, null); @@ -318,7 +317,7 @@ public class Preferences { public ProxyPrefs(boolean torEnabled, boolean normalPorxyEnabled, String hostName, int port, Proxy.Type type) { this.torEnabled = torEnabled; this.normalPorxyEnabled = normalPorxyEnabled; - if(!torEnabled && !normalPorxyEnabled) this.parcelableProxy = new ParcelableProxy(null, -1, null); + if (!torEnabled && !normalPorxyEnabled) this.parcelableProxy = new ParcelableProxy(null, -1, null); else this.parcelableProxy = new ParcelableProxy(hostName, port, type); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java index 58c250cab..d1d1ada2a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/TlsHelper.java @@ -27,7 +27,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.net.URLConnection; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; @@ -62,7 +61,7 @@ public class TlsHelper { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int reads = is.read(); - while(reads != -1){ + while (reads != -1) { baos.write(reads); reads = is.read(); } @@ -75,17 +74,6 @@ public class TlsHelper { } } - public static URLConnection opeanConnection(URL url) throws IOException, TlsHelperException { - if (url.getProtocol().equals("https")) { - for (String domain : sStaticCA.keySet()) { - if (url.getHost().endsWith(domain)) { - return openCAConnection(sStaticCA.get(domain), url); - } - } - } - return url.openConnection(); - } - public static void pinCertificateIfNecessary(OkHttpClient client, URL url) throws TlsHelperException, IOException { if (url.getProtocol().equals("https")) { for (String domain : sStaticCA.keySet()) { @@ -103,7 +91,7 @@ public class TlsHelper { * TODO: Refactor - More like SSH StrictHostKeyChecking than pinning? * * @param certificate certificate to pin - * @param client OkHttpClient to enforce pinning on + * @param client OkHttpClient to enforce pinning on * @throws TlsHelperException * @throws IOException */ diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java index 5e0e393c5..f57496767 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -77,15 +77,13 @@ public class OrbotHelper { public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; - public static boolean isOrbotRunning() - { + public static boolean isOrbotRunning() { int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); return (procId != -1); } - public static boolean isOrbotInstalled(Context context) - { + public static boolean isOrbotInstalled(Context context) { return isAppInstalled(ORBOT_PACKAGE_NAME, context); } @@ -102,30 +100,28 @@ public class OrbotHelper { } /** - * hack to get around teh fact that PreferenceActivity still supports only android.app.DialogFragment + * hack to get around the fact that PreferenceActivity still supports only android.app.DialogFragment * * @return */ - public static android.app.DialogFragment getPreferenceInstallDialogFragment() - { + public static android.app.DialogFragment getPreferenceInstallDialogFragment() { return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); } - public static DialogFragment getInstallDialogFragment() - { + public static DialogFragment getInstallDialogFragment() { return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); } - public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) - { + public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); } public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { - return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string.orbot_start_dialog_content, + return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string + .orbot_start_dialog_content, middleButton); } @@ -139,7 +135,7 @@ public class OrbotHelper { /** * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. * - * @param middleButton resourceId of string to display as the middle button of install and enable dialogs + * @param middleButton resourceId of string to display as the middle button of install and enable dialogs * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button * @param proxyPrefs * @param fragmentActivity @@ -159,7 +155,7 @@ public class OrbotHelper { return true; } - if(!OrbotHelper.isOrbotInstalled(fragmentActivity)) { + if (!OrbotHelper.isOrbotInstalled(fragmentActivity)) { OrbotHelper.getInstallDialogFragmentWithThirdButton( new Messenger(ignoreTorHandler), @@ -167,7 +163,7 @@ public class OrbotHelper { ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); return false; - } else if(!OrbotHelper.isOrbotRunning()) { + } else if (!OrbotHelper.isOrbotRunning()) { OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), R.string.orbot_install_dialog_ignore_tor) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java index 127e9d43f..b1d8327fb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java @@ -49,14 +49,14 @@ package org.sufficientlysecure.keychain.util.orbot; +import android.util.Log; + import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.net.URLEncoder; import java.util.StringTokenizer; -import android.util.Log; - /** * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ */ @@ -68,23 +68,18 @@ public class TorServiceUtils { public final static String SHELL_CMD_PS = "ps"; public final static String SHELL_CMD_PIDOF = "pidof"; - public static int findProcessId(String command) - { + public static int findProcessId(String command) { int procId = -1; - try - { + try { procId = findProcessIdWithPidOf(command); if (procId == -1) procId = findProcessIdWithPS(command); - } catch (Exception e) - { - try - { + } catch (Exception e) { + try { procId = findProcessIdWithPS(command); - } catch (Exception e2) - { + } catch (Exception e2) { Log.e(TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); } } @@ -93,8 +88,7 @@ public class TorServiceUtils { } // use 'pidof' command - public static int findProcessIdWithPidOf(String command) throws Exception - { + public static int findProcessIdWithPidOf(String command) throws Exception { int procId = -1; @@ -104,7 +98,7 @@ public class TorServiceUtils { String baseName = new File(command).getName(); // fix contributed my mikos on 2010.12.10 - procPs = r.exec(new String[] { + procPs = r.exec(new String[]{ SHELL_CMD_PIDOF, baseName }); // procPs = r.exec(SHELL_CMD_PIDOF); @@ -112,16 +106,13 @@ public class TorServiceUtils { BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); String line = null; - while ((line = reader.readLine()) != null) - { + while ((line = reader.readLine()) != null) { - try - { + try { // this line should just be the process id procId = Integer.parseInt(line.trim()); break; - } catch (NumberFormatException e) - { + } catch (NumberFormatException e) { Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); } } @@ -131,8 +122,7 @@ public class TorServiceUtils { } // use 'ps' command - public static int findProcessIdWithPS(String command) throws Exception - { + public static int findProcessIdWithPS(String command) throws Exception { int procId = -1; @@ -145,10 +135,8 @@ public class TorServiceUtils { BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); String line = null; - while ((line = reader.readLine()) != null) - { - if (line.indexOf(' ' + command) != -1) - { + while ((line = reader.readLine()) != null) { + if (line.indexOf(' ' + command) != -1) { StringTokenizer st = new StringTokenizer(line, " "); st.nextToken(); // proc owner -- cgit v1.2.3 From ab5763e2fab5177a7b0c72898381cabf2706cc6c Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Tue, 16 Jun 2015 15:00:24 +0530 Subject: added proxy support to YubiKey search --- .../keychain/ui/CreateYubiKeyImportFragment.java | 32 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index 39dea6f0d..8434eafbf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -145,7 +145,18 @@ public class CreateYubiKeyImportFragment view.findViewById(R.id.button_search).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - refreshSearch(); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()).getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + refreshSearch(new ParcelableProxy(null, -1, null)); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + getActivity())) { + refreshSearch(proxyPrefs.parcelableProxy); + } } }); @@ -184,10 +195,10 @@ public class CreateYubiKeyImportFragment } } - public void refreshSearch() { + public void refreshSearch(ParcelableProxy parcelableProxy) { // TODO: PHILIP implement proxy in YubiKey parts mListFragment.loadNew(new ImportKeysListFragment.CloudLoaderState("0x" + mNfcFingerprint, - Preferences.getPreferences(getActivity()).getCloudSearchPrefs()), null); + Preferences.getPreferences(getActivity()).getCloudSearchPrefs()), parcelableProxy); } public void importKey(ParcelableProxy parcelableProxy) { @@ -225,7 +236,20 @@ public class CreateYubiKeyImportFragment public void onNfcPostExecute() throws IOException { setData(); - refreshSearch(); + + Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()).getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + refreshSearch(new ParcelableProxy(null, -1, null)); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + getActivity())) { + refreshSearch(proxyPrefs.parcelableProxy); + } + } @Override -- cgit v1.2.3 From bde187b9bf9bbb334a12b6b18aa843048a926dc8 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Tue, 16 Jun 2015 15:02:04 +0530 Subject: code cleanup --- .../java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java | 3 +-- .../sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java | 2 +- .../org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 33565803b..8d32d80e0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -219,8 +219,7 @@ public class HkpKeyserver extends Keyserver { private String query(String request, Proxy proxy) throws QueryFailedException, HttpError { try { URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + request); - Log.d(Constants.TAG, "hkp keyserver query: " + url); - Log.d("PHILIP", "hkpKeyserver query(): " + proxy); + Log.d(Constants.TAG, "hkp keyserver query: " + url + " Proxy: " + proxy); OkHttpClient client = getClient(url, proxy); Response response = client.newCall(new Request.Builder().url(url).build()).execute(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index 8434eafbf..93614db56 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -196,7 +196,7 @@ public class CreateYubiKeyImportFragment } public void refreshSearch(ParcelableProxy parcelableProxy) { - // TODO: PHILIP implement proxy in YubiKey parts + // TODO: PHILIP verify proxy implementation in YubiKey parts mListFragment.loadNew(new ImportKeysListFragment.CloudLoaderState("0x" + mNfcFingerprint, Preferences.getPreferences(getActivity()).getCloudSearchPrefs()), parcelableProxy); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index 23368504b..55ccc3975 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -48,7 +48,6 @@ import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.net.Proxy; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -187,7 +186,6 @@ public class ImportKeysListFragment extends ListFragment implements static public class CloudLoaderState extends LoaderState { Preferences.CloudSearchPrefs mCloudPrefs; String mServerQuery; - Proxy proxy; CloudLoaderState(String serverQuery, Preferences.CloudSearchPrefs cloudPrefs) { mServerQuery = serverQuery; -- cgit v1.2.3 From bf2d9eda7c2a29186a1f69727221643e6629667a Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Tue, 16 Jun 2015 15:15:39 +0530 Subject: added proxy logging, removed test function in KeybaseLib --- .../java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 8d32d80e0..d06b8b0c4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -232,7 +232,8 @@ public class HkpKeyserver extends Keyserver { } } catch (IOException e) { e.printStackTrace(); - throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!"); + throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!" + + proxy == null?"":" Using proxy " + proxy); } } -- cgit v1.2.3 From e909d7a4d536caa9beb8f6b7771f9a5082090546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 17 Jun 2015 15:22:54 +0200 Subject: Fix crash on settings by adding proxy fragment to isValidFragment --- .../main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java | 1 + 1 file changed, 1 insertion(+) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 1b8d71d23..81e4b9eda 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -415,6 +415,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { protected boolean isValidFragment(String fragmentName) { return AdvancedPrefsFragment.class.getName().equals(fragmentName) || CloudSearchPrefsFragment.class.getName().equals(fragmentName) + || ProxyPrefsFragment.class.getName().equals(fragmentName) || super.isValidFragment(fragmentName); } -- cgit v1.2.3 From c4198854347ae97edfc4be1517c2720eacc552f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 17 Jun 2015 15:26:43 +0200 Subject: Cleanup tor code --- .../keychain/ui/CreateYubiKeyImportFragment.java | 2 +- .../keychain/ui/DecryptFragment.java | 2 +- .../keychain/ui/ImportKeysActivity.java | 2 +- .../keychain/ui/ImportKeysProxyActivity.java | 2 +- .../keychain/ui/KeyListFragment.java | 9 +- .../keychain/ui/SettingsActivity.java | 2 +- .../keychain/ui/UploadKeyActivity.java | 2 +- .../keychain/ui/ViewKeyActivity.java | 2 +- .../keychain/ui/ViewKeyTrustFragment.java | 2 +- .../ui/dialog/AddEditKeyserverDialogFragment.java | 16 +- .../ui/dialog/OrbotStartDialogFragment.java | 2 +- .../keychain/util/orbot/OrbotHelper.java | 177 --------------------- .../keychain/util/orbot/TorServiceUtils.java | 153 ------------------ .../keychain/util/tor/OrbotHelper.java | 175 ++++++++++++++++++++ .../keychain/util/tor/TorServiceUtils.java | 144 +++++++++++++++++ 15 files changed, 346 insertions(+), 346 deletions(-) delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index 93614db56..ec74ca7c8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -43,7 +43,7 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; public class CreateYubiKeyImportFragment diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java index adb15111d..e2cc21464 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -60,7 +60,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; public abstract class DecryptFragment extends Fragment implements LoaderManager.LoaderCallbacks { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index e2b90056d..c64fc5a2a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -43,7 +43,7 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.io.IOException; import java.util.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java index 1a03b8102..358dc108c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java @@ -46,7 +46,7 @@ import org.sufficientlysecure.keychain.util.IntentIntegratorSupportV4; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.util.ArrayList; import java.util.Locale; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index da181711e..35f76be5e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -83,6 +83,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; /** * Public key list with sticky list headers. It does _not_ exte /** @@ -472,13 +473,11 @@ public class KeyListFragment extends LoaderFragment return true; case R.id.menu_key_list_export: - ); + mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true); return true; - case R.id.menu_key_list_debug -_cons return true; - - consolidate(case R.id.menu_key_list_debug_cons:); + case R.id.menu_key_list_update_all_keys: + updateAllKeys(); return true; case R.id.menu_key_list_debug_read: diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 81e4b9eda..bdf10b065 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -42,7 +42,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.util.List; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java index ee37b4b98..fc6585bbb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java @@ -39,7 +39,7 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; /** * Sends the selected public key to a keyserver diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index ce8935bce..29fbbb3c8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -86,7 +86,7 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.NfcHelper; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.io.IOException; import java.util.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java index f6b580b01..8bcfd6111 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java @@ -53,7 +53,7 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.util.ArrayList; import java.util.Hashtable; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java index e5ab3a228..c409cbb7c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java @@ -55,6 +55,7 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.TlsHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.io.IOException; import java.net.HttpURLConnection; @@ -221,8 +222,19 @@ public class AddEditKeyserverDialogFragment extends DialogFragment implements On // behaviour same for edit and add String keyserverUrl = mKeyserverEditText.getText().toString(); if (mVerifyKeyserverCheckBox.isChecked()) { - // TODO: PHILIP Implement proxy - verifyConnection(keyserverUrl); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()) + .getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + verifyConnection(keyserverUrl, null); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + getActivity())) { + verifyConnection(keyserverUrl, proxyPrefs.parcelableProxy.getProxy()); + } } else { dismiss(); // return unverified keyserver back to activity diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java index 4736eddca..fdbab3b20 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java @@ -29,7 +29,7 @@ import android.support.v4.app.DialogFragment; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; /** * displays a dialog asking the user to enable Tor diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java deleted file mode 100644 index f57496767..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ /dev/null @@ -1,177 +0,0 @@ -/* This is the license for Orlib, a free software project to - provide anonymity on the Internet from a Google Android smartphone. - - For more information about Orlib, see https://guardianproject.info/ - - If you got this file as a part of a larger bundle, there may be other - license terms that you should be aware of. - =============================================================================== - Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - ***** - Orlib contains a binary distribution of the JSocks library: - http://code.google.com/p/jsocks-mirror/ - which is licensed under the GNU Lesser General Public License: - http://www.gnu.org/licenses/lgpl.html - - ***** -*/ - -package org.sufficientlysecure.keychain.util.orbot; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.Handler; -import android.os.Message; -import android.os.Messenger; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentActivity; -import android.support.v4.app.FragmentManager; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; -import org.sufficientlysecure.keychain.util.Preferences; - -/** - * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ - */ -public class OrbotHelper { - - public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; - public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; - - public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; - - public static boolean isOrbotRunning() { - int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); - - return (procId != -1); - } - - public static boolean isOrbotInstalled(Context context) { - return isAppInstalled(ORBOT_PACKAGE_NAME, context); - } - - private static boolean isAppInstalled(String uri, Context context) { - PackageManager pm = context.getPackageManager(); - boolean installed = false; - try { - pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); - installed = true; - } catch (PackageManager.NameNotFoundException e) { - installed = false; - } - return installed; - } - - /** - * hack to get around the fact that PreferenceActivity still supports only android.app.DialogFragment - * - * @return - */ - public static android.app.DialogFragment getPreferenceInstallDialogFragment() { - return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); - } - - public static DialogFragment getInstallDialogFragment() { - return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); - } - - public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { - return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); - } - - public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { - return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string - .orbot_start_dialog_content, - middleButton); - } - - public static Intent getOrbotStartIntent() { - Intent intent = new Intent(ACTION_START_TOR); - intent.setPackage(ORBOT_PACKAGE_NAME); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return intent; - } - - /** - * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. - * - * @param middleButton resourceId of string to display as the middle button of install and enable dialogs - * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button - * @param proxyPrefs - * @param fragmentActivity - * @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false - */ - public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable, - Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) { - Handler ignoreTorHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - // every message received by this handler will mean the middle button was pressed - middleButtonRunnable.run(); - } - }; - - if (!proxyPrefs.torEnabled) { - return true; - } - - if (!OrbotHelper.isOrbotInstalled(fragmentActivity)) { - - OrbotHelper.getInstallDialogFragmentWithThirdButton( - new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor - ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); - - return false; - } else if (!OrbotHelper.isOrbotRunning()) { - - OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor) - .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); - - return false; - } else { - return true; - } - } -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java deleted file mode 100644 index b1d8327fb..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ /dev/null @@ -1,153 +0,0 @@ -/* This is the license for Orlib, a free software project to - provide anonymity on the Internet from a Google Android smartphone. - - For more information about Orlib, see https://guardianproject.info/ - - If you got this file as a part of a larger bundle, there may be other - license terms that you should be aware of. - =============================================================================== - Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - ***** - Orlib contains a binary distribution of the JSocks library: - http://code.google.com/p/jsocks-mirror/ - which is licensed under the GNU Lesser General Public License: - http://www.gnu.org/licenses/lgpl.html - - ***** -*/ - -package org.sufficientlysecure.keychain.util.orbot; - -import android.util.Log; - -import java.io.BufferedReader; -import java.io.File; -import java.io.InputStreamReader; -import java.net.URLEncoder; -import java.util.StringTokenizer; - -/** - * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ - */ - -public class TorServiceUtils { - - private final static String TAG = "TorUtils"; - // various console cmds - public final static String SHELL_CMD_PS = "ps"; - public final static String SHELL_CMD_PIDOF = "pidof"; - - public static int findProcessId(String command) { - int procId = -1; - - try { - procId = findProcessIdWithPidOf(command); - - if (procId == -1) - procId = findProcessIdWithPS(command); - } catch (Exception e) { - try { - procId = findProcessIdWithPS(command); - } catch (Exception e2) { - Log.e(TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); - } - } - - return procId; - } - - // use 'pidof' command - public static int findProcessIdWithPidOf(String command) throws Exception { - - int procId = -1; - - Runtime r = Runtime.getRuntime(); - - Process procPs = null; - - String baseName = new File(command).getName(); - // fix contributed my mikos on 2010.12.10 - procPs = r.exec(new String[]{ - SHELL_CMD_PIDOF, baseName - }); - // procPs = r.exec(SHELL_CMD_PIDOF); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; - - while ((line = reader.readLine()) != null) { - - try { - // this line should just be the process id - procId = Integer.parseInt(line.trim()); - break; - } catch (NumberFormatException e) { - Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); - } - } - - return procId; - - } - - // use 'ps' command - public static int findProcessIdWithPS(String command) throws Exception { - - int procId = -1; - - Runtime r = Runtime.getRuntime(); - - Process procPs = null; - - procPs = r.exec(SHELL_CMD_PS); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; - - while ((line = reader.readLine()) != null) { - if (line.indexOf(' ' + command) != -1) { - - StringTokenizer st = new StringTokenizer(line, " "); - st.nextToken(); // proc owner - - procId = Integer.parseInt(st.nextToken().trim()); - - break; - } - } - - return procId; - - } -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java new file mode 100644 index 000000000..84e4f36ab --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java @@ -0,0 +1,175 @@ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. + + For more information about Orlib, see https://guardianproject.info/ + + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html + + ***** +*/ + +package org.sufficientlysecure.keychain.util.tor; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; + +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; +import org.sufficientlysecure.keychain.util.Preferences; + +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ +public class OrbotHelper { + + public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; + public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; + + public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; + + public static boolean isOrbotRunning() { + int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); + + return (procId != -1); + } + + public static boolean isOrbotInstalled(Context context) { + return isAppInstalled(ORBOT_PACKAGE_NAME, context); + } + + private static boolean isAppInstalled(String uri, Context context) { + PackageManager pm = context.getPackageManager(); + + boolean installed; + try { + pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); + installed = true; + } catch (PackageManager.NameNotFoundException e) { + installed = false; + } + return installed; + } + + /** + * hack to get around the fact that PreferenceActivity still supports only android.app.DialogFragment + * + * @return + */ + public static android.app.DialogFragment getPreferenceInstallDialogFragment() { + return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); + } + + public static DialogFragment getInstallDialogFragment() { + return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); + } + + public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { + return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); + } + + public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { + return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string + .orbot_start_dialog_content, + middleButton); + } + + public static Intent getOrbotStartIntent() { + Intent intent = new Intent(ACTION_START_TOR); + intent.setPackage(ORBOT_PACKAGE_NAME); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + return intent; + } + + /** + * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. + * + * @param middleButton resourceId of string to display as the middle button of install and enable dialogs + * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button + * @param proxyPrefs + * @param fragmentActivity + * @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false + */ + public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable, + Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) { + Handler ignoreTorHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + // every message received by this handler will mean the middle button was pressed + middleButtonRunnable.run(); + } + }; + + if (!proxyPrefs.torEnabled) { + return true; + } + + if (!OrbotHelper.isOrbotInstalled(fragmentActivity)) { + + OrbotHelper.getInstallDialogFragmentWithThirdButton( + new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor + ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); + + return false; + } else if (!OrbotHelper.isOrbotRunning()) { + + OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor) + .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); + + return false; + } else { + return true; + } + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java new file mode 100644 index 000000000..48adc9528 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java @@ -0,0 +1,144 @@ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. + + For more information about Orlib, see https://guardianproject.info/ + + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html + + ***** +*/ + +package org.sufficientlysecure.keychain.util.tor; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.util.Log; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.net.URLEncoder; +import java.util.StringTokenizer; + +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ +public class TorServiceUtils { + // various console cmds + public final static String SHELL_CMD_PS = "ps"; + public final static String SHELL_CMD_PIDOF = "pidof"; + + public static int findProcessId(String command) { + int procId = -1; + + try { + procId = findProcessIdWithPidOf(command); + + if (procId == -1) { + procId = findProcessIdWithPS(command); + } + } catch (Exception e) { + try { + procId = findProcessIdWithPS(command); + } catch (Exception e2) { + Log.e(Constants.TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); + } + } + + return procId; + } + + // use 'pidof' command + public static int findProcessIdWithPidOf(String command) throws Exception { + + int procId = -1; + Runtime r = Runtime.getRuntime(); + Process procPs; + + String baseName = new File(command).getName(); + // fix contributed my mikos on 2010.12.10 + procPs = r.exec(new String[]{ + SHELL_CMD_PIDOF, baseName + }); + // procPs = r.exec(SHELL_CMD_PIDOF); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line; + + while ((line = reader.readLine()) != null) { + + try { + // this line should just be the process id + procId = Integer.parseInt(line.trim()); + break; + } catch (NumberFormatException e) { + Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); + } + } + + return procId; + } + + // use 'ps' command + public static int findProcessIdWithPS(String command) throws Exception { + int procId = -1; + Runtime r = Runtime.getRuntime(); + Process procPs; + + procPs = r.exec(SHELL_CMD_PS); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + if (line.contains(' ' + command)) { + + StringTokenizer st = new StringTokenizer(line, " "); + st.nextToken(); // proc owner + + procId = Integer.parseInt(st.nextToken().trim()); + + break; + } + } + + return procId; + } +} -- cgit v1.2.3 From c74d6fc710119fd6e83840ae883b764bc128944a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 17 Jun 2015 15:30:35 +0200 Subject: Rename package back to orbot because these are orbot utils --- .../keychain/ui/CreateYubiKeyImportFragment.java | 2 +- .../keychain/ui/DecryptFragment.java | 2 +- .../keychain/ui/ImportKeysActivity.java | 2 +- .../keychain/ui/ImportKeysProxyActivity.java | 2 +- .../keychain/ui/KeyListFragment.java | 3 - .../keychain/ui/SettingsActivity.java | 2 +- .../keychain/ui/UploadKeyActivity.java | 2 +- .../keychain/ui/ViewKeyActivity.java | 2 +- .../keychain/ui/ViewKeyTrustFragment.java | 2 +- .../ui/dialog/AddEditKeyserverDialogFragment.java | 2 +- .../ui/dialog/OrbotStartDialogFragment.java | 2 +- .../keychain/util/orbot/OrbotHelper.java | 175 +++++++++++++++++++++ .../keychain/util/orbot/TorServiceUtils.java | 144 +++++++++++++++++ .../keychain/util/tor/OrbotHelper.java | 175 --------------------- .../keychain/util/tor/TorServiceUtils.java | 144 ----------------- 15 files changed, 329 insertions(+), 332 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index ec74ca7c8..93614db56 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -43,7 +43,7 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; public class CreateYubiKeyImportFragment diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java index e2cc21464..adb15111d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -60,7 +60,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; public abstract class DecryptFragment extends Fragment implements LoaderManager.LoaderCallbacks { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index c64fc5a2a..e2b90056d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -43,7 +43,7 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.io.IOException; import java.util.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java index 358dc108c..1a03b8102 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java @@ -46,7 +46,7 @@ import org.sufficientlysecure.keychain.util.IntentIntegratorSupportV4; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.ArrayList; import java.util.Locale; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 35f76be5e..88e7e6025 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -83,9 +83,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; -/** - * Public key list with sticky list headers. It does _not_ exte /** * Public key list with sticky list headers. It does _not_ extend ListFragment because it uses * StickyListHeaders library which does not extend upon ListView. diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index bdf10b065..81e4b9eda 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -42,7 +42,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.List; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java index fc6585bbb..ee37b4b98 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java @@ -39,7 +39,7 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; /** * Sends the selected public key to a keyserver diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 29fbbb3c8..ce8935bce 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -86,7 +86,7 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.NfcHelper; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.io.IOException; import java.util.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java index 8bcfd6111..f6b580b01 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java @@ -53,7 +53,7 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.ArrayList; import java.util.Hashtable; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java index c409cbb7c..ede4dac4a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java @@ -55,7 +55,7 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.TlsHelper; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.io.IOException; import java.net.HttpURLConnection; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java index fdbab3b20..4736eddca 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java @@ -29,7 +29,7 @@ import android.support.v4.app.DialogFragment; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.tor.OrbotHelper; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; /** * displays a dialog asking the user to enable Tor diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java new file mode 100644 index 000000000..b5cd0806b --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -0,0 +1,175 @@ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. + + For more information about Orlib, see https://guardianproject.info/ + + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html + + ***** +*/ + +package org.sufficientlysecure.keychain.util.orbot; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; + +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; +import org.sufficientlysecure.keychain.util.Preferences; + +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ +public class OrbotHelper { + + public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; + public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; + + public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; + + public static boolean isOrbotRunning() { + int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); + + return (procId != -1); + } + + public static boolean isOrbotInstalled(Context context) { + return isAppInstalled(ORBOT_PACKAGE_NAME, context); + } + + private static boolean isAppInstalled(String uri, Context context) { + PackageManager pm = context.getPackageManager(); + + boolean installed; + try { + pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); + installed = true; + } catch (PackageManager.NameNotFoundException e) { + installed = false; + } + return installed; + } + + /** + * hack to get around the fact that PreferenceActivity still supports only android.app.DialogFragment + * + * @return + */ + public static android.app.DialogFragment getPreferenceInstallDialogFragment() { + return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); + } + + public static DialogFragment getInstallDialogFragment() { + return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); + } + + public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { + return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); + } + + public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { + return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string + .orbot_start_dialog_content, + middleButton); + } + + public static Intent getOrbotStartIntent() { + Intent intent = new Intent(ACTION_START_TOR); + intent.setPackage(ORBOT_PACKAGE_NAME); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + return intent; + } + + /** + * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. + * + * @param middleButton resourceId of string to display as the middle button of install and enable dialogs + * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button + * @param proxyPrefs + * @param fragmentActivity + * @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false + */ + public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable, + Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) { + Handler ignoreTorHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + // every message received by this handler will mean the middle button was pressed + middleButtonRunnable.run(); + } + }; + + if (!proxyPrefs.torEnabled) { + return true; + } + + if (!OrbotHelper.isOrbotInstalled(fragmentActivity)) { + + OrbotHelper.getInstallDialogFragmentWithThirdButton( + new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor + ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); + + return false; + } else if (!OrbotHelper.isOrbotRunning()) { + + OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor) + .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); + + return false; + } else { + return true; + } + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java new file mode 100644 index 000000000..d11e80f7a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java @@ -0,0 +1,144 @@ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. + + For more information about Orlib, see https://guardianproject.info/ + + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html + + ***** +*/ + +package org.sufficientlysecure.keychain.util.orbot; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.util.Log; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.net.URLEncoder; +import java.util.StringTokenizer; + +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ +public class TorServiceUtils { + // various console cmds + public final static String SHELL_CMD_PS = "ps"; + public final static String SHELL_CMD_PIDOF = "pidof"; + + public static int findProcessId(String command) { + int procId = -1; + + try { + procId = findProcessIdWithPidOf(command); + + if (procId == -1) { + procId = findProcessIdWithPS(command); + } + } catch (Exception e) { + try { + procId = findProcessIdWithPS(command); + } catch (Exception e2) { + Log.e(Constants.TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); + } + } + + return procId; + } + + // use 'pidof' command + public static int findProcessIdWithPidOf(String command) throws Exception { + + int procId = -1; + Runtime r = Runtime.getRuntime(); + Process procPs; + + String baseName = new File(command).getName(); + // fix contributed my mikos on 2010.12.10 + procPs = r.exec(new String[]{ + SHELL_CMD_PIDOF, baseName + }); + // procPs = r.exec(SHELL_CMD_PIDOF); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line; + + while ((line = reader.readLine()) != null) { + + try { + // this line should just be the process id + procId = Integer.parseInt(line.trim()); + break; + } catch (NumberFormatException e) { + Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); + } + } + + return procId; + } + + // use 'ps' command + public static int findProcessIdWithPS(String command) throws Exception { + int procId = -1; + Runtime r = Runtime.getRuntime(); + Process procPs; + + procPs = r.exec(SHELL_CMD_PS); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + if (line.contains(' ' + command)) { + + StringTokenizer st = new StringTokenizer(line, " "); + st.nextToken(); // proc owner + + procId = Integer.parseInt(st.nextToken().trim()); + + break; + } + } + + return procId; + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java deleted file mode 100644 index 84e4f36ab..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java +++ /dev/null @@ -1,175 +0,0 @@ -/* This is the license for Orlib, a free software project to - provide anonymity on the Internet from a Google Android smartphone. - - For more information about Orlib, see https://guardianproject.info/ - - If you got this file as a part of a larger bundle, there may be other - license terms that you should be aware of. - =============================================================================== - Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - ***** - Orlib contains a binary distribution of the JSocks library: - http://code.google.com/p/jsocks-mirror/ - which is licensed under the GNU Lesser General Public License: - http://www.gnu.org/licenses/lgpl.html - - ***** -*/ - -package org.sufficientlysecure.keychain.util.tor; - -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.Handler; -import android.os.Message; -import android.os.Messenger; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentActivity; - -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; -import org.sufficientlysecure.keychain.util.Preferences; - -/** - * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ - */ -public class OrbotHelper { - - public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; - public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; - - public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; - - public static boolean isOrbotRunning() { - int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); - - return (procId != -1); - } - - public static boolean isOrbotInstalled(Context context) { - return isAppInstalled(ORBOT_PACKAGE_NAME, context); - } - - private static boolean isAppInstalled(String uri, Context context) { - PackageManager pm = context.getPackageManager(); - - boolean installed; - try { - pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); - installed = true; - } catch (PackageManager.NameNotFoundException e) { - installed = false; - } - return installed; - } - - /** - * hack to get around the fact that PreferenceActivity still supports only android.app.DialogFragment - * - * @return - */ - public static android.app.DialogFragment getPreferenceInstallDialogFragment() { - return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); - } - - public static DialogFragment getInstallDialogFragment() { - return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); - } - - public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { - return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); - } - - public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { - return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string - .orbot_start_dialog_content, - middleButton); - } - - public static Intent getOrbotStartIntent() { - Intent intent = new Intent(ACTION_START_TOR); - intent.setPackage(ORBOT_PACKAGE_NAME); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return intent; - } - - /** - * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. - * - * @param middleButton resourceId of string to display as the middle button of install and enable dialogs - * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button - * @param proxyPrefs - * @param fragmentActivity - * @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false - */ - public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable, - Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) { - Handler ignoreTorHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - // every message received by this handler will mean the middle button was pressed - middleButtonRunnable.run(); - } - }; - - if (!proxyPrefs.torEnabled) { - return true; - } - - if (!OrbotHelper.isOrbotInstalled(fragmentActivity)) { - - OrbotHelper.getInstallDialogFragmentWithThirdButton( - new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor - ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); - - return false; - } else if (!OrbotHelper.isOrbotRunning()) { - - OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor) - .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); - - return false; - } else { - return true; - } - } -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java deleted file mode 100644 index 48adc9528..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java +++ /dev/null @@ -1,144 +0,0 @@ -/* This is the license for Orlib, a free software project to - provide anonymity on the Internet from a Google Android smartphone. - - For more information about Orlib, see https://guardianproject.info/ - - If you got this file as a part of a larger bundle, there may be other - license terms that you should be aware of. - =============================================================================== - Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - ***** - Orlib contains a binary distribution of the JSocks library: - http://code.google.com/p/jsocks-mirror/ - which is licensed under the GNU Lesser General Public License: - http://www.gnu.org/licenses/lgpl.html - - ***** -*/ - -package org.sufficientlysecure.keychain.util.tor; - -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.util.Log; - -import java.io.BufferedReader; -import java.io.File; -import java.io.InputStreamReader; -import java.net.URLEncoder; -import java.util.StringTokenizer; - -/** - * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ - */ -public class TorServiceUtils { - // various console cmds - public final static String SHELL_CMD_PS = "ps"; - public final static String SHELL_CMD_PIDOF = "pidof"; - - public static int findProcessId(String command) { - int procId = -1; - - try { - procId = findProcessIdWithPidOf(command); - - if (procId == -1) { - procId = findProcessIdWithPS(command); - } - } catch (Exception e) { - try { - procId = findProcessIdWithPS(command); - } catch (Exception e2) { - Log.e(Constants.TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); - } - } - - return procId; - } - - // use 'pidof' command - public static int findProcessIdWithPidOf(String command) throws Exception { - - int procId = -1; - Runtime r = Runtime.getRuntime(); - Process procPs; - - String baseName = new File(command).getName(); - // fix contributed my mikos on 2010.12.10 - procPs = r.exec(new String[]{ - SHELL_CMD_PIDOF, baseName - }); - // procPs = r.exec(SHELL_CMD_PIDOF); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line; - - while ((line = reader.readLine()) != null) { - - try { - // this line should just be the process id - procId = Integer.parseInt(line.trim()); - break; - } catch (NumberFormatException e) { - Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); - } - } - - return procId; - } - - // use 'ps' command - public static int findProcessIdWithPS(String command) throws Exception { - int procId = -1; - Runtime r = Runtime.getRuntime(); - Process procPs; - - procPs = r.exec(SHELL_CMD_PS); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line; - while ((line = reader.readLine()) != null) { - if (line.contains(' ' + command)) { - - StringTokenizer st = new StringTokenizer(line, " "); - st.nextToken(); // proc owner - - procId = Integer.parseInt(st.nextToken().trim()); - - break; - } - } - - return procId; - } -} -- cgit v1.2.3 From ad00a511d631650f8a35be3ccd6df793acaa4dfe Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 18 Jun 2015 15:56:53 +0530 Subject: fixed orbot preference install dialog --- .../keychain/ui/dialog/PreferenceInstallDialogFragment.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java index b9b4da83a..b025cf5de 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java @@ -38,7 +38,7 @@ public class PreferenceInstallDialogFragment extends DialogFragment { private static final String ARG_MESSAGE = "message"; private static final String ARG_MIDDLE_BUTTON = "middleButton"; private static final String ARG_INSTALL_PATH = "installPath"; - private static final String ARG_USE_MIDDLE_BUTTON = "installPath"; + private static final String ARG_USE_MIDDLE_BUTTON = "useMiddleButton"; public static final String PLAY_STORE_PATH = "market://search?q=pname:"; @@ -92,10 +92,10 @@ public class PreferenceInstallDialogFragment extends DialogFragment { final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); - final String title = getArguments().getString(ARG_TITLE); - final String message = getArguments().getString(ARG_MESSAGE); + final int title = getArguments().getInt(ARG_TITLE); + final int message = getArguments().getInt(ARG_MESSAGE); + final int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON); final String installPath = getArguments().getString(ARG_INSTALL_PATH); - final String middleButton = getArguments().getString(ARG_MIDDLE_BUTTON); final boolean useMiddleButton = getArguments().getBoolean(ARG_USE_MIDDLE_BUTTON); CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); -- cgit v1.2.3 From 6d9da28ead0319db1bcb65f5e9efed5754326792 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 18 Jun 2015 16:12:11 +0530 Subject: removed e.printStackTrace from several places --- .../org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java | 6 +++--- .../keychain/operations/SignEncryptOperation.java | 3 ++- .../org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java | 2 +- .../sufficientlysecure/keychain/ui/PassphraseWizardActivity.java | 8 +++++--- .../keychain/ui/widget/PasswordStrengthView.java | 2 ++ 5 files changed, 13 insertions(+), 8 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index d06b8b0c4..bd85b7a0a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -231,7 +231,7 @@ public class HkpKeyserver extends Keyserver { throw new HttpError(response.code(), responseBody); } } catch (IOException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "IOException at HkpKeyserver", e); throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!" + proxy == null?"":" Using proxy " + proxy); } @@ -350,12 +350,12 @@ public class HkpKeyserver extends Keyserver { @Override public String get(String keyIdHex, Proxy proxy) throws QueryFailedException { String request = "/pks/lookup?op=get&options=mr&search=" + keyIdHex; - Log.d(Constants.TAG, "hkp keyserver get: " + request); + Log.d(Constants.TAG, "hkp keyserver get: " + request + " using Proxy: " + proxy); String data; try { data = query(request, proxy); } catch (HttpError httpError) { - httpError.printStackTrace(); + Log.e(Constants.TAG, "Failed to get key at HkpKeyserver", httpError); throw new QueryFailedException("not found"); } Matcher matcher = PgpHelper.PGP_PUBLIC_KEY.matcher(data); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/SignEncryptOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/SignEncryptOperation.java index 8fe5b86c5..976a054af 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/SignEncryptOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/SignEncryptOperation.java @@ -36,6 +36,7 @@ import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.NfcSign import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.RequiredInputType; import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.InputData; +import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ProgressScaler; import java.io.ByteArrayInputStream; @@ -84,7 +85,7 @@ public class SignEncryptOperation extends BaseOperation { input.getSignatureMasterKeyId()).getSecretSignId(); input.setSignatureSubKeyId(signKeyId); } catch (PgpKeyNotFoundException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Key not found", e); return new SignEncryptResult(SignEncryptResult.RESULT_ERROR, log, results); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java index a6d260d22..acac9ecff 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java @@ -96,7 +96,7 @@ public class PgpDecryptVerify extends BaseOperation long inputSize = FileHelper.getFileSize(mContext, input.getInputUri(), 0); inputData = new InputData(inputStream, inputSize); } catch (FileNotFoundException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "File not found at " + input.getInputUri(), e); return null; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseWizardActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseWizardActivity.java index 2e838535d..ed96156fe 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseWizardActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseWizardActivity.java @@ -43,7 +43,9 @@ import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; +import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -207,7 +209,7 @@ public class PassphraseWizardActivity extends FragmentActivity { // transaction.replace(R.id.fragmentContainer, lpf).addToBackStack(null).commit(); } } catch (IOException | FormatException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Failed to write on NFC tag", e); } } else if (readNFC && AUTHENTICATION.equals(selectedAction)) { @@ -232,7 +234,7 @@ public class PassphraseWizardActivity extends FragmentActivity { } } } catch (IOException | FormatException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Failed to read NFC tag", e); } } } @@ -263,7 +265,7 @@ public class PassphraseWizardActivity extends FragmentActivity { try { password = readText(ndefRecord); } catch (UnsupportedEncodingException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Failed to read password from tag", e); } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java index 0ec145657..30bdfb92a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java @@ -31,7 +31,9 @@ import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; +import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; /** * Created by Matt Allen -- cgit v1.2.3 From 7cf695dbaec89f341c302b1a699714e4ef79ef0d Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 18 Jun 2015 16:33:29 +0530 Subject: reduce code duplication --- .../keychain/ui/dialog/InstallDialogFragment.java | 79 +-------------- .../ui/dialog/PreferenceInstallDialogFragment.java | 81 +-------------- .../ui/util/InstallDialogFragmentHelper.java | 109 +++++++++++++++++++++ 3 files changed, 119 insertions(+), 150 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/InstallDialogFragmentHelper.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java index 7b3f9ad28..7bfd940e6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java @@ -17,30 +17,14 @@ package org.sufficientlysecure.keychain.ui.dialog; -import android.app.Activity; import android.app.Dialog; -import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; import android.os.Bundle; -import android.os.Message; import android.os.Messenger; -import android.os.RemoteException; import android.support.v4.app.DialogFragment; -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.ui.util.InstallDialogFragmentHelper; public class InstallDialogFragment extends DialogFragment { - private static final String ARG_MESSENGER = "messenger"; - private static final String ARG_TITLE = "title"; - private static final String ARG_MESSAGE = "message"; - private static final String ARG_MIDDLE_BUTTON = "middleButton"; - private static final String ARG_INSTALL_PATH = "installPath"; - private static final String ARG_USE_MIDDLE_BUTTON = "useMiddleButton"; - - public static final String PLAY_STORE_PATH = "market://search?q=pname:"; public static final int MESSAGE_MIDDLE_CLICKED = 1; @@ -60,13 +44,9 @@ public class InstallDialogFragment extends DialogFragment { useMiddleButton) { InstallDialogFragment frag = new InstallDialogFragment(); Bundle args = new Bundle(); - args.putParcelable(ARG_MESSENGER, messenger); - args.putInt(ARG_TITLE, title); - args.putInt(ARG_MESSAGE, message); - args.putInt(ARG_MIDDLE_BUTTON, middleButton); - args.putString(ARG_INSTALL_PATH, PLAY_STORE_PATH + packageToInstall); - args.putBoolean(ARG_USE_MIDDLE_BUTTON, useMiddleButton); + InstallDialogFragmentHelper.wrapIntoArgs(messenger, title, message, packageToInstall, middleButton, + useMiddleButton, args); frag.setArguments(args); @@ -88,57 +68,8 @@ public class InstallDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - final Activity activity = getActivity(); - - final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); - - final int title = getArguments().getInt(ARG_TITLE); - final int message = getArguments().getInt(ARG_MESSAGE); - final int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON); - final String installPath = getArguments().getString(ARG_INSTALL_PATH); - final boolean useMiddleButton = getArguments().getBoolean(ARG_USE_MIDDLE_BUTTON); - - CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); - - builder.setTitle(title).setMessage(message); - - builder.setNegativeButton(R.string.orbot_install_dialog_cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - - } - }); - - builder.setPositiveButton(R.string.orbot_install_dialog_install, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Uri uri = Uri.parse(installPath); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - activity.startActivity(intent); - } - } - ); - - if (useMiddleButton) { - builder.setNeutralButton(middleButton, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Message msg = new Message(); - msg.what = MESSAGE_MIDDLE_CLICKED; - try { - messenger.send(msg); - } catch (RemoteException e) { - Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); - } catch (NullPointerException e) { - Log.w(Constants.TAG, "Messenger is null!", e); - } - } - } - ); - } - return builder.show(); + return InstallDialogFragmentHelper.getInstallDialogFromArgs(getArguments(), getActivity(), + MESSAGE_MIDDLE_CLICKED); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java index b025cf5de..afeec285f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java @@ -17,30 +17,14 @@ package org.sufficientlysecure.keychain.ui.dialog; -import android.app.Activity; import android.app.Dialog; -import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; import android.os.Bundle; -import android.os.Message; import android.os.Messenger; -import android.os.RemoteException; import android.app.DialogFragment; -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.ui.util.InstallDialogFragmentHelper; public class PreferenceInstallDialogFragment extends DialogFragment { - private static final String ARG_MESSENGER = "messenger"; - private static final String ARG_TITLE = "title"; - private static final String ARG_MESSAGE = "message"; - private static final String ARG_MIDDLE_BUTTON = "middleButton"; - private static final String ARG_INSTALL_PATH = "installPath"; - private static final String ARG_USE_MIDDLE_BUTTON = "useMiddleButton"; - - public static final String PLAY_STORE_PATH = "market://search?q=pname:"; public static final int MESSAGE_MIDDLE_CLICKED = 1; @@ -60,13 +44,9 @@ public class PreferenceInstallDialogFragment extends DialogFragment { useMiddleButton) { PreferenceInstallDialogFragment frag = new PreferenceInstallDialogFragment(); Bundle args = new Bundle(); - args.putParcelable(ARG_MESSENGER, messenger); - args.putInt(ARG_TITLE, title); - args.putInt(ARG_MESSAGE, message); - args.putInt(ARG_MIDDLE_BUTTON, middleButton); - args.putString(ARG_INSTALL_PATH, PLAY_STORE_PATH + packageToInstall); - args.putBoolean(ARG_USE_MIDDLE_BUTTON, useMiddleButton); + InstallDialogFragmentHelper.wrapIntoArgs(messenger, title, message, packageToInstall, middleButton, + useMiddleButton, args); frag.setArguments(args); @@ -88,58 +68,7 @@ public class PreferenceInstallDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - final Activity activity = getActivity(); - - final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); - - final int title = getArguments().getInt(ARG_TITLE); - final int message = getArguments().getInt(ARG_MESSAGE); - final int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON); - final String installPath = getArguments().getString(ARG_INSTALL_PATH); - final boolean useMiddleButton = getArguments().getBoolean(ARG_USE_MIDDLE_BUTTON); - - CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); - - builder.setTitle(title).setMessage(message); - - builder.setNegativeButton(R.string.orbot_install_dialog_cancel, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - - } - }); - - builder.setPositiveButton(R.string.orbot_install_dialog_install, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Uri uri = Uri.parse(installPath); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - activity.startActivity(intent); - } - } - ); - - if (useMiddleButton) { - builder.setNeutralButton(middleButton, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Message msg = new Message(); - msg.what = MESSAGE_MIDDLE_CLICKED; - try { - messenger.send(msg); - } catch (RemoteException e) { - Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); - } catch (NullPointerException e) { - Log.w(Constants.TAG, "Messenger is null!", e); - } - } - } - ); - } - - return builder.show(); + return InstallDialogFragmentHelper.getInstallDialogFromArgs(getArguments(), getActivity(), + MESSAGE_MIDDLE_CLICKED); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/InstallDialogFragmentHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/InstallDialogFragmentHelper.java new file mode 100644 index 000000000..9f96db6d0 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/InstallDialogFragmentHelper.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.util; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder; +import org.sufficientlysecure.keychain.util.Log; + +public class InstallDialogFragmentHelper { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_TITLE = "title"; + private static final String ARG_MESSAGE = "message"; + private static final String ARG_MIDDLE_BUTTON = "middleButton"; + private static final String ARG_INSTALL_PATH = "installPath"; + private static final String ARG_USE_MIDDLE_BUTTON = "useMiddleButton"; + + private static final String PLAY_STORE_PATH = "market://search?q=pname:"; + + public static void wrapIntoArgs(Messenger messenger, int title, int message, String packageToInstall, + int middleButton, boolean useMiddleButton, Bundle args) { + args.putParcelable(ARG_MESSENGER, messenger); + + args.putInt(ARG_TITLE, title); + args.putInt(ARG_MESSAGE, message); + args.putInt(ARG_MIDDLE_BUTTON, middleButton); + args.putString(ARG_INSTALL_PATH, PLAY_STORE_PATH + packageToInstall); + args.putBoolean(ARG_USE_MIDDLE_BUTTON, useMiddleButton); + } + + public static AlertDialog getInstallDialogFromArgs(Bundle args, final Activity activity, + final int messengerMiddleButtonClicked) { + final Messenger messenger = args.getParcelable(ARG_MESSENGER); + + final int title = args.getInt(ARG_TITLE); + final int message = args.getInt(ARG_MESSAGE); + final int middleButton = args.getInt(ARG_MIDDLE_BUTTON); + final String installPath = args.getString(ARG_INSTALL_PATH); + final boolean useMiddleButton = args.getBoolean(ARG_USE_MIDDLE_BUTTON); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); + + builder.setTitle(title).setMessage(message); + + builder.setNegativeButton(R.string.orbot_install_dialog_cancel, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + + builder.setPositiveButton(R.string.orbot_install_dialog_install, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Uri uri = Uri.parse(installPath); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + activity.startActivity(intent); + } + } + ); + + if (useMiddleButton) { + builder.setNeutralButton(middleButton, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Message msg = new Message(); + msg.what = messengerMiddleButtonClicked; + try { + messenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + } + ); + } + + return builder.show(); + } +} -- cgit v1.2.3 From c95fa9fdc1d0eefe2b29fc2b1a176fa3a7b3d3b3 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 18 Jun 2015 16:35:57 +0530 Subject: renamed to clarify necessity for two install dialog fragments --- .../keychain/ui/dialog/InstallDialogFragment.java | 75 ---------------------- .../ui/dialog/SupportInstallDialogFragment.java | 75 ++++++++++++++++++++++ .../keychain/util/orbot/OrbotHelper.java | 6 +- 3 files changed, 78 insertions(+), 78 deletions(-) delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SupportInstallDialogFragment.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java deleted file mode 100644 index 7bfd940e6..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2012-2014 Dominik Schürmann - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui.dialog; - -import android.app.Dialog; -import android.os.Bundle; -import android.os.Messenger; -import android.support.v4.app.DialogFragment; - -import org.sufficientlysecure.keychain.ui.util.InstallDialogFragmentHelper; - -public class InstallDialogFragment extends DialogFragment { - - public static final int MESSAGE_MIDDLE_CLICKED = 1; - - /** - * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" - * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. - * - * @param messenger required only for callback from middle button if it has been set - * @param title - * @param message content of dialog - * @param packageToInstall package name of application to install - * @param middleButton if not null, adds a third button to the app with a call back - * @return The dialog to display - */ - public static InstallDialogFragment newInstance(Messenger messenger, int title, int message, - String packageToInstall, int middleButton, boolean - useMiddleButton) { - InstallDialogFragment frag = new InstallDialogFragment(); - Bundle args = new Bundle(); - - InstallDialogFragmentHelper.wrapIntoArgs(messenger, title, message, packageToInstall, middleButton, - useMiddleButton, args); - - frag.setArguments(args); - - return frag; - } - - /** - * To create a DialogFragment with only two buttons - * - * @param title - * @param message - * @param packageToInstall - * @return - */ - public static InstallDialogFragment newInstance(int title, int message, - String packageToInstall) { - return newInstance(null, title, message, packageToInstall, -1, false); - } - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - - return InstallDialogFragmentHelper.getInstallDialogFromArgs(getArguments(), getActivity(), - MESSAGE_MIDDLE_CLICKED); - } -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SupportInstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SupportInstallDialogFragment.java new file mode 100644 index 000000000..36ca13247 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SupportInstallDialogFragment.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Dialog; +import android.os.Bundle; +import android.os.Messenger; +import android.support.v4.app.DialogFragment; + +import org.sufficientlysecure.keychain.ui.util.InstallDialogFragmentHelper; + +public class SupportInstallDialogFragment extends DialogFragment { + + public static final int MESSAGE_MIDDLE_CLICKED = 1; + + /** + * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" + * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. + * + * @param messenger required only for callback from middle button if it has been set + * @param title + * @param message content of dialog + * @param packageToInstall package name of application to install + * @param middleButton if not null, adds a third button to the app with a call back + * @return The dialog to display + */ + public static SupportInstallDialogFragment newInstance(Messenger messenger, int title, int message, + String packageToInstall, int middleButton, boolean + useMiddleButton) { + SupportInstallDialogFragment frag = new SupportInstallDialogFragment(); + Bundle args = new Bundle(); + + InstallDialogFragmentHelper.wrapIntoArgs(messenger, title, message, packageToInstall, middleButton, + useMiddleButton, args); + + frag.setArguments(args); + + return frag; + } + + /** + * To create a DialogFragment with only two buttons + * + * @param title + * @param message + * @param packageToInstall + * @return + */ + public static SupportInstallDialogFragment newInstance(int title, int message, + String packageToInstall) { + return newInstance(null, title, message, packageToInstall, -1, false); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + + return InstallDialogFragmentHelper.getInstallDialogFromArgs(getArguments(), getActivity(), + MESSAGE_MIDDLE_CLICKED); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java index b5cd0806b..d371ec5cd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -59,7 +59,7 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.SupportInstallDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; import org.sufficientlysecure.keychain.util.Preferences; @@ -108,12 +108,12 @@ public class OrbotHelper { } public static DialogFragment getInstallDialogFragment() { - return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + return SupportInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); } public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { - return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, + return SupportInstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); } -- cgit v1.2.3 From 1856ca385de8172623c50598fba27d7ebf689f0f Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 18 Jun 2015 16:45:37 +0530 Subject: fixed middleButton parameter being ignored --- .../java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java index d371ec5cd..2cbf45699 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java @@ -157,14 +157,14 @@ public class OrbotHelper { OrbotHelper.getInstallDialogFragmentWithThirdButton( new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor + middleButton ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); return false; } else if (!OrbotHelper.isOrbotRunning()) { OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor) + middleButton) .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); return false; -- cgit v1.2.3 From 4d81a83baab301fafbf8ac0559ddc00341ac760c Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 3 Jul 2015 07:03:16 +0530 Subject: added proxy support to OperationHelper --- OpenKeychain/src/main/AndroidManifest.xml | 3 + .../keychain/operations/CertifyOperation.java | 28 ++++++-- .../keychain/operations/ExportOperation.java | 55 +++++++++++---- .../keychain/operations/ImportOperation.java | 64 +++++++++++------ .../operations/KeybaseVerificationOperation.java | 19 ++++- .../keychain/operations/results/ExportResult.java | 12 +++- .../operations/results/ImportKeyResult.java | 16 ++++- .../results/KeybaseVerificationResult.java | 11 ++- .../keychain/provider/ProviderHelper.java | 4 +- .../keychain/service/CertifyActionsParcel.java | 3 - .../keychain/service/input/CryptoInputParcel.java | 23 ++++++ .../service/input/RequiredInputParcel.java | 6 +- .../keychain/ui/CertifyKeyFragment.java | 5 +- .../keychain/ui/CreateYubiKeyImportFragment.java | 6 +- .../keychain/ui/DecryptFragment.java | 7 +- .../keychain/ui/ImportKeysActivity.java | 4 +- .../keychain/ui/ImportKeysProxyActivity.java | 22 ++---- .../keychain/ui/KeyListFragment.java | 2 +- .../keychain/ui/OrbotRequiredDialogActivity.java | 82 ++++++++++++++++++++++ .../keychain/ui/SettingsActivity.java | 2 + .../keychain/ui/UploadKeyActivity.java | 20 +----- .../keychain/ui/ViewKeyTrustFragment.java | 2 +- .../keychain/ui/base/CryptoOperationHelper.java | 52 +++++++++++--- .../ui/dialog/AddEditKeyserverDialogFragment.java | 14 +--- .../ui/dialog/OrbotStartDialogFragment.java | 14 +++- .../keychain/util/EmailKeyHelper.java | 22 +++--- .../keychain/util/ParcelableProxy.java | 35 +++------ .../keychain/util/orbot/OrbotHelper.java | 16 ++++- 28 files changed, 388 insertions(+), 161 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/OrbotRequiredDialogActivity.java (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/AndroidManifest.xml b/OpenKeychain/src/main/AndroidManifest.xml index 2dcdb3251..93c75cca6 100644 --- a/OpenKeychain/src/main/AndroidManifest.xml +++ b/OpenKeychain/src/main/AndroidManifest.xml @@ -682,6 +682,9 @@ +