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
95 public interface ConfigPropertiesConstants {
96 public static final String TEST_HEART_BEAT_IGNORE = "test.hb.ignore";
97 public static final String LOG_LEVEL = "log.level";
100 private Map<String, String> args;
101 private EmulatorUI dbiContents;
102 private Properties skinProperties;
103 private Properties configProperties;
104 private String skinPropertiesFilePath;
106 public EmulatorConfig(Map<String, String> args,
107 EmulatorUI dbiContents, Properties skinProperties,
108 String skinPropertiesFilePath, Properties configProperties) {
110 this.dbiContents = dbiContents;
111 this.skinProperties = skinProperties;
112 this.skinPropertiesFilePath = skinPropertiesFilePath;
113 this.configProperties = configProperties;
115 if (null == configProperties) {
116 this.configProperties = new Properties();
120 public static void validateArgs(Map<String, String> args) throws ConfigException {
125 if (args.containsKey(ArgsConstants.UID)) {
126 String uid = args.get(ArgsConstants.UID);
128 Integer.parseInt(uid);
129 } catch (NumberFormatException e) {
130 String msg = ArgsConstants.UID + " argument is not numeric. : " + uid;
131 throw new ConfigException(msg);
135 if (args.containsKey(ArgsConstants.SERVER_PORT)) {
136 String serverPort = args.get(ArgsConstants.SERVER_PORT);
138 Integer.parseInt(serverPort);
139 } catch (NumberFormatException e) {
140 String msg = ArgsConstants.SERVER_PORT + " argument is not numeric. : " + serverPort;
141 throw new ConfigException(msg);
144 String msg = ArgsConstants.SERVER_PORT + " is required argument.";
145 throw new ConfigException(msg);
148 if (args.containsKey(ArgsConstants.RESOLUTION_WIDTH)) {
149 String width = args.get(ArgsConstants.RESOLUTION_WIDTH);
151 Integer.parseInt(width);
152 } catch (NumberFormatException e) {
153 String msg = ArgsConstants.RESOLUTION_WIDTH + " argument is not numeric. : " + width;
154 throw new ConfigException(msg);
157 String msg = ArgsConstants.RESOLUTION_WIDTH + " is required argument.";
158 throw new ConfigException(msg);
161 if (args.containsKey(ArgsConstants.RESOLUTION_HEIGHT)) {
162 String height = args.get(ArgsConstants.RESOLUTION_HEIGHT);
164 Integer.parseInt(height);
165 } catch (NumberFormatException e) {
166 String msg = ArgsConstants.RESOLUTION_HEIGHT + " argument is not numeric. : " + height;
167 throw new ConfigException(msg);
170 String msg = ArgsConstants.RESOLUTION_HEIGHT + " is required argument.";
171 throw new ConfigException(msg);
175 public static void validateSkinProperties( Properties skinProperties ) throws ConfigException {
176 if ( null == skinProperties || 0 == skinProperties.size() ) {
180 Rectangle monitorBound = Display.getDefault().getBounds();
181 logger.info("current display size : " + monitorBound);
183 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_X ) ) {
184 String x = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_X );
188 xx = Integer.parseInt( x );
189 } catch ( NumberFormatException e ) {
190 String msg = SkinPropertiesConstants.WINDOW_X + " in .skin.properties is not numeric. : " + x;
191 throw new ConfigException( msg );
194 //location correction
195 if (xx < monitorBound.x) {
196 int correction = monitorBound.x;
197 logger.info("WINDOW_X = " + xx + ", set to " + correction);
199 } else if (xx > monitorBound.x + monitorBound.width - 30) {
200 int correction = monitorBound.x + monitorBound.width - 100;
201 logger.info("WINDOW_X = " + xx + ", set to " + correction);
204 logger.info("WINDOW_X = " + xx);
207 skinProperties.setProperty(SkinPropertiesConstants.WINDOW_X, "" + xx);
210 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_Y ) ) {
211 String y = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_Y );
215 yy = Integer.parseInt( y );
216 } catch ( NumberFormatException e ) {
217 String msg = SkinPropertiesConstants.WINDOW_Y + " in .skin.properties is not numeric. : " + y;
218 throw new ConfigException( msg );
221 //location correction
222 if (yy < monitorBound.y) {
223 int correction = monitorBound.y;
224 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
226 } else if (yy > monitorBound.y + monitorBound.height - 30) {
227 int correction = monitorBound.y + monitorBound.height - 100;
228 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
231 logger.info("WINDOW_Y = " + yy);
234 skinProperties.setProperty(SkinPropertiesConstants.WINDOW_Y, "" + yy);
237 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_ROTATION ) ) {
238 String rotation = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_ROTATION );
240 Integer.parseInt( rotation );
241 } catch ( NumberFormatException e ) {
242 String msg = SkinPropertiesConstants.WINDOW_ROTATION + " in .skin.properties is not numeric. : " + rotation;
243 throw new ConfigException( msg );
247 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_SCALE ) ) {
248 String scale = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_SCALE );
250 Integer.parseInt( scale );
251 } catch ( NumberFormatException e ) {
252 String msg = SkinPropertiesConstants.WINDOW_SCALE + " in .skin.properties is not numeric. : " + scale;
253 throw new ConfigException( msg );
259 public static void validateSkinConfigProperties( Properties skinConfigProperties ) throws ConfigException {
260 if ( null == skinConfigProperties || 0 == skinConfigProperties.size() ) {
265 public void saveSkinProperties() {
267 File file = new File( skinPropertiesFilePath );
269 if ( !file.exists() ) {
272 if ( !file.createNewFile() ) {
275 } catch ( IOException e ) {
276 logger.log( Level.SEVERE, "Fail to create skin properties file.", e );
282 FileOutputStream fos = null;
286 fos = new FileOutputStream( file );
287 skinProperties.store( fos, "Automatically generated by emulator skin." );
289 } catch ( IOException e ) {
290 logger.log( Level.SEVERE, e.getMessage(), e );
297 public EmulatorUI getDbiContents() {
301 public String getArg( String argKey ) {
302 return args.get( argKey );
305 public String getArg( String argKey, String defaultValue ) {
306 String arg = args.get( argKey );
307 if ( StringUtil.isEmpty( arg ) ) {
314 public int getArgInt( String argKey ) {
315 String arg = args.get( argKey );
316 if ( StringUtil.isEmpty( arg ) ) {
319 return Integer.parseInt( arg );
322 public int getArgInt( String argKey, int defaultValue ) {
323 String arg = args.get( argKey );
324 if ( StringUtil.isEmpty( arg ) ) {
327 return Integer.parseInt( arg );
330 public boolean getArgBoolean( String argKey ) {
331 String arg = args.get( argKey );
332 return Boolean.parseBoolean( arg );
335 private String getProperty( Properties properties, String key ) {
336 return properties.getProperty( key );
339 private String getProperty( Properties properties, String key, String defaultValue ) {
340 String property = properties.getProperty( key );
341 if ( StringUtil.isEmpty( property ) ) {
347 private int getPropertyInt( Properties properties, String key ) {
348 return Integer.parseInt( properties.getProperty( key ) );
351 private int getPropertyInt( Properties properties, String key, int defaultValue ) {
352 String property = properties.getProperty( key );
353 if ( StringUtil.isEmpty( property ) ) {
356 return Integer.parseInt( property );
359 private short getPropertyShort( Properties properties, String key ) {
360 return Short.parseShort( properties.getProperty( key ) );
363 private short getPropertyShort( Properties properties, String key, short defaultValue ) {
364 String property = properties.getProperty( key );
365 if ( StringUtil.isEmpty( property ) ) {
368 return Short.parseShort( property );
371 private void setProperty( Properties properties, String key, String value ) {
372 properties.put( key, value );
375 private void setProperty( Properties properties, String key, int value ) {
376 properties.put( key, Integer.toString( value ) );
379 /* skin properties */
381 public String getSkinProperty( String key ) {
382 return getProperty( skinProperties, key );
385 public String getSkinProperty( String key, String defaultValue ) {
386 return getProperty( skinProperties, key, defaultValue );
389 public int getSkinPropertyInt( String key ) {
390 return getPropertyInt( skinProperties, key );
393 public int getSkinPropertyInt( String key, int defaultValue ) {
394 return getPropertyInt( skinProperties, key, defaultValue );
397 public short getSkinPropertyShort( String key ) {
398 return getPropertyShort( skinProperties, key );
401 public short getSkinPropertyShort( String key, short defaultValue ) {
402 return getPropertyShort( skinProperties, key, defaultValue );
405 public void setSkinProperty( String key, String value ) {
406 setProperty( skinProperties, key, value );
409 public void setSkinProperty( String key, int value ) {
410 setProperty( skinProperties, key, value );
413 /* config properties */
415 public String getConfigProperty( String key ) {
416 return getProperty( configProperties, key );
419 public String getConfigProperty( String key, String defaultValue ) {
420 return getProperty( configProperties, key, defaultValue );
423 public int getConfigPropertyInt( String key, int defaultValue ) {
424 return getPropertyInt( configProperties, key, defaultValue );