summaryrefslogtreecommitdiff
path: root/base/src/main/java/com/smartdevicelink/managers/screen/SoftButtonReplaceOperation.java
blob: 1068065626fbb41a50ea20b12c4e5ebe1c735851 (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
329
330
331
332
333
334
335
336
337
338
package com.smartdevicelink.managers.screen;

import com.livio.taskmaster.Task;
import com.smartdevicelink.managers.CompletionListener;
import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.managers.file.MultipleFileCompletionListener;
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
import com.smartdevicelink.proxy.RPCResponse;
import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.SoftButton;
import com.smartdevicelink.proxy.rpc.SoftButtonCapabilities;
import com.smartdevicelink.proxy.rpc.enums.Result;
import com.smartdevicelink.proxy.rpc.enums.SoftButtonType;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener;
import com.smartdevicelink.util.DebugTool;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Bilal Alsharifi on 6/15/20.
 */
class SoftButtonReplaceOperation extends Task {
    private static final String TAG = "SoftButtonReplaceOperation";
    private final WeakReference<ISdl> internalInterface;
    private final WeakReference<FileManager> fileManager;
    private final SoftButtonCapabilities softButtonCapabilities;
    private final CopyOnWriteArrayList<SoftButtonObject> softButtonObjects;
    private String currentMainField1;

    SoftButtonReplaceOperation(ISdl internalInterface, FileManager fileManager, SoftButtonCapabilities softButtonCapabilities, CopyOnWriteArrayList<SoftButtonObject> softButtonObjects, String currentMainField1) {
        super("SoftButtonReplaceOperation");
        this.internalInterface = new WeakReference<>(internalInterface);
        this.fileManager = new WeakReference<>(fileManager);
        this.softButtonCapabilities = softButtonCapabilities;
        this.softButtonObjects = softButtonObjects;
        this.currentMainField1 = currentMainField1;
    }

    @Override
    public void onExecute() {
        start();
    }

    private void start() {
        if (getState() == Task.CANCELED) {
            return;
        }

        // Check the state of our images
        if (!supportsSoftButtonImages()) {
            // We don't support images at all
            DebugTool.logWarning(TAG, "Soft button images are not supported. Attempting to send text-only soft buttons. If any button does not contain text, no buttons will be sent.");

            // Send text buttons if all the soft buttons have text
            sendCurrentStateTextOnlySoftButtons(new CompletionListener() {
                @Override
                public void onComplete(boolean success) {
                    if (!success) {
                        DebugTool.logError(TAG, "Head unit does not support images and some of the soft buttons do not have text, so none of the buttons will be sent.");
                    }
                    onFinished();
                }
            });
        } else if (currentStateHasImages() && !allCurrentStateImagesAreUploaded()) {
            // If there are images that aren't uploaded
            // Send text buttons if all the soft buttons have text
            sendCurrentStateTextOnlySoftButtons(null);

            // Upload initial images
            uploadInitialStateImages(new CompletionListener() {
                @Override
                public void onComplete(boolean success) {
                    // Send initial soft buttons w/ images
                    sendCurrentStateSoftButtons(new CompletionListener() {
                        @Override
                        public void onComplete(boolean success) {
                            // Upload other images
                            uploadOtherStateImages(new CompletionListener() {
                                @Override
                                public void onComplete(boolean success) {
                                    DebugTool.logInfo(TAG, "Finished sending other images for soft buttons");
                                    onFinished();
                                }
                            });
                        }
                    });
                }
            });
        } else {
            // All the images are already uploaded. Send initial soft buttons w/ images.
            sendCurrentStateSoftButtons(new CompletionListener() {
                @Override
                public void onComplete(boolean success) {
                    DebugTool.logInfo(TAG, "Finished sending soft buttons with images");
                    // Upload other images
                    uploadOtherStateImages(new CompletionListener() {
                        @Override
                        public void onComplete(boolean success) {
                            if (success) {
                                DebugTool.logInfo(TAG, "Finished sending other images for soft buttons");
                            }
                            onFinished();
                        }
                    });
                }
            });
        }
    }

    private void uploadInitialStateImages(final CompletionListener completionListener) {
        // Upload all soft button images, the initial state images first, then the other states. We need to send updates when the initial state is ready.
        List<SdlArtwork> initialStatesToBeUploaded = new ArrayList<>();
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            SoftButtonState softButtonState = softButtonObject.getCurrentState();
            if (softButtonState != null && artworkNeedsUpload(softButtonState.getArtwork())) {
                initialStatesToBeUploaded.add(softButtonState.getArtwork());
            }
        }

        if (initialStatesToBeUploaded.isEmpty()) {
            DebugTool.logInfo(TAG, "No initial state artworks to upload");
            if (completionListener != null) {
                completionListener.onComplete(false);
            }
            return;
        }

        DebugTool.logInfo(TAG, "Uploading soft button initial artworks");
        if (fileManager.get() != null) {
            fileManager.get().uploadArtworks(initialStatesToBeUploaded, new MultipleFileCompletionListener() {
                @Override
                public void onComplete(Map<String, String> errors) {
                    if (errors != null) {
                        DebugTool.logError(TAG, "Error uploading soft button artworks: " + errors.keySet());
                    } else {
                        DebugTool.logInfo(TAG, "Soft button initial state artworks uploaded");
                    }

                    if (getState() == Task.CANCELED) {
                        onFinished();
                        if (completionListener != null) {
                            completionListener.onComplete(false);
                        }
                        return;
                    }
                    if (completionListener != null) {
                        completionListener.onComplete(true);
                    }
                }
            });
        }
    }

    private void uploadOtherStateImages(final CompletionListener completionListener) {
        // Upload all soft button images, the initial state images first, then the other states. We need to send updates when the initial state is ready.
        List<SdlArtwork> otherStatesToBeUploaded = new ArrayList<>();
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            for (SoftButtonState softButtonState : softButtonObject.getStates()) {
                if (softButtonState.getName().equals(softButtonObject.getCurrentState().getName())) {
                    continue;
                }
                if (artworkNeedsUpload(softButtonState.getArtwork())) {
                    otherStatesToBeUploaded.add(softButtonState.getArtwork());
                }
            }
        }

        if (otherStatesToBeUploaded.isEmpty()) {
            DebugTool.logInfo(TAG, "No other state artworks to upload");
            if (completionListener != null) {
                completionListener.onComplete(false);
            }
            return;
        }

        DebugTool.logInfo(TAG, "Uploading soft button other state artworks");
        if (fileManager.get() != null) {
            fileManager.get().uploadArtworks(otherStatesToBeUploaded, new MultipleFileCompletionListener() {
                @Override
                public void onComplete(Map<String, String> errors) {
                    if (errors != null) {
                        DebugTool.logError(TAG, "Error uploading soft button artworks: " + errors.keySet());
                    } else {
                        DebugTool.logInfo(TAG, "Soft button other state artworks uploaded");
                    }

                    if (getState() == Task.CANCELED) {
                        onFinished();
                        if (completionListener != null) {
                            completionListener.onComplete(false);
                        }
                        return;
                    }

                    if (completionListener != null) {
                        completionListener.onComplete(true);
                    }
                }
            });
        }
    }

    private void sendCurrentStateSoftButtons(final CompletionListener completionListener) {
        if (getState() == Task.CANCELED) {
            onFinished();
        }

        DebugTool.logInfo(TAG, "Preparing to send full soft buttons");
        List<SoftButton> softButtons = new ArrayList<>();
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            softButtons.add(softButtonObject.getCurrentStateSoftButton());
        }

        Show show = new Show();
        show.setOnRPCResponseListener(new OnRPCResponseListener() {
            @Override
            public void onResponse(int correlationId, RPCResponse response) {
                if (response.getSuccess()) {
                    DebugTool.logInfo(TAG, "Finished sending text only soft buttons");
                } else {
                    DebugTool.logWarning(TAG, "Failed to update soft buttons with text buttons");
                }
                if (completionListener != null) {
                    completionListener.onComplete(response.getSuccess());
                }
            }

            @Override
            public void onError(int correlationId, Result resultCode, String info) {
                DebugTool.logWarning(TAG, "Failed to update soft buttons with text buttons");
                if (completionListener != null) {
                    completionListener.onComplete(false);
                }
            }
        });
        show.setMainField1(currentMainField1);
        show.setSoftButtons(softButtons);
        if (internalInterface.get() != null) {
            internalInterface.get().sendRPC(show);
        }
    }


    // Returns text soft buttons representing the current states of the button objects, or returns if _any_ of the buttons' current states are image only buttons.
    private void sendCurrentStateTextOnlySoftButtons(final CompletionListener completionListener) {
        if (getState() == Task.CANCELED) {
            onFinished();
        }

        DebugTool.logInfo(TAG, "Preparing to send text-only soft buttons");
        List<SoftButton> textButtons = new ArrayList<>();
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            SoftButton softButton = softButtonObject.getCurrentStateSoftButton();
            if (softButton.getText() == null) {
                DebugTool.logWarning(TAG, "Attempted to create text buttons, but some buttons don't support text, so no text-only soft buttons will be sent");
                if (completionListener != null) {
                    completionListener.onComplete(false);
                }
                return;
            }

            // We should create a new softButtonObject rather than modifying the original one
            SoftButton textOnlySoftButton = new SoftButton(SoftButtonType.SBT_TEXT, softButton.getSoftButtonID());
            textOnlySoftButton.setText(softButton.getText());
            textOnlySoftButton.setSystemAction(softButton.getSystemAction());
            textOnlySoftButton.setIsHighlighted(softButton.getIsHighlighted());
            textButtons.add(textOnlySoftButton);
        }

        Show show = new Show();
        show.setOnRPCResponseListener(new OnRPCResponseListener() {
            @Override
            public void onResponse(int correlationId, RPCResponse response) {
                if (response.getSuccess()) {
                    DebugTool.logInfo(TAG, "Finished sending text only soft buttons");
                } else {
                    DebugTool.logWarning(TAG, "Failed to update soft buttons with text buttons");
                }
                if (completionListener != null) {
                    completionListener.onComplete(response.getSuccess());
                }
            }

            @Override
            public void onError(int correlationId, Result resultCode, String info) {
                DebugTool.logWarning(TAG, "Failed to update soft buttons with text buttons");
                if (completionListener != null) {
                    completionListener.onComplete(false);
                }
            }
        });
        show.setMainField1(currentMainField1);
        show.setSoftButtons(textButtons);
        if (internalInterface.get() != null) {
            internalInterface.get().sendRPC(show);
        }
    }

    private boolean artworkNeedsUpload(SdlArtwork artwork) {
        return (artwork != null
                && fileManager.get() != null
                && !fileManager.get().hasUploadedFile(artwork)
                && softButtonCapabilities.getImageSupported()
                && !artwork.isStaticIcon());
    }

    private boolean currentStateHasImages() {
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            if (softButtonObject.getCurrentState().getArtwork() != null) {
                return true;
            }
        }
        return false;
    }

    private boolean allCurrentStateImagesAreUploaded() {
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            SdlArtwork artwork = softButtonObject.getCurrentState().getArtwork();
            if (artworkNeedsUpload(artwork)) {
                return false;
            }
        }
        return true;
    }

    private boolean supportsSoftButtonImages() {
        return softButtonCapabilities.getImageSupported();
    }

    void setCurrentMainField1(String currentMainField1) {
        this.currentMainField1 = currentMainField1;
    }
}