Passthrough of data Intent in methods of ApiPendingIntentFactory instead of constructor

This commit is contained in:
Dominik Schürmann
2016-02-23 16:06:54 +01:00
parent c1d268a5ec
commit 73ce6fc7bb
3 changed files with 58 additions and 50 deletions

View File

@@ -41,25 +41,23 @@ import java.util.ArrayList;
public class ApiPendingIntentFactory { public class ApiPendingIntentFactory {
Context mContext; Context mContext;
private Intent mPendingIntentData;
public ApiPendingIntentFactory(Context context, Intent data) { public ApiPendingIntentFactory(Context context) {
mContext = context; mContext = context;
mPendingIntentData = data;
} }
PendingIntent requiredInputPi(RequiredInputParcel requiredInput, PendingIntent requiredInputPi(Intent data, RequiredInputParcel requiredInput,
CryptoInputParcel cryptoInput) { CryptoInputParcel cryptoInput) {
switch (requiredInput.mType) { switch (requiredInput.mType) {
case NFC_MOVE_KEY_TO_CARD: case NFC_MOVE_KEY_TO_CARD:
case NFC_DECRYPT: case NFC_DECRYPT:
case NFC_SIGN: { case NFC_SIGN: {
return createNfcOperationPendingIntent(requiredInput, cryptoInput); return createNfcOperationPendingIntent(data, requiredInput, cryptoInput);
} }
case PASSPHRASE: { case PASSPHRASE: {
return createPassphrasePendingIntent(requiredInput, cryptoInput); return createPassphrasePendingIntent(data, requiredInput, cryptoInput);
} }
default: default:
@@ -67,25 +65,25 @@ public class ApiPendingIntentFactory {
} }
} }
private PendingIntent createNfcOperationPendingIntent(RequiredInputParcel requiredInput, CryptoInputParcel cryptoInput) { private PendingIntent createNfcOperationPendingIntent(Intent data, RequiredInputParcel requiredInput, CryptoInputParcel cryptoInput) {
Intent intent = new Intent(mContext, RemoteSecurityTokenOperationActivity.class); Intent intent = new Intent(mContext, RemoteSecurityTokenOperationActivity.class);
// pass params through to activity that it can be returned again later to repeat pgp operation // pass params through to activity that it can be returned again later to repeat pgp operation
intent.putExtra(RemoteSecurityTokenOperationActivity.EXTRA_REQUIRED_INPUT, requiredInput); intent.putExtra(RemoteSecurityTokenOperationActivity.EXTRA_REQUIRED_INPUT, requiredInput);
intent.putExtra(RemoteSecurityTokenOperationActivity.EXTRA_CRYPTO_INPUT, cryptoInput); intent.putExtra(RemoteSecurityTokenOperationActivity.EXTRA_CRYPTO_INPUT, cryptoInput);
return createInternal(intent); return createInternal(data, intent);
} }
private PendingIntent createPassphrasePendingIntent(RequiredInputParcel requiredInput, CryptoInputParcel cryptoInput) { private PendingIntent createPassphrasePendingIntent(Intent data, RequiredInputParcel requiredInput, CryptoInputParcel cryptoInput) {
Intent intent = new Intent(mContext, RemotePassphraseDialogActivity.class); Intent intent = new Intent(mContext, RemotePassphraseDialogActivity.class);
// pass params through to activity that it can be returned again later to repeat pgp operation // pass params through to activity that it can be returned again later to repeat pgp operation
intent.putExtra(RemotePassphraseDialogActivity.EXTRA_REQUIRED_INPUT, requiredInput); intent.putExtra(RemotePassphraseDialogActivity.EXTRA_REQUIRED_INPUT, requiredInput);
intent.putExtra(RemotePassphraseDialogActivity.EXTRA_CRYPTO_INPUT, cryptoInput); intent.putExtra(RemotePassphraseDialogActivity.EXTRA_CRYPTO_INPUT, cryptoInput);
return createInternal(intent); return createInternal(data, intent);
} }
PendingIntent createSelectPublicKeyPendingIntent(long[] keyIdsArray, ArrayList<String> missingEmails, PendingIntent createSelectPublicKeyPendingIntent(Intent data, long[] keyIdsArray, ArrayList<String> missingEmails,
ArrayList<String> duplicateEmails, boolean noUserIdsCheck) { ArrayList<String> duplicateEmails, boolean noUserIdsCheck) {
Intent intent = new Intent(mContext, RemoteSelectPubKeyActivity.class); Intent intent = new Intent(mContext, RemoteSelectPubKeyActivity.class);
intent.putExtra(RemoteSelectPubKeyActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray); intent.putExtra(RemoteSelectPubKeyActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray);
@@ -93,58 +91,58 @@ public class ApiPendingIntentFactory {
intent.putExtra(RemoteSelectPubKeyActivity.EXTRA_MISSING_EMAILS, missingEmails); intent.putExtra(RemoteSelectPubKeyActivity.EXTRA_MISSING_EMAILS, missingEmails);
intent.putExtra(RemoteSelectPubKeyActivity.EXTRA_DUPLICATE_EMAILS, duplicateEmails); intent.putExtra(RemoteSelectPubKeyActivity.EXTRA_DUPLICATE_EMAILS, duplicateEmails);
return createInternal(intent); return createInternal(data, intent);
} }
PendingIntent createImportFromKeyserverPendingIntent(long masterKeyId) { PendingIntent createImportFromKeyserverPendingIntent(Intent data, long masterKeyId) {
Intent intent = new Intent(mContext, RemoteImportKeysActivity.class); Intent intent = new Intent(mContext, RemoteImportKeysActivity.class);
intent.setAction(RemoteImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT); intent.setAction(RemoteImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT);
intent.putExtra(RemoteImportKeysActivity.EXTRA_KEY_ID, masterKeyId); intent.putExtra(RemoteImportKeysActivity.EXTRA_KEY_ID, masterKeyId);
return createInternal(intent); return createInternal(data, intent);
} }
PendingIntent createSelectAllowedKeysPendingIntent(String packageName) { PendingIntent createSelectAllowedKeysPendingIntent(Intent data, String packageName) {
Intent intent = new Intent(mContext, SelectAllowedKeysActivity.class); Intent intent = new Intent(mContext, SelectAllowedKeysActivity.class);
intent.setData(KeychainContract.ApiApps.buildByPackageNameUri(packageName)); intent.setData(KeychainContract.ApiApps.buildByPackageNameUri(packageName));
return createInternal(intent); return createInternal(data, intent);
} }
PendingIntent createShowKeyPendingIntent(long masterKeyId) { PendingIntent createShowKeyPendingIntent(Intent data, long masterKeyId) {
Intent intent = new Intent(mContext, ViewKeyActivity.class); Intent intent = new Intent(mContext, ViewKeyActivity.class);
intent.setData(KeychainContract.KeyRings.buildGenericKeyRingUri(masterKeyId)); intent.setData(KeychainContract.KeyRings.buildGenericKeyRingUri(masterKeyId));
return createInternal(intent); return createInternal(data, intent);
} }
PendingIntent createSelectSignKeyIdPendingIntent(String packageName, String preferredUserId) { PendingIntent createSelectSignKeyIdPendingIntent(Intent data, String packageName, String preferredUserId) {
Intent intent = new Intent(mContext, SelectSignKeyIdActivity.class); Intent intent = new Intent(mContext, SelectSignKeyIdActivity.class);
intent.setData(KeychainContract.ApiApps.buildByPackageNameUri(packageName)); intent.setData(KeychainContract.ApiApps.buildByPackageNameUri(packageName));
intent.putExtra(SelectSignKeyIdActivity.EXTRA_USER_ID, preferredUserId); intent.putExtra(SelectSignKeyIdActivity.EXTRA_USER_ID, preferredUserId);
return createInternal(intent); return createInternal(data, intent);
} }
@Deprecated @Deprecated
PendingIntent createAccountCreationPendingIntent(String packageName, String accountName) { PendingIntent createAccountCreationPendingIntent(Intent data, String packageName, String accountName) {
Intent intent = new Intent(mContext, RemoteCreateAccountActivity.class); Intent intent = new Intent(mContext, RemoteCreateAccountActivity.class);
intent.putExtra(RemoteCreateAccountActivity.EXTRA_PACKAGE_NAME, packageName); intent.putExtra(RemoteCreateAccountActivity.EXTRA_PACKAGE_NAME, packageName);
intent.putExtra(RemoteCreateAccountActivity.EXTRA_ACC_NAME, accountName); intent.putExtra(RemoteCreateAccountActivity.EXTRA_ACC_NAME, accountName);
return createInternal(intent); return createInternal(data, intent);
} }
PendingIntent createErrorPendingIntent(String errorMessage) { PendingIntent createErrorPendingIntent(Intent data, String errorMessage) {
Intent intent = new Intent(mContext, RemoteErrorActivity.class); Intent intent = new Intent(mContext, RemoteErrorActivity.class);
intent.putExtra(RemoteErrorActivity.EXTRA_ERROR_MESSAGE, errorMessage); intent.putExtra(RemoteErrorActivity.EXTRA_ERROR_MESSAGE, errorMessage);
return createInternal(intent); return createInternal(data, intent);
} }
private PendingIntent createInternal(Intent intent) { private PendingIntent createInternal(Intent data, Intent intent) {
// re-attach "data" for pass through. It will be used later to repeat pgp operation // re-attach "data" for pass through. It will be used later to repeat pgp operation
intent.putExtra(RemoteSecurityTokenOperationActivity.EXTRA_DATA, mPendingIntentData); intent.putExtra(RemoteSecurityTokenOperationActivity.EXTRA_DATA, data);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//noinspection ResourceType, looks like lint is missing FLAG_IMMUTABLE //noinspection ResourceType, looks like lint is missing FLAG_IMMUTABLE
@@ -158,11 +156,11 @@ public class ApiPendingIntentFactory {
} }
} }
PendingIntent createRegisterPendingIntent(String packageName, byte[] packageCertificate) { PendingIntent createRegisterPendingIntent(Intent data, String packageName, byte[] packageCertificate) {
Intent intent = new Intent(mContext, RemoteRegisterActivity.class); Intent intent = new Intent(mContext, RemoteRegisterActivity.class);
intent.putExtra(RemoteRegisterActivity.EXTRA_PACKAGE_NAME, packageName); intent.putExtra(RemoteRegisterActivity.EXTRA_PACKAGE_NAME, packageName);
intent.putExtra(RemoteRegisterActivity.EXTRA_PACKAGE_SIGNATURE, packageCertificate); intent.putExtra(RemoteRegisterActivity.EXTRA_PACKAGE_SIGNATURE, packageCertificate);
intent.putExtra(RemoteRegisterActivity.EXTRA_DATA, mPendingIntentData); intent.putExtra(RemoteRegisterActivity.EXTRA_DATA, data);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//noinspection ResourceType, looks like lint is missing FLAG_IMMUTABLE //noinspection ResourceType, looks like lint is missing FLAG_IMMUTABLE

View File

@@ -72,7 +72,7 @@ public class ApiPermissionHelper {
* @return null if caller is allowed, or a Bundle with a PendingIntent * @return null if caller is allowed, or a Bundle with a PendingIntent
*/ */
protected Intent isAllowed(Intent data) { protected Intent isAllowed(Intent data) {
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(mContext, data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(mContext);
try { try {
if (isCallerAllowed()) { if (isCallerAllowed()) {
@@ -95,7 +95,8 @@ public class ApiPermissionHelper {
} }
Log.e(Constants.TAG, "Not allowed to use service! return PendingIntent for registration!"); Log.e(Constants.TAG, "Not allowed to use service! return PendingIntent for registration!");
PendingIntent pi = piFactory.createRegisterPendingIntent(packageName, packageCertificate); PendingIntent pi = piFactory.createRegisterPendingIntent(data,
packageName, packageCertificate);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();
@@ -107,7 +108,7 @@ public class ApiPermissionHelper {
} catch (WrongPackageCertificateException e) { } catch (WrongPackageCertificateException e) {
Log.e(Constants.TAG, "wrong signature!", e); Log.e(Constants.TAG, "wrong signature!", e);
PendingIntent pi = piFactory.createErrorPendingIntent(mContext.getString(R.string.api_error_wrong_signature)); PendingIntent pi = piFactory.createErrorPendingIntent(data, mContext.getString(R.string.api_error_wrong_signature));
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();
@@ -175,9 +176,10 @@ public class ApiPermissionHelper {
String packageName = getCurrentCallingPackage(); String packageName = getCurrentCallingPackage();
Log.d(Constants.TAG, "getCreateAccountIntent accountName: " + accountName); Log.d(Constants.TAG, "getCreateAccountIntent accountName: " + accountName);
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(mContext, data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(mContext);
PendingIntent pi = piFactory.createAccountCreationPendingIntent(packageName, accountName); PendingIntent pi = piFactory.createAccountCreationPendingIntent(data,
packageName, accountName);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();

View File

@@ -143,9 +143,9 @@ public class OpenPgpService extends Service {
if (noUserIdsCheck || missingUserIdsCheck || duplicateUserIdsCheck) { if (noUserIdsCheck || missingUserIdsCheck || duplicateUserIdsCheck) {
// allow the user to verify pub key selection // allow the user to verify pub key selection
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext(), data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
PendingIntent pi = piFactory.createSelectPublicKeyPendingIntent(keyIdsArray, missingEmails, PendingIntent pi = piFactory.createSelectPublicKeyPendingIntent(data, keyIdsArray,
duplicateEmails, noUserIdsCheck); missingEmails, duplicateEmails, noUserIdsCheck);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();
@@ -226,10 +226,11 @@ public class OpenPgpService extends Service {
PgpSignEncryptResult pgpResult = pse.execute(pseInput, inputParcel, inputData, outputStream); PgpSignEncryptResult pgpResult = pse.execute(pseInput, inputParcel, inputData, outputStream);
if (pgpResult.isPending()) { if (pgpResult.isPending()) {
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext(), data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
RequiredInputParcel requiredInput = pgpResult.getRequiredInputParcel(); RequiredInputParcel requiredInput = pgpResult.getRequiredInputParcel();
PendingIntent pIntent = piFactory.requiredInputPi(requiredInput, pgpResult.mCryptoInputParcel); PendingIntent pIntent = piFactory.requiredInputPi(data,
requiredInput, pgpResult.mCryptoInputParcel);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();
@@ -363,10 +364,11 @@ public class OpenPgpService extends Service {
PgpSignEncryptResult pgpResult = op.execute(pseInput, inputParcel, inputData, outputStream); PgpSignEncryptResult pgpResult = op.execute(pseInput, inputParcel, inputData, outputStream);
if (pgpResult.isPending()) { if (pgpResult.isPending()) {
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext(), data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
RequiredInputParcel requiredInput = pgpResult.getRequiredInputParcel(); RequiredInputParcel requiredInput = pgpResult.getRequiredInputParcel();
PendingIntent pIntent = piFactory.requiredInputPi(requiredInput, pgpResult.mCryptoInputParcel); PendingIntent pIntent = piFactory.requiredInputPi(data,
requiredInput, pgpResult.mCryptoInputParcel);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();
@@ -436,12 +438,13 @@ public class OpenPgpService extends Service {
DecryptVerifyResult pgpResult = op.execute(input, cryptoInput, inputData, outputStream); DecryptVerifyResult pgpResult = op.execute(input, cryptoInput, inputData, outputStream);
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext(), data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
if (pgpResult.isPending()) { if (pgpResult.isPending()) {
// prepare and return PendingIntent to be executed by client // prepare and return PendingIntent to be executed by client
RequiredInputParcel requiredInput = pgpResult.getRequiredInputParcel(); RequiredInputParcel requiredInput = pgpResult.getRequiredInputParcel();
PendingIntent pIntent = piFactory.requiredInputPi(requiredInput, pgpResult.mCryptoInputParcel); PendingIntent pIntent = piFactory.requiredInputPi(data,
requiredInput, pgpResult.mCryptoInputParcel);
Intent result = new Intent(); Intent result = new Intent();
result.putExtra(OpenPgpApi.RESULT_INTENT, pIntent); result.putExtra(OpenPgpApi.RESULT_INTENT, pIntent);
@@ -459,7 +462,8 @@ public class OpenPgpService extends Service {
case OpenPgpSignatureResult.RESULT_KEY_MISSING: { case OpenPgpSignatureResult.RESULT_KEY_MISSING: {
// If signature key is missing we return a PendingIntent to retrieve the key // If signature key is missing we return a PendingIntent to retrieve the key
result.putExtra(OpenPgpApi.RESULT_INTENT, result.putExtra(OpenPgpApi.RESULT_INTENT,
piFactory.createImportFromKeyserverPendingIntent(signatureResult.getKeyId())); piFactory.createImportFromKeyserverPendingIntent(data,
signatureResult.getKeyId()));
break; break;
} }
case OpenPgpSignatureResult.RESULT_VALID_CONFIRMED: case OpenPgpSignatureResult.RESULT_VALID_CONFIRMED:
@@ -468,7 +472,8 @@ public class OpenPgpService extends Service {
case OpenPgpSignatureResult.RESULT_INVALID_KEY_EXPIRED: case OpenPgpSignatureResult.RESULT_INVALID_KEY_EXPIRED:
case OpenPgpSignatureResult.RESULT_INVALID_INSECURE: { case OpenPgpSignatureResult.RESULT_INVALID_INSECURE: {
// If signature key is known, return PendingIntent to show key // If signature key is known, return PendingIntent to show key
result.putExtra(OpenPgpApi.RESULT_INTENT, piFactory.createShowKeyPendingIntent(signatureResult.getKeyId())); result.putExtra(OpenPgpApi.RESULT_INTENT,
piFactory.createShowKeyPendingIntent(data, signatureResult.getKeyId()));
break; break;
} }
default: default:
@@ -542,7 +547,8 @@ public class OpenPgpService extends Service {
// allow user to select allowed keys // allow user to select allowed keys
Intent result = new Intent(); Intent result = new Intent();
String packageName = mApiPermissionHelper.getCurrentCallingPackage(); String packageName = mApiPermissionHelper.getCurrentCallingPackage();
result.putExtra(OpenPgpApi.RESULT_INTENT, piFactory.createSelectAllowedKeysPendingIntent(packageName)); result.putExtra(OpenPgpApi.RESULT_INTENT,
piFactory.createSelectAllowedKeysPendingIntent(data, packageName));
result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED); result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED);
return result; return result;
} }
@@ -566,7 +572,7 @@ public class OpenPgpService extends Service {
private Intent getKeyImpl(Intent data, OutputStream outputStream) { private Intent getKeyImpl(Intent data, OutputStream outputStream) {
try { try {
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext(), data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
long masterKeyId = data.getLongExtra(OpenPgpApi.EXTRA_KEY_ID, 0); long masterKeyId = data.getLongExtra(OpenPgpApi.EXTRA_KEY_ID, 0);
@@ -597,14 +603,16 @@ public class OpenPgpService extends Service {
} }
// also return PendingIntent that opens the key view activity // also return PendingIntent that opens the key view activity
result.putExtra(OpenPgpApi.RESULT_INTENT, piFactory.createShowKeyPendingIntent(masterKeyId)); result.putExtra(OpenPgpApi.RESULT_INTENT,
piFactory.createShowKeyPendingIntent(data, masterKeyId));
return result; return result;
} catch (ProviderHelper.NotFoundException e) { } catch (ProviderHelper.NotFoundException e) {
// If keys are not in db we return an additional PendingIntent // If keys are not in db we return an additional PendingIntent
// to retrieve the missing key // to retrieve the missing key
Intent result = new Intent(); Intent result = new Intent();
result.putExtra(OpenPgpApi.RESULT_INTENT, piFactory.createImportFromKeyserverPendingIntent(masterKeyId)); result.putExtra(OpenPgpApi.RESULT_INTENT,
piFactory.createImportFromKeyserverPendingIntent(data, masterKeyId));
result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED); result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED);
return result; return result;
} }
@@ -633,8 +641,8 @@ public class OpenPgpService extends Service {
String currentPkg = mApiPermissionHelper.getCurrentCallingPackage(); String currentPkg = mApiPermissionHelper.getCurrentCallingPackage();
String preferredUserId = data.getStringExtra(OpenPgpApi.EXTRA_USER_ID); String preferredUserId = data.getStringExtra(OpenPgpApi.EXTRA_USER_ID);
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext(), data); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
PendingIntent pi = piFactory.createSelectSignKeyIdPendingIntent(currentPkg, preferredUserId); PendingIntent pi = piFactory.createSelectSignKeyIdPendingIntent(data, currentPkg, preferredUserId);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();