From af90db96a5d05bb37985afa7d01246fe963674f0 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 10:51:12 +0200 Subject: new PassphraseCache, storing UserIDs as well --- .../keychain/service/PassphraseCacheService.java | 58 ++++++++++++++++++---- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 28f230f71..9a68b8367 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -37,6 +37,7 @@ import android.support.v4.util.LongSparseArray; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.Log; @@ -54,6 +55,8 @@ public class PassphraseCacheService extends Service { + "PASSPHRASE_CACHE_ADD"; public static final String ACTION_PASSPHRASE_CACHE_GET = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_GET"; + public static final String ACTION_PASSPHRASE_CACHE_PURGE = Constants.INTENT_PREFIX + + "PASSPHRASE_CACHE_PURGE"; public static final String BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_BROADCAST"; @@ -62,13 +65,14 @@ public class PassphraseCacheService extends Service { public static final String EXTRA_KEY_ID = "key_id"; public static final String EXTRA_PASSPHRASE = "passphrase"; public static final String EXTRA_MESSENGER = "messenger"; + public static final String EXTRA_USERID = "userid"; private static final int REQUEST_ID = 0; private static final long DEFAULT_TTL = 15; private BroadcastReceiver mIntentReceiver; - private LongSparseArray mPassphraseCache = new LongSparseArray(); + private LongSparseArray mPassphraseCache = new LongSparseArray(); Context mContext; @@ -81,14 +85,17 @@ public class PassphraseCacheService extends Service { * @param keyId * @param passphrase */ - public static void addCachedPassphrase(Context context, long keyId, String passphrase) { + public static void addCachedPassphrase(Context context, long keyId, String passphrase, + String primaryUserId) { Log.d(Constants.TAG, "PassphraseCacheService.cacheNewPassphrase() for " + keyId); Intent intent = new Intent(context, PassphraseCacheService.class); intent.setAction(ACTION_PASSPHRASE_CACHE_ADD); + intent.putExtra(EXTRA_TTL, Preferences.getPreferences(context).getPassphraseCacheTtl()); intent.putExtra(EXTRA_PASSPHRASE, passphrase); intent.putExtra(EXTRA_KEY_ID, keyId); + intent.putExtra(EXTRA_USERID, primaryUserId); context.startService(intent); } @@ -159,11 +166,11 @@ public class PassphraseCacheService extends Service { // passphrase for symmetric encryption? if (keyId == Constants.key.symmetric) { Log.d(Constants.TAG, "PassphraseCacheService.getCachedPassphraseImpl() for symmetric encryption"); - String cachedPassphrase = mPassphraseCache.get(Constants.key.symmetric); + String cachedPassphrase = mPassphraseCache.get(Constants.key.symmetric).getPassphrase(); if (cachedPassphrase == null) { return null; } - addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase); + addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase, "Password"); return cachedPassphrase; } @@ -176,13 +183,17 @@ public class PassphraseCacheService extends Service { if (!key.hasPassphrase()) { Log.d(Constants.TAG, "Key has no passphrase! Caches and returns empty passphrase!"); - addCachedPassphrase(this, keyId, ""); + try { + addCachedPassphrase(this, keyId, "", key.getPrimaryUserId()); + } catch (PgpGeneralException e) { + Log.d(Constants.TAG, "PgpGeneralException occured"); + } return ""; } // get cached passphrase - String cachedPassphrase = mPassphraseCache.get(keyId); - if (cachedPassphrase == null) { + CachedPassphrase cachedPassphrase = mPassphraseCache.get(keyId); + if (cachedPassphrase == null && cachedPassphrase.getPassphrase() != null) { Log.d(Constants.TAG, "PassphraseCacheService Passphrase not (yet) cached, returning null"); // not really an error, just means the passphrase is not cached but not empty either return null; @@ -190,8 +201,8 @@ public class PassphraseCacheService extends Service { // set it again to reset the cache life cycle Log.d(Constants.TAG, "PassphraseCacheService Cache passphrase again when getting it!"); - addCachedPassphrase(this, keyId, cachedPassphrase); - return cachedPassphrase; + addCachedPassphrase(this, keyId, cachedPassphrase.getPassphrase(), cachedPassphrase.getPrimaryUserID()); + return cachedPassphrase.getPassphrase(); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "PassphraseCacheService Passphrase for unknown key was requested!"); @@ -256,14 +267,16 @@ public class PassphraseCacheService extends Service { if (ACTION_PASSPHRASE_CACHE_ADD.equals(intent.getAction())) { long ttl = intent.getLongExtra(EXTRA_TTL, DEFAULT_TTL); long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1); + String passphrase = intent.getStringExtra(EXTRA_PASSPHRASE); + String primaryUserID = intent.getStringExtra(EXTRA_USERID); Log.d(Constants.TAG, "PassphraseCacheService Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: " + keyId + ", ttl: " + ttl); // add keyId and passphrase to memory - mPassphraseCache.put(keyId, passphrase); + mPassphraseCache.put(keyId, new CachedPassphrase(passphrase, primaryUserID)); if (ttl > 0) { // register new alarm with keyId for this passphrase @@ -341,4 +354,27 @@ public class PassphraseCacheService extends Service { private final IBinder mBinder = new PassphraseCacheBinder(); -} + public class CachedPassphrase { + private String primaryUserID = ""; + private String passphrase = ""; + + public CachedPassphrase(String passphrase, String primaryUserID) { + setPassphrase(passphrase); + setPrimaryUserID(primaryUserID); + } + + public String getPrimaryUserID() { + return primaryUserID; + } + public String getPassphrase() { + return passphrase; + } + + public void setPrimaryUserID(String primaryUserID) { + this.primaryUserID = primaryUserID; + } + public void setPassphrase(String passphrase) { + this.passphrase = passphrase; + } + } +} \ No newline at end of file -- cgit v1.2.3 From cf40517eaca2c852a0a0022b93c46e7a41766d80 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 12:27:19 +0200 Subject: Implemented Notification, no fallback yet --- .../keychain/service/KeychainIntentService.java | 3 +- .../keychain/service/PassphraseCacheService.java | 72 +++++++++++++++++++++- .../ui/dialog/PassphraseDialogFragment.java | 15 ++++- 3 files changed, 83 insertions(+), 7 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index c4c31bdad..10faa8786 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -44,6 +44,7 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.pgp.WrappedPublicKey; import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.WrappedSecretKey; import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; @@ -351,7 +352,7 @@ public class KeychainIntentService extends IntentService // cache new passphrase if (saveParcel.newPassphrase != null) { PassphraseCacheService.addCachedPassphrase(this, ring.getMasterKeyId(), - saveParcel.newPassphrase); + saveParcel.newPassphrase, ring.getPublicKey().getPrimaryUserId()); } } catch (ProviderHelper.NotFoundException e) { sendErrorToHandler(e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 9a68b8367..9d998f18f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.service; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; +import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -32,9 +33,12 @@ import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; + import android.support.v4.util.LongSparseArray; +import android.support.v4.app.NotificationCompat; import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; @@ -70,6 +74,8 @@ public class PassphraseCacheService extends Service { private static final int REQUEST_ID = 0; private static final long DEFAULT_TTL = 15; + private static final int NOTIFICATION_ID = 1; + private BroadcastReceiver mIntentReceiver; private LongSparseArray mPassphraseCache = new LongSparseArray(); @@ -193,7 +199,7 @@ public class PassphraseCacheService extends Service { // get cached passphrase CachedPassphrase cachedPassphrase = mPassphraseCache.get(keyId); - if (cachedPassphrase == null && cachedPassphrase.getPassphrase() != null) { + if (cachedPassphrase == null) { Log.d(Constants.TAG, "PassphraseCacheService Passphrase not (yet) cached, returning null"); // not really an error, just means the passphrase is not cached but not empty either return null; @@ -273,9 +279,9 @@ public class PassphraseCacheService extends Service { Log.d(Constants.TAG, "PassphraseCacheService Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: " - + keyId + ", ttl: " + ttl); + + keyId + ", ttl: " + ttl + ", usrId: " + primaryUserID); - // add keyId and passphrase to memory + // add keyId, passphrase and primary user id to memory mPassphraseCache.put(keyId, new CachedPassphrase(passphrase, primaryUserID)); if (ttl > 0) { @@ -284,6 +290,9 @@ public class PassphraseCacheService extends Service { AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, triggerTime, buildIntent(this, keyId)); } + + updateNotifications(); + } else if (ACTION_PASSPHRASE_CACHE_GET.equals(intent.getAction())) { long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1); Messenger messenger = intent.getParcelableExtra(EXTRA_MESSENGER); @@ -299,6 +308,17 @@ public class PassphraseCacheService extends Service { } catch (RemoteException e) { Log.e(Constants.TAG, "PassphraseCacheService Sending message failed", e); } + } else if (ACTION_PASSPHRASE_CACHE_PURGE.equals(intent.getAction())) { + AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); + + // Stop all ttl alarms + for(int i = 0; i < mPassphraseCache.size(); i++) { + am.cancel(buildIntent(this, mPassphraseCache.keyAt(i))); + } + + mPassphraseCache.clear(); + + updateNotifications(); } else { Log.e(Constants.TAG, "PassphraseCacheService Intent or Intent Action not supported!"); } @@ -324,6 +344,52 @@ public class PassphraseCacheService extends Service { Log.d(Constants.TAG, "PassphraseCacheServic No passphrases remaining in memory, stopping service!"); stopSelf(); } + + updateNotifications(); + } + + private void updateNotifications() { + NotificationManager notificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + if(mPassphraseCache.size() > 0) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + + builder.setSmallIcon(R.drawable.ic_launcher) + .setContentTitle("Open Keychain") + .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); + + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); + + inboxStyle.setBigContentTitle("Cached Passphrases:"); + + // Moves events into the big view + for (int i = 0; i < mPassphraseCache.size(); i++) { + inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); + } + + // Moves the big view style object into the notification object. + builder.setStyle(inboxStyle); + + // Add purging action + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + builder.addAction( + R.drawable.abc_ic_clear_normal, + "Purge Cache", + PendingIntent.getService( + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + ) + ); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + + } else { + notificationManager.cancel(NOTIFICATION_ID); + } } @Override diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 59e4d8dee..5bbbf20a7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -189,7 +189,8 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // Early breakout if we are dealing with a symmetric key if (secretRing == null) { - PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, passphrase); + PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, + passphrase, "Password"); // also return passphrase back to activity Bundle data = new Bundle(); data.putString(MESSAGE_DATA_PASSPHRASE, passphrase); @@ -228,10 +229,18 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // cache the new passphrase Log.d(Constants.TAG, "Everything okay! Caching entered passphrase"); - PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase); + + try { + PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase, + secretRing.getPrimaryUserId()); + } catch(PgpGeneralException e) { + Log.e(Constants.TAG, e.getMessage()); + } + if (unlockedSecretKey.getKeyId() != masterKeyId) { PassphraseCacheService.addCachedPassphrase( - activity, unlockedSecretKey.getKeyId(), passphrase); + activity, unlockedSecretKey.getKeyId(), passphrase, + unlockedSecretKey.getPrimaryUserId()); } // also return passphrase back to activity -- cgit v1.2.3 From 2568ea4b2edaa1a13b15395d1951e7f941fba073 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 12:42:48 +0200 Subject: Added Purging for Android < 4.1 --- .../keychain/service/PassphraseCacheService.java | 79 ++++++++++++++-------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 9d998f18f..84ad441cb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -26,6 +26,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Binder; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; @@ -353,39 +354,61 @@ public class PassphraseCacheService extends Service { (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if(mPassphraseCache.size() > 0) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - builder.setSmallIcon(R.drawable.ic_launcher) - .setContentTitle("Open Keychain") - .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); + builder.setSmallIcon(R.drawable.ic_launcher) + .setContentTitle("Open Keychain") + .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); - NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); - inboxStyle.setBigContentTitle("Cached Passphrases:"); + inboxStyle.setBigContentTitle("Cached Passphrases:"); - // Moves events into the big view - for (int i = 0; i < mPassphraseCache.size(); i++) { - inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); - } + // Moves events into the big view + for (int i = 0; i < mPassphraseCache.size(); i++) { + inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); + } - // Moves the big view style object into the notification object. - builder.setStyle(inboxStyle); - - // Add purging action - Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); - builder.addAction( - R.drawable.abc_ic_clear_normal, - "Purge Cache", - PendingIntent.getService( - getApplicationContext(), - 0, - intent, - PendingIntent.FLAG_UPDATE_CURRENT - ) - ); - - notificationManager.notify(NOTIFICATION_ID, builder.build()); + // Moves the big view style object into the notification object. + builder.setStyle(inboxStyle); + + // Add purging action + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + builder.addAction( + R.drawable.abc_ic_clear_normal, + "Purge Cache", + PendingIntent.getService( + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + ) + ); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + } else { // Fallback, since expandable notifications weren't available back then + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + + builder.setSmallIcon(R.drawable.ic_launcher) + .setContentTitle("Open Keychain has cached " + mPassphraseCache.size() + " keys.") + .setContentText("Click to purge the cache"); + + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + + builder.setContentIntent( + PendingIntent.getService( + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + ) + ); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + } } else { notificationManager.cancel(NOTIFICATION_ID); -- cgit v1.2.3 From 7a3fe61a1f497ba431ed5fe3c68b11ed9578eaba Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 12:52:44 +0200 Subject: Put text into strings.xml, for internationalization --- .../keychain/service/PassphraseCacheService.java | 12 ++++++------ OpenKeychain/src/main/res/values/strings.xml | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 84ad441cb..9b868c859 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -358,12 +358,12 @@ public class PassphraseCacheService extends Service { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher) - .setContentTitle("Open Keychain") - .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); + .setContentTitle(getString(R.string.app_name)) + .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); - inboxStyle.setBigContentTitle("Cached Passphrases:"); + inboxStyle.setBigContentTitle(getString(R.string.passp_cache_notif_keys)); // Moves events into the big view for (int i = 0; i < mPassphraseCache.size(); i++) { @@ -378,7 +378,7 @@ public class PassphraseCacheService extends Service { intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); builder.addAction( R.drawable.abc_ic_clear_normal, - "Purge Cache", + getString(R.string.passp_cache_notif_purge), PendingIntent.getService( getApplicationContext(), 0, @@ -392,8 +392,8 @@ public class PassphraseCacheService extends Service { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher) - .setContentTitle("Open Keychain has cached " + mPassphraseCache.size() + " keys.") - .setContentText("Click to purge the cache"); + .setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))) + .setContentText(getString(R.string.passp_cache_notif_click_to_purge)); Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 274309fd0..456a22086 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -660,6 +660,12 @@ Error unlocking keyring! Unlocking keyring + + Click to purge cached passphrases + OpenKeychain has cached %i keys + Cached Keys: + Purge Cache + Certifier Certificate Details -- cgit v1.2.3 From 079194abe5b48c2b6a5cf943b45a67b956937435 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 18:12:03 +0200 Subject: Fixed issues discussed in #713 --- .../keychain/service/PassphraseCacheService.java | 20 ++++++++++---------- .../keychain/ui/dialog/PassphraseDialogFragment.java | 4 ++-- OpenKeychain/src/main/res/values/strings.xml | 5 +++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 9b868c859..fb1da28fc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -60,8 +60,8 @@ public class PassphraseCacheService extends Service { + "PASSPHRASE_CACHE_ADD"; public static final String ACTION_PASSPHRASE_CACHE_GET = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_GET"; - public static final String ACTION_PASSPHRASE_CACHE_PURGE = Constants.INTENT_PREFIX - + "PASSPHRASE_CACHE_PURGE"; + public static final String ACTION_PASSPHRASE_CACHE_CLEAR = Constants.INTENT_PREFIX + + "PASSPHRASE_CACHE_CLEAR"; public static final String BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_BROADCAST"; @@ -177,7 +177,7 @@ public class PassphraseCacheService extends Service { if (cachedPassphrase == null) { return null; } - addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase, "Password"); + addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase, getString(R.string.passp_cache_notif_pwd)); return cachedPassphrase; } @@ -309,7 +309,7 @@ public class PassphraseCacheService extends Service { } catch (RemoteException e) { Log.e(Constants.TAG, "PassphraseCacheService Sending message failed", e); } - } else if (ACTION_PASSPHRASE_CACHE_PURGE.equals(intent.getAction())) { + } else if (ACTION_PASSPHRASE_CACHE_CLEAR.equals(intent.getAction())) { AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); // Stop all ttl alarms @@ -375,10 +375,10 @@ public class PassphraseCacheService extends Service { // Add purging action Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); builder.addAction( R.drawable.abc_ic_clear_normal, - getString(R.string.passp_cache_notif_purge), + getString(R.string.passp_cache_notif_clear), PendingIntent.getService( getApplicationContext(), 0, @@ -393,10 +393,10 @@ public class PassphraseCacheService extends Service { builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))) - .setContentText(getString(R.string.passp_cache_notif_click_to_purge)); + .setContentText(getString(R.string.passp_cache_notif_click_to_clear)); Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); builder.setContentIntent( PendingIntent.getService( @@ -444,8 +444,8 @@ public class PassphraseCacheService extends Service { private final IBinder mBinder = new PassphraseCacheBinder(); public class CachedPassphrase { - private String primaryUserID = ""; - private String passphrase = ""; + private String primaryUserID; + private String passphrase; public CachedPassphrase(String passphrase, String primaryUserID) { setPassphrase(passphrase); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 5bbbf20a7..4d0b73d30 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -190,7 +190,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // Early breakout if we are dealing with a symmetric key if (secretRing == null) { PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, - passphrase, "Password"); + passphrase, getString(R.string.passp_cache_notif_pwd)); // also return passphrase back to activity Bundle data = new Bundle(); data.putString(MESSAGE_DATA_PASSPHRASE, passphrase); @@ -234,7 +234,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase, secretRing.getPrimaryUserId()); } catch(PgpGeneralException e) { - Log.e(Constants.TAG, e.getMessage()); + Log.e(Constants.TAG, "adding of a passhrase failed", e); } if (unlockedSecretKey.getKeyId() != masterKeyId) { diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 456a22086..1adecad01 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -661,10 +661,11 @@ Unlocking keyring - Click to purge cached passphrases + Click to clear cached passphrases OpenKeychain has cached %i keys Cached Keys: - Purge Cache + Clear Cache + Password Certifier -- cgit v1.2.3 From 066591dab1efa9d0bd75056fe06ecd6d8278fefa Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 18:53:52 +0200 Subject: Wello there, That's Java, not C^^ --- OpenKeychain/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 1adecad01..5f04aa6e6 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -662,7 +662,7 @@ Click to clear cached passphrases - OpenKeychain has cached %i keys + OpenKeychain has cached %d keys Cached Keys: Clear Cache Password -- cgit v1.2.3 From bd909375c2cac5f1b50838a9591eebbddc007b61 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 19:24:51 +0200 Subject: Fixed misplaced bracket --- .../org/sufficientlysecure/keychain/service/PassphraseCacheService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index fb1da28fc..72c1a8f67 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -359,7 +359,7 @@ public class PassphraseCacheService extends Service { builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle(getString(R.string.app_name)) - .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))); + .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys), mPassphraseCache.size())); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); -- cgit v1.2.3 From 45dfb3974908d953ff656fb69696f69c0af017c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 12 Jul 2014 20:39:23 +0200 Subject: more work on edit key --- .../keychain/ui/EditKeyFragment.java | 42 ++++- .../ui/dialog/ChangeExpiryDialogFragment.java | 187 +++++++++++++++++++++ 2 files changed, 222 insertions(+), 7 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 78ccafcbc..94ca883d6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -54,6 +54,7 @@ import org.sufficientlysecure.keychain.ui.adapter.SubkeysAdapter; import org.sufficientlysecure.keychain.ui.adapter.SubkeysAddedAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAddedAdapter; +import org.sufficientlysecure.keychain.ui.dialog.ChangeExpiryDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.EditSubkeyDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.EditUserIdDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; @@ -61,6 +62,7 @@ import org.sufficientlysecure.keychain.ui.dialog.SetPassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; +import java.util.Date; public class EditKeyFragment extends LoaderFragment implements LoaderManager.LoaderCallbacks { @@ -357,13 +359,7 @@ public class EditKeyFragment extends LoaderFragment implements public void handleMessage(Message message) { switch (message.what) { case EditSubkeyDialogFragment.MESSAGE_CHANGE_EXPIRY: - // toggle -// if (mSaveKeyringParcel.changePrimaryUserId != null -// && mSaveKeyringParcel.changePrimaryUserId.equals(userId)) { -// mSaveKeyringParcel.changePrimaryUserId = null; -// } else { -// mSaveKeyringParcel.changePrimaryUserId = userId; -// } + editSubkeyExpiry(keyId); break; case EditSubkeyDialogFragment.MESSAGE_REVOKE: // toggle @@ -391,6 +387,38 @@ public class EditKeyFragment extends LoaderFragment implements }); } + private void editSubkeyExpiry(final long keyId) { + Handler returnHandler = new Handler() { + @Override + public void handleMessage(Message message) { + switch (message.what) { + case ChangeExpiryDialogFragment.MESSAGE_NEW_EXPIRY_DATE: + // toggle +// if (mSaveKeyringParcel.changePrimaryUserId != null +// && mSaveKeyringParcel.changePrimaryUserId.equals(userId)) { +// mSaveKeyringParcel.changePrimaryUserId = null; +// } else { +// mSaveKeyringParcel.changePrimaryUserId = userId; +// } + break; + } + getLoaderManager().getLoader(LOADER_ID_SUBKEYS).forceLoad(); + } + }; + + // Create a new Messenger for the communication back + final Messenger messenger = new Messenger(returnHandler); + + DialogFragmentWorkaround.INTERFACE.runnableRunDelayed(new Runnable() { + public void run() { + ChangeExpiryDialogFragment dialogFragment = + ChangeExpiryDialogFragment.newInstance(messenger, new Date(), new Date()); + + dialogFragment.show(getActivity().getSupportFragmentManager(), "editSubkeyExpiryDialog"); + } + }); + } + private void addUserId() { mUserIdsAddedAdapter.add(new UserIdsAddedAdapter.UserIdModel()); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java new file mode 100644 index 000000000..d5354a9f6 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java @@ -0,0 +1,187 @@ +/* + * 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.ui.dialog; + +import android.app.DatePickerDialog; +import android.app.Dialog; +import android.content.Context; +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 android.text.format.DateUtils; +import android.widget.DatePicker; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +public class ChangeExpiryDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_CREATION_DATE = "creation_date"; + private static final String ARG_EXPIRY_DATE = "expiry_date"; + + public static final int MESSAGE_NEW_EXPIRY_DATE = 1; + public static final String MESSAGE_DATA_EXPIRY_DATE = "expiry_date"; + + private Messenger mMessenger; + private Calendar mCreationCal; + private Calendar mExpiryCal; + + private int mDatePickerResultCount = 0; + private DatePickerDialog.OnDateSetListener mExpiryDateSetListener = + new DatePickerDialog.OnDateSetListener() { + public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { + // Note: Ignore results after the first one - android sends multiples. + if (mDatePickerResultCount++ == 0) { + Calendar selectedCal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + selectedCal.set(year, monthOfYear, dayOfMonth); + if (mExpiryCal != null) { + long numDays = (selectedCal.getTimeInMillis() / 86400000) + - (mExpiryCal.getTimeInMillis() / 86400000); + if (numDays > 0) { + Bundle data = new Bundle(); + data.putSerializable(MESSAGE_DATA_EXPIRY_DATE, selectedCal.getTime()); + sendMessageToHandler(MESSAGE_NEW_EXPIRY_DATE, data); + } + } else { + Bundle data = new Bundle(); + data.putSerializable(MESSAGE_DATA_EXPIRY_DATE, selectedCal.getTime()); + sendMessageToHandler(MESSAGE_NEW_EXPIRY_DATE, data); + } + } + } + }; + + public class ExpiryDatePickerDialog extends DatePickerDialog { + + public ExpiryDatePickerDialog(Context context, OnDateSetListener callBack, + int year, int monthOfYear, int dayOfMonth) { + super(context, callBack, year, monthOfYear, dayOfMonth); + } + + // set permanent title + public void setTitle(CharSequence title) { + super.setTitle(getContext().getString(R.string.expiry_date_dialog_title)); + } + } + + /** + * Creates new instance of this dialog fragment + */ + public static ChangeExpiryDialogFragment newInstance(Messenger messenger, + Date creationDate, Date expiryDate) { + ChangeExpiryDialogFragment frag = new ChangeExpiryDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + args.putSerializable(ARG_CREATION_DATE, creationDate); + args.putSerializable(ARG_EXPIRY_DATE, expiryDate); + + frag.setArguments(args); + + return frag; + } + + /** + * Creates dialog + */ + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + mMessenger = getArguments().getParcelable(ARG_MESSENGER); + Date creationDate = (Date) getArguments().getSerializable(ARG_CREATION_DATE); + Date expiryDate = (Date) getArguments().getSerializable(ARG_EXPIRY_DATE); + + mCreationCal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + mCreationCal.setTime(creationDate); + mExpiryCal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + mExpiryCal.setTime(expiryDate); + + /* + * Using custom DatePickerDialog which overrides the setTitle because + * the DatePickerDialog title is buggy (unix warparound bug). + * See: https://code.google.com/p/android/issues/detail?id=49066 + */ + DatePickerDialog dialog = new ExpiryDatePickerDialog(getActivity(), + mExpiryDateSetListener, mExpiryCal.get(Calendar.YEAR), mExpiryCal.get(Calendar.MONTH), + mExpiryCal.get(Calendar.DAY_OF_MONTH)); + mDatePickerResultCount = 0; + dialog.setCancelable(true); + dialog.setButton(Dialog.BUTTON_NEGATIVE, + getActivity().getString(R.string.btn_no_date), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // Note: Ignore results after the first one - android sends multiples. + if (mDatePickerResultCount++ == 0) { + // none expiry dates corresponds to a null message + Bundle data = new Bundle(); + data.putSerializable(MESSAGE_DATA_EXPIRY_DATE, null); + sendMessageToHandler(MESSAGE_NEW_EXPIRY_DATE, data); + } + } + } + ); + + // setCalendarViewShown() is supported from API 11 onwards. + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { + // Hide calendarView in tablets because of the unix warparound bug. + dialog.getDatePicker().setCalendarViewShown(false); + } + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { + // will crash with IllegalArgumentException if we set a min date + // that is not before expiry + if (mCreationCal != null && mCreationCal.before(mExpiryCal)) { + dialog.getDatePicker().setMinDate(mCreationCal.getTime().getTime() + + DateUtils.DAY_IN_MILLIS); + } else { + // When created date isn't available + dialog.getDatePicker().setMinDate(mExpiryCal.getTime().getTime() + + DateUtils.DAY_IN_MILLIS); + } + } + + return dialog; + } + + /** + * Send message back to handler which is initialized in a activity + * + * @param what Message integer you want to send + */ + private void sendMessageToHandler(Integer what, Bundle data) { + Message msg = Message.obtain(); + msg.what = what; + if (data != null) { + msg.setData(data); + } + + try { + mMessenger.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); + } + } +} -- cgit v1.2.3 From 0ce9c131327ce754eb8c0c934ea59eecf005a26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 12 Jul 2014 21:07:29 +0200 Subject: Fix strings from 'keys' to 'passphrases' --- OpenKeychain/src/main/res/values/strings.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 5f04aa6e6..aec4ecd2c 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -662,8 +662,8 @@ Click to clear cached passphrases - OpenKeychain has cached %d keys - Cached Keys: + OpenKeychain has cached %d passphrases + Cached Passphrases: Clear Cache Password -- cgit v1.2.3