Refactor ProviderHelper to be non-static using a constructor based on context (first commit to get context out of pgp classes)

This commit is contained in:
Dominik Schürmann
2014-04-11 17:45:52 +02:00
parent bbd97cf800
commit 094fb698de
23 changed files with 266 additions and 228 deletions

View File

@@ -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<Long> 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);

View File

@@ -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<String> allowedPkgs = ProviderHelper.getRegisteredApiApps(this);
ArrayList<String> 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);
}
}

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);