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.Intent;
 33import android.content.pm.PackageManager;
 34import android.provider.MediaStore;
 35import android.support.annotation.NonNull;
 36import android.view.Menu;
 37import android.view.MenuItem;
 38
 39import eu.siacs.conversations.Config;
 40import eu.siacs.conversations.R;
 41import eu.siacs.conversations.crypto.OmemoSetting;
 42import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 43import eu.siacs.conversations.entities.Conversation;
 44import eu.siacs.conversations.entities.Message;
 45
 46public class ConversationMenuConfigurator {
 47
 48	public static void configureAttachmentMenu(@NonNull Conversation conversation, Menu menu) {
 49		final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
 50
 51		final boolean visible;
 52		if (conversation.getMode() == Conversation.MODE_MULTI) {
 53			visible = conversation.getAccount().httpUploadAvailable() && conversation.getMucOptions().participating();
 54		} else {
 55			visible = true;
 56		}
 57
 58		menuAttach.setVisible(visible);
 59
 60		if (!visible) {
 61			return;
 62		}
 63	}
 64
 65	public static void configureEncryptionMenu(@NonNull Conversation conversation, Menu menu) {
 66		final MenuItem menuSecure = menu.findItem(R.id.action_security);
 67		final MenuItem none = menu.findItem(R.id.encryption_choice_none);
 68		final MenuItem pgp = menu.findItem(R.id.encryption_choice_pgp);
 69		final MenuItem axolotl = menu.findItem(R.id.encryption_choice_axolotl);
 70
 71		boolean visible;
 72		if (OmemoSetting.isAlways()) {
 73			visible = false;
 74		} else if (conversation.getMode() == Conversation.MODE_MULTI) {
 75			visible = (Config.supportOpenPgp() || Config.supportOmemo()) && Config.multipleEncryptionChoices();
 76		} else {
 77			visible = Config.multipleEncryptionChoices();
 78		}
 79
 80		menuSecure.setVisible(visible);
 81
 82		if (!visible) {
 83			return;
 84		}
 85
 86		if (conversation.getNextEncryption() != Message.ENCRYPTION_NONE) {
 87			menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
 88		}
 89
 90		pgp.setVisible(Config.supportOpenPgp());
 91		none.setVisible(Config.supportUnencrypted() || conversation.getMode() == Conversation.MODE_MULTI);
 92		axolotl.setVisible(Config.supportOmemo());
 93		final AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
 94		if (axolotlService == null || !axolotlService.isConversationAxolotlCapable(conversation)) {
 95			axolotl.setEnabled(false);
 96		}
 97		switch (conversation.getNextEncryption()) {
 98			case Message.ENCRYPTION_NONE:
 99				none.setChecked(true);
100				break;
101			case Message.ENCRYPTION_PGP:
102				pgp.setChecked(true);
103				break;
104			case Message.ENCRYPTION_AXOLOTL:
105				axolotl.setChecked(true);
106				break;
107			default:
108				none.setChecked(true);
109				break;
110		}
111	}
112}