summaryrefslogtreecommitdiff
path: root/base/src/main/java/com/smartdevicelink/managers/screen/BaseScreenManager.java
blob: 74ba6fc5875a544db4d7d59d643cd8b263bc6ca3 (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
/*
 * 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;

import android.support.annotation.NonNull;
import android.util.Log;

import com.smartdevicelink.managers.BaseSubManager;
import com.smartdevicelink.managers.CompletionListener;
import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.proxy.rpc.enums.MetadataType;
import com.smartdevicelink.proxy.rpc.enums.TextAlignment;
import com.smartdevicelink.util.DebugTool;

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

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

	private static String TAG = "ScreenManager";
	private final WeakReference<FileManager> fileManager;
	private SoftButtonManager softButtonManager;
	private TextAndGraphicManager textAndGraphicManager;

	// Sub manager listener
	private final CompletionListener subManagerListener = new CompletionListener() {
		@Override
		public synchronized void onComplete(boolean success) {
			if (softButtonManager != null && textAndGraphicManager != null) {
				if (softButtonManager.getState() == BaseSubManager.READY && textAndGraphicManager.getState() == BaseSubManager.READY) {
					DebugTool.logInfo("Starting screen manager, all sub managers are in ready state");
					transitionToState(READY);
				} else if (softButtonManager.getState() == BaseSubManager.ERROR && textAndGraphicManager.getState() == BaseSubManager.ERROR) {
					Log.e(TAG, "ERROR starting screen manager, both sub managers in error state");
					transitionToState(ERROR);
				} else if (textAndGraphicManager.getState() == BaseSubManager.SETTING_UP || softButtonManager.getState() == BaseSubManager.SETTING_UP) {
					DebugTool.logInfo("SETTING UP screen manager, one sub manager is still setting up");
					transitionToState(SETTING_UP);
				} else {
					Log.w(TAG, "LIMITED starting screen manager, one sub manager in error state and the other is ready");
					transitionToState(LIMITED);
				}
			} else {
				// We should never be here, but somehow one of the sub-sub managers is null
				Log.e(TAG, "ERROR one of the screen sub managers is null");
				transitionToState(ERROR);
			}
		}
	};

	BaseScreenManager(@NonNull ISdl internalInterface, @NonNull FileManager fileManager) {
		super(internalInterface);
		this.fileManager = new WeakReference<>(fileManager);
		initialize();
	}

	@Override
	public void start(CompletionListener listener) {
		super.start(listener);
		this.softButtonManager.start(subManagerListener);
		this.textAndGraphicManager.start(subManagerListener);
	}

	private void initialize(){
		if (fileManager.get() != null) {
			this.softButtonManager = new SoftButtonManager(internalInterface, fileManager.get());
			this.textAndGraphicManager = new TextAndGraphicManager(internalInterface, fileManager.get(), softButtonManager);
		}
	}

	/**
	 * <p>Called when manager is being torn down</p>
	 */
	@Override
	public void dispose() {
		softButtonManager.dispose();
		textAndGraphicManager.dispose();
		super.dispose();
	}

	/**
	 * Set the textField1 on the head unit screen
	 * Sending an empty String "" will clear the field
	 * @param textField1 String value represents the textField1
	 */
	public void setTextField1(String textField1) {
		this.softButtonManager.setCurrentMainField1(textField1);
		this.textAndGraphicManager.setTextField1(textField1);
	}

	/**
	 * Get the current textField1 value
	 * @return a String value represents the current textField1 value
	 */
	public String getTextField1() {
		return this.textAndGraphicManager.getTextField1();
	}

	/**
	 * Set the textField2 on the head unit screen
	 * Sending an empty String "" will clear the field
	 * @param textField2 String value represents the textField1
	 */
	public void setTextField2(String textField2) {
		this.textAndGraphicManager.setTextField2(textField2);
	}

	/**
	 * Get the current textField2 value
	 * @return a String value represents the current textField2 value
	 */
	public String getTextField2() {
		return this.textAndGraphicManager.getTextField2();
	}

	/**
	 * Set the textField3 on the head unit screen
	 * Sending an empty String "" will clear the field
	 * @param textField3 String value represents the textField1
	 */
	public void setTextField3(String textField3) {
		this.textAndGraphicManager.setTextField3(textField3);
	}

	/**
	 * Get the current textField3 value
	 * @return a String value represents the current textField3 value
	 */
	public String getTextField3() {
		return this.textAndGraphicManager.getTextField3();
	}

	/**
	 * Set the textField4 on the head unit screen
	 * Sending an empty String "" will clear the field
	 * @param textField4 String value represents the textField1
	 */
	public void setTextField4(String textField4) {
		this.textAndGraphicManager.setTextField4(textField4);
	}

	/**
	 * Get the current textField4 value
	 * @return a String value represents the current textField4 value
	 */
	public String getTextField4() {
		return this.textAndGraphicManager.getTextField4();
	}

	/**
	 * Set the mediaTrackTextField on the head unit screen
	 * @param mediaTrackTextField String value represents the mediaTrackTextField
	 */
	public void setMediaTrackTextField(String mediaTrackTextField) {
		this.textAndGraphicManager.setMediaTrackTextField(mediaTrackTextField);
	}

	/**
	 * Get the current mediaTrackTextField value
	 * @return a String value represents the current mediaTrackTextField
	 */
	public String getMediaTrackTextField() {
		return this.textAndGraphicManager.getMediaTrackTextField();
	}

	/**
	 * Set the primaryGraphic on the head unit screen
	 * @param primaryGraphic an SdlArtwork object represents the primaryGraphic
	 */
	public void setPrimaryGraphic(SdlArtwork primaryGraphic) {
		if (primaryGraphic == null){
			primaryGraphic = textAndGraphicManager.getBlankArtwork();
		}
		this.textAndGraphicManager.setPrimaryGraphic(primaryGraphic);
	}

	/**
	 * Get the current primaryGraphic value
	 * @return an SdlArtwork object represents the current primaryGraphic
	 */
	public SdlArtwork getPrimaryGraphic() {
		return this.textAndGraphicManager.getPrimaryGraphic();
	}

	/**
	 * Set the secondaryGraphic on the head unit screen
	 * @param secondaryGraphic an SdlArtwork object represents the secondaryGraphic
	 */
	public void setSecondaryGraphic(SdlArtwork secondaryGraphic) {
		if (secondaryGraphic == null){
			secondaryGraphic = textAndGraphicManager.getBlankArtwork();
		}
		this.textAndGraphicManager.setSecondaryGraphic(secondaryGraphic);
	}

	/**
	 * Get the current secondaryGraphic value
	 * @return an SdlArtwork object represents the current secondaryGraphic
	 */
	public SdlArtwork getSecondaryGraphic() {
		return this.textAndGraphicManager.getSecondaryGraphic();
	}

	/**
	 * Set the alignment for the text fields
	 * @param textAlignment TextAlignment value represents the alignment for the text fields
	 */
	public void setTextAlignment(TextAlignment textAlignment) {
		this.textAndGraphicManager.setTextAlignment(textAlignment);
	}

	/**
	 * Get the alignment for the text fields
	 * @return a TextAlignment value represents the alignment for the text fields
	 */
	public TextAlignment getTextAlignment() {
		return this.textAndGraphicManager.getTextAlignment();
	}

	/**
	 * Set the metadata type for the textField1
	 * @param textField1Type a MetadataType value represents the metadata for textField1
	 */
	public void setTextField1Type(MetadataType textField1Type) {
		this.textAndGraphicManager.setTextField1Type(textField1Type);
	}

	/**
	 * Get the metadata type for textField1
	 * @return a MetadataType value represents the metadata for textField1
	 */
	public MetadataType getTextField1Type() {
		return this.textAndGraphicManager.getTextField1Type();
	}

	/**
	 * Set the metadata type for the textField2
	 * @param textField2Type a MetadataType value represents the metadata for textField2
	 */
	public void setTextField2Type(MetadataType textField2Type) {
		this.textAndGraphicManager.setTextField2Type(textField2Type);
	}

	/**
	 * Get the metadata type for textField2
	 * @return a MetadataType value represents the metadata for textField2
	 */
	public MetadataType getTextField2Type() {
		return this.textAndGraphicManager.getTextField2Type();
	}

	/**
	 * Set the metadata type for the textField3
	 * @param textField3Type a MetadataType value represents the metadata for textField3
	 */
	public void setTextField3Type(MetadataType textField3Type) {
		this.textAndGraphicManager.setTextField3Type(textField3Type);
	}

	/**
	 * Get the metadata type for textField3
	 * @return a MetadataType value represents the metadata for textField3
	 */
	public MetadataType getTextField3Type() {
		return this.textAndGraphicManager.getTextField3Type();
	}

	/**
	 * Set the metadata type for the textField4
	 * @param textField4Type a MetadataType value represents the metadata for textField4
	 */
	public void setTextField4Type(MetadataType textField4Type) {
		this.textAndGraphicManager.setTextField4Type(textField4Type);
	}

	/**
	 * Get the metadata type for textField4
	 * @return a MetadataType value represents the metadata for textField4
	 */
	public MetadataType getTextField4Type() {
		return this.textAndGraphicManager.getTextField4Type();
	}

	/**
	 * Set softButtonObjects list and upload the images to the head unit
	 * @param softButtonObjects the list of the SoftButtonObject values that should be displayed on the head unit
	 */
	public void setSoftButtonObjects(@NonNull List<SoftButtonObject> softButtonObjects) {
		softButtonManager.setSoftButtonObjects(softButtonObjects);
	}

	/**
	 * Get the soft button objects list
	 * @return a List<SoftButtonObject>
	 */
	public List<SoftButtonObject> getSoftButtonObjects() {
		return softButtonManager.getSoftButtonObjects();
	}

	/**
	 * Get the SoftButtonObject that has the provided name
	 * @param name a String value that represents the name
	 * @return a SoftButtonObject
	 */
	public SoftButtonObject getSoftButtonObjectByName(@NonNull String name){
		return softButtonManager.getSoftButtonObjectByName(name);
	}

	/**
	 * Get the SoftButtonObject that has the provided buttonId
	 * @param buttonId a int value that represents the id of the button
	 * @return a SoftButtonObject
	 */
	public SoftButtonObject getSoftButtonObjectById(int buttonId){
		return softButtonManager.getSoftButtonObjectById(buttonId);
	}

	/**
	 * Begin a multiple updates transaction. The updates will be applied when commit() is called<br>
	 * Note: if we don't use beginTransaction & commit, every update will be sent individually.
	 */
	public void beginTransaction(){
		softButtonManager.setBatchUpdates(true);
		textAndGraphicManager.setBatchUpdates(true);
	}

	/**
	 * Send the updates that were started after beginning the transaction
	 * @param listener a CompletionListener that has a callback that will be called when the updates are finished
	 */
	public void commit(final CompletionListener listener){
		softButtonManager.setBatchUpdates(false);
		softButtonManager.update(new CompletionListener() {
			boolean updateSuccessful = true;
			@Override
			public void onComplete(boolean success) {
				if (!success){
					updateSuccessful = false;
				}
				textAndGraphicManager.setBatchUpdates(false);
				textAndGraphicManager.update(new CompletionListener() {
					@Override
					public void onComplete(boolean success) {
						if (!success){
							updateSuccessful = false;
						}
						if (listener != null) {
							listener.onComplete(updateSuccessful);
						}
					}
				});
			}
		});
	}

}