From 49d544d558e9c7f1106b5923204b1fbec2696cf7 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: [PATCH] Cleanup tor code --- .../keychain/ui/CertifyKeyFragment.java | 6 +--- .../ui/CreateKeyYubiKeyImportFragment.java | 2 +- .../keychain/ui/DecryptFragment.java | 3 +- .../keychain/ui/ImportKeysActivity.java | 2 +- .../keychain/ui/ImportKeysProxyActivity.java | 2 +- .../keychain/ui/KeyListFragment.java | 2 +- .../keychain/ui/SettingsActivity.java | 2 +- .../keychain/ui/UploadKeyActivity.java | 2 +- .../keychain/ui/ViewKeyActivity.java | 2 +- .../keychain/ui/ViewKeyTrustFragment.java | 2 +- .../ui/dialog/AddKeyserverDialogFragment.java | 2 +- .../ui/dialog/OrbotStartDialogFragment.java | 2 +- .../util/{orbot => tor}/OrbotHelper.java | 10 +++--- .../util/{orbot => tor}/TorServiceUtils.java | 31 +++++++------------ 14 files changed, 27 insertions(+), 43 deletions(-) rename OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/{orbot => tor}/OrbotHelper.java (96%) rename OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/{orbot => tor}/TorServiceUtils.java (91%) 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 ae5bb77a4..4b1ebc4b3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java @@ -19,7 +19,6 @@ package org.sufficientlysecure.keychain.ui; import android.app.Activity; -import android.app.ProgressDialog; import android.content.Intent; import android.database.Cursor; import android.database.MatrixCursor; @@ -53,16 +52,13 @@ import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.CertifyActionsParcel; import org.sufficientlysecure.keychain.service.CertifyActionsParcel.CertifyAction; -import org.sufficientlysecure.keychain.service.KeychainService; -import org.sufficientlysecure.keychain.service.ServiceProgressHandler; -import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.ui.adapter.MultiUserIdsAdapter; import org.sufficientlysecure.keychain.ui.base.CachingCryptoOperationFragment; import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.CertifyKeySpinner; 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.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java index d8c2c52d1..2b1c90efb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java @@ -47,7 +47,7 @@ import org.sufficientlysecure.keychain.ui.CreateKeyActivity.NfcListenerFragment; 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 CreateKeyYubiKeyImportFragment extends Fragment implements NfcListenerFragment { 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 00bfc6f25..7ed98d6da 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -51,14 +51,13 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.KeychainService; import org.sufficientlysecure.keychain.service.ServiceProgressHandler; import org.sufficientlysecure.keychain.ui.base.CachingCryptoOperationFragment; -import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; 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; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; public abstract class DecryptFragment extends CachingCryptoOperationFragment 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 45111fc0d..2622294e7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -44,7 +44,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 013f5664e..ceb7adfe9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java @@ -49,7 +49,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 afeaec4ea..a807f5254 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -81,7 +81,7 @@ import org.sufficientlysecure.keychain.util.FabContainer; 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 se.emilsjolander.stickylistheaders.StickyListHeadersAdapter; import se.emilsjolander.stickylistheaders.StickyListHeadersListView; 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 959e38584..a4fedc53b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -43,7 +43,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 30d248f9d..609989056 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java @@ -41,7 +41,7 @@ import org.sufficientlysecure.keychain.service.ServiceProgressHandler; 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 a63c5b687..f4ec22cdc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -84,7 +84,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 3b88462e5..7e4977249 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,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/AddKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddKeyserverDialogFragment.java index f6d97b300..ae563ad65 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddKeyserverDialogFragment.java @@ -49,7 +49,7 @@ import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.TlsHelper; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.io.IOException; import java.net.MalformedURLException; 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/tor/OrbotHelper.java similarity index 96% rename from OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java rename to OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java index f57496767..84e4f36ab 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java @@ -47,12 +47,9 @@ ***** */ -package org.sufficientlysecure.keychain.util.orbot; +package org.sufficientlysecure.keychain.util.tor; -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; @@ -60,7 +57,7 @@ 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; @@ -89,7 +86,8 @@ public class OrbotHelper { private static boolean isAppInstalled(String uri, Context context) { PackageManager pm = context.getPackageManager(); - boolean installed = false; + + boolean installed; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); installed = true; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java similarity index 91% rename from OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java rename to OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java index b1d8327fb..48adc9528 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java @@ -47,9 +47,10 @@ ***** */ -package org.sufficientlysecure.keychain.util.orbot; +package org.sufficientlysecure.keychain.util.tor; -import android.util.Log; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.util.Log; import java.io.BufferedReader; import java.io.File; @@ -60,10 +61,7 @@ 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"; @@ -74,13 +72,14 @@ public class TorServiceUtils { try { procId = findProcessIdWithPidOf(command); - if (procId == -1) + 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); + Log.e(Constants.TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); } } @@ -91,10 +90,8 @@ public class TorServiceUtils { public static int findProcessIdWithPidOf(String command) throws Exception { int procId = -1; - Runtime r = Runtime.getRuntime(); - - Process procPs = null; + Process procPs; String baseName = new File(command).getName(); // fix contributed my mikos on 2010.12.10 @@ -104,7 +101,7 @@ public class TorServiceUtils { // procPs = r.exec(SHELL_CMD_PIDOF); BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; + String line; while ((line = reader.readLine()) != null) { @@ -118,25 +115,20 @@ public class TorServiceUtils { } return procId; - } // use 'ps' command public static int findProcessIdWithPS(String command) throws Exception { - int procId = -1; - Runtime r = Runtime.getRuntime(); - - Process procPs = null; + Process procPs; procPs = r.exec(SHELL_CMD_PS); BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; - + String line; while ((line = reader.readLine()) != null) { - if (line.indexOf(' ' + command) != -1) { + if (line.contains(' ' + command)) { StringTokenizer st = new StringTokenizer(line, " "); st.nextToken(); // proc owner @@ -148,6 +140,5 @@ public class TorServiceUtils { } return procId; - } }