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