From 8a8d7c7738f62eb66e41f7598a5e23a50b2f0821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 9 Sep 2013 19:38:13 +0200 Subject: Handle dublicate or missing pub keys corresponding to user ids, handle navigating back from service activity properly --- .../keychain/remote_api/CryptoService.java | 54 ++++++++------ .../keychain/remote_api/CryptoServiceActivity.java | 82 ++++++++++++++++++++-- 2 files changed, 108 insertions(+), 28 deletions(-) (limited to 'OpenPGP-Keychain/src/org') diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java index ae0e55671..8ff229af0 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java @@ -66,6 +66,17 @@ public class CryptoService extends Service { final Object userInputLock = new Object(); + private class MyBaseCallback implements Handler.Callback { + public static final int OKAY = 1; + public static final int CANCEL = 0; + + @Override + public boolean handleMessage(Message msg) { + return false; + } + + } + @Override public void onCreate() { super.onCreate(); @@ -116,9 +127,7 @@ public class CryptoService extends Service { return passphrase; } - public class PassphraseActivityCallback implements Handler.Callback { - public static final int SUCCESS = 1; - public static final int NO_SUCCESS = 0; + public class PassphraseActivityCallback extends MyBaseCallback { private boolean success = false; @@ -128,7 +137,7 @@ public class CryptoService extends Service { @Override public boolean handleMessage(Message msg) { - if (msg.arg1 == SUCCESS) { + if (msg.arg1 == OKAY) { success = true; } else { success = false; @@ -151,9 +160,13 @@ public class CryptoService extends Service { */ private long[] getKeyIdsFromEmails(String[] encryptionUserIds, long ownKeyId) { // find key ids to given emails in database - boolean manySameUserIds = false; - boolean missingUserIds = false; ArrayList keyIds = new ArrayList(); + + boolean missingUserIdsCheck = false; + boolean dublicateUserIdsCheck = false; + ArrayList missingUserIds = new ArrayList(); + ArrayList dublicateUserIds = new ArrayList(); + for (String email : encryptionUserIds) { Uri uri = KeychainContract.KeyRings.buildPublicKeyRingsByEmailsUri(email); Cursor cur = getContentResolver().query(uri, null, null, null, null); @@ -161,11 +174,13 @@ public class CryptoService extends Service { long id = cur.getLong(cur.getColumnIndex(KeychainContract.KeyRings.MASTER_KEY_ID)); keyIds.add(id); } else { - missingUserIds = true; + missingUserIdsCheck = true; + missingUserIds.add(email); Log.d(Constants.TAG, "user id missing"); } if (cur.moveToNext()) { - manySameUserIds = true; + dublicateUserIdsCheck = true; + dublicateUserIds.add(email); Log.d(Constants.TAG, "more than one user id with the same email"); } } @@ -179,12 +194,16 @@ public class CryptoService extends Service { keyIdsArray[i] = keyIds.get(i); } - if (missingUserIds || manySameUserIds) { + if (missingUserIdsCheck || dublicateUserIdsCheck) { SelectPubKeysActivityCallback callback = new SelectPubKeysActivityCallback(); Messenger messenger = new Messenger(new Handler(getMainLooper(), callback)); Bundle extras = new Bundle(); extras.putLongArray(CryptoServiceActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray); + extras.putStringArrayList(CryptoServiceActivity.EXTRA_MISSING_USER_IDS, missingUserIds); + extras.putStringArrayList(CryptoServiceActivity.EXTRA_DUBLICATE_USER_IDS, + dublicateUserIds); + pauseQueueAndStartServiceActivity(CryptoServiceActivity.ACTION_SELECT_PUB_KEYS, messenger, extras); @@ -199,9 +218,7 @@ public class CryptoService extends Service { return keyIdsArray; } - public class SelectPubKeysActivityCallback implements Handler.Callback { - public static final int OKAY = 1; - public static final int CANCEL = 0; + public class SelectPubKeysActivityCallback extends MyBaseCallback { public static final String PUB_KEY_IDS = "pub_key_ids"; private boolean newSelection = false; @@ -540,17 +557,14 @@ public class CryptoService extends Service { if (callback.isAllowed()) { mThreadPool.execute(r); + Log.d(Constants.TAG, "Enqueued runnable…"); } else { Log.d(Constants.TAG, "User disallowed app!"); } - - Log.d(Constants.TAG, "Enqueued runnable…"); } } - public class RegisterActivityCallback implements Handler.Callback { - public static final int ALLOW = 1; - public static final int DISALLOW = 0; + public class RegisterActivityCallback extends MyBaseCallback { public static final String PACKAGE_NAME = "package_name"; private boolean allowed = false; @@ -566,9 +580,7 @@ public class CryptoService extends Service { @Override public boolean handleMessage(Message msg) { - Log.d(Constants.TAG, "msg what: " + msg.what); - - if (msg.arg1 == ALLOW) { + if (msg.arg1 == OKAY) { allowed = true; packageName = msg.getData().getString(PACKAGE_NAME); @@ -591,7 +603,7 @@ public class CryptoService extends Service { } mThreadPool.resume(); } - return false; + return true; } } diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java index 06e558659..3360388b8 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java @@ -17,6 +17,9 @@ package org.sufficientlysecure.keychain.remote_api; +import java.util.ArrayList; + +import org.sufficientlysecure.htmltextview.HtmlTextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Id; import org.sufficientlysecure.keychain.R; @@ -48,9 +51,13 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { public static final String EXTRA_MESSENGER = "messenger"; - public static final String EXTRA_SECRET_KEY_ID = "secretKeyId"; - public static final String EXTRA_PACKAGE_NAME = "packageName"; - public static final String EXTRA_SELECTED_MASTER_KEY_IDS = "masterKeyIds"; + public static final String EXTRA_SECRET_KEY_ID = "secret_key_id"; + public static final String EXTRA_PACKAGE_NAME = "package_name"; + + // select activity + public static final String EXTRA_SELECTED_MASTER_KEY_IDS = "master_key_ids"; + public static final String EXTRA_MISSING_USER_IDS = "missing_user_ids"; + public static final String EXTRA_DUBLICATE_USER_IDS = "dublicate_user_ids"; private Messenger mMessenger; @@ -59,6 +66,9 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { // select pub key view private SelectPublicKeyFragment mSelectFragment; + // has the user clicked one of the buttons? + private boolean finishHandled; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -66,7 +76,24 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { handleActions(getIntent(), savedInstanceState); } + @Override + protected void onStop() { + super.onStop(); + + if (!finishHandled) { + Message msg = Message.obtain(); + msg.arg1 = CryptoService.RegisterActivityCallback.CANCEL; + try { + mMessenger.send(msg); + } catch (RemoteException e) { + Log.e(Constants.TAG, "CryptoServiceActivity", e); + } + } + } + protected void handleActions(Intent intent, Bundle savedInstanceState) { + finishHandled = false; + String action = intent.getAction(); Bundle extras = intent.getExtras(); @@ -99,7 +126,7 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { mSettingsFragment.getAppSettings()); Message msg = Message.obtain(); - msg.arg1 = CryptoService.RegisterActivityCallback.ALLOW; + msg.arg1 = CryptoService.RegisterActivityCallback.OKAY; Bundle data = new Bundle(); data.putString(CryptoService.RegisterActivityCallback.PACKAGE_NAME, packageName); @@ -109,6 +136,8 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoServiceActivity", e); } + + finishHandled = true; finish(); } } @@ -118,12 +147,14 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { // Disallow Message msg = Message.obtain(); - msg.arg1 = CryptoService.RegisterActivityCallback.DISALLOW; + msg.arg1 = CryptoService.RegisterActivityCallback.CANCEL; try { mMessenger.send(msg); } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoServiceActivity", e); } + + finishHandled = true; finish(); } }); @@ -141,6 +172,33 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { showPassphraseDialog(secretKeyId); } else if (ACTION_SELECT_PUB_KEYS.equals(action)) { long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS); + ArrayList missingUserIds = intent + .getStringArrayListExtra(EXTRA_MISSING_USER_IDS); + ArrayList dublicateUserIds = intent + .getStringArrayListExtra(EXTRA_DUBLICATE_USER_IDS); + + String text = new String(); + text += "" + getString(R.string.api_select_pub_keys_text) + ""; + text += "

"; + if (missingUserIds != null && missingUserIds.size() > 0) { + text += getString(R.string.api_select_pub_keys_missing_text); + text += "
"; + text += "
    "; + for (String userId : missingUserIds) { + text += "
  • " + userId + "
  • "; + } + text += "
"; + text += "
"; + } + if (dublicateUserIds != null && dublicateUserIds.size() > 0) { + text += getString(R.string.api_select_pub_keys_dublicates_text); + text += "
"; + text += "
    "; + for (String userId : dublicateUserIds) { + text += "
  • " + userId + "
  • "; + } + text += "
"; + } // Inflate a "Done"/"Cancel" custom action bar view ActionBarHelper.setDoneCancelView(getSupportActionBar(), R.string.btn_okay, @@ -161,6 +219,8 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoServiceActivity", e); } + + finishHandled = true; finish(); } }, R.string.btn_doNotSave, new View.OnClickListener() { @@ -176,12 +236,18 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoServiceActivity", e); } + + finishHandled = true; finish(); } }); setContentView(R.layout.api_app_select_pub_keys_activity); + // set text on view + HtmlTextView textView = (HtmlTextView) findViewById(R.id.api_select_pub_keys_text); + textView.setHtmlFromString(text); + /* Load select pub keys fragment */ // Check that the activity is using the layout version with // the fragment_container FrameLayout @@ -220,7 +286,7 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { public void handleMessage(Message message) { if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { Message msg = Message.obtain(); - msg.arg1 = CryptoService.PassphraseActivityCallback.SUCCESS; + msg.arg1 = CryptoService.PassphraseActivityCallback.OKAY; try { mMessenger.send(msg); } catch (RemoteException e) { @@ -228,13 +294,15 @@ public class CryptoServiceActivity extends SherlockFragmentActivity { } } else { Message msg = Message.obtain(); - msg.arg1 = CryptoService.PassphraseActivityCallback.NO_SUCCESS; + msg.arg1 = CryptoService.PassphraseActivityCallback.CANCEL; try { mMessenger.send(msg); } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoServiceActivity", e); } } + + finishHandled = true; finish(); } }; -- cgit v1.2.3