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.preference.PreferenceManager;
36
37import eu.siacs.conversations.R;
38import eu.siacs.conversations.entities.Conversation;
39import eu.siacs.conversations.entities.Presence;
40import eu.siacs.conversations.ui.ConversationFragment;
41import eu.siacs.conversations.utils.UIHelper;
42
43public class SendButtonTool {
44
45 public static SendButtonAction getAction(final Activity activity, final Conversation c, final String text) {
46 if (activity == null) {
47 return SendButtonAction.TEXT;
48 }
49 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
50 final boolean empty = text.length() == 0;
51 final boolean conference = c.getMode() == Conversation.MODE_MULTI;
52 if (c.getCorrectingMessage() != null && (empty || (text.equals(c.getCorrectingMessage().getBody()) && (c.getThread() == c.getCorrectingMessage().getThread() || (c.getThread() != null && c.getThread().equals(c.getCorrectingMessage().getThread())))))) {
53 return SendButtonAction.CANCEL;
54 } else if (conference && !c.getAccount().httpUploadAvailable()) {
55 if (empty && c.getNextCounterpart() != null) {
56 return SendButtonAction.CANCEL;
57 } else {
58 return SendButtonAction.TEXT;
59 }
60 } else {
61 if (empty) {
62 if (conference && c.getNextCounterpart() != null) {
63 return SendButtonAction.CANCEL;
64 } else {
65 String setting = preferences.getString("quick_action", activity.getResources().getString(R.string.quick_action));
66 if (!"none".equals(setting) && UIHelper.receivedLocationQuestion(c.getLatestMessage())) {
67 return SendButtonAction.SEND_LOCATION;
68 } else {
69 if ("recent".equals(setting)) {
70 setting = preferences.getString(ConversationFragment.RECENTLY_USED_QUICK_ACTION, SendButtonAction.TEXT.toString());
71 return SendButtonAction.valueOfOrDefault(setting);
72 } else {
73 return SendButtonAction.valueOfOrDefault(setting);
74 }
75 }
76 }
77 } else {
78 return SendButtonAction.TEXT;
79 }
80 }
81 }
82
83 public static int getSendButtonImageResource(Activity activity, SendButtonAction action, Presence.Status status) {
84 switch (action) {
85 case TEXT:
86 switch (status) {
87 case CHAT:
88 case ONLINE:
89 return R.drawable.ic_send_text_online;
90 case AWAY:
91 return R.drawable.ic_send_text_away;
92 case XA:
93 case DND:
94 return R.drawable.ic_send_text_dnd;
95 default:
96 return getThemeResource(activity, R.attr.ic_send_text_offline, R.drawable.ic_send_text_offline);
97 }
98 case RECORD_VIDEO:
99 switch (status) {
100 case CHAT:
101 case ONLINE:
102 return R.drawable.ic_send_videocam_online;
103 case AWAY:
104 return R.drawable.ic_send_videocam_away;
105 case XA:
106 case DND:
107 return R.drawable.ic_send_videocam_dnd;
108 default:
109 return getThemeResource(activity, R.attr.ic_send_videocam_offline, R.drawable.ic_send_videocam_offline);
110 }
111 case TAKE_PHOTO:
112 switch (status) {
113 case CHAT:
114 case ONLINE:
115 return R.drawable.ic_send_photo_online;
116 case AWAY:
117 return R.drawable.ic_send_photo_away;
118 case XA:
119 case DND:
120 return R.drawable.ic_send_photo_dnd;
121 default:
122 return getThemeResource(activity, R.attr.ic_send_photo_offline, R.drawable.ic_send_photo_offline);
123 }
124 case RECORD_VOICE:
125 switch (status) {
126 case CHAT:
127 case ONLINE:
128 return R.drawable.ic_send_voice_online;
129 case AWAY:
130 return R.drawable.ic_send_voice_away;
131 case XA:
132 case DND:
133 return R.drawable.ic_send_voice_dnd;
134 default:
135 return getThemeResource(activity, R.attr.ic_send_voice_offline, R.drawable.ic_send_voice_offline);
136 }
137 case SEND_LOCATION:
138 switch (status) {
139 case CHAT:
140 case ONLINE:
141 return R.drawable.ic_send_location_online;
142 case AWAY:
143 return R.drawable.ic_send_location_away;
144 case XA:
145 case DND:
146 return R.drawable.ic_send_location_dnd;
147 default:
148 return getThemeResource(activity, R.attr.ic_send_location_offline, R.drawable.ic_send_location_offline);
149 }
150 case CANCEL:
151 switch (status) {
152 case CHAT:
153 case ONLINE:
154 return R.drawable.ic_send_cancel_online;
155 case AWAY:
156 return R.drawable.ic_send_cancel_away;
157 case XA:
158 case DND:
159 return R.drawable.ic_send_cancel_dnd;
160 default:
161 return getThemeResource(activity, R.attr.ic_send_cancel_offline, R.drawable.ic_send_cancel_offline);
162 }
163 case CHOOSE_PICTURE:
164 switch (status) {
165 case CHAT:
166 case ONLINE:
167 return R.drawable.ic_send_picture_online;
168 case AWAY:
169 return R.drawable.ic_send_picture_away;
170 case XA:
171 case DND:
172 return R.drawable.ic_send_picture_dnd;
173 default:
174 return getThemeResource(activity, R.attr.ic_send_picture_offline, R.drawable.ic_send_picture_offline);
175 }
176 }
177 return getThemeResource(activity, R.attr.ic_send_text_offline, R.drawable.ic_send_text_offline);
178 }
179
180 private static int getThemeResource(Activity activity, int r_attr_name, int r_drawable_def) {
181 int[] attrs = {r_attr_name};
182 TypedArray ta = activity.getTheme().obtainStyledAttributes(attrs);
183
184 int res = ta.getResourceId(0, r_drawable_def);
185 ta.recycle();
186
187 return res;
188 }
189
190}