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 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 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 /** 98 * Initialized the structure from an ILaunchConfiguration object. 99 * @param config 100 */ 101 public void set(ILaunchConfiguration config) { 102 try { 103 mLaunchAction = config.getAttribute(LaunchConfigDelegate.ATTR_LAUNCH_ACTION, 104 mLaunchAction); 105 } catch (CoreException e1) { 106 // nothing to be done here, we'll use the default value 107 } 108 109 mTargetMode = parseTargetMode(config, mTargetMode); 110 111 try { 112 mAvdName = config.getAttribute(LaunchConfigDelegate.ATTR_AVD_NAME, mAvdName); 113 } catch (CoreException e) { 114 // ignore 115 } 116 117 int index = LaunchConfigDelegate.DEFAULT_SPEED; 118 try { 119 index = config.getAttribute(LaunchConfigDelegate.ATTR_SPEED, index); 120 } catch (CoreException e) { 121 // nothing to be done here, we'll use the default value 122 } 123 mNetworkSpeed = EmulatorConfigTab.getSpeed(index); 124 125 index = LaunchConfigDelegate.DEFAULT_DELAY; 126 try { 127 index = config.getAttribute(LaunchConfigDelegate.ATTR_DELAY, index); 128 } catch (CoreException e) { 129 // nothing to be done here, we'll use the default value 130 } 131 mNetworkDelay = EmulatorConfigTab.getDelay(index); 132 133 try { 134 mEmulatorCommandLine = config.getAttribute( 135 LaunchConfigDelegate.ATTR_COMMANDLINE, ""); //$NON-NLS-1$ 136 } catch (CoreException e) { 137 // lets not do anything here, we'll use the default value 138 } 139 140 try { 141 mWipeData = config.getAttribute(LaunchConfigDelegate.ATTR_WIPE_DATA, mWipeData); 142 } catch (CoreException e) { 143 // nothing to be done here, we'll use the default value 144 } 145 146 try { 147 mNoBootAnim = config.getAttribute(LaunchConfigDelegate.ATTR_NO_BOOT_ANIM, 148 mNoBootAnim); 149 } catch (CoreException e) { 150 // nothing to be done here, we'll use the default value 151 } 152 } 153 154 /** 155 * Retrieve the {@link TargetMode} saved in the provided launch configuration. 156 * Returns defaultMode if there are any errors while retrieving or parsing the saved setting. 157 */ 158 public static TargetMode parseTargetMode(ILaunchConfiguration config, TargetMode defaultMode) { 159 try { 160 String value = config.getAttribute(LaunchConfigDelegate.ATTR_TARGET_MODE, 161 defaultMode.toString()); 162 return TargetMode.getMode(value); 163 } catch (CoreException e) { 164 // ADT R20 changes the attribute type of ATTR_TARGET_MODE to be a string from a bool. 165 // So if parsing as a string fails, attempt parsing as a boolean. 166 try { 167 boolean value = config.getAttribute(LaunchConfigDelegate.ATTR_TARGET_MODE, true); 168 return value ? TargetMode.AUTO : TargetMode.MANUAL; 169 } catch (CoreException e1) { 170 return defaultMode; 171 } 172 } catch (IllegalArgumentException e) { 173 return defaultMode; 174 } 175 } 176 } 177 178