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.graphics.PorterDuff;
 36import android.graphics.drawable.Drawable;
 37import android.preference.PreferenceManager;
 38
 39import eu.siacs.conversations.R;
 40import eu.siacs.conversations.entities.Conversation;
 41import eu.siacs.conversations.entities.Presence;
 42import eu.siacs.conversations.ui.ConversationFragment;
 43import eu.siacs.conversations.utils.UIHelper;
 44
 45public class SendButtonTool {
 46
 47	public static SendButtonAction getAction(final Activity activity, final Conversation c, final String text, final String subject) {
 48		if (activity == null) {
 49			return SendButtonAction.TEXT;
 50		}
 51		final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
 52		final boolean empty = text.length() == 0;
 53		final boolean conference = c.getMode() == Conversation.MODE_MULTI;
 54		if (c.getCorrectingMessage() != null && (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())))))) {
 55			return SendButtonAction.CANCEL;
 56		} else if (conference && !c.getAccount().httpUploadAvailable()) {
 57			if (empty && c.getNextCounterpart() != null) {
 58				return SendButtonAction.CANCEL;
 59			} else {
 60				return SendButtonAction.TEXT;
 61			}
 62		} else {
 63			if (empty && (c.getThread() == null || subject.length() == 0)) {
 64				if (conference && c.getNextCounterpart() != null) {
 65					return SendButtonAction.CANCEL;
 66				} else {
 67					String setting = preferences.getString("quick_action", activity.getResources().getString(R.string.quick_action));
 68					if (!"none".equals(setting) && UIHelper.receivedLocationQuestion(c.getLatestMessage())) {
 69						return SendButtonAction.SEND_LOCATION;
 70					} else {
 71						if ("recent".equals(setting)) {
 72							setting = preferences.getString(ConversationFragment.RECENTLY_USED_QUICK_ACTION, SendButtonAction.TEXT.toString());
 73							return SendButtonAction.valueOfOrDefault(setting);
 74						} else {
 75							return SendButtonAction.valueOfOrDefault(setting);
 76						}
 77					}
 78				}
 79			} else {
 80				return SendButtonAction.TEXT;
 81			}
 82		}
 83	}
 84
 85	public static int colorForStatus(Activity activity, Presence.Status status) {
 86		switch (status) {
 87		case CHAT:
 88		case ONLINE:
 89			return 0xff4ab04a;
 90		case AWAY:
 91			return 0xfff69c44;
 92		case XA:
 93		case DND:
 94			return 0xffe7524a;
 95		default:
 96			return StyledAttributes.getColor(activity, R.attr.icon_tint);
 97		}
 98	}
 99
100	public static Drawable getSendButtonImageResource(Activity activity, SendButtonAction action, Presence.Status status, boolean canSend) {
101		final Drawable d;
102		switch (action) {
103			case TEXT:
104				d = canSend ?
105					activity.getResources().getDrawable(R.drawable.ic_send_text_online) :
106					activity.getResources().getDrawable(R.drawable.ic_attach_file_white_24dp);
107				break;
108			case RECORD_VIDEO:
109				d = activity.getResources().getDrawable(R.drawable.ic_send_videocam_online);
110				break;
111			case TAKE_PHOTO:
112				d = activity.getResources().getDrawable(R.drawable.ic_send_photo_online);
113				break;
114			case RECORD_VOICE:
115				d = activity.getResources().getDrawable(R.drawable.ic_send_voice_online);
116				break;
117			case SEND_LOCATION:
118				d = activity.getResources().getDrawable(R.drawable.ic_send_location_online);
119				break;
120			case CANCEL:
121				d = activity.getResources().getDrawable(R.drawable.ic_send_cancel_online);
122				break;
123			case CHOOSE_PICTURE:
124				d = activity.getResources().getDrawable(R.drawable.ic_send_picture_online);
125				break;
126			default:
127				return null;
128		}
129		d.mutate().setColorFilter(colorForStatus(activity, status), PorterDuff.Mode.SRC_IN);
130		return d;
131	}
132}