|
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; |
|
34 import com.nokia.carbide.trk.support.connection.USBConnectionType; |
|
35 import com.nokia.carbide.trk.support.service.TRKConnectedService; |
|
36 import com.nokia.carbide.trk.support.service.TracingConnectedService; |
|
37 |
|
38 /** |
|
39 * A singleton object that manages the device status of dynamic connections |
|
40 * based on the status of the TRK and Tracing services. |
|
41 */ |
|
42 public class ConnectionStatusReconciler { |
|
43 |
|
44 private class ConnectionListener implements IConnectionListener { |
|
45 |
|
46 @Override |
|
47 public void connectionAdded(IConnection connection) { |
|
48 addConnection(connection); |
|
49 } |
|
50 |
|
51 @Override |
|
52 public void connectionRemoved(IConnection connection) { |
|
53 removeConnection(connection); |
|
54 } |
|
55 |
|
56 @Override |
|
57 public void defaultConnectionSet(IConnection connection) {} |
|
58 |
|
59 } |
|
60 |
|
61 private class ServiceStatusListener implements IStatusChangedListener { |
|
62 |
|
63 @Override |
|
64 public void statusChanged(IStatus status) { |
|
65 handleServiceStatusChange(status); |
|
66 } |
|
67 |
|
68 } |
|
69 |
|
70 private static ConnectionStatusReconciler instance; |
|
71 private IConnectionsManager manager; |
|
72 private IConnectionListener connectionListener; |
|
73 private List<IConnection2> handledConnections; |
|
74 private ServiceStatusListener serviceStatusListener; |
|
75 |
|
76 private ConnectionStatusReconciler() { |
|
77 connectionListener = new ConnectionListener(); |
|
78 manager = RemoteConnectionsActivator.getConnectionsManager(); |
|
79 manager.addConnectionListener(connectionListener); |
|
80 handledConnections = new ArrayList<IConnection2>(); |
|
81 serviceStatusListener = new ServiceStatusListener(); |
|
82 } |
|
83 |
|
84 public static ConnectionStatusReconciler getInstance() { |
|
85 if (instance == null) |
|
86 instance = new ConnectionStatusReconciler(); |
|
87 |
|
88 return instance; |
|
89 } |
|
90 |
|
91 public void dispose() { |
|
92 manager.removeConnectionListener(connectionListener); |
|
93 for (IConnection2 connection : new ArrayList<IConnection2>(handledConnections)) { |
|
94 removeConnection(connection); |
|
95 } |
|
96 } |
|
97 |
|
98 private boolean handlesConnection(IConnection connection) { |
|
99 // only manage status for USBConnectionTypes for now |
|
100 // in future other IConnection2 types may also be managed |
|
101 return connection.getConnectionType() instanceof USBConnectionType; |
|
102 } |
|
103 |
|
104 private boolean isSysTRK(TRKConnectedService service) { |
|
105 String value = service.getProperties().get(TRKConnectedService.PROP_SYS_TRK); |
|
106 return value != null && Boolean.getBoolean(value); |
|
107 } |
|
108 |
|
109 private void addConnection(IConnection connection) { |
|
110 if (!handlesConnection(connection)) |
|
111 return; |
|
112 |
|
113 handledConnections.add((IConnection2) connection); |
|
114 for (IConnectedService service : manager.getConnectedServices(connection)) { |
|
115 if (service instanceof TRKConnectedService || |
|
116 service instanceof TracingConnectedService) { |
|
117 service.addStatusChangedListener(serviceStatusListener); |
|
118 } |
|
119 } |
|
120 reconcileConnection((IConnection2) connection); |
|
121 } |
|
122 |
|
123 private void reconcileConnection(IConnection2 connection) { |
|
124 boolean isSysTRK = false; |
|
125 EStatus trkStatus = EStatus.UNKNOWN; |
|
126 EStatus traceStatus = EStatus.UNKNOWN; |
|
127 for (IConnectedService service : manager.getConnectedServices(connection)) { |
|
128 if (service instanceof TRKConnectedService) { |
|
129 isSysTRK = isSysTRK((TRKConnectedService) service); |
|
130 trkStatus = service.getStatus().getEStatus(); |
|
131 } |
|
132 if (service instanceof TracingConnectedService) { |
|
133 traceStatus = service.getStatus().getEStatus(); |
|
134 } |
|
135 } |
|
136 setConnectionStatus((IConnection2) connection, isSysTRK, trkStatus, traceStatus); |
|
137 } |
|
138 |
|
139 private void setConnectionStatus(IConnection2 connection, boolean isSysTRK, EStatus trkStatus, EStatus traceStatus) { |
|
140 // use trk status |
|
141 EConnectionStatus connectionStatus = service2ConnectionStatus(trkStatus); |
|
142 // if sys trk, tracing also used |
|
143 if (isSysTRK && connectionStatus.equals(EConnectionStatus.READY)) { |
|
144 connectionStatus = service2ConnectionStatus(traceStatus); |
|
145 } |
|
146 |
|
147 String shortDesc = getShortDescriptionForStatus(connectionStatus); |
|
148 StringBuilder longDesc = new StringBuilder("TRK service "); |
|
149 longDesc.append(getServiceStatusString(trkStatus)); |
|
150 if (isSysTRK) { |
|
151 longDesc.append(", Tracing service "); |
|
152 longDesc.append(getServiceStatusString(traceStatus)); |
|
153 } |
|
154 |
|
155 connection.setStatus(new ConnectionStatus(connectionStatus, shortDesc, longDesc.toString())); |
|
156 } |
|
157 |
|
158 private String getShortDescriptionForStatus(EConnectionStatus connectionStatus) { |
|
159 switch (connectionStatus) { |
|
160 case READY: |
|
161 return "Ready"; |
|
162 case NOT_READY: |
|
163 return "Not Ready"; |
|
164 case IN_USE: |
|
165 return "In Use"; |
|
166 case IN_USE_DISCONNECTED: |
|
167 return "Disconnected"; |
|
168 } |
|
169 return ""; |
|
170 } |
|
171 |
|
172 private String getServiceStatusString(EStatus status) { |
|
173 switch (status) { |
|
174 case UP: |
|
175 return "available"; |
|
176 case DOWN: |
|
177 return "unavailable"; |
|
178 case IN_USE: |
|
179 return "in use"; |
|
180 } |
|
181 return ""; |
|
182 } |
|
183 |
|
184 private EConnectionStatus service2ConnectionStatus(EStatus serviceStatus) { |
|
185 switch (serviceStatus) { |
|
186 case UP: |
|
187 return EConnectionStatus.READY; |
|
188 case DOWN: |
|
189 return EConnectionStatus.NOT_READY; |
|
190 case IN_USE: |
|
191 return EConnectionStatus.IN_USE; |
|
192 } |
|
193 return EConnectionStatus.NONE; |
|
194 } |
|
195 |
|
196 private void removeConnection(IConnection connection) { |
|
197 handledConnections.remove(connection); |
|
198 } |
|
199 |
|
200 private IConnection2 findConnection(IConnectedService cs) { |
|
201 for (IConnection2 connection : handledConnections) { |
|
202 for (IConnectedService connectedService : manager.getConnectedServices(connection)) { |
|
203 if (cs.equals(connectedService)) |
|
204 return connection; |
|
205 } |
|
206 } |
|
207 return null; |
|
208 } |
|
209 |
|
210 public void handleServiceStatusChange(IStatus status) { |
|
211 IConnectedService service = status.getConnectedService(); |
|
212 IConnection2 connection = findConnection(service); |
|
213 if (connection != null) { |
|
214 reconcileConnection(connection); |
|
215 } |
|
216 } |
|
217 |
|
218 } |