From 094fb698deca4aa269a754e89acacde2f339a97f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 11 Apr 2014 17:45:52 +0200 Subject: Refactor ProviderHelper to be non-static using a constructor based on context (first commit to get context out of pgp classes) --- .../keychain/remote/OpenPgpService.java | 12 +++++----- .../keychain/remote/RemoteService.java | 10 ++++---- .../remote/ui/AccountSettingsActivity.java | 4 ++-- .../remote/ui/AccountSettingsFragment.java | 4 ++-- .../keychain/remote/ui/AppSettingsActivity.java | 2 +- .../keychain/remote/ui/RemoteServiceActivity.java | 28 +++++++++++----------- 6 files changed, 31 insertions(+), 29 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 06df2f881..48cc8502f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -296,7 +296,7 @@ public class OpenPgpService extends RemoteService { PgpDecryptVerify.Builder builder = new PgpDecryptVerify.Builder(this, inputData, os); builder.allowSymmetricDecryption(false) // no support for symmetric encryption .allowedKeyIds(allowedKeyIds) // allow only private keys associated with - // accounts of this app + // accounts of this app .passphrase(passphrase); // TODO: currently does not support binary signed-only content @@ -305,10 +305,10 @@ public class OpenPgpService extends RemoteService { if (PgpDecryptVerifyResult.KEY_PASSHRASE_NEEDED == decryptVerifyResult.getStatus()) { // get PendingIntent for passphrase input, add it to given params and return to client Intent passphraseBundle = - getPassphraseBundleIntent(data, decryptVerifyResult.getKeyIdPassphraseNeeded()); + getPassphraseBundleIntent(data, decryptVerifyResult.getKeyIdPassphraseNeeded()); return passphraseBundle; } else if (PgpDecryptVerifyResult.SYMMETRIC_PASSHRASE_NEEDED == - decryptVerifyResult.getStatus()) { + decryptVerifyResult.getStatus()) { throw new PgpGeneralException("Decryption of symmetric content not supported by API!"); } @@ -352,7 +352,7 @@ public class OpenPgpService extends RemoteService { try { long keyId = data.getLongExtra(OpenPgpApi.EXTRA_KEY_ID, 0); - if (ProviderHelper.getPGPPublicKeyRing(this, keyId) == null) { + if (mProviderHelper.getPGPPublicKeyRing(keyId) == null) { Intent result = new Intent(); // If keys are not in db we return an additional PendingIntent @@ -462,8 +462,8 @@ public class OpenPgpService extends RemoteService { } else if (OpenPgpApi.ACTION_DECRYPT_VERIFY.equals(action)) { String currentPkg = getCurrentCallingPackage(); Set allowedKeyIds = - ProviderHelper.getAllKeyIdsForApp(mContext, - ApiAccounts.buildBaseUri(currentPkg)); + mProviderHelper.getAllKeyIdsForApp( + ApiAccounts.buildBaseUri(currentPkg)); return decryptAndVerifyImpl(data, input, output, allowedKeyIds); } else if (OpenPgpApi.ACTION_GET_KEY.equals(action)) { return getKeyImpl(data); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/RemoteService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/RemoteService.java index 16a800022..82d41dff7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/RemoteService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/RemoteService.java @@ -45,6 +45,7 @@ import java.util.Arrays; */ public abstract class RemoteService extends Service { Context mContext; + ProviderHelper mProviderHelper; public Context getContext() { return mContext; @@ -148,7 +149,7 @@ public abstract class RemoteService extends Service { Uri uri = KeychainContract.ApiAccounts.buildByPackageAndAccountUri(currentPkg, accountName); - AccountSettings settings = ProviderHelper.getApiAccountSettings(this, uri); + AccountSettings settings = mProviderHelper.getApiAccountSettings(uri); return settings; // can be null! } @@ -221,7 +222,7 @@ public abstract class RemoteService extends Service { private boolean isPackageAllowed(String packageName) throws WrongPackageSignatureException { Log.d(Constants.TAG, "isPackageAllowed packageName: " + packageName); - ArrayList allowedPkgs = ProviderHelper.getRegisteredApiApps(this); + ArrayList allowedPkgs = mProviderHelper.getRegisteredApiApps(); Log.d(Constants.TAG, "allowed: " + allowedPkgs); // check if package is allowed to use our service @@ -236,7 +237,7 @@ public abstract class RemoteService extends Service { throw new WrongPackageSignatureException(e.getMessage()); } - byte[] storedSig = ProviderHelper.getApiAppSignature(this, packageName); + byte[] storedSig = mProviderHelper.getApiAppSignature(packageName); if (Arrays.equals(currentSig, storedSig)) { Log.d(Constants.TAG, "Package signature is correct! (equals signature from database)"); @@ -244,7 +245,7 @@ public abstract class RemoteService extends Service { } else { throw new WrongPackageSignatureException( "PACKAGE NOT ALLOWED! Signature wrong! (Signature not " + - "equals signature from database)"); + "equals signature from database)"); } } @@ -256,6 +257,7 @@ public abstract class RemoteService extends Service { public void onCreate() { super.onCreate(); mContext = this; + mProviderHelper = new ProviderHelper(this); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java index 123ed526f..87d60e657 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java @@ -90,7 +90,7 @@ public class AccountSettingsActivity extends ActionBarActivity { } private void loadData(Uri accountUri) { - AccountSettings settings = ProviderHelper.getApiAccountSettings(this, accountUri); + AccountSettings settings = new ProviderHelper(this).getApiAccountSettings(accountUri); mAccountSettingsFragment.setAccSettings(settings); } @@ -102,7 +102,7 @@ public class AccountSettingsActivity extends ActionBarActivity { } private void save() { - ProviderHelper.updateApiAccount(this, mAccountSettingsFragment.getAccSettings(), mAccountUri); + new ProviderHelper(this).updateApiAccount(mAccountSettingsFragment.getAccSettings(), mAccountUri); finish(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java index a13c7a953..4aefc38da 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java @@ -180,8 +180,8 @@ public class AccountSettingsFragment extends Fragment implements if (resultCode == Activity.RESULT_OK) { // select newly created key try { - long masterKeyId = ProviderHelper.extractOrGetMasterKeyId( - getActivity(), data.getData()); + long masterKeyId = new ProviderHelper(getActivity()) + .extractOrGetMasterKeyId(data.getData()); mSelectKeyFragment.selectKey(masterKeyId); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java index 818c296c1..66f9411ff 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java @@ -85,7 +85,7 @@ public class AppSettingsActivity extends ActionBarActivity { } private void loadData(Bundle savedInstanceState, Uri appUri) { - AppSettings settings = ProviderHelper.getApiAppSettings(this, appUri); + AppSettings settings = new ProviderHelper(this).getApiAppSettings(appUri); mSettingsFragment.setAppSettings(settings); String appName; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java index ab95f2691..dddd92979 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java @@ -103,7 +103,7 @@ public class RemoteServiceActivity extends ActionBarActivity { public void onClick(View v) { // Allow - ProviderHelper.insertApiApp(RemoteServiceActivity.this, + new ProviderHelper(RemoteServiceActivity.this).insertApiApp( mAppSettingsFragment.getAppSettings()); // give data through for new service call @@ -146,7 +146,7 @@ public class RemoteServiceActivity extends ActionBarActivity { mAccSettingsFragment.setErrorOnSelectKeyFragment( getString(R.string.api_register_error_select_key)); } else { - ProviderHelper.insertApiAccount(RemoteServiceActivity.this, + new ProviderHelper(RemoteServiceActivity.this).insertApiAccount( KeychainContract.ApiAccounts.buildBaseUri(packageName), mAccSettingsFragment.getAccSettings()); @@ -179,19 +179,19 @@ public class RemoteServiceActivity extends ActionBarActivity { final Intent resultData = extras.getParcelable(EXTRA_DATA); PassphraseDialogFragment.show(this, secretKeyId, - new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { - // return given params again, for calling the service method again - RemoteServiceActivity.this.setResult(RESULT_OK, resultData); - } else { - RemoteServiceActivity.this.setResult(RESULT_CANCELED); - } + new Handler() { + @Override + public void handleMessage(Message message) { + if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { + // return given params again, for calling the service method again + RemoteServiceActivity.this.setResult(RESULT_OK, resultData); + } else { + RemoteServiceActivity.this.setResult(RESULT_CANCELED); + } - RemoteServiceActivity.this.finish(); - } - }); + RemoteServiceActivity.this.finish(); + } + }); } else if (ACTION_SELECT_PUB_KEYS.equals(action)) { long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS); -- cgit v1.2.3