|
1 /* |
|
2 * Copyright (c) 2009 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 "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.s60tools.hticonnection.services.applicationcontrolservice; |
|
19 |
|
20 import com.nokia.HTI.HTIMessage; |
|
21 import com.nokia.s60tools.hticonnection.core.RequestQueueManager; |
|
22 import com.nokia.s60tools.hticonnection.core.RequestResult; |
|
23 import com.nokia.s60tools.hticonnection.exceptions.ConnectionException; |
|
24 import com.nokia.s60tools.hticonnection.exceptions.HTIErrorDetails; |
|
25 import com.nokia.s60tools.hticonnection.exceptions.HTIException; |
|
26 import com.nokia.s60tools.hticonnection.exceptions.ServiceShutdownException; |
|
27 import com.nokia.s60tools.hticonnection.resources.Messages; |
|
28 import com.nokia.s60tools.hticonnection.services.AppStatus; |
|
29 import com.nokia.s60tools.hticonnection.services.IApplicationControlService; |
|
30 import com.nokia.s60tools.util.console.IConsolePrintUtility; |
|
31 |
|
32 /** |
|
33 * Service that contains interface for controlling applications on device. |
|
34 */ |
|
35 public class ApplicationControlService implements IApplicationControlService{ |
|
36 |
|
37 /** |
|
38 * Print utility used to report errors, warnings, and info messages. |
|
39 */ |
|
40 private final IConsolePrintUtility printUtility; |
|
41 |
|
42 /** |
|
43 * Constructor. |
|
44 * @param printUtility Used for printing messages. |
|
45 */ |
|
46 public ApplicationControlService(IConsolePrintUtility printUtility){ |
|
47 this.printUtility = printUtility; |
|
48 } |
|
49 |
|
50 /* (non-Javadoc) |
|
51 * @see com.nokia.s60tools.hticonnection.services.IApplicationControlService#startApplicationByName(java.lang.String, java.lang.String, long) |
|
52 */ |
|
53 public void startApplicationByName(String programName, String docName, long timeout) throws ServiceShutdownException, HTIException, ConnectionException { |
|
54 StartApplicationByNameRequest request = new StartApplicationByNameRequest(programName, docName, timeout); |
|
55 RequestResult result = RequestQueueManager.getInstance().submit(request, printUtility); |
|
56 |
|
57 // Checking if there has been error. |
|
58 HTIMessage msg = result.getHTIMessage(); |
|
59 if(msg.isErrorResponse()){ |
|
60 // Reporting error and throwing appropriate error message. |
|
61 String errorMsg = Messages.getString("AppControlServiceService.FailedToStartApp_ExceptionMsg") + programName; //$NON-NLS-1$ |
|
62 printUtility.println(errorMsg, IConsolePrintUtility.MSG_ERROR); |
|
63 HTIErrorDetails details = new HTIErrorDetails(msg.getHTIErrorCode(), |
|
64 msg.getServiceErrorCode(), msg.getErrorDescription()); |
|
65 throw new HTIException(errorMsg, details); |
|
66 } |
|
67 } |
|
68 |
|
69 /* (non-Javadoc) |
|
70 * @see com.nokia.s60tools.hticonnection.services.IApplicationControlService#startProcess(java.lang.String, java.lang.String, long) |
|
71 */ |
|
72 public void startProcess(String programName, String parameters, long timeout) |
|
73 throws ServiceShutdownException, HTIException, ConnectionException { |
|
74 StartCommandByNameRequest request = new StartCommandByNameRequest(programName, parameters, timeout); |
|
75 RequestResult result = RequestQueueManager.getInstance().submit(request, printUtility); |
|
76 |
|
77 // Checking if there has been error. |
|
78 HTIMessage msg = result.getHTIMessage(); |
|
79 if(msg.isErrorResponse()){ |
|
80 // Reporting error and throwing appropriate error message. |
|
81 String errorMsg = |
|
82 Messages.getString("AppControlServiceService.FailedToStartProgram_ExceptionMsg_Part1")//$NON-NLS-1$ |
|
83 + " "//$NON-NLS-1$ |
|
84 + programName |
|
85 + " "//$NON-NLS-1$ |
|
86 + Messages.getString("AppControlServiceService.FailedToStartProgram_ExceptionMsg_Part2")//$NON-NLS-1$ |
|
87 + " "//$NON-NLS-1$ |
|
88 +parameters; |
|
89 printUtility.println(errorMsg, IConsolePrintUtility.MSG_ERROR); |
|
90 HTIErrorDetails details = new HTIErrorDetails(msg.getHTIErrorCode(), |
|
91 msg.getServiceErrorCode(), msg.getErrorDescription()); |
|
92 throw new HTIException(errorMsg, details); |
|
93 } |
|
94 |
|
95 |
|
96 } |
|
97 |
|
98 /* (non-Javadoc) |
|
99 * @see com.nokia.s60tools.hticonnection.services.IApplicationControlService#startApplicationByUid(int, java.lang.String, long) |
|
100 */ |
|
101 public void startApplicationByUid(int uid, String docName, long timeout) throws ServiceShutdownException, HTIException, ConnectionException { |
|
102 StartApplicationByUidRequest request = new StartApplicationByUidRequest(uid, docName, timeout); |
|
103 RequestResult result = RequestQueueManager.getInstance().submit(request, printUtility); |
|
104 |
|
105 // Checking if there has been error. |
|
106 com.nokia.HTI.ApplicationControlService.AppStatus status = result.getAppStatus(); |
|
107 if (status.getStatus() == AppStatus.NOT_FOUND) { |
|
108 // Reporting error and throwing appropriate error message. |
|
109 String errorMsg = Messages.getString("ApplicationControlService.FailedToStartAppByUidExceptionMsg_Part1") //$NON-NLS-1$ |
|
110 +uid +Messages.getString("ApplicationControlService.FailedToStartAppByUid_AppNotFoundExceptionMsg_Suffix"); //$NON-NLS-1$ |
|
111 printUtility.println(errorMsg, IConsolePrintUtility.MSG_ERROR); |
|
112 throw new HTIException(errorMsg, null); |
|
113 } |
|
114 else if (status.getStatus() == com.nokia.HTI.ApplicationControlService.AppStatus.ALREADY_RUNNING) { |
|
115 // Reporting error and throwing appropriate error message. |
|
116 String errorMsg = Messages.getString("ApplicationControlService.FailedToStartAppByUidExceptionMsg_Part1") //$NON-NLS-1$ |
|
117 +uid +Messages.getString("ApplicationControlService.FailedToStartAppByUidAlreaydyRunningExceptionMsg_Suffix"); //$NON-NLS-1$ |
|
118 printUtility.println(errorMsg, IConsolePrintUtility.MSG_ERROR); |
|
119 throw new HTIException(errorMsg, null); |
|
120 } |
|
121 } |
|
122 |
|
123 /* (non-Javadoc) |
|
124 * @see com.nokia.s60tools.hticonnection.services.IApplicationControlService#getApplicationStatusByName(java.lang.String, long) |
|
125 */ |
|
126 public AppStatus getApplicationStatusByName(String programName, long timeout) throws ServiceShutdownException, HTIException, ConnectionException { |
|
127 GetApplicationStatusByNameRequest request = new GetApplicationStatusByNameRequest(programName, timeout); |
|
128 RequestResult result = RequestQueueManager.getInstance().submit(request, printUtility); |
|
129 |
|
130 return new AppStatus(result.getAppStatus().getStatus()); |
|
131 } |
|
132 |
|
133 /* (non-Javadoc) |
|
134 * @see com.nokia.s60tools.hticonnection.services.IApplicationControlService#getApplicationStatusByUid(int, long) |
|
135 */ |
|
136 public AppStatus getApplicationStatusByUid(int uid, long timeout) throws ServiceShutdownException, HTIException, ConnectionException { |
|
137 GetApplicationStatusByUidRequest request = new GetApplicationStatusByUidRequest(uid, timeout); |
|
138 RequestResult result = RequestQueueManager.getInstance().submit(request, printUtility); |
|
139 |
|
140 return new AppStatus(result.getAppStatus().getStatus()); |
|
141 } |
|
142 |
|
143 /* (non-Javadoc) |
|
144 * @see com.nokia.s60tools.hticonnection.services.IApplicationControlService#stopApplicationByName(java.lang.String, long) |
|
145 */ |
|
146 public void stopApplicationByName(String programName, long timeout) throws ServiceShutdownException, HTIException, ConnectionException { |
|
147 StopApplicationByNameRequest request = new StopApplicationByNameRequest(programName, timeout); |
|
148 RequestResult result = RequestQueueManager.getInstance().submit(request, printUtility); |
|
149 |
|
150 // Checking if there has been error. |
|
151 HTIMessage msg = result.getHTIMessage(); |
|
152 if(msg.isErrorResponse()){ |
|
153 // Reporting error and throwing appropriate error message. |
|
154 String errorMsg = Messages.getString("AppControlServiceService.FailedToStopApp_ExceptionMsg") + programName; //$NON-NLS-1$ |
|
155 printUtility.println(errorMsg, IConsolePrintUtility.MSG_ERROR); |
|
156 HTIErrorDetails details = new HTIErrorDetails(msg.getHTIErrorCode(), |
|
157 msg.getServiceErrorCode(), msg.getErrorDescription()); |
|
158 throw new HTIException(errorMsg, details); |
|
159 } |
|
160 } |
|
161 |
|
162 /* (non-Javadoc) |
|
163 * @see com.nokia.s60tools.hticonnection.services.IApplicationControlService#stopApplicationByUid(int, long) |
|
164 */ |
|
165 public void stopApplicationByUid(int uid, long timeout) throws ServiceShutdownException, HTIException, ConnectionException { |
|
166 StopApplicationByUidRequest request = new StopApplicationByUidRequest(uid, timeout); |
|
167 RequestQueueManager.getInstance().submit(request, printUtility); |
|
168 // No need to check result. Returned AppStatus is Ok or NotFound. |
|
169 } |
|
170 } |