htiextension/com.nokia.s60tools.hticonnection/src/com/nokia/s60tools/hticonnection/services/screencaptureservice/ScreenCaptureRequest.java
changeset 0 61163b28edca
child 9 e67492608de0
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     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.screencaptureservice;
       
    19 
       
    20 import java.io.ByteArrayInputStream;
       
    21 
       
    22 import org.eclipse.swt.graphics.Image;
       
    23 import org.eclipse.swt.widgets.Display;
       
    24 
       
    25 import com.nokia.HTI.BaseService;
       
    26 import com.nokia.HTI.IService;
       
    27 import com.nokia.s60tools.hticonnection.core.AbstractRequest;
       
    28 import com.nokia.s60tools.hticonnection.core.RequestResult;
       
    29 import com.nokia.s60tools.hticonnection.resources.Messages;
       
    30 
       
    31 /**
       
    32  * Callable object that can be used to wait for request to complete.
       
    33  */
       
    34 public class ScreenCaptureRequest extends AbstractRequest{
       
    35 	
       
    36 	/**
       
    37 	 * Name used for request.
       
    38 	 */
       
    39 	private static final String REQUEST_NAME = "Screen capture"; //$NON-NLS-1$
       
    40 	
       
    41 	// Settings for capture.
       
    42 	private final String imgMimeType;
       
    43 	private final int colorDepth;
       
    44 	private final long timeout;
       
    45 
       
    46 	/**
       
    47 	 * Capture a full screen.
       
    48 	 * @param imgMimeType Image MIME type, e.g. "image/png", "image/gif", "image/jpeg"
       
    49 	 * @param colorDepth Color depth e.g. ConnectionTestService.COLOR_DEPTH_ECOLOR64K.
       
    50 	 * @param timeout Time that is waited for operation to complete. Use 0 for infinite wait.
       
    51 	 */
       
    52 	public ScreenCaptureRequest(String imgMimeType, int colorDepth, long timeout){
       
    53 		super(REQUEST_NAME);
       
    54 		this.imgMimeType = imgMimeType;
       
    55 		this.colorDepth = colorDepth;
       
    56 		this.timeout = timeout;
       
    57 	}
       
    58 
       
    59 	/* (non-Javadoc)
       
    60 	 * @see com.nokia.s60tools.hticonnection.core.AbstractRequest#createService()
       
    61 	 */
       
    62 	public BaseService createService(){
       
    63 		return new com.nokia.HTI.ScreenCapturingService.ScreenCapturingService();
       
    64 		}
       
    65 	
       
    66 	/* (non-Javadoc)
       
    67 	 * @see com.nokia.s60tools.hticonnection.core.AbstractRequest#invokeService(com.nokia.HTI.IService)
       
    68 	 */
       
    69 	public RequestResult invokeService(IService service) throws Exception{
       
    70 		byte[] result = ((com.nokia.HTI.ScreenCapturingService.ScreenCapturingService)service)
       
    71 						.captureFullScreen(imgMimeType, (byte)colorDepth, timeout);
       
    72 		
       
    73 		// Testing image to prevent sending invalid data.
       
    74 		testImageData(result);
       
    75 		
       
    76 		return new RequestResult(result);
       
    77     }
       
    78 	
       
    79 	/**
       
    80 	 * Test data by creating image from data.
       
    81 	 * @param imageData Image in binary format.
       
    82 	 * @throws Exception Thrown if creating the image fails.
       
    83 	 */
       
    84 	private void testImageData(byte[] imageData) throws Exception {
       
    85 		try {
       
    86 			// Converting bytes to image to test that image is valid.
       
    87 			ByteArrayInputStream is = new ByteArrayInputStream(imageData);
       
    88 			Image image = new Image(Display.getDefault(), is);
       
    89 			image.dispose();
       
    90 		} 
       
    91 		catch(Exception e) {
       
    92 			// Couldn't create an image from data. Throwing an error.
       
    93 			// Connection will be reseted in abstractRequest after error.
       
    94 			throw new Exception(Messages.getString("ScreenCaptureRequest.InvalidImage_Exception_Msg")); //$NON-NLS-1$
       
    95 		}
       
    96 	}
       
    97 }