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.app.Activity;
33import android.content.SharedPreferences;
34import android.preference.PreferenceManager;
35import android.view.View;
36import androidx.annotation.ColorInt;
37import androidx.annotation.DrawableRes;
38import androidx.core.content.ContextCompat;
39import com.google.android.material.color.MaterialColors;
40import eu.siacs.conversations.R;
41import eu.siacs.conversations.entities.Conversation;
42import eu.siacs.conversations.ui.Activities;
43import eu.siacs.conversations.ui.ConversationFragment;
44import eu.siacs.conversations.utils.UIHelper;
45import im.conversations.android.xmpp.model.stanza.Presence;
46
47public class SendButtonTool {
48
49 public static SendButtonAction getAction(
50 final Activity activity, final Conversation c, final String text) {
51 if (activity == null) {
52 return SendButtonAction.TEXT;
53 }
54 final SharedPreferences preferences =
55 PreferenceManager.getDefaultSharedPreferences(activity);
56 final boolean empty = text.isEmpty();
57 final boolean conference = c.getMode() == Conversation.MODE_MULTI;
58 if (c.getCorrectingMessage() != null
59 && (empty || text.equals(c.getCorrectingMessage().getBody()))) {
60 return SendButtonAction.CANCEL;
61 } else if (conference && !c.getAccount().httpUploadAvailable()) {
62 if (empty && c.getNextCounterpart() != null) {
63 return SendButtonAction.CANCEL;
64 } else {
65 return SendButtonAction.TEXT;
66 }
67 } else {
68 if (empty) {
69 if (conference && c.getNextCounterpart() != null) {
70 return SendButtonAction.CANCEL;
71 } else {
72 String setting =
73 preferences.getString(
74 "quick_action",
75 activity.getResources().getString(R.string.quick_action));
76 if (!"none".equals(setting)
77 && UIHelper.receivedLocationQuestion(c.getLatestMessage())) {
78 return SendButtonAction.SEND_LOCATION;
79 } else {
80 if ("recent".equals(setting)) {
81 setting =
82 preferences.getString(
83 ConversationFragment.RECENTLY_USED_QUICK_ACTION,
84 SendButtonAction.TEXT.toString());
85 return SendButtonAction.valueOfOrDefault(setting);
86 } else {
87 return SendButtonAction.valueOfOrDefault(setting);
88 }
89 }
90 }
91 } else {
92 return SendButtonAction.TEXT;
93 }
94 }
95 }
96
97 public @DrawableRes static int getSendButtonImageResource(final SendButtonAction action) {
98 return switch (action) {
99 case TEXT -> R.drawable.ic_send_24dp;
100 case TAKE_PHOTO -> R.drawable.ic_camera_alt_24dp;
101 case SEND_LOCATION -> R.drawable.ic_location_pin_24dp;
102 case CHOOSE_PICTURE -> R.drawable.ic_image_24dp;
103 case RECORD_VIDEO -> R.drawable.ic_videocam_24dp;
104 case RECORD_VOICE -> R.drawable.ic_mic_24dp;
105 case CANCEL -> R.drawable.ic_cancel_24dp;
106 };
107 }
108
109 public @ColorInt static int getSendButtonColor(
110 final View view, final Presence.Availability status) {
111 final boolean nightMode = Activities.isNightMode(view.getContext());
112 return switch (status) {
113 case OFFLINE ->
114 MaterialColors.getColor(
115 view, com.google.android.material.R.attr.colorOnSurface);
116 case ONLINE, CHAT ->
117 MaterialColors.harmonizeWithPrimary(
118 view.getContext(),
119 ContextCompat.getColor(
120 view.getContext(),
121 nightMode ? R.color.green_300 : R.color.green_800));
122 case AWAY ->
123 MaterialColors.harmonizeWithPrimary(
124 view.getContext(),
125 ContextCompat.getColor(
126 view.getContext(),
127 nightMode ? R.color.amber_300 : R.color.amber_800));
128 case XA ->
129 MaterialColors.harmonizeWithPrimary(
130 view.getContext(),
131 ContextCompat.getColor(
132 view.getContext(),
133 nightMode ? R.color.orange_300 : R.color.orange_800));
134 case DND ->
135 MaterialColors.harmonizeWithPrimary(
136 view.getContext(),
137 ContextCompat.getColor(
138 view.getContext(),
139 nightMode ? R.color.red_300 : R.color.red_800));
140 };
141 }
142}