import-log: switch to flags instead of statuses for result int

This commit is contained in:
Vincent Breitmoser
2014-06-10 16:24:04 +02:00
parent e41e6ea0de
commit f38556cab1
16 changed files with 204 additions and 159 deletions

View File

@@ -33,9 +33,9 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.OperationResultParcel;
import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog;
import org.sufficientlysecure.keychain.service.OperationResults.ImportResult;
import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResult;
import org.sufficientlysecure.keychain.util.Log;
import java.io.ByteArrayOutputStream;
@@ -152,9 +152,12 @@ public class PgpImportExport {
}
}
OperationResultParcel result = mProviderHelper.savePublicKeyRing(key);
newKeys += 1;
SaveKeyringResult result = mProviderHelper.savePublicKeyRing(key);
if (result.updated()) {
newKeys += 1;
} else {
oldKeys += 1;
}
} catch (PgpGeneralException e) {
Log.e(Constants.TAG, "Encountered bad key on import!", e);
@@ -166,23 +169,26 @@ public class PgpImportExport {
}
OperationLog log = mProviderHelper.getLog();
int resultType;
// Any key imported - overall success
if (newKeys > 0 || oldKeys > 0) {
if (badKeys > 0) {
resultType = ImportResult.RESULT_PARTIAL_WITH_ERRORS;
} else if (newKeys > 0 && oldKeys > 0) {
resultType = ImportResult.RESULT_OK_BOTHKEYS;
} else if (newKeys > 0) {
resultType = ImportResult.RESULT_OK_NEWKEYS;
} else {
resultType = ImportResult.RESULT_OK_UPDATED;
}
// No keys imported, overall failure
} else if (badKeys > 0) {
resultType = ImportResult.RESULT_FAIL_ERROR;
} else {
int resultType = 0;
// special return case: no new keys at all
if (badKeys == 0 && newKeys == 0 && oldKeys == 0) {
resultType = ImportResult.RESULT_FAIL_NOTHING;
} else {
if (newKeys > 0) {
resultType |= ImportResult.RESULT_OK_NEWKEYS;
}
if (oldKeys > 0) {
resultType |= ImportResult.RESULT_OK_UPDATED;
}
if (badKeys > 0) {
resultType |= ImportResult.RESULT_WITH_ERRORS;
if (newKeys == 0 && oldKeys == 0) {
resultType |= ImportResult.RESULT_ERROR;
}
}
if (log.containsWarnings()) {
resultType |= ImportResult.RESULT_WITH_WARNINGS;
}
}
return new ImportResult(resultType, log, newKeys, oldKeys, badKeys);

View File

@@ -49,6 +49,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;
import org.sufficientlysecure.keychain.remote.AccountSettings;
import org.sufficientlysecure.keychain.remote.AppSettings;
import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResult;
import org.sufficientlysecure.keychain.util.IterableIterator;
import org.sufficientlysecure.keychain.util.Log;
@@ -267,12 +268,15 @@ public class ProviderHelper {
* Saves PGPPublicKeyRing with its keys and userIds in DB
*/
@SuppressWarnings("unchecked")
public OperationResultParcel savePublicKeyRing(UncachedKeyRing keyRing) {
public SaveKeyringResult savePublicKeyRing(UncachedKeyRing keyRing) {
if (keyRing.isSecret()) {
log(LogLevel.ERROR, LogType.MSG_IP_BAD_TYPE_SECRET);
return new OperationResultParcel(1, mLog);
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
// start with ok result
int result = SaveKeyringResult.SAVED_PUBLIC;
long masterKeyId = keyRing.getMasterKeyId();
log(LogLevel.START, LogType.MSG_IP,
new String[]{ PgpKeyHelper.convertKeyIdToHex(masterKeyId) });
@@ -296,6 +300,7 @@ public class ProviderHelper {
try {
mContentResolver.delete(KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null);
log(LogLevel.DEBUG, LogType.MSG_IP_DELETE_OLD_OK);
result |= SaveKeyringResult.UPDATED;
} catch (UnsupportedOperationException e) {
Log.e(Constants.TAG, "Key could not be deleted! Maybe we are creating a new one!", e);
log(LogLevel.DEBUG, LogType.MSG_IP_DELETE_OLD_FAIL);
@@ -315,7 +320,7 @@ public class ProviderHelper {
values.put(KeyRingData.KEY_RING_DATA, keyRing.getEncoded());
} catch (IOException e) {
log(LogLevel.ERROR, LogType.MSG_IP_ENCODE_FAIL);
return new OperationResultParcel(1, mLog);
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
Uri uri = KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId));
@@ -371,7 +376,7 @@ public class ProviderHelper {
log(LogLevel.ERROR, LogType.MSG_IP_SUBKEY_FUTURE, new String[] {
creation.toString()
});
return new OperationResultParcel(1, mLog);
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
Date expiryDate = key.getExpiryTime();
if (expiryDate != null) {
@@ -436,7 +441,7 @@ public class ProviderHelper {
if (!cert.verifySignature(masterKey, userId)) {
// Bad self certification? That's kinda bad...
log(LogLevel.ERROR, LogType.MSG_IP_UID_SELF_BAD);
return new OperationResultParcel(1, mLog);
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
// if we already have a cert..
@@ -526,17 +531,17 @@ public class ProviderHelper {
log(LogLevel.ERROR, LogType.MSG_IP_FAIL_IO_EXC);
Log.e(Constants.TAG, "IOException during import", e);
mIndent -= 1;
return new OperationResultParcel(1, mLog);
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
} catch (RemoteException e) {
log(LogLevel.ERROR, LogType.MSG_IP_FAIL_REMOTE_EX);
Log.e(Constants.TAG, "RemoteException during import", e);
mIndent -= 1;
return new OperationResultParcel(1, mLog);
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
} catch (OperationApplicationException e) {
log(LogLevel.ERROR, LogType.MSG_IP_FAIL_OP_EX);
Log.e(Constants.TAG, "OperationApplicationException during import", e);
mIndent -= 1;
return new OperationResultParcel(1, mLog);
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
// Save the saved keyring (if any)
@@ -544,12 +549,13 @@ public class ProviderHelper {
log(LogLevel.DEBUG, LogType.MSG_IP_REINSERT_SECRET);
mIndent += 1;
saveSecretKeyRing(secretRing);
result |= SaveKeyringResult.SAVED_SECRET;
mIndent -= 1;
}
log(LogLevel.OK, LogType.MSG_IP_SUCCESS);
mIndent -= 1;
return new OperationResultParcel(0, mLog);
return new SaveKeyringResult(result, mLog);
}

View File

@@ -18,12 +18,13 @@ import java.util.ArrayList;
*
*/
public class OperationResultParcel implements Parcelable {
/** Holds the overall result, the number specifying varying degrees of success.
* Values smaller than 100 are considered an overall success. */
/** Holds the overall result, the number specifying varying degrees of success. The first bit
* is 0 on overall success, 1 on overall failure. All other bits may be used for more specific
* conditions. */
final int mResult;
public static final int RESULT_OK = 0;
public static final int RESULT_ERROR = 100;
public static final int RESULT_ERROR = 1;
/// A list of log entries tied to the operation result.
final OperationLog mLog;
@@ -44,7 +45,7 @@ public class OperationResultParcel implements Parcelable {
}
public boolean isSuccessful() {
return mResult < 100;
return (mResult & 1) == 1;
}
public OperationLog getLog() {

View File

@@ -8,20 +8,31 @@ public abstract class OperationResults {
public final int mNewKeys, mUpdatedKeys, mBadKeys;
// Operation ok, at least one new key (no warnings)
public static final int RESULT_OK_NEWKEYS = 1;
// Operation ok, at least one new and one updated key (no warnings)
public static final int RESULT_OK_BOTHKEYS = 2;
// Operation ok, no new keys but upated ones (no warnings)
public static final int RESULT_OK_UPDATED = 3;
// At least one new key
public static final int RESULT_OK_NEWKEYS = 2;
// At least one updated key
public static final int RESULT_OK_UPDATED = 4;
// At least one key failed (might still be an overall success)
public static final int RESULT_WITH_ERRORS = 8;
// There are warnings in the log
public static final int RESULT_WITH_WARNINGS = 16;
// Operation partially ok, but at least one key failed!
public static final int RESULT_PARTIAL_WITH_ERRORS = 50;
// No keys to import...
public static final int RESULT_FAIL_NOTHING = 32 +1;
// Operation failed, errors thrown and no new keys imported
public static final int RESULT_FAIL_ERROR = 100;
// Operation failed, no keys to import...
public static final int RESULT_FAIL_NOTHING = 101;
public boolean isOkBoth() {
return (mResult & (RESULT_OK_NEWKEYS | RESULT_OK_UPDATED))
== (RESULT_OK_NEWKEYS | RESULT_OK_UPDATED);
}
public boolean isOkNew() {
return (mResult & RESULT_OK_NEWKEYS) > 0;
}
public boolean isOkUpdated() {
return (mResult & RESULT_OK_UPDATED) > 0;
}
public boolean isFailNothing() {
return (mResult & RESULT_FAIL_NOTHING) > 0;
}
public ImportResult(Parcel source) {
super(source);
@@ -58,4 +69,24 @@ public abstract class OperationResults {
}
public static class SaveKeyringResult extends OperationResultParcel {
public SaveKeyringResult(int result, OperationLog log) {
super(result, log);
}
// Some old key was updated
public static final int UPDATED = 2;
// Public key was saved
public static final int SAVED_PUBLIC = 8;
// Secret key was saved (not exclusive with public!)
public static final int SAVED_SECRET = 16;
public boolean updated() {
return (mResult & UPDATED) == UPDATED;
}
}
}

View File

@@ -378,55 +378,54 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O
final ImportResult result =
returnData.<ImportResult>getParcelable(KeychainIntentService.RESULT);
String str = "";
boolean hasWarnings = result.getLog().containsWarnings();
int duration = 0, color = hasWarnings ? Style.ORANGE : Style.GREEN;
String withWarnings = hasWarnings
? getResources().getString(R.string.with_warnings) : "";
int resultType = result.getResult();
switch(result.getResult()) {
case ImportResult.RESULT_OK_NEWKEYS:
if (!hasWarnings) {
duration = SuperToast.Duration.LONG;
}
str = getResources().getQuantityString(
R.plurals.keys_added, result.mNewKeys, result.mNewKeys, withWarnings);
break;
String str;
int duration, color;
case ImportResult.RESULT_OK_UPDATED:
if (!hasWarnings) {
duration = SuperToast.Duration.LONG;
}
str = getResources().getQuantityString(
R.plurals.keys_updated, result.mNewKeys, result.mNewKeys, withWarnings);
break;
// Not an overall failure
if ((resultType & ImportResult.RESULT_ERROR) == 0) {
String withWarnings;
case ImportResult.RESULT_OK_BOTHKEYS:
if (!hasWarnings) {
duration = SuperToast.Duration.LONG;
}
str = getResources().getQuantityString(
R.plurals.keys_added_and_updated_1, result.mNewKeys, result.mNewKeys);
str += getResources().getQuantityString(
R.plurals.keys_added_and_updated_2, result.mUpdatedKeys, result.mUpdatedKeys, withWarnings);
break;
case ImportResult.RESULT_PARTIAL_WITH_ERRORS:
str = "partial with errors";
// Any warnings?
if ((resultType & ImportResult.RESULT_WITH_WARNINGS) > 0) {
duration = 0;
color = Style.ORANGE;
break;
withWarnings = getResources().getString(R.string.import_with_warnings);
} else {
duration = SuperToast.Duration.LONG;
color = Style.GREEN;
withWarnings = "";
}
case ImportResult.RESULT_FAIL_ERROR:
str = "fail error";
// 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.isOkNew()) {
str = getResources().getQuantityString(
R.plurals.import_keys_added, result.mNewKeys, result.mNewKeys, withWarnings);
} else if (result.isOkUpdated()) {
str = getResources().getQuantityString(
R.plurals.import_keys_updated, result.mUpdatedKeys, result.mUpdatedKeys, withWarnings);
} else {
duration = 0;
color = Style.RED;
break;
case ImportResult.RESULT_FAIL_NOTHING:
str = getString(R.string.no_keys_added_or_updated);
color = Style.RED;
break;
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);
}
}
SuperCardToast toast = new SuperCardToast(ImportKeysActivity.this,
SuperToast.Type.BUTTON, Style.getStyle(color, SuperToast.Animations.POPUP));
toast.setText(str);
@@ -434,9 +433,9 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O
toast.setIndeterminate(duration == 0);
toast.setSwipeToDismiss(true);
toast.setButtonIcon(R.drawable.ic_action_view_as_list,
getResources().getString(R.string.view_log));
toast.setButtonTextColor(R.color.emphasis_dark);
toast.setTextColor(R.color.emphasis_dark);
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