ConversationMenuConfigurator.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.ui.util;
 31
 32import android.content.Context;
 33import android.content.pm.PackageManager;
 34import android.view.Menu;
 35import android.view.MenuItem;
 36
 37import androidx.annotation.NonNull;
 38
 39import eu.siacs.conversations.Config;
 40import eu.siacs.conversations.R;
 41import eu.siacs.conversations.crypto.OmemoSetting;
 42import eu.siacs.conversations.entities.Conversation;
 43import eu.siacs.conversations.entities.Conversational;
 44import eu.siacs.conversations.entities.Message;
 45
 46public class ConversationMenuConfigurator {
 47
 48	private static boolean microphoneAvailable = false;
 49
 50	public static void reloadFeatures(Context context) {
 51		microphoneAvailable = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
 52	}
 53
 54	public static void configureAttachmentMenu(@NonNull Conversation conversation, Menu menu, boolean isTextEmpty) {
 55		final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
 56
 57		final boolean visible;
 58		if (conversation.getMode() == Conversation.MODE_MULTI) {
 59			visible = conversation.getAccount().httpUploadAvailable() && conversation.getMucOptions().participating();
 60		} else {
 61			visible = true;
 62		}
 63		if (menuAttach != null) menuAttach.setVisible(visible);
 64		if (visible) menu.findItem(R.id.attach_record_voice).setVisible(microphoneAvailable);
 65		menu.findItem(R.id.attach_subject).setVisible(conversation.getNextEncryption() == Message.ENCRYPTION_NONE);
 66		if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N || isTextEmpty) {
 67			menu.findItem(R.id.attach_schedule).setVisible(false);
 68		}
 69	}
 70
 71	public static void configureEncryptionMenu(@NonNull Conversation conversation, Menu menu) {
 72		final MenuItem menuSecure = menu.findItem(R.id.action_security);
 73
 74		final boolean participating = conversation.getMode() == Conversational.MODE_SINGLE || conversation.getMucOptions().participating();
 75
 76		if (!participating) {
 77			menuSecure.setVisible(false);
 78			return;
 79		}
 80
 81		final MenuItem none = menu.findItem(R.id.encryption_choice_none);
 82		final MenuItem pgp = menu.findItem(R.id.encryption_choice_pgp);
 83		final MenuItem axolotl = menu.findItem(R.id.encryption_choice_axolotl);
 84
 85		final int next = conversation.getNextEncryption();
 86
 87		boolean visible;
 88		if (OmemoSetting.isAlways()) {
 89			visible = false;
 90		} else if (conversation.getMode() == Conversation.MODE_MULTI) {
 91			if (next == Message.ENCRYPTION_NONE && !conversation.isPrivateAndNonAnonymous() && !conversation.getBooleanAttribute(Conversation.ATTRIBUTE_FORMERLY_PRIVATE_NON_ANONYMOUS, false)) {
 92				visible = false;
 93			} else {
 94				visible = (Config.supportOpenPgp() || Config.supportOmemo()) && Config.multipleEncryptionChoices();
 95			}
 96		} else {
 97			visible = Config.multipleEncryptionChoices();
 98		}
 99
100		menuSecure.setVisible(visible);
101
102		if (!visible) {
103			return;
104		}
105
106		menuSecure.setIcon(R.drawable.ic_lock_24dp);
107
108		pgp.setVisible(Config.supportOpenPgp());
109		none.setVisible(Config.supportUnencrypted() || conversation.getMode() == Conversation.MODE_MULTI);
110		axolotl.setVisible(Config.supportOmemo());
111		switch (next) {
112			case Message.ENCRYPTION_PGP:
113				//menuSecure.setTitle(R.string.encrypted_with_openpgp);
114				pgp.setChecked(true);
115				break;
116			case Message.ENCRYPTION_AXOLOTL:
117				//menuSecure.setTitle(R.string.encrypted_with_omemo);
118				axolotl.setChecked(true);
119				break;
120			default:
121				//menuSecure.setTitle(R.string.not_encrypted);
122				none.setChecked(true);
123				break;
124		}
125	}
126}