4 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
7 * GiWoong Kim <giwoong.kim@samsung.com>
8 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 package org.tizen.emulator.skin.config;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
36 import java.util.Properties;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
40 import org.eclipse.swt.graphics.Rectangle;
41 import org.eclipse.swt.widgets.Display;
42 import org.tizen.emulator.skin.comm.ICommunicator.RotationInfo;
43 import org.tizen.emulator.skin.comm.ICommunicator.Scale;
44 import org.tizen.emulator.skin.dbi.EmulatorUI;
45 import org.tizen.emulator.skin.exception.ConfigException;
46 import org.tizen.emulator.skin.log.SkinLogger;
47 import org.tizen.emulator.skin.log.SkinLogger.SkinLogLevel;
48 import org.tizen.emulator.skin.util.IOUtil;
49 import org.tizen.emulator.skin.util.StringUtil;
56 public class EmulatorConfig {
58 private static Logger logger =
59 SkinLogger.getSkinLogger(EmulatorConfig.class).getLogger();
61 public static final int DEFAULT_WINDOW_SCALE = Scale.SCALE_50.value();
62 public static final short DEFAULT_WINDOW_ROTATION = RotationInfo.PORTRAIT.id();
63 public static final int DEFAULT_WINDOW_X = 50;
64 public static final int DEFAULT_WINDOW_Y = 50;
65 public static final SkinLogLevel DEFAULT_LOG_LEVEL = SkinLogLevel.DEBUG;
67 public interface ArgsConstants {
68 public static final String UID = "uid";
69 public static final String SERVER_PORT = "svr.port";
70 public static final String RESOLUTION_WIDTH = "width";
71 public static final String RESOLUTION_HEIGHT = "height";
72 public static final String TEST_HEART_BEAT_IGNORE = "test.hb.ignore";
73 public static final String VM_PATH = "vm.path";
74 public static final String LOG_LEVEL = "log.level";
75 public static final String NET_BASE_PORT = "net.baseport";
76 public static final String SKIN_PATH = "skin.path";
77 public static final String MAX_TOUCHPOINT = "max.touchpoint";
80 public interface SkinInfoConstants {
81 public static final String SDK_VERSION_NAME = "sdk.version-name";
82 public static final String SKIN_NAME = "skin.name";
83 public static final String RESOLUTION_WIDTH = "resolution.width";
84 public static final String RESOLUTION_HEIGHT = "resolution.height";
87 public interface SkinPropertiesConstants {
88 public static final String WINDOW_X = "window.x";
89 public static final String WINDOW_Y = "window.y";
90 public static final String WINDOW_ROTATION = "window.rotate";
91 public static final String WINDOW_SCALE = "window.scale";
92 public static final String WINDOW_ONTOP = "window.ontop"; /* always on top */
93 public static final String KEYWINDOW_POSITION = "window.keywindow.position";
96 public interface ConfigPropertiesConstants {
97 public static final String TEST_HEART_BEAT_IGNORE = "test.hb.ignore";
98 public static final String LOG_LEVEL = "log.level";
101 private Map<String, String> args;
102 private EmulatorUI dbiContents;
103 private Properties skinProperties;
104 private Properties configProperties;
105 private String skinPropertiesFilePath;
107 public EmulatorConfig(Map<String, String> args,
108 EmulatorUI dbiContents, Properties skinProperties,
109 String skinPropertiesFilePath, Properties configProperties) {
111 this.dbiContents = dbiContents;
112 this.skinProperties = skinProperties;
113 this.skinPropertiesFilePath = skinPropertiesFilePath;
114 this.configProperties = configProperties;
116 if (null == configProperties) {
117 this.configProperties = new Properties();
121 public static void validateArgs(Map<String, String> args) throws ConfigException {
126 if (args.containsKey(ArgsConstants.UID)) {
127 String uid = args.get(ArgsConstants.UID);
129 Integer.parseInt(uid);
130 } catch (NumberFormatException e) {
131 String msg = ArgsConstants.UID + " argument is not numeric. : " + uid;
132 throw new ConfigException(msg);
136 if (args.containsKey(ArgsConstants.SERVER_PORT)) {
137 String serverPort = args.get(ArgsConstants.SERVER_PORT);
139 Integer.parseInt(serverPort);
140 } catch (NumberFormatException e) {
141 String msg = ArgsConstants.SERVER_PORT + " argument is not numeric. : " + serverPort;
142 throw new ConfigException(msg);
145 String msg = ArgsConstants.SERVER_PORT + " is required argument.";
146 throw new ConfigException(msg);
149 if (args.containsKey(ArgsConstants.RESOLUTION_WIDTH)) {
150 String width = args.get(ArgsConstants.RESOLUTION_WIDTH);
152 Integer.parseInt(width);
153 } catch (NumberFormatException e) {
154 String msg = ArgsConstants.RESOLUTION_WIDTH + " argument is not numeric. : " + width;
155 throw new ConfigException(msg);
158 String msg = ArgsConstants.RESOLUTION_WIDTH + " is required argument.";
159 throw new ConfigException(msg);
162 if (args.containsKey(ArgsConstants.RESOLUTION_HEIGHT)) {
163 String height = args.get(ArgsConstants.RESOLUTION_HEIGHT);
165 Integer.parseInt(height);
166 } catch (NumberFormatException e) {
167 String msg = ArgsConstants.RESOLUTION_HEIGHT + " argument is not numeric. : " + height;
168 throw new ConfigException(msg);
171 String msg = ArgsConstants.RESOLUTION_HEIGHT + " is required argument.";
172 throw new ConfigException(msg);
176 public static void validateSkinProperties( Properties skinProperties ) throws ConfigException {
177 if ( null == skinProperties || 0 == skinProperties.size() ) {
181 Rectangle monitorBound = Display.getDefault().getBounds();
182 logger.info("current display size : " + monitorBound);
184 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_X ) ) {
185 String x = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_X );
189 xx = Integer.parseInt( x );
190 } catch ( NumberFormatException e ) {
191 String msg = SkinPropertiesConstants.WINDOW_X + " in .skin.properties is not numeric. : " + x;
192 throw new ConfigException( msg );
195 //location correction
196 if (xx < monitorBound.x) {
197 int correction = monitorBound.x;
198 logger.info("WINDOW_X = " + xx + ", set to " + correction);
200 } else if (xx > monitorBound.x + monitorBound.width - 30) {
201 int correction = monitorBound.x + monitorBound.width - 100;
202 logger.info("WINDOW_X = " + xx + ", set to " + correction);
205 logger.info("WINDOW_X = " + xx);
208 skinProperties.setProperty(SkinPropertiesConstants.WINDOW_X, "" + xx);
211 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_Y ) ) {
212 String y = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_Y );
216 yy = Integer.parseInt( y );
217 } catch ( NumberFormatException e ) {
218 String msg = SkinPropertiesConstants.WINDOW_Y + " in .skin.properties is not numeric. : " + y;
219 throw new ConfigException( msg );
222 //location correction
223 if (yy < monitorBound.y) {
224 int correction = monitorBound.y;
225 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
227 } else if (yy > monitorBound.y + monitorBound.height - 30) {
228 int correction = monitorBound.y + monitorBound.height - 100;
229 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
232 logger.info("WINDOW_Y = " + yy);
235 skinProperties.setProperty(SkinPropertiesConstants.WINDOW_Y, "" + yy);
238 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_ROTATION ) ) {
239 String rotation = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_ROTATION );
241 Integer.parseInt( rotation );
242 } catch ( NumberFormatException e ) {
243 String msg = SkinPropertiesConstants.WINDOW_ROTATION + " in .skin.properties is not numeric. : " + rotation;
244 throw new ConfigException( msg );
248 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_SCALE ) ) {
249 String scale = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_SCALE );
251 Integer.parseInt( scale );
252 } catch ( NumberFormatException e ) {
253 String msg = SkinPropertiesConstants.WINDOW_SCALE + " in .skin.properties is not numeric. : " + scale;
254 throw new ConfigException( msg );
260 public static void validateSkinConfigProperties( Properties skinConfigProperties ) throws ConfigException {
261 if ( null == skinConfigProperties || 0 == skinConfigProperties.size() ) {
266 public void saveSkinProperties() {
268 File file = new File( skinPropertiesFilePath );
270 if ( !file.exists() ) {
273 if ( !file.createNewFile() ) {
276 } catch ( IOException e ) {
277 logger.log( Level.SEVERE, "Fail to create skin properties file.", e );
283 FileOutputStream fos = null;
287 fos = new FileOutputStream( file );
288 skinProperties.store( fos, "Automatically generated by emulator skin." );
290 } catch ( IOException e ) {
291 logger.log( Level.SEVERE, e.getMessage(), e );
298 public EmulatorUI getDbiContents() {
302 public String getArg( String argKey ) {
303 return args.get( argKey );
306 public String getArg( String argKey, String defaultValue ) {
307 String arg = args.get( argKey );
308 if ( StringUtil.isEmpty( arg ) ) {
315 public int getArgInt( String argKey ) {
316 String arg = args.get( argKey );
317 if ( StringUtil.isEmpty( arg ) ) {
320 return Integer.parseInt( arg );
323 public int getArgInt( String argKey, int defaultValue ) {
324 String arg = args.get( argKey );
325 if ( StringUtil.isEmpty( arg ) ) {
328 return Integer.parseInt( arg );
331 public boolean getArgBoolean( String argKey ) {
332 String arg = args.get( argKey );
333 return Boolean.parseBoolean( arg );
336 private String getProperty( Properties properties, String key ) {
337 return properties.getProperty( key );
340 private String getProperty( Properties properties, String key, String defaultValue ) {
341 String property = properties.getProperty( key );
342 if ( StringUtil.isEmpty( property ) ) {
348 private int getPropertyInt( Properties properties, String key ) {
349 return Integer.parseInt( properties.getProperty( key ) );
352 private int getPropertyInt( Properties properties, String key, int defaultValue ) {
353 String property = properties.getProperty( key );
354 if ( StringUtil.isEmpty( property ) ) {
357 return Integer.parseInt( property );
360 private short getPropertyShort( Properties properties, String key ) {
361 return Short.parseShort( properties.getProperty( key ) );
364 private short getPropertyShort( Properties properties, String key, short defaultValue ) {
365 String property = properties.getProperty( key );
366 if ( StringUtil.isEmpty( property ) ) {
369 return Short.parseShort( property );
372 private void setProperty( Properties properties, String key, String value ) {
373 properties.put( key, value );
376 private void setProperty( Properties properties, String key, int value ) {
377 properties.put( key, Integer.toString( value ) );
380 /* skin properties */
382 public String getSkinProperty( String key ) {
383 return getProperty( skinProperties, key );
386 public String getSkinProperty( String key, String defaultValue ) {
387 return getProperty( skinProperties, key, defaultValue );
390 public int getSkinPropertyInt( String key ) {
391 return getPropertyInt( skinProperties, key );
394 public int getSkinPropertyInt( String key, int defaultValue ) {
395 return getPropertyInt( skinProperties, key, defaultValue );
398 public short getSkinPropertyShort( String key ) {
399 return getPropertyShort( skinProperties, key );
402 public short getSkinPropertyShort( String key, short defaultValue ) {
403 return getPropertyShort( skinProperties, key, defaultValue );
406 public void setSkinProperty( String key, String value ) {
407 setProperty( skinProperties, key, value );
410 public void setSkinProperty( String key, int value ) {
411 setProperty( skinProperties, key, value );
414 /* config properties */
416 public String getConfigProperty( String key ) {
417 return getProperty( configProperties, key );
420 public String getConfigProperty( String key, String defaultValue ) {
421 return getProperty( configProperties, key, defaultValue );
424 public int getConfigPropertyInt( String key, int defaultValue ) {
425 return getPropertyInt( configProperties, key, defaultValue );