summaryrefslogtreecommitdiff
path: root/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/ChoiceCell.java
blob: 7d59bf5554bcf21d60b0eeed7303429970d635b8 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (c) 2019 Livio, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 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.
 *
 * Neither the name of the Livio Inc. 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 com.smartdevicelink.managers.screen.choiceset;

import androidx.annotation.NonNull;

import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
import com.smartdevicelink.util.DebugTool;

import java.util.ArrayList;
import java.util.List;

public class ChoiceCell implements Cloneable{
    private String text, secondaryText, tertiaryText, uniqueText;
    private List<String> voiceCommands;
    private SdlArtwork artwork, secondaryArtwork;
    private Integer choiceId;

    /**
     * MAX ID for cells - Cannot use Integer.MAX_INT as the value is too high.
     */
    private static final int MAX_ID = 2000000000;

    /**
     * Initialize the cell with text and nothing else.
     *
     * @param text - The primary text of the cell.
     */
    public ChoiceCell(@NonNull String text) {
        setText(text);
        setUniqueText(text);
        setChoiceId(MAX_ID);
    }

    /**
     * Initialize the cell with text, optional artwork, and optional voice commands
     *
     * @param text          - The primary text of the cell
     * @param voiceCommands - Strings that can be spoken by the user to activate this cell in a voice or both interaction mode
     * @param artwork       - The primary artwork of the cell
     */
    public ChoiceCell(@NonNull String text, List<String> voiceCommands, SdlArtwork artwork) {
        setText(text);
        setUniqueText(text);
        setVoiceCommands(voiceCommands);
        setArtwork(artwork);
        setChoiceId(MAX_ID);
    }

    /**
     * Initialize the cell with all optional items
     *
     * @param text             - The primary text
     * @param secondaryText    - The secondary text
     * @param tertiaryText     - The tertiary text
     * @param voiceCommands    - Strings that can be spoken by the user to activate this cell in a voice or both interaction mode
     * @param artwork          - The primary artwork of the cell
     * @param secondaryArtwork - The secondary artwork of the cell
     */
    public ChoiceCell(@NonNull String text, String secondaryText, String tertiaryText, List<String> voiceCommands, SdlArtwork artwork, SdlArtwork secondaryArtwork) {
        setText(text);
        setSecondaryText(secondaryText);
        setTertiaryText(tertiaryText);
        setUniqueText(text);
        setVoiceCommands(voiceCommands);
        setArtwork(artwork);
        setSecondaryArtwork(secondaryArtwork);
        setChoiceId(MAX_ID);
    }

    /**
     * Maps to Choice.menuName. The primary text of the cell. Duplicates within an `ChoiceSet`
     * are not permitted and will result in the `ChoiceSet` failing to initialize.
     *
     * @return The primary text of the cell
     */
    public String getText() {
        return text;
    }

    /**
     * @param text - Maps to Choice.menuName. The primary text of the cell. Duplicates within an `ChoiceSet`
     *             are not permitted and will result in the `ChoiceSet` failing to initialize.
     */
    void setText(@NonNull String text) {
        this.text = text;
    }

    /**
     * Maps to Choice.secondaryText. Optional secondary text of the cell, if available. Duplicates
     * within an `ChoiceSet` are permitted.
     *
     * @return Optional secondary text of the cell
     */
    public String getSecondaryText() {
        return secondaryText;
    }

    /**
     * @param secondaryText - Maps to Choice.secondaryText. Optional secondary text of the cell, if
     *                      available. Duplicates within an `ChoiceSet` are permitted.
     */
    void setSecondaryText(String secondaryText) {
        this.secondaryText = secondaryText;
    }

    /**
     * Maps to Choice.tertiaryText. Optional tertiary text of the cell, if available. Duplicates within an `ChoiceSet` are permitted.
     *
     * @return Optional tertiary text of the cell
     */
    public String getTertiaryText() {
        return tertiaryText;
    }

    /**
     * @param tertiaryText - Maps to Choice.tertiaryText. Optional tertiary text of the cell, if
     *                     available. Duplicates within an `ChoiceSet` are permitted.
     */
    void setTertiaryText(String tertiaryText) {
        this.tertiaryText = tertiaryText;
    }

    /**
     * Maps to Choice.vrCommands. Optional voice commands the user can speak to activate the cell.
     * If not set and the head unit requires it, this will be set to the number in the list that this
     * item appears. However, this would be a very poor experience for a user if the choice set is
     * presented as a voice only interaction or both interaction mode. Therefore, consider not setting
     * this only when you know the choice set will be presented as a touch only interaction.
     *
     * @return The list of voice command strings
     */
    public List<String> getVoiceCommands() {
        return voiceCommands;
    }

    /**
     * @param voiceCommands - Maps to Choice.vrCommands. Optional voice commands the user can speak to activate the cell.
     *                      If not set and the head unit requires it, this will be set to the number in the list that this
     *                      item appears. However, this would be a very poor experience for a user if the choice set is
     *                      presented as a voice only interaction or both interaction mode. Therefore, consider not setting
     *                      this only when you know the choice set will be presented as a touch only interaction.
     */
    void setVoiceCommands(List<String> voiceCommands) {
        this.voiceCommands = voiceCommands;
    }

    /**
     * Maps to Choice.image. Optional image for the cell. This will be uploaded before the cell is
     * used when the cell is preloaded or presented for the first time.
     *
     * @return The SdlArtwork
     */
    public SdlArtwork getArtwork() {
        return artwork;
    }

    /**
     * @param artwork - Maps to Choice.image. Optional image for the cell. This will be uploaded
     *                before the cell is used when the cell is preloaded or presented for the first time.
     */
    void setArtwork(SdlArtwork artwork) {
        this.artwork = artwork;
    }

    /**
     * Maps to Choice.secondaryImage. Optional secondary image for the cell. This will be uploaded
     * before the cell is used when the cell is preloaded or presented for the first time.
     *
     * @return The SdlArtwork
     */
    public SdlArtwork getSecondaryArtwork() {
        return secondaryArtwork;
    }

    /**
     * @param secondaryArtwork - Maps to Choice.secondaryImage. Optional secondary image for the cell.
     *                         This will be uploaded before the cell is used when the cell is preloaded or presented for the first time.
     */
    void setSecondaryArtwork(SdlArtwork secondaryArtwork) {
        this.secondaryArtwork = secondaryArtwork;
    }

    /**
     * NOTE: USED INTERNALLY
     * Set the choice Id.
     *
     * @param choiceId - the choice Id
     */
    void setChoiceId(int choiceId) {
        this.choiceId = choiceId;
    }

    /**
     * NOTE: USED INTERNALLY
     * Get the choiceId
     *
     * @return the choiceId for this Choice Cell
     */
    int getChoiceId() {
        return choiceId;
    }

    /**
     * NOTE: USED INTERNALLY
     * Primary text of the cell to be displayed on the module. Used to distinguish cells with the
     * same `text` but other fields are different. This is autogenerated by the screen manager.
     * Attempting to use cells that are exactly the same (all text and artwork fields are the same)
     * will not cause this to be used. This will not be used when connected to modules supporting RPC 7.1+.
     *
     * @param uniqueText - the uniqueText to be used in place of primaryText when core does not support identical names for ChoiceSets
     */
    void setUniqueText(String uniqueText) {
        this.uniqueText = uniqueText;
    }

    /**
     * NOTE: USED INTERNALLY
     * Get the uniqueText that was used in place of primaryText
     *
     * @return the uniqueText for this Choice Cell
     */
    String getUniqueText() {
        return uniqueText;
    }

    @Override
    public int hashCode() {
        int result = 1;
        result += ((getText() == null) ? 0 : Integer.rotateLeft(getText().hashCode(), 1));
        result += ((getSecondaryText() == null) ? 0 : Integer.rotateLeft(getSecondaryText().hashCode(), 2));
        result += ((getTertiaryText() == null) ? 0 : Integer.rotateLeft(getTertiaryText().hashCode(), 3));
        result += ((getArtwork() == null) ? 0 : Integer.rotateLeft(getArtwork().hashCode(), 4));
        result += ((getSecondaryArtwork() == null) ? 0 : Integer.rotateLeft(getSecondaryArtwork().hashCode(), 5));
        result += ((getVoiceCommands() == null) ? 0 : Integer.rotateLeft(getVoiceCommands().hashCode(), 6));
        return result;
    }

    /**
     * Uses our custom hashCode for ChoiceCell objects
     *
     * @param o - The object to compare
     * @return boolean of whether the objects are the same or not
     */
    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        // if this is the same memory address, its the same
        if (this == o) return true;
        // if this is not an instance of this class, not the same
        if (!(o instanceof ChoiceCell)) return false;
        // return comparison
        return hashCode() == o.hashCode();
    }

    /**
     * @return A string description of the cell, useful for debugging.
     */
    @Override
    @NonNull
    public String toString() {
        return "ChoiceCell: ID: " + this.choiceId + " Text: " + text + " - Secondary Text: " + secondaryText + " - Tertiary Text: " + tertiaryText + " " +
                (text.equals(uniqueText) ? "" : "| Unique Text: " + uniqueText) + " | Artwork Names: " + ((getArtwork() == null || getArtwork().getName() == null) ? "Primary Art null" : getArtwork().getName())
                + " Secondary Art - " + ((getSecondaryArtwork() == null || getSecondaryArtwork().getName() == null) ? "Secondary Art null" : getSecondaryArtwork().getName()) +
                " | Voice Commands Size: " + ((getVoiceCommands() == null) ? 0 : getVoiceCommands().size());
    }

    /**
     * Creates a deep copy of the object
     *
     * @return deep copy of the object, null if an exception occurred
     */
    @Override
    public ChoiceCell clone() {
        try {
            ChoiceCell clone = (ChoiceCell) super.clone();
            if (this.artwork != null) {
                clone.artwork = this.artwork.clone();
            }
            if (this.secondaryArtwork != null) {
                clone.secondaryArtwork = this.secondaryArtwork.clone();
            }
            if (this.voiceCommands != null) {
                clone.voiceCommands = new ArrayList<>(voiceCommands);
            }

            return clone;
        } catch (CloneNotSupportedException e) {
            if (DebugTool.isDebugEnabled()) {
                throw new RuntimeException("Clone not supported by super class");
            }
        }

        return null;
    }
}