summaryrefslogtreecommitdiff
path: root/base/src/main/java/com/smartdevicelink/managers/screen/BaseTextAndGraphicManager.java
blob: 3542dd269ea12883983107317ba5dd73925dffb1 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
 * Copyright (c) 2019 - 2020 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;

import androidx.annotation.NonNull;

import com.livio.taskmaster.Queue;
import com.livio.taskmaster.Task;
import com.smartdevicelink.managers.BaseSubManager;
import com.smartdevicelink.managers.CompletionListener;
import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
import com.smartdevicelink.managers.lifecycle.OnSystemCapabilityListener;
import com.smartdevicelink.managers.lifecycle.SystemCapabilityManager;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCNotification;
import com.smartdevicelink.proxy.rpc.DisplayCapability;
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
import com.smartdevicelink.proxy.rpc.enums.MetadataType;
import com.smartdevicelink.proxy.rpc.enums.PredefinedWindows;
import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType;
import com.smartdevicelink.proxy.rpc.enums.TextAlignment;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.util.DebugTool;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import static com.smartdevicelink.proxy.rpc.enums.TextAlignment.CENTERED;

/**
 * <strong>TextAndGraphicManager</strong> <br>
 * <p>
 * Note: This class must be accessed through the SdlManager. Do not instantiate it by itself. <br>
 */
abstract class BaseTextAndGraphicManager extends BaseSubManager {

	private static final String TAG = "TextAndGraphicManager";

	boolean isDirty;
	Show currentScreenData;
	HMILevel currentHMILevel;
	private final WeakReference<SoftButtonManager> softButtonManager;
	WindowCapability defaultMainWindowCapability;
	private boolean batchingUpdates;
	private final WeakReference<FileManager> fileManager;
	SdlArtwork blankArtwork;
	private OnRPCNotificationListener hmiListener;
	private OnSystemCapabilityListener onDisplaysCapabilityListener;
	private SdlArtwork primaryGraphic, secondaryGraphic;
	private TextAlignment textAlignment;
	private String textField1, textField2, textField3, textField4, mediaTrackTextField, title;
	private MetadataType textField1Type, textField2Type, textField3Type, textField4Type;
	TextAndGraphicUpdateOperation updateOperation;
	private CompletionListener currentOperationListener;
	Queue transactionQueue;

	//Constructors

	BaseTextAndGraphicManager(@NonNull ISdl internalInterface, @NonNull FileManager fileManager, @NonNull SoftButtonManager softButtonManager) {
		// set class vars
		super(internalInterface);
		this.fileManager = new WeakReference<>(fileManager);
		this.softButtonManager = new WeakReference<>(softButtonManager);
		batchingUpdates = false;
		isDirty = false;
		textAlignment = CENTERED;
		currentHMILevel = HMILevel.HMI_NONE;
		currentScreenData = new Show();
		this.transactionQueue = newTransactionQueue();
		addListeners();
	}

	@Override
	public void start(CompletionListener listener) {
		transitionToState(READY);
		super.start(listener);
	}

	@Override
	public void dispose() {
		textField1 = null;
		textField1Type = null;
		textField2 = null;
		textField2Type = null;
		textField3 = null;
		textField3Type = null;
		textField4 = null;
		textField4Type = null;
		mediaTrackTextField = null;
		title = null;
		textAlignment = null;
		primaryGraphic = null;
		secondaryGraphic = null;
		blankArtwork = null;
		defaultMainWindowCapability = null;
		currentScreenData = null;
		isDirty = false;
		updateOperation = null;

		// Cancel the operations
		if (transactionQueue != null) {
			transactionQueue.close();
			transactionQueue = null;
		}

		// remove listeners
		internalInterface.removeOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, hmiListener);
		if (internalInterface.getSystemCapabilityManager() != null) {
			internalInterface.getSystemCapabilityManager().removeOnSystemCapabilityListener(SystemCapabilityType.DISPLAYS, onDisplaysCapabilityListener);
		}

		super.dispose();
	}

	private Queue newTransactionQueue() {
		Queue queue = internalInterface.getTaskmaster().createQueue("TextAndGraphicManager", 3, false);
		queue.pause();
		return queue;
	}

	// Suspend the queue if the WindowCapabilities are null
	// OR if the HMI level is NONE since we want to delay sending RPCs until we're in non-NONE
	private void updateTransactionQueueSuspended() {
		if (defaultMainWindowCapability == null || HMILevel.HMI_NONE.equals(currentHMILevel)) {
			DebugTool.logInfo(TAG, String.format("Suspending the transaction queue. Current HMI level is NONE: %b, window capabilities are null: %b", HMILevel.HMI_NONE.equals(currentHMILevel), defaultMainWindowCapability == null));
			transactionQueue.pause();
		} else {
			DebugTool.logInfo(TAG, "Starting the transaction queue");
			transactionQueue.resume();
		}
	}

	// Upload / Send

	protected void update(CompletionListener listener) {
		// check if is batch update
		if (batchingUpdates) {
			return;
		}
		if (isDirty) {
			isDirty = false;
			sdlUpdate(listener);
		} else if (listener != null) {
			listener.onComplete(true);
		}
	}

	private synchronized void sdlUpdate(final CompletionListener listener) {
		if (this.transactionQueue.getTasksAsList().size() > 0) {
			// Transactions already in queue, we need to clear it out
			transactionQueue.clear();
			updateOperation = null;
			if (currentOperationListener != null) {
				currentOperationListener.onComplete(false);
			}
		}

		// Task can be READY, about to start and popped of the queue, so we have to cancel it, to prevent it from starting
		if (updateOperation != null && updateOperation.getState() == Task.READY) {
			updateOperation.cancelTask();
			if (currentOperationListener != null) {
				currentOperationListener.onComplete(false);
			}
		}

		// If Task is IN_PROGRESS, it’s not on the queue, we need to mark it as cancelled. The task will return at some point when it checks its status and call the listener back
		if (updateOperation != null && updateOperation.getState() == Task.IN_PROGRESS) {
			updateOperation.cancelTask();
		}

		currentOperationListener = listener;

		CurrentScreenDataUpdatedListener currentScreenDataUpdateListener = new CurrentScreenDataUpdatedListener() {
			@Override
			public void onUpdate(Show show) {
				updatePendingOperationsWithNewScreenData(show);
				currentScreenData = show;
			}
		};

		updateOperation = new TextAndGraphicUpdateOperation(internalInterface, fileManager.get(), defaultMainWindowCapability, currentScreenData, currentState(), currentOperationListener, currentScreenDataUpdateListener);
		transactionQueue.add(updateOperation, false);
	}

	//Updates pending task with current screen data
	void updatePendingOperationsWithNewScreenData(Show newScreenData) {
		for (Task task : transactionQueue.getTasksAsList()) {
			if (!(task instanceof TextAndGraphicUpdateOperation)) {
				continue;
			}
			((TextAndGraphicUpdateOperation) task).setCurrentScreenData(newScreenData);
		}
		if (this.softButtonManager.get() != null && newScreenData.getMainField1() != null) {
			this.softButtonManager.get().setCurrentMainField1(currentScreenData.getMainField1());
		}
	}

	interface CurrentScreenDataUpdatedListener {
		void onUpdate(Show show);
	}


	private List<String> findNonNullTextFields() {
		List<String> array = new ArrayList<>();

		if (textField1 != null) {
			array.add(textField1);
		}

		if (textField2 != null) {
			array.add(textField2);
		}

		if (textField3 != null) {
			array.add(textField3);
		}

		if (textField4 != null) {
			array.add(textField4);
		}

		if (title != null) {
			array.add(title);
		}

		if (mediaTrackTextField != null) {
			array.add(mediaTrackTextField);
		}

		return array;
	}

	Boolean hasData() {
		boolean hasTextFields = (findNonNullTextFields().size() > 0);
		boolean hasImageFields = (primaryGraphic != null) || (secondaryGraphic != null);

		return hasTextFields || hasImageFields;
	}

	abstract SdlArtwork getBlankArtwork();


	// Convert to State

	private TextsAndGraphicsState currentState() {
		return new TextsAndGraphicsState(textField1, textField2, textField3, textField4, mediaTrackTextField,
				title, primaryGraphic, secondaryGraphic, textAlignment, textField1Type, textField2Type, textField3Type, textField4Type);
	}

	// Getters / Setters

	void setTextAlignment(TextAlignment textAlignment) {
		this.textAlignment = textAlignment;
		// If we aren't batching, send the update immediately, if we are, set ourselves as dirty (so we know we should send an update after the batch ends)
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	TextAlignment getTextAlignment() {
		return textAlignment;
	}

	void setMediaTrackTextField(String mediaTrackTextField) {
		this.mediaTrackTextField = mediaTrackTextField;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	String getMediaTrackTextField() {
		return mediaTrackTextField;
	}

	void setTextField1(String textField1) {
		this.textField1 = textField1;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	String getTextField1() {
		return textField1;
	}

	void setTextField2(String textField2) {
		this.textField2 = textField2;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	String getTextField2() {
		return textField2;
	}

	void setTextField3(String textField3) {
		this.textField3 = textField3;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	String getTextField3() {
		return textField3;
	}

	void setTextField4(String textField4) {
		this.textField4 = textField4;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	String getTextField4() {
		return textField4;
	}

	void setTextField1Type(MetadataType textField1Type) {
		this.textField1Type = textField1Type;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	MetadataType getTextField1Type() {
		return textField1Type;
	}

	void setTextField2Type(MetadataType textField2Type) {
		this.textField2Type = textField2Type;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	MetadataType getTextField2Type() {
		return textField2Type;
	}

	void setTextField3Type(MetadataType textField3Type) {
		this.textField3Type = textField3Type;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	MetadataType getTextField3Type() {
		return textField3Type;
	}

	void setTextField4Type(MetadataType textField4Type) {
		this.textField4Type = textField4Type;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	MetadataType getTextField4Type() {
		return textField4Type;
	}

	void setTitle(String title) {
		this.title = title;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	String getTitle() {
		return title;
	}

	void setPrimaryGraphic(SdlArtwork primaryGraphic) {
		this.primaryGraphic = primaryGraphic;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	SdlArtwork getPrimaryGraphic() {
		return primaryGraphic;
	}

	void setSecondaryGraphic(SdlArtwork secondaryGraphic) {
		this.secondaryGraphic = secondaryGraphic;
		if (!batchingUpdates) {
			sdlUpdate(null);
		} else {
			isDirty = true;
		}
	}

	SdlArtwork getSecondaryGraphic() {
		return secondaryGraphic;
	}

	void setBatchUpdates(boolean batching) {
		this.batchingUpdates = batching;
	}

	private void addListeners() {
		// add listener
		hmiListener = new OnRPCNotificationListener() {
			@Override
			public void onNotified(RPCNotification notification) {
				OnHMIStatus onHMIStatus = (OnHMIStatus) notification;
				if (onHMIStatus.getWindowID() != null && onHMIStatus.getWindowID() != PredefinedWindows.DEFAULT_WINDOW.getValue()) {
					return;
				}
				currentHMILevel = onHMIStatus.getHmiLevel();
				updateTransactionQueueSuspended();
			}
		};
		internalInterface.addOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, hmiListener);

		onDisplaysCapabilityListener = new OnSystemCapabilityListener() {
			@Override
			public void onCapabilityRetrieved(Object capability) {
				// instead of using the parameter it's more safe to use the convenience method
				List<DisplayCapability> capabilities = SystemCapabilityManager.convertToList(capability, DisplayCapability.class);
				if (capabilities == null || capabilities.size() == 0) {
					DebugTool.logError(TAG, "TextAndGraphic Manager - Capabilities sent here are null or empty");
					defaultMainWindowCapability = null;
				} else {
					DisplayCapability display = capabilities.get(0);
					for (WindowCapability windowCapability : display.getWindowCapabilities()) {
						int currentWindowID = windowCapability.getWindowID() != null ? windowCapability.getWindowID() : PredefinedWindows.DEFAULT_WINDOW.getValue();
						if (currentWindowID == PredefinedWindows.DEFAULT_WINDOW.getValue()) {
							defaultMainWindowCapability = windowCapability;
						}
					}
				}
				// Update the queue's suspend state
				updateTransactionQueueSuspended();
				if (hasData()) {
					sdlUpdate(null);
				}
			}

			@Override
			public void onError(String info) {
				DebugTool.logError(TAG, "Display Capability cannot be retrieved");
				defaultMainWindowCapability = null;
				updateTransactionQueueSuspended();
			}
		};
		if (internalInterface.getSystemCapabilityManager() != null) {
			this.internalInterface.getSystemCapabilityManager().addOnSystemCapabilityListener(SystemCapabilityType.DISPLAYS, onDisplaysCapabilityListener);
		}
	}
}