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.axolotl.AxolotlService;
42import eu.siacs.conversations.entities.Conversation;
43import eu.siacs.conversations.entities.Message;
44
45public class ConversationMenuConfigurator {
46
47 private static boolean showSoundRecorderAttachment = false;
48 private static boolean showLocationAttachment = false;
49
50
51 public static void configureAttachmentMenu(@NonNull Conversation conversation, Menu menu) {
52 final MenuItem menuAttachSoundRecorder = menu.findItem(R.id.attach_record_voice);
53 final MenuItem menuAttachLocation = menu.findItem(R.id.attach_location);
54 final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
55
56 final boolean visible;
57 if (conversation.getMode() == Conversation.MODE_MULTI) {
58 visible = conversation.getAccount().httpUploadAvailable() && conversation.getMucOptions().participating();
59 } else {
60 visible = true;
61 }
62
63 menuAttach.setVisible(visible);
64
65 if (!visible) {
66 return;
67 }
68
69 menuAttachLocation.setVisible(showLocationAttachment);
70 menuAttachSoundRecorder.setVisible(showSoundRecorderAttachment);
71 }
72
73 public static void configureEncryptionMenu(@NonNull Conversation conversation, Menu menu) {
74 final MenuItem menuSecure = menu.findItem(R.id.action_security);
75 final MenuItem none = menu.findItem(R.id.encryption_choice_none);
76 final MenuItem pgp = menu.findItem(R.id.encryption_choice_pgp);
77 final MenuItem axolotl = menu.findItem(R.id.encryption_choice_axolotl);
78
79 boolean visible;
80 if (conversation.getMode() == Conversation.MODE_MULTI) {
81 visible = (Config.supportOpenPgp() || Config.supportOmemo()) && Config.multipleEncryptionChoices();
82 } else {
83 visible = Config.multipleEncryptionChoices();
84 }
85
86 menuSecure.setVisible(visible);
87
88 if (!visible) {
89 return;
90 }
91
92 if (conversation.getNextEncryption() != Message.ENCRYPTION_NONE) {
93 menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
94 }
95
96 pgp.setVisible(Config.supportOpenPgp());
97 none.setVisible(Config.supportUnencrypted() || conversation.getMode() == Conversation.MODE_MULTI);
98 axolotl.setVisible(Config.supportOmemo());
99 final AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
100 if (axolotlService == null || !axolotlService.isConversationAxolotlCapable(conversation)) {
101 axolotl.setEnabled(false);
102 }
103 switch (conversation.getNextEncryption()) {
104 case Message.ENCRYPTION_NONE:
105 none.setChecked(true);
106 break;
107 case Message.ENCRYPTION_PGP:
108 pgp.setChecked(true);
109 break;
110 case Message.ENCRYPTION_AXOLOTL:
111 axolotl.setChecked(true);
112 break;
113 default:
114 none.setChecked(true);
115 break;
116 }
117 }
118
119 public static void updateAttachmentAvailability(PackageManager packageManager) {
120 showSoundRecorderAttachment = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(packageManager) != null;
121 showLocationAttachment = new Intent("eu.siacs.conversations.location.request").resolveActivity(packageManager) != null;
122 }
123}