fix rest of resource leaks (#1351)

This commit is contained in:
Vincent Breitmoser 2015-06-17 21:30:25 +02:00
parent 5895385153
commit f5aa36ef9f
7 changed files with 79 additions and 65 deletions

View File

@ -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);
}

View File

@ -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();
}
}

View File

@ -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

View File

@ -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<ParcelableKeyRing> 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<ParcelableKeyRing> 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()) {

View File

@ -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) {

View File

@ -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();
}
/**

View File

@ -1329,5 +1329,6 @@
<string name="snack_armor_off">"Output encoded as Binary."</string>
<string name="snack_compression_on">"Compression <b>enabled</b>."</string>
<string name="snack_compression_off">"Compression <b>disabled</b>."</string>
<string name="error_loading_keys">Error loading keys!</string>
</resources>