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