From df45a1153db622511cab205395c2dc7fd33a25f5 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 13 Apr 2018 22:34:52 +0200 Subject: introduced tri state omemo setting (off by default, on by default, always) --- .../de/pixart/messenger/crypto/OmemoSetting.java | 74 ++++++++++++++++++++++ .../de/pixart/messenger/entities/Conversation.java | 21 +++--- .../messenger/services/XmppConnectionService.java | 2 + .../pixart/messenger/ui/ConversationsActivity.java | 2 + .../de/pixart/messenger/ui/SettingsActivity.java | 29 ++++++++- .../ui/util/ConversationMenuConfigurator.java | 5 +- src/main/res/values/arrays.xml | 12 ++++ src/main/res/values/defaults.xml | 1 + src/main/res/values/strings.xml | 6 ++ src/main/res/xml/preferences.xml | 7 ++ 10 files changed, 148 insertions(+), 11 deletions(-) create mode 100644 src/main/java/de/pixart/messenger/crypto/OmemoSetting.java (limited to 'src/main') diff --git a/src/main/java/de/pixart/messenger/crypto/OmemoSetting.java b/src/main/java/de/pixart/messenger/crypto/OmemoSetting.java new file mode 100644 index 000000000..2c9717da6 --- /dev/null +++ b/src/main/java/de/pixart/messenger/crypto/OmemoSetting.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2018, Daniel Gultsch All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package de.pixart.messenger.crypto; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import de.pixart.messenger.R; +import de.pixart.messenger.entities.Message; +import de.pixart.messenger.ui.SettingsActivity; + +public class OmemoSetting { + + private static boolean always = false; + private static int encryption = Message.ENCRYPTION_AXOLOTL; + + public static boolean isAlways() { + return always; + } + + public static int getEncryption() { + return encryption; + } + + public static void load(final Context context, final SharedPreferences sharedPreferences) { + final String value = sharedPreferences.getString(SettingsActivity.OMEMO_SETTING, context.getResources().getString(R.string.omemo_setting_default)); + switch (value) { + case "always": + always = true; + encryption = Message.ENCRYPTION_AXOLOTL; + break; + case "default_off": + always = false; + encryption = Message.ENCRYPTION_NONE; + break; + default: + always = false; + encryption = Message.ENCRYPTION_AXOLOTL; + break; + + } + } + + public static void load(final Context context) { + load(context, PreferenceManager.getDefaultSharedPreferences(context)); + } +} \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/entities/Conversation.java b/src/main/java/de/pixart/messenger/entities/Conversation.java index 266e06e10..a010a3799 100644 --- a/src/main/java/de/pixart/messenger/entities/Conversation.java +++ b/src/main/java/de/pixart/messenger/entities/Conversation.java @@ -26,6 +26,7 @@ import java.util.Locale; import java.util.concurrent.atomic.AtomicBoolean; import de.pixart.messenger.Config; +import de.pixart.messenger.crypto.OmemoSetting; import de.pixart.messenger.crypto.PgpDecryptionService; import de.pixart.messenger.crypto.axolotl.AxolotlService; import de.pixart.messenger.xmpp.chatstate.ChatState; @@ -749,29 +750,31 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl } public int getNextEncryption() { - final int defaultEncryption; if (!Config.supportOmemo() && !Config.supportOpenPgp() && !Config.supportOtr()) { return Message.ENCRYPTION_NONE; } - if (contactJid.asBareJid().equals(Config.BUG_REPORTS)) { - defaultEncryption = Message.ENCRYPTION_NONE; - } else if (suitableForOmemoByDefault(this)) { - defaultEncryption = Message.ENCRYPTION_AXOLOTL; + if (OmemoSetting.isAlways()) { + return suitableForOmemoByDefault(this) ? Message.ENCRYPTION_AXOLOTL : Message.ENCRYPTION_NONE; + } + final int defaultEncryption; + if (suitableForOmemoByDefault(this)) { + defaultEncryption = OmemoSetting.getEncryption(); } else { defaultEncryption = Message.ENCRYPTION_NONE; } int encryption = this.getIntAttribute(ATTRIBUTE_NEXT_ENCRYPTION, defaultEncryption); - - if (encryption == Message.ENCRYPTION_OTR) { - return encryption; - } else if (encryption < 0) { + if (encryption == Message.ENCRYPTION_OTR || encryption < 0) { return defaultEncryption; } else { return encryption; } } + private static boolean suitableForOmemoByDefault(final Conversation conversation) { + if (conversation.getJid().asBareJid().equals(Config.BUG_REPORTS)) { + return false; + } final String contact = conversation.getJid().getDomain(); final String account = conversation.getAccount().getServer(); if (Config.OMEMO_EXCEPTIONS.CONTACT_DOMAINS.contains(contact) || Config.OMEMO_EXCEPTIONS.ACCOUNT_DOMAINS.contains(account)) { diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 48c39c18b..5d36f7bd0 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -74,6 +74,7 @@ import java.util.concurrent.atomic.AtomicLong; import de.pixart.messenger.BuildConfig; import de.pixart.messenger.Config; import de.pixart.messenger.R; +import de.pixart.messenger.crypto.OmemoSetting; import de.pixart.messenger.crypto.PgpDecryptionService; import de.pixart.messenger.crypto.PgpEngine; import de.pixart.messenger.crypto.axolotl.AxolotlService; @@ -1068,6 +1069,7 @@ public class XmppConnectionService extends Service { @SuppressLint("TrulyRandom") @Override public void onCreate() { + OmemoSetting.load(this); ExceptionHelper.init(getApplicationContext()); PRNGFixes.apply(); Resolver.init(this); diff --git a/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java b/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java index e64e4afb8..3c4b9fb59 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java @@ -68,6 +68,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import de.pixart.messenger.Config; import de.pixart.messenger.R; +import de.pixart.messenger.crypto.OmemoSetting; import de.pixart.messenger.databinding.ActivityConversationsBinding; import de.pixart.messenger.entities.Account; import de.pixart.messenger.entities.Conversation; @@ -425,6 +426,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + OmemoSetting.load(this); new EmojiService(this).init(useBundledEmoji()); this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations); setSupportActionBar((Toolbar) binding.toolbar); diff --git a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java index aa7407c32..48c5f46fd 100644 --- a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java @@ -33,6 +33,7 @@ import java.util.List; import de.pixart.messenger.Config; import de.pixart.messenger.R; +import de.pixart.messenger.crypto.OmemoSetting; import de.pixart.messenger.entities.Account; import de.pixart.messenger.services.ExportLogsService; import de.pixart.messenger.services.MemorizingTrustManager; @@ -53,6 +54,7 @@ public class SettingsActivity extends XmppActivity implements public static final String WARN_UNENCRYPTED_CHAT = "warn_unencrypted_chat"; public static final String THEME = "theme"; public static final String SHOW_DYNAMIC_TAGS = "show_dynamic_tags"; + public static final String OMEMO_SETTING = "omemo"; public static final String SHOW_FOREGROUND_SERVICE = "show_foreground_service"; public static final String USE_BUNDLED_EMOJIS = "use_bundled_emoji"; public static final String USE_MULTI_ACCOUNTS = "use_multi_accounts"; @@ -102,6 +104,8 @@ public class SettingsActivity extends XmppActivity implements isBundledEmojiChecked = ((CheckBoxPreference) BundledEmojiPreference).isChecked(); } + changeOmemoSettingSummary(); + if (Config.FORCE_ORBOT) { PreferenceCategory connectionOptions = (PreferenceCategory) mSettingsFragment.findPreference("connection_options"); PreferenceScreen expert = (PreferenceScreen) mSettingsFragment.findPreference("expert"); @@ -265,6 +269,26 @@ public class SettingsActivity extends XmppActivity implements } } + private void changeOmemoSettingSummary() { + ListPreference omemoPreference = (ListPreference) mSettingsFragment.findPreference(OMEMO_SETTING); + if (omemoPreference != null) { + String value = omemoPreference.getValue(); + switch (value) { + case "always": + omemoPreference.setSummary(R.string.pref_omemo_setting_summary_always); + break; + case "default_on": + omemoPreference.setSummary(R.string.pref_omemo_setting_summary_default_on); + break; + case "default_off": + omemoPreference.setSummary(R.string.pref_omemo_setting_summary_default_off); + break; + } + } else { + Log.d(Config.LOGTAG, "unable to find preference named " + OMEMO_SETTING); + } + } + private boolean isCallable(final Intent i) { return i != null && getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY).size() > 0; } @@ -450,7 +474,10 @@ public class SettingsActivity extends XmppActivity implements TREAT_VIBRATE_AS_SILENT, MANUALLY_CHANGE_PRESENCE, BROADCAST_LAST_ACTIVITY); - if (name.equals(SHOW_FOREGROUND_SERVICE)) { + if (name.equals(OMEMO_SETTING)) { + OmemoSetting.load(this, preferences); + changeOmemoSettingSummary(); + } else if (name.equals(SHOW_FOREGROUND_SERVICE)) { xmppConnectionService.toggleForegroundService(); } else if (resendPresence.contains(name)) { if (xmppConnectionServiceBound) { diff --git a/src/main/java/de/pixart/messenger/ui/util/ConversationMenuConfigurator.java b/src/main/java/de/pixart/messenger/ui/util/ConversationMenuConfigurator.java index 3986ca52c..c82b34760 100644 --- a/src/main/java/de/pixart/messenger/ui/util/ConversationMenuConfigurator.java +++ b/src/main/java/de/pixart/messenger/ui/util/ConversationMenuConfigurator.java @@ -35,6 +35,7 @@ import android.view.MenuItem; import de.pixart.messenger.Config; import de.pixart.messenger.R; +import de.pixart.messenger.crypto.OmemoSetting; import de.pixart.messenger.crypto.axolotl.AxolotlService; import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.entities.Message; @@ -45,7 +46,9 @@ public class ConversationMenuConfigurator { final MenuItem menuAttach = menu.findItem(R.id.action_attach_file); final boolean visible; - if (conversation.getMode() == Conversation.MODE_MULTI) { + if (OmemoSetting.isAlways()) { + visible = false; + } else if (conversation.getMode() == Conversation.MODE_MULTI) { visible = conversation.getAccount().httpUploadAvailable() && conversation.getMucOptions().participating(); } else { visible = true; diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index 062f29b48..3ded5490e 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -82,4 +82,16 @@ 31622400 + + always + default_on + default_off + + + + @string/always + @string/default_on + @string/default_off + + diff --git a/src/main/res/values/defaults.xml b/src/main/res/values/defaults.xml index 425e397c9..201b9aedc 100644 --- a/src/main/res/values/defaults.xml +++ b/src/main/res/values/defaults.xml @@ -102,5 +102,6 @@ false true true + default_off diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index db7d54663..b78ea9b49 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -772,4 +772,10 @@ Are you sure you want to disable OMEMO encryption for this conversation?\nThis will allow your server administrator to read your messages, but it might be the only way to communicate with people using outdated clients. Disable now Draft: + OMEMO Encryption + OMEMO will always be used for one-on-one and private group chats. + OMEMO will be used by default for new conversations. + OMEMO will have to be turned on explicitly for new conversations. + On by default + Off by default diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index 68a25d3b0..22d015ab8 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -275,6 +275,13 @@ android:key="warn_unencrypted_chat" android:title="@string/pref_warn_unencrypted_chat" android:summary="@string/pref_warn_unencrypted_chat_summary"/> +