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