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