aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2015-04-24 14:18:01 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2015-04-24 14:18:01 +0200
commitb4aec3114d9911cf9aef0d14ee697e5131b2853f (patch)
tree7237de5955ec34d8849737b2f9229bfbb37d0c45 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util
parentc2163460971cc01e65d7dfd18eec491f01ebc744 (diff)
parentbaac30508d24dcda6135bf8ae338c99d8c3b8ad8 (diff)
downloadopen-keychain-b4aec3114d9911cf9aef0d14ee697e5131b2853f.tar.gz
open-keychain-b4aec3114d9911cf9aef0d14ee697e5131b2853f.tar.bz2
open-keychain-b4aec3114d9911cf9aef0d14ee697e5131b2853f.zip
Merge branch 'development' into linked-identities
Conflicts: Graphics/update-drawables.sh OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/operations/CertifyOperationTest.java OpenKeychain/build.gradle OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CertifyActionsParcel.java OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/AlgorithmNames.java93
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java51
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java17
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FabContainer.java17
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/KeyUpdateHelper.java2
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/NfcHelper.java9
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableCache.java94
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java8
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressFixedScaler.java17
9 files changed, 185 insertions, 123 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/AlgorithmNames.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/AlgorithmNames.java
deleted file mode 100644
index c1955f75b..000000000
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/AlgorithmNames.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Copyright (C) 2013-2014 Dominik Schürmann <dominik@dominikschuermann.de>
- *
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-package org.sufficientlysecure.keychain.util;
-
-import android.annotation.SuppressLint;
-import android.app.Activity;
-
-import org.spongycastle.bcpg.CompressionAlgorithmTags;
-import org.spongycastle.bcpg.HashAlgorithmTags;
-import org.spongycastle.openpgp.PGPEncryptedData;
-import org.sufficientlysecure.keychain.R;
-
-import java.util.HashMap;
-
-@SuppressLint("UseSparseArrays")
-public class AlgorithmNames {
- Activity mActivity;
-
- HashMap<Integer, String> mEncryptionNames = new HashMap<>();
- HashMap<Integer, String> mHashNames = new HashMap<>();
- HashMap<Integer, String> mCompressionNames = new HashMap<>();
-
- public AlgorithmNames(Activity context) {
- super();
- this.mActivity = context;
-
- mEncryptionNames.put(PGPEncryptedData.AES_128, "AES-128");
- mEncryptionNames.put(PGPEncryptedData.AES_192, "AES-192");
- mEncryptionNames.put(PGPEncryptedData.AES_256, "AES-256");
- mEncryptionNames.put(PGPEncryptedData.BLOWFISH, "Blowfish");
- mEncryptionNames.put(PGPEncryptedData.TWOFISH, "Twofish");
- mEncryptionNames.put(PGPEncryptedData.CAST5, "CAST5");
- mEncryptionNames.put(PGPEncryptedData.DES, "DES");
- mEncryptionNames.put(PGPEncryptedData.TRIPLE_DES, "Triple DES");
- mEncryptionNames.put(PGPEncryptedData.IDEA, "IDEA");
-
- mHashNames.put(HashAlgorithmTags.RIPEMD160, "RIPEMD-160");
- mHashNames.put(HashAlgorithmTags.SHA1, "SHA-1");
- mHashNames.put(HashAlgorithmTags.SHA224, "SHA-224");
- mHashNames.put(HashAlgorithmTags.SHA256, "SHA-256");
- mHashNames.put(HashAlgorithmTags.SHA384, "SHA-384");
- mHashNames.put(HashAlgorithmTags.SHA512, "SHA-512");
-
- mCompressionNames.put(CompressionAlgorithmTags.UNCOMPRESSED, mActivity.getString(R.string.choice_none)
- + " (" + mActivity.getString(R.string.compression_fast) + ")");
- mCompressionNames.put(CompressionAlgorithmTags.ZIP,
- "ZIP (" + mActivity.getString(R.string.compression_fast) + ")");
- mCompressionNames.put(CompressionAlgorithmTags.ZLIB,
- "ZLIB (" + mActivity.getString(R.string.compression_fast) + ")");
- mCompressionNames.put(CompressionAlgorithmTags.BZIP2,
- "BZIP2 (" + mActivity.getString(R.string.compression_very_slow) + ")");
- }
-
- public HashMap<Integer, String> getEncryptionNames() {
- return mEncryptionNames;
- }
-
- public void setEncryptionNames(HashMap<Integer, String> encryptionNames) {
- this.mEncryptionNames = encryptionNames;
- }
-
- public HashMap<Integer, String> getHashNames() {
- return mHashNames;
- }
-
- public void setHashNames(HashMap<Integer, String> hashNames) {
- this.mHashNames = hashNames;
- }
-
- public HashMap<Integer, String> getCompressionNames() {
- return mCompressionNames;
- }
-
- public void setCompressionNames(HashMap<Integer, String> compressionNames) {
- this.mCompressionNames = compressionNames;
- }
-
-}
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 c782d2507..609288bf1 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java
@@ -220,15 +220,14 @@ public class ContactHelper {
*/
public static long getMainProfileContactId(ContentResolver resolver) {
Cursor profileCursor = resolver.query(ContactsContract.Profile.CONTENT_URI,
- new String[]{ ContactsContract.Profile._ID}, null, null, null);
+ new String[]{ContactsContract.Profile._ID}, null, null, null);
- if(profileCursor != null && profileCursor.getCount() != 0 && profileCursor.moveToNext()) {
+ if (profileCursor != null && profileCursor.getCount() != 0 && profileCursor.moveToNext()) {
long contactId = profileCursor.getLong(0);
profileCursor.close();
return contactId;
- }
- else {
- if(profileCursor != null) {
+ } else {
+ if (profileCursor != null) {
profileCursor.close();
}
return -1;
@@ -330,7 +329,9 @@ public class ContactHelper {
ContactsContract.RawContacts.SOURCE_ID + "=? AND " +
ContactsContract.RawContacts.DELETED + "=?",
new String[]{//"0" for "not deleted"
- Constants.ACCOUNT_TYPE, Long.toString(masterKeyId), "0"
+ Constants.ACCOUNT_TYPE,
+ Long.toString(masterKeyId),
+ "0"
}, null);
if (raw != null) {
if (raw.moveToNext()) {
@@ -385,23 +386,37 @@ public class ContactHelper {
return null;
}
try {
- long rawContactId = findRawContactId(contentResolver, masterKeyId);
- if (rawContactId == -1) {
- return null;
- }
- Uri rawContactUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId);
- Uri contactUri = ContactsContract.RawContacts.getContactLookupUri(contentResolver, rawContactUri);
- InputStream photoInputStream =
- ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, contactUri, highRes);
- if (photoInputStream == null) {
- return null;
- }
- return BitmapFactory.decodeStream(photoInputStream);
+ long contactId = findContactId(contentResolver, masterKeyId);
+ return loadPhotoByContactId(contentResolver, contactId, highRes);
+
} catch (Throwable ignored) {
return null;
}
}
+ public static Bitmap loadPhotoByContactId(ContentResolver contentResolver, long contactId,
+ boolean highRes) {
+ if (contactId == -1) {
+ return null;
+ }
+ Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
+ // older android versions (tested on API level 15) fail on lookupuris being passed to
+ // openContactPhotoInputStream
+ // http://stackoverflow.com/a/21214524/3000919
+ // Uri lookupUri = ContactsContract.Contacts.getLookupUri(contentResolver, contactUri);
+ // Also, we don't need a permanent shortcut to the contact since we load it afresh each time
+
+ InputStream photoInputStream = ContactsContract.Contacts.openContactPhotoInputStream(
+ contentResolver,
+ contactUri,
+ highRes);
+
+ if (photoInputStream == null) {
+ return null;
+ }
+ return BitmapFactory.decodeStream(photoInputStream);
+ }
+
public static final String[] KEYS_TO_CONTACT_PROJECTION = new String[]{
KeychainContract.KeyRings.MASTER_KEY_ID,
KeychainContract.KeyRings.USER_ID,
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java
index c18e5cabd..93b6a5697 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java
@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
package org.sufficientlysecure.keychain.util;
import android.text.TextUtils;
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FabContainer.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FabContainer.java
index 116b98d1e..ffc0484f7 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FabContainer.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FabContainer.java
@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
package org.sufficientlysecure.keychain.util;
public interface FabContainer {
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/KeyUpdateHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/KeyUpdateHelper.java
index 943b913d7..3bbd86d6a 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/KeyUpdateHelper.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/KeyUpdateHelper.java
@@ -57,7 +57,7 @@ public class KeyUpdateHelper {
Bundle importData = new Bundle();
importData.putParcelableArrayList(KeychainIntentService.DOWNLOAD_KEY_LIST,
new ArrayList<ImportKeysListEntry>(keys));
- importIntent.putExtra(KeychainIntentService.EXTRA_DATA, importData);
+ importIntent.putExtra(KeychainIntentService.EXTRA_SERVICE_INTENT, importData);
importIntent.putExtra(KeychainIntentService.EXTRA_MESSENGER, new Messenger(mHandler));
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/NfcHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/NfcHelper.java
index e4e4e4d05..2b47fd623 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/NfcHelper.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/NfcHelper.java
@@ -191,9 +191,6 @@ public class NfcHelper {
mNfcAdapter.invokeBeam(mActivity);
}
- /**
- * A static subclass of {@link Handler} with a {@link WeakReference} to an {@link Activity} to avoid memory leaks.
- */
private static class NfcHandler extends Handler {
private final WeakReference<Activity> mActivityReference;
@@ -203,12 +200,10 @@ public class NfcHelper {
@Override
public void handleMessage(Message msg) {
- Activity activity = mActivityReference.get();
-
- if (activity != null) {
+ if (mActivityReference.get() != null) {
switch (msg.what) {
case NFC_SENT:
- Notify.create(activity, R.string.nfc_successful, Notify.Style.OK).show();
+ Notify.create(mActivityReference.get(), R.string.nfc_successful, Notify.Style.OK).show();
break;
}
}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableCache.java
new file mode 100644
index 000000000..75bdee00a
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableCache.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+package org.sufficientlysecure.keychain.util;
+
+import android.os.Parcel;
+
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * If Parcelables are above 1 MB, Android OS fails to send them via the Binder IPC:
+ * JavaBinder E !!! FAILED BINDER TRANSACTION !!!
+ * To overcome this issue this class allows to cache Parcelables, mapped by unique UUIDs,
+ * which are written to the parcel instead of the whole Parcelable.
+ */
+public class ParcelableCache<E> {
+
+ private static final UUID NULL_UUID = new UUID(0, 0);
+
+ /**
+ * A HashMap of UUID:Object
+ * This is used such that when we become parceled, we are
+ * well below the 1 MB boundary that is specified.
+ */
+ private ConcurrentHashMap<UUID, E> objectCache = new ConcurrentHashMap<>();
+
+ /**
+ * Dehydrate a Parcelable (such that it is available after deparcelization)
+ * Returns the NULL uuid (0) if you hand it null.
+ *
+ * @param parcelable A Parcelable to dehydrate
+ * @return a UUID, the ticket for your dehydrated Parcelable
+ */
+ private UUID dehydrateParcelable(E parcelable) {
+ if (parcelable == null) {
+ return NULL_UUID;
+ } else {
+ UUID uuid = UUID.randomUUID();
+ objectCache.put(uuid, parcelable);
+ return uuid;
+ }
+ }
+
+ /**
+ * Rehydrate a Parcelable after going through parcelization,
+ * invalidating its place in the dehydration pool.
+ * This is used such that when parcelized, the Parcelable is no larger than 1 MB.
+ *
+ * @param uuid A UUID ticket that identifies the log in question.
+ * @return An OperationLog.
+ */
+ private E rehydrateParcelable(UUID uuid) {
+ // UUID.equals isn't well documented; we use compareTo instead.
+ if (NULL_UUID.compareTo(uuid) == 0) {
+ return null;
+ } else {
+ E parcelable = objectCache.get(uuid);
+ objectCache.remove(uuid);
+ return parcelable;
+ }
+ }
+
+ public E readFromParcelAndGetFromCache(Parcel source) {
+ long mostSig = source.readLong();
+ long leastSig = source.readLong();
+ UUID mTicket = new UUID(mostSig, leastSig);
+ // fetch the dehydrated parcelable out of storage (this removes it from the dehydration pool)
+ return rehydrateParcelable(mTicket);
+ }
+
+ public void cacheAndWriteToParcel(E parcelable, Parcel dest) {
+ // Get a ticket for our parcelable.
+ UUID mTicket = dehydrateParcelable(parcelable);
+ // And write out the UUID most and least significant bits.
+ dest.writeLong(mTicket.getMostSignificantBits());
+ dest.writeLong(mTicket.getLeastSignificantBits());
+ }
+
+}
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 44c1e6b6c..8a7638054 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java
@@ -109,21 +109,21 @@ public class Preferences {
return mSharedPreferences.getBoolean(Constants.Pref.FIRST_TIME, true);
}
- public boolean useDefaultYubikeyPin() {
+ public boolean useDefaultYubiKeyPin() {
return mSharedPreferences.getBoolean(Pref.USE_DEFAULT_YUBIKEY_PIN, false);
}
- public void setUseDefaultYubikeyPin(boolean useDefaultYubikeyPin) {
+ public void setUseDefaultYubiKeyPin(boolean useDefaultYubikeyPin) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(Pref.USE_DEFAULT_YUBIKEY_PIN, useDefaultYubikeyPin);
editor.commit();
}
- public boolean useNumKeypadForYubikeyPin() {
+ public boolean useNumKeypadForYubiKeyPin() {
return mSharedPreferences.getBoolean(Pref.USE_NUMKEYPAD_FOR_YUBIKEY_PIN, true);
}
- public void setUseNumKeypadForYubikeyPin(boolean useNumKeypadForYubikeyPin) {
+ public void setUseNumKeypadForYubiKeyPin(boolean useNumKeypadForYubikeyPin) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(Pref.USE_NUMKEYPAD_FOR_YUBIKEY_PIN, useNumKeypadForYubikeyPin);
editor.commit();
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressFixedScaler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressFixedScaler.java
index 4bb4ca5de..861298f56 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressFixedScaler.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressFixedScaler.java
@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
package org.sufficientlysecure.keychain.util;
import org.sufficientlysecure.keychain.pgp.Progressable;