aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/AndroidLaunchConfiguration.java
blob: 580bbbee5f0d61198680e73d71d79fdf4472eee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.launch;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;

/**
 * Launch configuration data. This stores the result of querying the
 * {@link ILaunchConfiguration} so that it's only done once.
 */
public class AndroidLaunchConfiguration {

    /**
     * Launch action. See {@link LaunchConfigDelegate#ACTION_DEFAULT},
     * {@link LaunchConfigDelegate#ACTION_ACTIVITY},
     * {@link LaunchConfigDelegate#ACTION_DO_NOTHING}
     */
    public int mLaunchAction = LaunchConfigDelegate.DEFAULT_LAUNCH_ACTION;

    /** Target selection mode for the configuration. */
    public enum TargetMode {
        /** Automatic target selection mode. */
        AUTO,
        /** Manual target selection mode. */
        MANUAL,
        /** All active devices */
        ALL_DEVICES,
        /** All active emulators */
        ALL_EMULATORS,
        /** All active devices and emulators */
        ALL_DEVICES_AND_EMULATORS;

        public static TargetMode getMode(String s) {
            for (TargetMode m: values()) {
                if (m.toString().equals(s)) {
                    return m;
                }
            }

            throw new IllegalArgumentException(String.format(
                    "Invalid representation (%s) for TargetMode", s));
        }

        public boolean isMultiDevice() {
            return this == ALL_DEVICES
                    || this == ALL_EMULATORS
                    || this == ALL_DEVICES_AND_EMULATORS;
        }
    }

    /**
     * Target selection mode.
     * @see TargetMode
     */
    public TargetMode mTargetMode = LaunchConfigDelegate.DEFAULT_TARGET_MODE;

    /**
     * Indicates whether the emulator should be called with -wipe-data
     */
    public boolean mWipeData = LaunchConfigDelegate.DEFAULT_WIPE_DATA;

    /**
     * Indicates whether the emulator should be called with -no-boot-anim
     */
    public boolean mNoBootAnim = LaunchConfigDelegate.DEFAULT_NO_BOOT_ANIM;

    /**
     * AVD Name.
     */
    public String mAvdName = null;

    public String mNetworkSpeed = EmulatorConfigTab.getSpeed(
            LaunchConfigDelegate.DEFAULT_SPEED);
    public String mNetworkDelay = EmulatorConfigTab.getDelay(
            LaunchConfigDelegate.DEFAULT_DELAY);

    /**
     * Optional custom command line parameter to launch the emulator
     */
    public String mEmulatorCommandLine;

    /** Flag indicating whether the same device should be used for future launches. */
    public boolean mReuseLastUsedDevice = false;

    /** Serial number of the device used in the last launch of this config. */
    public String mLastUsedDevice = null;

    /**
     * Initialized the structure from an ILaunchConfiguration object.
     * @param config
     */
    public void set(ILaunchConfiguration config) {
        try {
            mLaunchAction = config.getAttribute(LaunchConfigDelegate.ATTR_LAUNCH_ACTION,
                    mLaunchAction);
        } catch (CoreException e1) {
            // nothing to be done here, we'll use the default value
        }

        mTargetMode = parseTargetMode(config, mTargetMode);

        try {
            mReuseLastUsedDevice = config.getAttribute(
                    LaunchConfigDelegate.ATTR_REUSE_LAST_USED_DEVICE, false);
            mLastUsedDevice = config.getAttribute(
                    LaunchConfigDelegate.ATTR_LAST_USED_DEVICE, (String)null);
        } catch (CoreException e) {
         // nothing to be done here, we'll use the default value
        }

        try {
            mAvdName = config.getAttribute(LaunchConfigDelegate.ATTR_AVD_NAME, mAvdName);
        } catch (CoreException e) {
            // ignore
        }

        int index = LaunchConfigDelegate.DEFAULT_SPEED;
        try {
            index = config.getAttribute(LaunchConfigDelegate.ATTR_SPEED, index);
        } catch (CoreException e) {
            // nothing to be done here, we'll use the default value
        }
        mNetworkSpeed = EmulatorConfigTab.getSpeed(index);

        index = LaunchConfigDelegate.DEFAULT_DELAY;
        try {
            index = config.getAttribute(LaunchConfigDelegate.ATTR_DELAY, index);
        } catch (CoreException e) {
            // nothing to be done here, we'll use the default value
        }
        mNetworkDelay = EmulatorConfigTab.getDelay(index);

        try {
            mEmulatorCommandLine = config.getAttribute(
                    LaunchConfigDelegate.ATTR_COMMANDLINE, ""); //$NON-NLS-1$
        } catch (CoreException e) {
            // lets not do anything here, we'll use the default value
        }

        try {
            mWipeData = config.getAttribute(LaunchConfigDelegate.ATTR_WIPE_DATA, mWipeData);
        } catch (CoreException e) {
            // nothing to be done here, we'll use the default value
        }

        try {
            mNoBootAnim = config.getAttribute(LaunchConfigDelegate.ATTR_NO_BOOT_ANIM,
                                              mNoBootAnim);
        } catch (CoreException e) {
            // nothing to be done here, we'll use the default value
        }
    }

    /**
     * Retrieve the {@link TargetMode} saved in the provided launch configuration.
     * Returns defaultMode if there are any errors while retrieving or parsing the saved setting.
     */
    public static TargetMode parseTargetMode(ILaunchConfiguration config, TargetMode defaultMode) {
        try {
            String value = config.getAttribute(LaunchConfigDelegate.ATTR_TARGET_MODE,
                    defaultMode.toString());
            return TargetMode.getMode(value);
        } catch (CoreException e) {
            // ADT R20 changes the attribute type of ATTR_TARGET_MODE to be a string from a bool.
            // So if parsing as a string fails, attempt parsing as a boolean.
            try {
                boolean value = config.getAttribute(LaunchConfigDelegate.ATTR_TARGET_MODE, true);
                return value ? TargetMode.AUTO : TargetMode.MANUAL;
            } catch (CoreException e1) {
                return defaultMode;
            }
        } catch (IllegalArgumentException e) {
            return defaultMode;
        }
    }
}