author | dadubrow |
Wed, 13 Jan 2010 09:44:43 -0600 | |
changeset 758 | c3cf3704d340 |
parent 752 | a0609ea7401a |
child 763 | 751f5182b866 |
permissions | -rw-r--r-- |
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 |
||
752
a0609ea7401a
add sys trk checking for usb only and use with reconciler
dadubrow
parents:
750
diff
changeset
|
95 |
private boolean isDynamic(IConnection connection) { |
a0609ea7401a
add sys trk checking for usb only and use with reconciler
dadubrow
parents:
750
diff
changeset
|
96 |
return connection instanceof IConnection2 && ((IConnection2) connection).isDynamic(); |
748 | 97 |
} |
98 |
||
99 |
private boolean isSysTRK(TRKConnectedService service) { |
|
100 |
String value = service.getProperties().get(TRKConnectedService.PROP_SYS_TRK); |
|
752
a0609ea7401a
add sys trk checking for usb only and use with reconciler
dadubrow
parents:
750
diff
changeset
|
101 |
return Boolean.parseBoolean(value); |
748 | 102 |
} |
103 |
||
104 |
private void addConnection(IConnection connection) { |
|
752
a0609ea7401a
add sys trk checking for usb only and use with reconciler
dadubrow
parents:
750
diff
changeset
|
105 |
if (!isDynamic(connection)) |
748 | 106 |
return; |
107 |
||
758
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
108 |
reconcileAsDefault(connection); |
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
109 |
|
748 | 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 |
||
758
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
120 |
private void reconcileAsDefault(IConnection connection) { |
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
121 |
// USB connections become default when added |
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
122 |
if (USBConnectionType.ID.equals(connection.getConnectionType().getIdentifier())) |
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
123 |
manager.setDefaultConnection(connection); |
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
124 |
} |
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
125 |
|
748 | 126 |
private void reconcileConnection(IConnection2 connection) { |
127 |
boolean isSysTRK = false; |
|
128 |
EStatus trkStatus = EStatus.UNKNOWN; |
|
129 |
EStatus traceStatus = EStatus.UNKNOWN; |
|
130 |
for (IConnectedService service : manager.getConnectedServices(connection)) { |
|
131 |
if (service instanceof TRKConnectedService) { |
|
132 |
isSysTRK = isSysTRK((TRKConnectedService) service); |
|
133 |
trkStatus = service.getStatus().getEStatus(); |
|
134 |
} |
|
135 |
if (service instanceof TracingConnectedService) { |
|
136 |
traceStatus = service.getStatus().getEStatus(); |
|
137 |
} |
|
138 |
} |
|
139 |
setConnectionStatus((IConnection2) connection, isSysTRK, trkStatus, traceStatus); |
|
140 |
} |
|
141 |
||
142 |
private void setConnectionStatus(IConnection2 connection, boolean isSysTRK, EStatus trkStatus, EStatus traceStatus) { |
|
143 |
// use trk status |
|
144 |
EConnectionStatus connectionStatus = service2ConnectionStatus(trkStatus); |
|
145 |
// if sys trk, tracing also used |
|
146 |
if (isSysTRK && connectionStatus.equals(EConnectionStatus.READY)) { |
|
147 |
connectionStatus = service2ConnectionStatus(traceStatus); |
|
148 |
} |
|
149 |
||
150 |
String shortDesc = getShortDescriptionForStatus(connectionStatus); |
|
749 | 151 |
StringBuilder longDesc = new StringBuilder(Messages.getString("ConnectionStatusReconciler_TRKServicePrefix")); //$NON-NLS-1$ |
748 | 152 |
longDesc.append(getServiceStatusString(trkStatus)); |
153 |
if (isSysTRK) { |
|
749 | 154 |
longDesc.append(Messages.getString("ConnectionStatusReconciler_TracingServicePrefix")); //$NON-NLS-1$ |
748 | 155 |
longDesc.append(getServiceStatusString(traceStatus)); |
156 |
} |
|
157 |
||
158 |
connection.setStatus(new ConnectionStatus(connectionStatus, shortDesc, longDesc.toString())); |
|
159 |
} |
|
160 |
||
161 |
private String getShortDescriptionForStatus(EConnectionStatus connectionStatus) { |
|
162 |
switch (connectionStatus) { |
|
163 |
case READY: |
|
749 | 164 |
return Messages.getString("ConnectionStatusReconciler_ReadyLabel"); //$NON-NLS-1$ |
748 | 165 |
case NOT_READY: |
749 | 166 |
return Messages.getString("ConnectionStatusReconciler_NotReadyLabel"); //$NON-NLS-1$ |
748 | 167 |
case IN_USE: |
749 | 168 |
return Messages.getString("ConnectionStatusReconciler_InUseLabel"); //$NON-NLS-1$ |
748 | 169 |
case IN_USE_DISCONNECTED: |
749 | 170 |
return Messages.getString("ConnectionStatusReconciler_DisconnectedLabel"); //$NON-NLS-1$ |
748 | 171 |
} |
749 | 172 |
return ""; //$NON-NLS-1$ |
748 | 173 |
} |
174 |
||
175 |
private String getServiceStatusString(EStatus status) { |
|
176 |
switch (status) { |
|
177 |
case UP: |
|
749 | 178 |
return Messages.getString("ConnectionStatusReconciler_availableLabel"); //$NON-NLS-1$ |
748 | 179 |
case DOWN: |
749 | 180 |
return Messages.getString("ConnectionStatusReconciler_unavailableLabel"); //$NON-NLS-1$ |
748 | 181 |
case IN_USE: |
749 | 182 |
return Messages.getString("ConnectionStatusReconciler_inUseLabel_lower"); //$NON-NLS-1$ |
748 | 183 |
} |
749 | 184 |
return ""; //$NON-NLS-1$ |
748 | 185 |
} |
186 |
||
187 |
private EConnectionStatus service2ConnectionStatus(EStatus serviceStatus) { |
|
188 |
switch (serviceStatus) { |
|
189 |
case UP: |
|
190 |
return EConnectionStatus.READY; |
|
191 |
case DOWN: |
|
192 |
return EConnectionStatus.NOT_READY; |
|
193 |
case IN_USE: |
|
194 |
return EConnectionStatus.IN_USE; |
|
195 |
} |
|
196 |
return EConnectionStatus.NONE; |
|
197 |
} |
|
198 |
||
199 |
private void removeConnection(IConnection connection) { |
|
200 |
handledConnections.remove(connection); |
|
201 |
} |
|
202 |
||
203 |
private IConnection2 findConnection(IConnectedService cs) { |
|
204 |
for (IConnection2 connection : handledConnections) { |
|
205 |
for (IConnectedService connectedService : manager.getConnectedServices(connection)) { |
|
206 |
if (cs.equals(connectedService)) |
|
207 |
return connection; |
|
208 |
} |
|
209 |
} |
|
210 |
return null; |
|
211 |
} |
|
212 |
||
213 |
public void handleServiceStatusChange(IStatus status) { |
|
214 |
IConnectedService service = status.getConnectedService(); |
|
215 |
IConnection2 connection = findConnection(service); |
|
216 |
if (connection != null) { |
|
217 |
reconcileConnection(connection); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
} |