1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.launch;
18 
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 
22 /**
23  * Launch configuration data. This stores the result of querying the
24  * {@link ILaunchConfiguration} so that it's only done once.
25  */
26 public class AndroidLaunchConfiguration {
27 
28     /**
29      * Launch action. See {@link LaunchConfigDelegate#ACTION_DEFAULT},
30      * {@link LaunchConfigDelegate#ACTION_ACTIVITY},
31      * {@link LaunchConfigDelegate#ACTION_DO_NOTHING}
32      */
33     public int mLaunchAction = LaunchConfigDelegate.DEFAULT_LAUNCH_ACTION;
34 
35     /** Target selection mode for the configuration. */
36     public enum TargetMode {
37         /** Automatic target selection mode. */
38         AUTO,
39         /** Manual target selection mode. */
40         MANUAL,
41         /** All active devices */
42         ALL_DEVICES,
43         /** All active emulators */
44         ALL_EMULATORS,
45         /** All active devices and emulators */
46         ALL_DEVICES_AND_EMULATORS;
47 
getMode(String s)48         public static TargetMode getMode(String s) {
49             for (TargetMode m: values()) {
50                 if (m.toString().equals(s)) {
51                     return m;
52                 }
53             }
54 
55             throw new IllegalArgumentException(String.format(
56                     "Invalid representation (%s) for TargetMode", s));
57         }
58 
isMultiDevice()59         public boolean isMultiDevice() {
60             return this == ALL_DEVICES
61                     || this == ALL_EMULATORS
62                     || this == ALL_DEVICES_AND_EMULATORS;
63         }
64     }
65 
66     /**
67      * Target selection mode.
68      * @see TargetMode
69      */
70     public TargetMode mTargetMode = LaunchConfigDelegate.DEFAULT_TARGET_MODE;
71 
72     /**
73      * Indicates whether the emulator should be called with -wipe-data
74      */
75     public boolean mWipeData = LaunchConfigDelegate.DEFAULT_WIPE_DATA;
76 
77     /**
78      * Indicates whether the emulator should be called with -no-boot-anim
79      */
80     public boolean mNoBootAnim = LaunchConfigDelegate.DEFAULT_NO_BOOT_ANIM;
81 
82     /**
83      * AVD Name.
84      */
85     public String mAvdName = null;
86 
87     public String mNetworkSpeed = EmulatorConfigTab.getSpeed(
88             LaunchConfigDelegate.DEFAULT_SPEED);
89     public String mNetworkDelay = EmulatorConfigTab.getDelay(
90             LaunchConfigDelegate.DEFAULT_DELAY);
91 
92     /**
93      * Optional custom command line parameter to launch the emulator
94      */
95     public String mEmulatorCommandLine;
96 
97     /** Flag indicating whether the same device should be used for future launches. */
98     public boolean mReuseLastUsedDevice = false;
99 
100     /** Serial number of the device used in the last launch of this config. */
101     public String mLastUsedDevice = null;
102 
103     /**
104      * Initialized the structure from an ILaunchConfiguration object.
105      * @param config
106      */
set(ILaunchConfiguration config)107     public void set(ILaunchConfiguration config) {
108         try {
109             mLaunchAction = config.getAttribute(LaunchConfigDelegate.ATTR_LAUNCH_ACTION,
110                     mLaunchAction);
111         } catch (CoreException e1) {
112             // nothing to be done here, we'll use the default value
113         }
114 
115         mTargetMode = parseTargetMode(config, mTargetMode);
116 
117         try {
118             mReuseLastUsedDevice = config.getAttribute(
119                     LaunchConfigDelegate.ATTR_REUSE_LAST_USED_DEVICE, false);
120             mLastUsedDevice = config.getAttribute(
121                     LaunchConfigDelegate.ATTR_LAST_USED_DEVICE, (String)null);
122         } catch (CoreException e) {
123          // nothing to be done here, we'll use the default value
124         }
125 
126         try {
127             mAvdName = config.getAttribute(LaunchConfigDelegate.ATTR_AVD_NAME, mAvdName);
128         } catch (CoreException e) {
129             // ignore
130         }
131 
132         int index = LaunchConfigDelegate.DEFAULT_SPEED;
133         try {
134             index = config.getAttribute(LaunchConfigDelegate.ATTR_SPEED, index);
135         } catch (CoreException e) {
136             // nothing to be done here, we'll use the default value
137         }
138         mNetworkSpeed = EmulatorConfigTab.getSpeed(index);
139 
140         index = LaunchConfigDelegate.DEFAULT_DELAY;
141         try {
142             index = config.getAttribute(LaunchConfigDelegate.ATTR_DELAY, index);
143         } catch (CoreException e) {
144             // nothing to be done here, we'll use the default value
145         }
146         mNetworkDelay = EmulatorConfigTab.getDelay(index);
147 
148         try {
149             mEmulatorCommandLine = config.getAttribute(
150                     LaunchConfigDelegate.ATTR_COMMANDLINE, ""); //$NON-NLS-1$
151         } catch (CoreException e) {
152             // lets not do anything here, we'll use the default value
153         }
154 
155         try {
156             mWipeData = config.getAttribute(LaunchConfigDelegate.ATTR_WIPE_DATA, mWipeData);
157         } catch (CoreException e) {
158             // nothing to be done here, we'll use the default value
159         }
160 
161         try {
162             mNoBootAnim = config.getAttribute(LaunchConfigDelegate.ATTR_NO_BOOT_ANIM,
163                                               mNoBootAnim);
164         } catch (CoreException e) {
165             // nothing to be done here, we'll use the default value
166         }
167     }
168 
169     /**
170      * Retrieve the {@link TargetMode} saved in the provided launch configuration.
171      * Returns defaultMode if there are any errors while retrieving or parsing the saved setting.
172      */
parseTargetMode(ILaunchConfiguration config, TargetMode defaultMode)173     public static TargetMode parseTargetMode(ILaunchConfiguration config, TargetMode defaultMode) {
174         try {
175             String value = config.getAttribute(LaunchConfigDelegate.ATTR_TARGET_MODE,
176                     defaultMode.toString());
177             return TargetMode.getMode(value);
178         } catch (CoreException e) {
179             // ADT R20 changes the attribute type of ATTR_TARGET_MODE to be a string from a bool.
180             // So if parsing as a string fails, attempt parsing as a boolean.
181             try {
182                 boolean value = config.getAttribute(LaunchConfigDelegate.ATTR_TARGET_MODE, true);
183                 return value ? TargetMode.AUTO : TargetMode.MANUAL;
184             } catch (CoreException e1) {
185                 return defaultMode;
186             }
187         } catch (IllegalArgumentException e) {
188             return defaultMode;
189         }
190     }
191 }
192 
193