diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-04-13 22:34:52 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-04-13 22:49:42 +0200 |
commit | df45a1153db622511cab205395c2dc7fd33a25f5 (patch) | |
tree | 0292b864ee1f2244ea71771c482e18c3efa8e107 /src/main/java/de | |
parent | 0f7f4bf5ed4bc8d07c333daf2c5c519dd0c7fdc9 (diff) |
introduced tri state omemo setting (off by default, on by default, always)
Diffstat (limited to 'src/main/java/de')
6 files changed, 122 insertions, 11 deletions
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; |