aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/util/SendButtonTool.java
blob: 2c9307323b772e635b3199671128bd7ea40c13cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.pixart.messenger.ui.util;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.preference.PreferenceManager;

import de.pixart.messenger.R;
import de.pixart.messenger.entities.Conversation;
import de.pixart.messenger.entities.Presence;
import de.pixart.messenger.ui.ConversationFragment;
import de.pixart.messenger.ui.SettingsActivity;
import de.pixart.messenger.utils.UIHelper;

public class SendButtonTool {

    public static SendButtonAction getAction(final Activity activity, final Conversation c, final String text) {
        if (activity == null) {
            return SendButtonAction.TEXT;
        }
        final boolean empty = text.length() == 0;
        final boolean conference = c.getMode() == Conversation.MODE_MULTI;
        if (c.getCorrectingMessage() != null && (empty || text.equals(c.getCorrectingMessage().getBody()))) {
            return SendButtonAction.CANCEL;
        } else if (conference && !c.getAccount().httpUploadAvailable()) {
            if (empty && c.getNextCounterpart() != null) {
                return SendButtonAction.CANCEL;
            } else {
                return SendButtonAction.TEXT;
            }
        } else {
            if (empty) {
                final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
                if (conference && c.getNextCounterpart() != null) {
                    return SendButtonAction.CANCEL;
                } else {
                    String setting = preferences.getString("quick_action", activity.getResources().getString(R.string.quick_action));
                    if (quickShareChoice(activity) && AttachmentsVisible(c)) {
                        return SendButtonAction.CHOOSE_ATTACHMENT;
                    } else if (quickShareChoice(activity) && !AttachmentsVisible(c)) {
                        return SendButtonAction.TEXT;
                    } else {
                        if (!"none".equals(setting) && UIHelper.receivedLocationQuestion(c.getLatestMessage())) {
                            return SendButtonAction.SEND_LOCATION;
                        } else {
                            if ("recent".equals(setting)) {
                                setting = preferences.getString(ConversationFragment.RECENTLY_USED_QUICK_ACTION, SendButtonAction.TEXT.toString());
                                return SendButtonAction.valueOfOrDefault(setting);
                            } else {
                                return SendButtonAction.valueOfOrDefault(setting);
                            }
                        }
                    }
                }
            } else {
                return SendButtonAction.TEXT;
            }
        }
    }

    public static boolean AttachmentsVisible(Conversation conversation) {
        final boolean visible;
        visible = conversation.getMode() != Conversation.MODE_MULTI || conversation.getAccount().httpUploadAvailable() && conversation.getMucOptions().participating();
        return visible;
    }

    public static int getSendButtonImageResource(Activity activity, SendButtonAction action, Presence.Status status) {
        switch (action) {
            case TEXT:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_text_online;
                    case AWAY:
                        return R.drawable.ic_send_text_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_text_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_text_offline, R.drawable.ic_send_text_offline);
                }
            case RECORD_VIDEO:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_videocam_online;
                    case AWAY:
                        return R.drawable.ic_send_videocam_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_videocam_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_videocam_offline, R.drawable.ic_send_videocam_offline);
                }
            case TAKE_PHOTO:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_photo_online;
                    case AWAY:
                        return R.drawable.ic_send_photo_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_photo_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_photo_offline, R.drawable.ic_send_photo_offline);
                }
            case RECORD_VOICE:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_voice_online;
                    case AWAY:
                        return R.drawable.ic_send_voice_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_voice_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_voice_offline, R.drawable.ic_send_voice_offline);
                }
            case SEND_LOCATION:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_location_online;
                    case AWAY:
                        return R.drawable.ic_send_location_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_location_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_location_offline, R.drawable.ic_send_location_offline);
                }
            case CANCEL:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_cancel_online;
                    case AWAY:
                        return R.drawable.ic_send_cancel_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_cancel_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_cancel_offline, R.drawable.ic_send_cancel_offline);
                }
            case CHOOSE_PICTURE:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_picture_online;
                    case AWAY:
                        return R.drawable.ic_send_picture_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_picture_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_picture_offline, R.drawable.ic_send_picture_offline);
                }

            case CHOOSE_ATTACHMENT:
                switch (status) {
                    case CHAT:
                    case ONLINE:
                        return R.drawable.ic_send_attachment_online;
                    case AWAY:
                        return R.drawable.ic_send_attachment_away;
                    case XA:
                    case DND:
                        return R.drawable.ic_send_attachment_dnd;
                    default:
                        return getThemeResource(activity, R.attr.ic_send_attachment_offline, R.drawable.ic_send_attachment_offline);
                }
        }
        return getThemeResource(activity, R.attr.ic_send_text_offline, R.drawable.ic_send_text_offline);
    }

    private static int getThemeResource(Activity activity, int r_attr_name, int r_drawable_def) {
        int[] attrs = {r_attr_name};
        TypedArray ta = activity.getTheme().obtainStyledAttributes(attrs);

        int res = ta.getResourceId(0, r_drawable_def);
        ta.recycle();

        return res;
    }

    public static boolean quickShareChoice(Activity activity) {
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        return preferences.getBoolean(SettingsActivity.QUICK_SHARE_ATTACHMENT_CHOICE, activity.getResources().getBoolean(R.bool.quick_share_attachment_choice));
    }
}