diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index fc445cc712..c064377028 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -136,7 +136,6 @@ import org.jsoup.nodes.Node; import org.jsoup.nodes.TextNode; import org.jsoup.select.Elements; import org.jsoup.select.NodeFilter; -import org.openintents.openpgp.IOpenPgpService2; import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; @@ -885,20 +884,7 @@ public class FragmentCompose extends FragmentBase { final String pkg = Helper.getOpenKeychainPackage(getContext()); Log.i("PGP binding to " + pkg); - pgpService = new OpenPgpServiceConnection(getContext(), pkg, new OpenPgpServiceConnection.OnBound() { - @Override - public void onBound(IOpenPgpService2 service) { - Log.i("PGP bound to " + pkg); - } - - @Override - public void onError(Exception ex) { - if ("bindService() returned false!".equals(ex.getMessage())) - Log.i("PGP " + ex.getMessage()); - else - Log.e("PGP", ex); - } - }); + pgpService = new OpenPgpServiceConnection(getContext(), pkg); pgpService.bindToService(); return view; @@ -1016,8 +1002,10 @@ public class FragmentCompose extends FragmentBase { public void onDestroyView() { adapter = null; - if (pgpService != null && pgpService.isBound()) + if (pgpService != null && pgpService.isBound()) { + Log.i("PGP unbinding"); pgpService.unbindFromService(); + } pgpService = null; super.onDestroyView(); diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 10393e64d8..4dfc8f317f 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -145,7 +145,6 @@ import org.json.JSONException; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.openintents.openpgp.AutocryptPeerUpdate; -import org.openintents.openpgp.IOpenPgpService2; import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; @@ -1262,20 +1261,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. final String pkg = Helper.getOpenKeychainPackage(getContext()); Log.i("PGP binding to " + pkg); - pgpService = new OpenPgpServiceConnection(getContext(), pkg, new OpenPgpServiceConnection.OnBound() { - @Override - public void onBound(IOpenPgpService2 service) { - Log.i("PGP bound to " + pkg); - } - - @Override - public void onError(Exception ex) { - if ("bindService() returned false!".equals(ex.getMessage())) - Log.i("PGP " + ex.getMessage()); - else - Log.e("PGP", ex); - } - }); + pgpService = new OpenPgpServiceConnection(getContext(), pkg); pgpService.bindToService(); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); @@ -1292,8 +1278,10 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); lbm.unregisterReceiver(creceiver); - if (pgpService != null && pgpService.isBound()) + if (pgpService != null && pgpService.isBound()) { + Log.i("PGP unbinding"); pgpService.unbindFromService(); + } pgpService = null; kv.clear(); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java b/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java index 9e7e358550..def7bafae1 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java @@ -47,7 +47,9 @@ import androidx.lifecycle.Lifecycle; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; +import org.openintents.openpgp.IOpenPgpService2; import org.openintents.openpgp.util.OpenPgpApi; +import org.openintents.openpgp.util.OpenPgpServiceConnection; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -68,6 +70,7 @@ public class FragmentOptionsEncryption extends FragmentBase implements SharedPre private Button btnManageKeys; private TextView tvKeySize; + private OpenPgpServiceConnection pgpService; private List openPgpProvider = new ArrayList<>(); private final static String[] RESET_OPTIONS = new String[]{ @@ -159,7 +162,14 @@ public class FragmentOptionsEncryption extends FragmentBase implements SharedPre spOpenPgp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView adapterView, View view, int position, long id) { - prefs.edit().putString("openpgp_provider", openPgpProvider.get(position)).apply(); + String pkg = openPgpProvider.get(position); + prefs.edit().putString("openpgp_provider", pkg).apply(); + + String tag = (String) spOpenPgp.getTag(); + if (tag != null && !tag.equals(pkg)) { + spOpenPgp.setTag(pkg); + testOpenPgp(pkg); + } } @Override @@ -226,6 +236,13 @@ public class FragmentOptionsEncryption extends FragmentBase implements SharedPre @Override public void onDestroyView() { PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this); + + if (pgpService != null && pgpService.isBound()) { + Log.i("PGP unbinding"); + pgpService.unbindFromService(); + } + pgpService = null; + super.onDestroyView(); } @@ -274,14 +291,35 @@ public class FragmentOptionsEncryption extends FragmentBase implements SharedPre swAutoDecrypt.setChecked(prefs.getBoolean("auto_decrypt", false)); String provider = prefs.getString("openpgp_provider", "org.sufficientlysecure.keychain"); + spOpenPgp.setTag(provider); for (int pos = 0; pos < openPgpProvider.size(); pos++) if (provider.equals(openPgpProvider.get(pos))) { spOpenPgp.setSelection(pos); break; } + testOpenPgp(provider); swAutocrypt.setChecked(prefs.getBoolean("autocrypt", true)); swAutocryptMutual.setChecked(prefs.getBoolean("autocrypt_mutual", true)); swAutocryptMutual.setEnabled(swAutocrypt.isChecked()); } + + private void testOpenPgp(String pkg) { + if (pgpService != null && pgpService.isBound()) + pgpService.unbindFromService(); + + Log.i("PGP binding to " + pkg); + pgpService = new OpenPgpServiceConnection(getContext(), pkg, new OpenPgpServiceConnection.OnBound() { + @Override + public void onBound(IOpenPgpService2 service) { + Log.i("PGP bound to " + pkg); + } + + @Override + public void onError(Exception ex) { + Log.e("PGP", ex); + } + }); + pgpService.bindToService(); + } }