diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java index ffce2f39c..cdda42dae 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java @@ -411,13 +411,17 @@ public class ImportExportOperation extends BaseOperation { try { OutputStream outStream = new FileOutputStream(outputFile); - ExportResult result = exportKeyRings(log, masterKeyIds, exportSecret, outStream); - if (result.cancelled()) { - //noinspection ResultOfMethodCallIgnored - new File(outputFile).delete(); + try { + ExportResult result = exportKeyRings(log, masterKeyIds, exportSecret, outStream); + if (result.cancelled()) { + //noinspection ResultOfMethodCallIgnored + new File(outputFile).delete(); + } + return result; + } finally { + outStream.close(); } - return result; - } catch (FileNotFoundException e) { + } catch (IOException e) { log.add(LogType.MSG_EXPORT_ERROR_FOPEN, 1); return new ExportResult(ExportResult.RESULT_ERROR, log); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java index 36ba47672..a91eca453 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java @@ -391,10 +391,15 @@ public class KeychainDatabase extends SQLiteOpenHelper { private static void copy(File in, File out) throws IOException { FileInputStream is = new FileInputStream(in); FileOutputStream os = new FileOutputStream(out); - byte[] buf = new byte[512]; - while (is.available() > 0) { - int count = is.read(buf, 0, 512); - os.write(buf, 0, count); + try { + byte[] buf = new byte[512]; + while (is.available() > 0) { + int count = is.read(buf, 0, 512); + os.write(buf, 0, count); + } + } finally { + is.close(); + os.close(); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java index 2b71d6dc1..d25249b14 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java @@ -217,13 +217,15 @@ public class AppSettingsActivity extends BaseActivity { // show accounts only if available (deprecated API) Cursor cursor = getContentResolver().query(accountsUri, null, null, null, null); - if (cursor.moveToFirst()) { + if (cursor != null && cursor.moveToFirst()) try { mAccountsLabel.setVisibility(View.VISIBLE); mAccountsListFragment = AccountsListFragment.newInstance(accountsUri); // Create an instance of the fragments getSupportFragmentManager().beginTransaction() .replace(R.id.api_accounts_list_fragment, mAccountsListFragment) .commitAllowingStateLoss(); + } finally { + cursor.close(); } // Create an instance of the 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 36074f6ba..780558b27 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -71,6 +71,7 @@ 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; @@ -553,9 +554,12 @@ public class KeyListFragment extends LoaderFragment } private void updateAllKeys() { - Context context = getActivity(); + Activity activity = getActivity(); + if (activity == null) { + return; + } - ProviderHelper providerHelper = new ProviderHelper(context); + ProviderHelper providerHelper = new ProviderHelper(activity); Cursor cursor = providerHelper.getContentResolver().query( KeyRings.buildUnifiedKeyRingsUri(), new String[]{ @@ -563,13 +567,21 @@ public class KeyListFragment extends LoaderFragment }, null, null, null ); - ArrayList keyList = new ArrayList<>(); + if (cursor == null) { + Notify.create(activity, R.string.error_loading_keys, Style.ERROR); + return; + } - while (cursor.moveToNext()) { - byte[] blob = cursor.getBlob(0);//fingerprint column is 0 - String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob); - ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); - keyList.add(keyEntry); + ArrayList keyList = new ArrayList<>(); + try { + while (cursor.moveToNext()) { + byte[] blob = cursor.getBlob(0);//fingerprint column is 0 + String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob); + ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); + keyList.add(keyEntry); + } + } finally { + cursor.close(); } ServiceProgressHandler serviceHandler = new ServiceProgressHandler(getActivity()) { 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 cfad76fc3..2c1714b67 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 @@ -17,6 +17,12 @@ package org.sufficientlysecure.keychain.ui.dialog; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; + import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -29,8 +35,8 @@ import android.os.Bundle; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; +import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; -import android.test.suitebuilder.TestSuiteBuilder; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -47,15 +53,6 @@ import org.sufficientlysecure.keychain.R; 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; - public class AddKeyserverDialogFragment extends DialogFragment implements OnEditorActionListener { private static final String ARG_MESSENGER = "messenger"; @@ -70,20 +67,11 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit private EditText mKeyserverEditText; private CheckBox mVerifyKeyserverCheckBox; - public static enum FailureReason { + public enum FailureReason { INVALID_URL, CONNECTION_FAILED } - ; - - /** - * Creates new instance of this dialog fragment - * - * @param title title of dialog - * @param messenger to communicate back after setting the passphrase - * @return - */ public static AddKeyserverDialogFragment newInstance(Messenger messenger) { AddKeyserverDialogFragment frag = new AddKeyserverDialogFragment(); Bundle args = new Bundle(); @@ -94,9 +82,7 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit return frag; } - /** - * Creates dialog - */ + @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity activity = getActivity(); @@ -222,19 +208,23 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit String scheme = keyserverUri.getScheme(); String schemeSpecificPart = keyserverUri.getSchemeSpecificPart(); String fragment = keyserverUri.getFragment(); - if (scheme == null) throw new MalformedURLException(); - if (scheme.equalsIgnoreCase("hkps")) scheme = "https"; - else if (scheme.equalsIgnoreCase("hkp")) scheme = "http"; + if (scheme == null) { + throw new MalformedURLException(); + } + if ("hkps".equalsIgnoreCase(scheme)) { + scheme = "https"; + } else if ("hkp".equalsIgnoreCase(scheme)) { + scheme = "http"; + } URI newKeyserver = new URI(scheme, schemeSpecificPart, fragment); - Log.d("Converted URL", newKeyserver.toString()); - TlsHelper.openConnection(newKeyserver.toURL()).getInputStream(); + Log.d(Constants.TAG, "Converted URL" + newKeyserver); + + // just see if we can get a connection, then immediately close + TlsHelper.openConnection(newKeyserver.toURL()).getInputStream().close(); } catch (TlsHelper.TlsHelperException e) { reason = FailureReason.CONNECTION_FAILED; - } catch (MalformedURLException e) { - Log.w(Constants.TAG, "Invalid keyserver URL entered by user."); - reason = FailureReason.INVALID_URL; - } catch (URISyntaxException e) { + } catch (MalformedURLException | URISyntaxException e) { Log.w(Constants.TAG, "Invalid keyserver URL entered by user."); reason = FailureReason.INVALID_URL; } catch (IOException 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 1487c3053..0ec145657 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 @@ -97,26 +97,23 @@ public class PasswordStrengthView extends View { public PasswordStrengthView(Context context, AttributeSet attrs) { super(context, attrs); - int COLOR_FAIL = context.getResources().getColor(R.color.android_red_light); - int COLOR_WEAK = context.getResources().getColor(R.color.android_orange_light); - int COLOR_STRONG = context.getResources().getColor(R.color.android_green_light); + int COLOR_FAIL = getResources().getColor(R.color.android_red_light); + int COLOR_WEAK = getResources().getColor(R.color.android_orange_light); + int COLOR_STRONG = getResources().getColor(R.color.android_green_light); TypedArray style = context.getTheme().obtainStyledAttributes( attrs, R.styleable.PasswordStrengthView, 0, 0); - try { - mStrengthRequirement = style.getInteger(R.styleable.PasswordStrengthView_strength, - STRENGTH_MEDIUM); - mShowGuides = style.getBoolean(R.styleable.PasswordStrengthView_showGuides, true); - mColorFail = style.getColor(R.styleable.PasswordStrengthView_color_fail, COLOR_FAIL); - mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK); - mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong, - COLOR_STRONG); - } catch (Exception e) { - e.printStackTrace(); - } + mStrengthRequirement = style.getInteger(R.styleable.PasswordStrengthView_strength, + STRENGTH_MEDIUM); + mShowGuides = style.getBoolean(R.styleable.PasswordStrengthView_showGuides, true); + mColorFail = style.getColor(R.styleable.PasswordStrengthView_color_fail, COLOR_FAIL); + mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK); + mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong, + COLOR_STRONG); + // Create and style the paint used for drawing the guide on the indicator mGuidePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mGuidePaint.setStyle(Paint.Style.FILL_AND_STROKE); @@ -124,6 +121,9 @@ public class PasswordStrengthView extends View { // Create and style paint for indicator mIndicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mIndicatorPaint.setStyle(Paint.Style.FILL); + + style.recycle(); + } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index e1efd5abc..77aa1a055 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -303,10 +303,13 @@ public class ContactHelper { Cursor contactMasterKey = context.getContentResolver().query(contactUri, new String[]{ContactsContract.Data.DATA2}, null, null, null); if (contactMasterKey != null) { - if (contactMasterKey.moveToNext()) { - return KeychainContract.KeyRings.buildGenericKeyRingUri(contactMasterKey.getLong(0)); + try { + if (contactMasterKey.moveToNext()) { + return KeychainContract.KeyRings.buildGenericKeyRingUri(contactMasterKey.getLong(0)); + } + } finally { + contactMasterKey.close(); } - contactMasterKey.close(); } return null; } @@ -537,7 +540,7 @@ public class ContactHelper { KEYS_TO_CONTACT_PROJECTION, KeychainContract.KeyRings.HAS_ANY_SECRET + "!=0", null, null); - if (cursor != null) { + if (cursor != null) try { while (cursor.moveToNext()) { long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0; @@ -565,6 +568,8 @@ public class ContactHelper { } } } + } finally { + cursor.close(); } for (long masterKeyId : keysToDelete) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java index 5a314ad0b..eabbf83b8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java @@ -66,21 +66,24 @@ public class ParcelableFileCache { File tempFile = new File(mContext.getCacheDir(), mFilename); + DataOutputStream oos = new DataOutputStream(new FileOutputStream(tempFile)); - oos.writeInt(numEntries); + try { + oos.writeInt(numEntries); - while (it.hasNext()) { - Parcel p = Parcel.obtain(); // creating empty parcel object - p.writeParcelable(it.next(), 0); // saving bundle as parcel - byte[] buf = p.marshall(); - oos.writeInt(buf.length); - oos.write(buf); - p.recycle(); + while (it.hasNext()) { + Parcel p = Parcel.obtain(); // creating empty parcel object + p.writeParcelable(it.next(), 0); // saving bundle as parcel + byte[] buf = p.marshall(); + oos.writeInt(buf.length); + oos.write(buf); + p.recycle(); + } + } finally { + oos.close(); } - oos.close(); - } /** diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index ae131e029..c21b34d55 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -1331,5 +1331,6 @@ "Output encoded as Binary." "Compression enabled." "Compression disabled." + Error loading keys!