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