From b1bd26424c010585c82d96415bfda8619f189808 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 29 Oct 2018 15:47:27 +0000 Subject: [PATCH] Help installing OpenKeychain --- .../java/eu/faircode/email/ActivityView.java | 28 +++++--- .../eu/faircode/email/FragmentCompose.java | 69 ++++++++++++------- .../main/java/eu/faircode/email/Helper.java | 6 ++ 3 files changed, 69 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 0efa2311..63d39d06 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -316,6 +316,9 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB iff.addAction(ACTION_SHOW_PRO); lbm.registerReceiver(receiver, iff); + if (!pgpService.isBound()) + pgpService.bindToService(); + Intent intent = getIntent(); String action = intent.getAction(); Log.i(Helper.TAG, "View intent=" + intent + " action=" + action); @@ -899,12 +902,24 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB private void onDecrypt(Intent intent) { if (Helper.isPro(this)) { - Intent data = new Intent(); - data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); - data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{intent.getStringExtra("to")}); - data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); + if (pgpService.isBound()) { + Intent data = new Intent(); + data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); + data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{intent.getStringExtra("to")}); + data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); - decrypt(data, intent.getLongExtra("id", -1)); + decrypt(data, intent.getLongExtra("id", -1)); + } else { + Snackbar snackbar = Snackbar.make(view, R.string.title_no_openpgp, Snackbar.LENGTH_LONG); + if (Helper.getIntentOpenKeychain().resolveActivity(getPackageManager()) != null) + snackbar.setAction(R.string.title_fix, new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(Helper.getIntentOpenKeychain()); + } + }); + snackbar.show(); + } } else onShowPro(intent); } @@ -927,9 +942,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB long id = args.getLong("id"); Intent data = args.getParcelable("data"); - if (!pgpService.isBound()) - throw new IllegalArgumentException(getString(R.string.title_no_openpgp)); - DB db = DB.getInstance(context); // Find encrypted data diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 3868e0c6..1fc00737 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -367,6 +367,14 @@ public class FragmentCompose extends FragmentEx { } } + @Override + public void onResume() { + super.onResume(); + + if (!pgpService.isBound()) + pgpService.bindToService(); + } + @Override public void onPause() { if (autosave) @@ -488,30 +496,42 @@ public class FragmentCompose extends FragmentEx { } private void onEncrypt() { - if (Helper.isPro(getContext())) - try { - String to = etTo.getText().toString(); - InternetAddress ato[] = (TextUtils.isEmpty(to) ? new InternetAddress[0] : InternetAddress.parse(to)); - if (ato.length == 0) - throw new IllegalArgumentException(getString(R.string.title_to_missing)); - - String[] tos = new String[ato.length]; - for (int i = 0; i < ato.length; i++) - tos[i] = ato[i].getAddress(); - - Intent data = new Intent(); - data.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); - data.putExtra(OpenPgpApi.EXTRA_USER_IDS, tos); - data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); - - encrypt(data); - } catch (Throwable ex) { - if (ex instanceof IllegalArgumentException) - Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); - else - Helper.unexpectedError(getContext(), ex); + if (Helper.isPro(getContext())) { + if (pgpService.isBound()) + try { + String to = etTo.getText().toString(); + InternetAddress ato[] = (TextUtils.isEmpty(to) ? new InternetAddress[0] : InternetAddress.parse(to)); + if (ato.length == 0) + throw new IllegalArgumentException(getString(R.string.title_to_missing)); + + String[] tos = new String[ato.length]; + for (int i = 0; i < ato.length; i++) + tos[i] = ato[i].getAddress(); + + Intent data = new Intent(); + data.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); + data.putExtra(OpenPgpApi.EXTRA_USER_IDS, tos); + data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); + + encrypt(data); + } catch (Throwable ex) { + if (ex instanceof IllegalArgumentException) + Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); + else + Helper.unexpectedError(getContext(), ex); + } + else { + Snackbar snackbar = Snackbar.make(view, R.string.title_no_openpgp, Snackbar.LENGTH_LONG); + if (Helper.getIntentOpenKeychain().resolveActivity(getContext().getPackageManager()) != null) + snackbar.setAction(R.string.title_fix, new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(Helper.getIntentOpenKeychain()); + } + }); + snackbar.show(); } - else { + } else { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro"); fragmentTransaction.commit(); @@ -530,9 +550,6 @@ public class FragmentCompose extends FragmentEx { long id = args.getLong("id"); Intent data = args.getParcelable("data"); - if (!pgpService.isBound()) - throw new IllegalArgumentException(getString(R.string.title_no_openpgp)); - DB db = DB.getInstance(context); // Get attachments diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index f9ef23fd..35454970 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -109,6 +109,12 @@ public class Helper { return intent; } + static Intent getIntentOpenKeychain() { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse("https://f-droid.org/en/packages/org.sufficientlysecure.keychain/")); + return intent; + } + static int resolveColor(Context context, int attr) { int[] attrs = new int[]{attr}; TypedArray a = context.getTheme().obtainStyledAttributes(attrs);