From 6d7a9ec48a6517adfe94ed2edfa875def538d088 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 28 Jun 2014 20:53:37 +0200 Subject: [PATCH] pass import results through to viewkeyactivity on update --- .../keychain/provider/ProviderHelper.java | 4 +- .../keychain/service/OperationResults.java | 93 ++++++++++++++++++ .../keychain/ui/ImportKeysActivity.java | 97 ++++--------------- .../keychain/ui/ViewKeyActivity.java | 8 +- .../src/main/res/layout/view_key_activity.xml | 6 ++ 5 files changed, 125 insertions(+), 83 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 3656d590f..28495d51d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -686,7 +686,7 @@ public class ProviderHelper { if (Arrays.hashCode(publicRing.getEncoded()) == Arrays.hashCode(oldPublicRing.getEncoded())) { log(LogLevel.OK, LogType.MSG_IP_SUCCESS_IDENTICAL); - return new SaveKeyringResult(SaveKeyringResult.RESULT_OK, mLog); + return new SaveKeyringResult(SaveKeyringResult.UPDATED, mLog); } } catch (NotFoundException e) { // Not an issue, just means we are dealing with a new keyring. @@ -771,7 +771,7 @@ public class ProviderHelper { == Arrays.hashCode(oldSecretRing.getEncoded())) { log(LogLevel.OK, LogType.MSG_IS_SUCCESS_IDENTICAL, PgpKeyHelper.convertKeyIdToHex(masterKeyId) ); - return new SaveKeyringResult(SaveKeyringResult.RESULT_OK, mLog); + return new SaveKeyringResult(SaveKeyringResult.UPDATED, mLog); } } catch (NotFoundException e) { // Not an issue, just means we are dealing with a new keyring diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java index 6c44b01f1..e5d06ccbb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -1,6 +1,19 @@ package org.sufficientlysecure.keychain.service; +import android.app.Activity; +import android.content.Intent; import android.os.Parcel; +import android.os.Parcelable; +import android.view.View; + +import com.github.johnpersano.supertoasts.SuperCardToast; +import com.github.johnpersano.supertoasts.SuperToast; +import com.github.johnpersano.supertoasts.util.OnClickWrapper; +import com.github.johnpersano.supertoasts.util.Style; + +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.LogDisplayActivity; +import org.sufficientlysecure.keychain.ui.LogDisplayFragment; public abstract class OperationResults { @@ -67,6 +80,86 @@ public abstract class OperationResults { } }; + public void displayToast(final Activity activity) { + + int resultType = getResult(); + + String str; + int duration, color; + + // Not an overall failure + if ((resultType & ImportResult.RESULT_ERROR) == 0) { + String withWarnings; + + // Any warnings? + if ((resultType & ImportResult.RESULT_WITH_WARNINGS) > 0) { + duration = 0; + color = Style.ORANGE; + withWarnings = activity.getResources().getString(R.string.import_with_warnings); + } else { + duration = SuperToast.Duration.LONG; + color = Style.GREEN; + withWarnings = ""; + } + + // New and updated keys + if (this.isOkBoth()) { + str = activity.getResources().getQuantityString( + R.plurals.import_keys_added_and_updated_1, mNewKeys, mNewKeys); + str += activity.getResources().getQuantityString( + R.plurals.import_keys_added_and_updated_2, mUpdatedKeys, mUpdatedKeys, withWarnings); + } else if (isOkUpdated()) { + str = activity.getResources().getQuantityString( + R.plurals.import_keys_updated, mUpdatedKeys, mUpdatedKeys, withWarnings); + } else if (isOkNew()) { + str = activity.getResources().getQuantityString( + R.plurals.import_keys_added, mNewKeys, mNewKeys, withWarnings); + } else { + duration = 0; + color = Style.RED; + str = "internal error"; + } + + } else { + duration = 0; + color = Style.RED; + if (isFailNothing()) { + str = activity.getString(R.string.import_error_nothing); + } else { + str = activity.getString(R.string.import_error); + } + } + + boolean button = getLog() != null && !getLog().isEmpty(); + SuperCardToast toast = new SuperCardToast(activity, + button ? SuperToast.Type.BUTTON : SuperToast.Type.STANDARD, + Style.getStyle(color, SuperToast.Animations.POPUP)); + toast.setText(str); + toast.setDuration(duration); + toast.setIndeterminate(duration == 0); + toast.setSwipeToDismiss(true); + // If we have a log and it's non-empty, show a View Log button + if (button) { + toast.setButtonIcon(R.drawable.ic_action_view_as_list, + activity.getResources().getString(R.string.import_view_log)); + toast.setButtonTextColor(activity.getResources().getColor(R.color.black)); + toast.setTextColor(activity.getResources().getColor(R.color.black)); + toast.setOnClickWrapper(new OnClickWrapper("supercardtoast", + new SuperToast.OnClickListener() { + @Override + public void onClick(View view, Parcelable token) { + Intent intent = new Intent( + activity, LogDisplayActivity.class); + intent.putExtra(LogDisplayFragment.EXTRA_RESULT, ImportResult.this); + activity.startActivity(intent); + } + } + )); + } + toast.show(); + + } + } public static class SaveKeyringResult extends OperationResultParcel { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 81061501e..0d8c8233a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -65,6 +65,8 @@ public class ImportKeysActivity extends ActionBarActivity { + "IMPORT_KEY_FROM_QR_CODE"; public static final String ACTION_IMPORT_KEY_FROM_KEYSERVER = Constants.INTENT_PREFIX + "IMPORT_KEY_FROM_KEYSERVER"; + public static final String ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT = + Constants.INTENT_PREFIX + "IMPORT_KEY_FROM_KEY_SERVER_AND_RETURN_RESULT"; public static final String ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN = Constants.INTENT_PREFIX + "IMPORT_KEY_FROM_KEY_SERVER_AND_RETURN"; public static final String ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN = Constants.INTENT_PREFIX @@ -78,6 +80,8 @@ public class ImportKeysActivity extends ActionBarActivity { public static final String ACTION_IMPORT_KEY_FROM_NFC = Constants.INTENT_PREFIX + "IMPORT_KEY_FROM_NFC"; + public static final String EXTRA_RESULT = "result"; + // only used by ACTION_IMPORT_KEY public static final String EXTRA_KEY_BYTES = "key_bytes"; @@ -168,7 +172,8 @@ public class ImportKeysActivity extends ActionBarActivity { startListFragment(savedInstanceState, importData, null, null); } } else if (ACTION_IMPORT_KEY_FROM_KEYSERVER.equals(action) - || ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(action)) { + || ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(action) + || ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(action)) { // only used for OpenPgpService if (extras.containsKey(EXTRA_PENDING_INTENT_DATA)) { @@ -436,92 +441,26 @@ public class ImportKeysActivity extends ActionBarActivity { if (result == null) { return; } - int resultType = result.getResult(); - String str; - int duration, color; - - // Not an overall failure - if ((resultType & ImportResult.RESULT_ERROR) == 0) { - String withWarnings; - - // Any warnings? - if ((resultType & ImportResult.RESULT_WITH_WARNINGS) > 0) { - duration = 0; - color = Style.ORANGE; - withWarnings = getResources().getString(R.string.import_with_warnings); - } else { - duration = SuperToast.Duration.LONG; - color = Style.GREEN; - withWarnings = ""; - } - - // New and updated keys - if (result.isOkBoth()) { - str = getResources().getQuantityString( - R.plurals.import_keys_added_and_updated_1, result.mNewKeys, result.mNewKeys); - str += getResources().getQuantityString( - R.plurals.import_keys_added_and_updated_2, result.mUpdatedKeys, result.mUpdatedKeys, withWarnings); - } else if (result.isOkUpdated()) { - str = getResources().getQuantityString( - R.plurals.import_keys_updated, result.mUpdatedKeys, result.mUpdatedKeys, withWarnings); - } else if (result.isOkNew()) { - str = getResources().getQuantityString( - R.plurals.import_keys_added, result.mNewKeys, result.mNewKeys, withWarnings); - } else { - duration = 0; - color = Style.RED; - str = "internal error"; - } - - } else { - duration = 0; - color = Style.RED; - if (result.isFailNothing()) { - str = getString(R.string.import_error_nothing); - } else { - str = getString(R.string.import_error); - } + if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction())) { + Intent intent = new Intent(); + intent.putExtra(EXTRA_RESULT, result); + ImportKeysActivity.this.setResult(RESULT_OK, intent); + ImportKeysActivity.this.finish(); + return; } - - SuperCardToast toast = new SuperCardToast(ImportKeysActivity.this, - SuperToast.Type.BUTTON, Style.getStyle(color, SuperToast.Animations.POPUP)); - toast.setText(str); - toast.setDuration(duration); - toast.setIndeterminate(duration == 0); - toast.setSwipeToDismiss(true); - toast.setButtonIcon(R.drawable.ic_action_view_as_list, - getResources().getString(R.string.import_view_log)); - toast.setButtonTextColor(getResources().getColor(R.color.black)); - toast.setTextColor(getResources().getColor(R.color.black)); - toast.setOnClickWrapper(new OnClickWrapper("supercardtoast", - new SuperToast.OnClickListener() { - @Override - public void onClick(View view, Parcelable token) { - Intent intent = new Intent( - ImportKeysActivity.this, LogDisplayActivity.class); - intent.putExtra(LogDisplayFragment.EXTRA_RESULT, result); - startActivity(intent); - } - } - )); - toast.show(); - - /* - if (bad > 0) { - BadImportKeyDialogFragment badImportKeyDialogFragment = - BadImportKeyDialogFragment.newInstance(bad); - badImportKeyDialogFragment.show(getSupportFragmentManager(), "badKeyDialog"); - } - */ - if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(getIntent().getAction())) { ImportKeysActivity.this.setResult(RESULT_OK, mPendingIntentData); ImportKeysActivity.this.finish(); - } else if (ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) { + return; + } + if (ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) { ImportKeysActivity.this.setResult(RESULT_OK); ImportKeysActivity.this.finish(); + return; } + + result.displayToast(ImportKeysActivity.this); } } }; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 1912b6e7d..8cc587b10 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -55,6 +55,7 @@ import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.service.OperationResults.ImportResult; import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter; import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout.TabColorizer; import org.sufficientlysecure.keychain.util.Log; @@ -331,7 +332,7 @@ public class ViewKeyActivity extends ActionBarActivity implements String fingerprint = PgpKeyHelper.convertFingerprintToHex(blob); Intent queryIntent = new Intent(this, ImportKeysActivity.class); - queryIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN); + queryIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT); queryIntent.putExtra(ImportKeysActivity.EXTRA_FINGERPRINT, fingerprint); startActivityForResult(queryIntent, REQUEST_CODE_LOOKUP_KEY); @@ -355,7 +356,10 @@ public class ViewKeyActivity extends ActionBarActivity implements switch (requestCode) { case REQUEST_CODE_LOOKUP_KEY: { if (resultCode == Activity.RESULT_OK) { - // TODO: reload key??? move this into fragment? + ImportResult result = data.getParcelableExtra(ImportKeysActivity.EXTRA_RESULT); + if (result != null) { + result.displayToast(this); + } } break; } diff --git a/OpenKeychain/src/main/res/layout/view_key_activity.xml b/OpenKeychain/src/main/res/layout/view_key_activity.xml index 5aa1cd167..481b1ddf5 100644 --- a/OpenKeychain/src/main/res/layout/view_key_activity.xml +++ b/OpenKeychain/src/main/res/layout/view_key_activity.xml @@ -4,6 +4,12 @@ android:layout_height="match_parent" android:orientation="vertical"> + +