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