summaryrefslogtreecommitdiff
path: root/sdl_android/src/main/java/com/smartdevicelink/managers/BaseSubManager.java
blob: dc0abe067fffbc652fc9bd49fc91ff4faed72539 (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
package com.smartdevicelink.managers;

import android.support.annotation.NonNull;

import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.transport.utl.TransportRecord;

import java.util.List;

/**
 * <strong>BaseSubManager</strong> <br>
 *
 * Note: This class is extended by SubManagers <br>
 *
 * It is broken down to these areas: <br>
 *
 * 1. <br>
 */
public abstract class BaseSubManager {

	// states - if this gets more complicated we can move elsewhere
	private int state;
	private final Object STATE_LOCK = new Object();
	public static final int SETTING_UP = 0x00, READY = 0x30, LIMITED = 0x50, SHUTDOWN = 0x80, ERROR = 0xC0;
	protected final ISdl internalInterface;
	private CompletionListener completionListener;

	public BaseSubManager(@NonNull ISdl internalInterface){
		this.internalInterface = internalInterface;
		transitionToState(SETTING_UP);
	}

	/**
	 * Starts up a BaseSubManager, and calls provided callback once BaseSubManager is done setting up or failed setup.
	 * @param listener CompletionListener that is called once the BaseSubManager's state is READY, LIMITED, or ERROR
	 */
	public void start(CompletionListener listener){
		this.completionListener = listener;
		int state = getState();
		if((state == READY || state == LIMITED || state == ERROR) && completionListener != null){
			completionListener.onComplete(state == READY || state == LIMITED);
			completionListener = null;
		}
	}

	/**
	 * <p>Called when manager is being torn down</p>
	 */
	public void dispose(){
		transitionToState(SHUTDOWN);
	}

	public void transitionToState(int state) {
		synchronized (STATE_LOCK) {
			this.state = state;
		}
		if((state == READY || state == LIMITED || state == ERROR) && completionListener != null ){
			completionListener.onComplete(state == READY || state == LIMITED);
			completionListener = null;
		}
	}

	public int getState() {
		synchronized (STATE_LOCK) {
			return state;
		}
	}

	//This allows the method to not be exposed to developers
	protected void handleTransportUpdated(List<TransportRecord> connectedTransports, boolean audioStreamTransportAvail, boolean videoStreamTransportAvail){
		this.onTransportUpdate(connectedTransports,audioStreamTransportAvail,videoStreamTransportAvail);
	}

	/**
	 * Transport status has been updated
	 * @param connectedTransports currently connected transports
	 * @param audioStreamTransportAvail if there is a transport that could be used to carry the
	 *                                     audio service
	 * @param videoStreamTransportAvail if there is a transport that could be used to carry the
	 *                                     video service
	 */
	protected void onTransportUpdate(List<TransportRecord> connectedTransports, boolean audioStreamTransportAvail, boolean videoStreamTransportAvail){}
}