748
|
1 |
/**
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.carbide.trk.support.status;
|
|
19 |
|
|
20 |
import java.util.ArrayList;
|
|
21 |
import java.util.List;
|
|
22 |
|
|
23 |
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
|
|
24 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
|
|
25 |
import com.nokia.carbide.remoteconnections.interfaces.IConnection;
|
|
26 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
|
|
27 |
import com.nokia.carbide.remoteconnections.interfaces.AbstractConnection.ConnectionStatus;
|
|
28 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
|
|
29 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
|
|
30 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
|
|
31 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
|
|
32 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
|
|
33 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus;
|
749
|
34 |
import com.nokia.carbide.trk.support.Messages;
|
748
|
35 |
import com.nokia.carbide.trk.support.connection.USBConnectionType;
|
|
36 |
import com.nokia.carbide.trk.support.service.TRKConnectedService;
|
|
37 |
import com.nokia.carbide.trk.support.service.TracingConnectedService;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* A singleton object that manages the device status of dynamic connections
|
|
41 |
* based on the status of the TRK and Tracing services.
|
|
42 |
*/
|
|
43 |
public class ConnectionStatusReconciler {
|
|
44 |
|
|
45 |
private class ConnectionListener implements IConnectionListener {
|
|
46 |
|
|
47 |
public void connectionAdded(IConnection connection) {
|
|
48 |
addConnection(connection);
|
|
49 |
}
|
|
50 |
|
|
51 |
public void connectionRemoved(IConnection connection) {
|
|
52 |
removeConnection(connection);
|
|
53 |
}
|
|
54 |
|
|
55 |
public void defaultConnectionSet(IConnection connection) {}
|
|
56 |
|
|
57 |
}
|
|
58 |
|
|
59 |
private class ServiceStatusListener implements IStatusChangedListener {
|
|
60 |
|
|
61 |
public void statusChanged(IStatus status) {
|
|
62 |
handleServiceStatusChange(status);
|
|
63 |
}
|
|
64 |
|
|
65 |
}
|
|
66 |
|
|
67 |
private static ConnectionStatusReconciler instance;
|
|
68 |
private IConnectionsManager manager;
|
|
69 |
private IConnectionListener connectionListener;
|
|
70 |
private List<IConnection2> handledConnections;
|
|
71 |
private ServiceStatusListener serviceStatusListener;
|
|
72 |
|
|
73 |
private ConnectionStatusReconciler() {
|
|
74 |
connectionListener = new ConnectionListener();
|
|
75 |
manager = RemoteConnectionsActivator.getConnectionsManager();
|
|
76 |
manager.addConnectionListener(connectionListener);
|
|
77 |
handledConnections = new ArrayList<IConnection2>();
|
|
78 |
serviceStatusListener = new ServiceStatusListener();
|
|
79 |
}
|
|
80 |
|
|
81 |
public static ConnectionStatusReconciler getInstance() {
|
|
82 |
if (instance == null)
|
|
83 |
instance = new ConnectionStatusReconciler();
|
|
84 |
|
|
85 |
return instance;
|
|
86 |
}
|
|
87 |
|
|
88 |
public void dispose() {
|
|
89 |
manager.removeConnectionListener(connectionListener);
|
|
90 |
for (IConnection2 connection : new ArrayList<IConnection2>(handledConnections)) {
|
|
91 |
removeConnection(connection);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
private boolean handlesConnection(IConnection connection) {
|
|
96 |
// only manage status for USBConnectionTypes for now
|
|
97 |
// in future other IConnection2 types may also be managed
|
|
98 |
return connection.getConnectionType() instanceof USBConnectionType;
|
|
99 |
}
|
|
100 |
|
|
101 |
private boolean isSysTRK(TRKConnectedService service) {
|
|
102 |
String value = service.getProperties().get(TRKConnectedService.PROP_SYS_TRK);
|
|
103 |
return value != null && Boolean.getBoolean(value);
|
|
104 |
}
|
|
105 |
|
|
106 |
private void addConnection(IConnection connection) {
|
|
107 |
if (!handlesConnection(connection))
|
|
108 |
return;
|
|
109 |
|
|
110 |
handledConnections.add((IConnection2) connection);
|
|
111 |
for (IConnectedService service : manager.getConnectedServices(connection)) {
|
|
112 |
if (service instanceof TRKConnectedService ||
|
|
113 |
service instanceof TracingConnectedService) {
|
|
114 |
service.addStatusChangedListener(serviceStatusListener);
|
|
115 |
}
|
|
116 |
}
|
|
117 |
reconcileConnection((IConnection2) connection);
|
|
118 |
}
|
|
119 |
|
|
120 |
private void reconcileConnection(IConnection2 connection) {
|
|
121 |
boolean isSysTRK = false;
|
|
122 |
EStatus trkStatus = EStatus.UNKNOWN;
|
|
123 |
EStatus traceStatus = EStatus.UNKNOWN;
|
|
124 |
for (IConnectedService service : manager.getConnectedServices(connection)) {
|
|
125 |
if (service instanceof TRKConnectedService) {
|
|
126 |
isSysTRK = isSysTRK((TRKConnectedService) service);
|
|
127 |
trkStatus = service.getStatus().getEStatus();
|
|
128 |
}
|
|
129 |
if (service instanceof TracingConnectedService) {
|
|
130 |
traceStatus = service.getStatus().getEStatus();
|
|
131 |
}
|
|
132 |
}
|
|
133 |
setConnectionStatus((IConnection2) connection, isSysTRK, trkStatus, traceStatus);
|
|
134 |
}
|
|
135 |
|
|
136 |
private void setConnectionStatus(IConnection2 connection, boolean isSysTRK, EStatus trkStatus, EStatus traceStatus) {
|
|
137 |
// use trk status
|
|
138 |
EConnectionStatus connectionStatus = service2ConnectionStatus(trkStatus);
|
|
139 |
// if sys trk, tracing also used
|
|
140 |
if (isSysTRK && connectionStatus.equals(EConnectionStatus.READY)) {
|
|
141 |
connectionStatus = service2ConnectionStatus(traceStatus);
|
|
142 |
}
|
|
143 |
|
|
144 |
String shortDesc = getShortDescriptionForStatus(connectionStatus);
|
749
|
145 |
StringBuilder longDesc = new StringBuilder(Messages.getString("ConnectionStatusReconciler_TRKServicePrefix")); //$NON-NLS-1$
|
748
|
146 |
longDesc.append(getServiceStatusString(trkStatus));
|
|
147 |
if (isSysTRK) {
|
749
|
148 |
longDesc.append(Messages.getString("ConnectionStatusReconciler_TracingServicePrefix")); //$NON-NLS-1$
|
748
|
149 |
longDesc.append(getServiceStatusString(traceStatus));
|
|
150 |
}
|
|
151 |
|
|
152 |
connection.setStatus(new ConnectionStatus(connectionStatus, shortDesc, longDesc.toString()));
|
|
153 |
}
|
|
154 |
|
|
155 |
private String getShortDescriptionForStatus(EConnectionStatus connectionStatus) {
|
|
156 |
switch (connectionStatus) {
|
|
157 |
case READY:
|
749
|
158 |
return Messages.getString("ConnectionStatusReconciler_ReadyLabel"); //$NON-NLS-1$
|
748
|
159 |
case NOT_READY:
|
749
|
160 |
return Messages.getString("ConnectionStatusReconciler_NotReadyLabel"); //$NON-NLS-1$
|
748
|
161 |
case IN_USE:
|
749
|
162 |
return Messages.getString("ConnectionStatusReconciler_InUseLabel"); //$NON-NLS-1$
|
748
|
163 |
case IN_USE_DISCONNECTED:
|
749
|
164 |
return Messages.getString("ConnectionStatusReconciler_DisconnectedLabel"); //$NON-NLS-1$
|
748
|
165 |
}
|
749
|
166 |
return ""; //$NON-NLS-1$
|
748
|
167 |
}
|
|
168 |
|
|
169 |
private String getServiceStatusString(EStatus status) {
|
|
170 |
switch (status) {
|
|
171 |
case UP:
|
749
|
172 |
return Messages.getString("ConnectionStatusReconciler_availableLabel"); //$NON-NLS-1$
|
748
|
173 |
case DOWN:
|
749
|
174 |
return Messages.getString("ConnectionStatusReconciler_unavailableLabel"); //$NON-NLS-1$
|
748
|
175 |
case IN_USE:
|
749
|
176 |
return Messages.getString("ConnectionStatusReconciler_inUseLabel_lower"); //$NON-NLS-1$
|
748
|
177 |
}
|
749
|
178 |
return ""; //$NON-NLS-1$
|
748
|
179 |
}
|
|
180 |
|
|
181 |
private EConnectionStatus service2ConnectionStatus(EStatus serviceStatus) {
|
|
182 |
switch (serviceStatus) {
|
|
183 |
case UP:
|
|
184 |
return EConnectionStatus.READY;
|
|
185 |
case DOWN:
|
|
186 |
return EConnectionStatus.NOT_READY;
|
|
187 |
case IN_USE:
|
|
188 |
return EConnectionStatus.IN_USE;
|
|
189 |
}
|
|
190 |
return EConnectionStatus.NONE;
|
|
191 |
}
|
|
192 |
|
|
193 |
private void removeConnection(IConnection connection) {
|
|
194 |
handledConnections.remove(connection);
|
|
195 |
}
|
|
196 |
|
|
197 |
private IConnection2 findConnection(IConnectedService cs) {
|
|
198 |
for (IConnection2 connection : handledConnections) {
|
|
199 |
for (IConnectedService connectedService : manager.getConnectedServices(connection)) {
|
|
200 |
if (cs.equals(connectedService))
|
|
201 |
return connection;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
return null;
|
|
205 |
}
|
|
206 |
|
|
207 |
public void handleServiceStatusChange(IStatus status) {
|
|
208 |
IConnectedService service = status.getConnectedService();
|
|
209 |
IConnection2 connection = findConnection(service);
|
|
210 |
if (connection != null) {
|
|
211 |
reconcileConnection(connection);
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
}
|