1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17package vogar;
18
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.List;
22
23public enum ModeId {
24    /** ART (works >= L) */
25    DEVICE,
26    /** ART (works >= L) */
27    HOST,
28    /** Local Java */
29    JVM,
30    /** Device, execution as an Android app with Zygote */
31    ACTIVITY,
32    /** Device using app_process binary */
33    APP_PROCESS;
34
35    // $BOOTCLASSPATH defined by system/core/rootdir/init.rc
36    private static final String[] DEVICE_JARS = new String[] {
37            "core-libart",
38            "core-oj",
39            "conscrypt",
40            "okhttp",
41            "core-junit",
42            "bouncycastle",
43            "ext",
44            "framework",
45            "telephony-common",
46            "mms-common",
47            "framework",
48            "android.policy",
49            "services",
50            "apache-xml"};
51
52    private static final String[] HOST_JARS = new String[] {
53            "core-libart-hostdex",
54            "core-oj-hostdex",
55            "conscrypt-hostdex",
56            "okhttp-hostdex",
57            "bouncycastle-hostdex",
58            "apache-xml-hostdex"
59    };
60
61    public boolean acceptsVmArgs() {
62        return this != ACTIVITY;
63    }
64
65    /**
66     * Returns {@code true} if execution happens on the local machine. e.g. host-mode android or a
67     * JVM.
68     */
69    public boolean isLocal() {
70        return isHost() || this == ModeId.JVM;
71    }
72
73    /** Returns {@code true} if execution takes place with a host-mode Android runtime */
74    public boolean isHost() {
75        return this == HOST;
76    }
77
78    /** Returns {@code true} if execution takes place with a device-mode Android runtime */
79    public boolean isDevice() {
80        return this == ModeId.DEVICE || this == ModeId.APP_PROCESS;
81    }
82
83    public boolean requiresAndroidSdk() {
84        return this != JVM;
85    }
86
87    public boolean supportsVariant(Variant variant) {
88        return (variant == Variant.X32)
89                || ((this == HOST || this == DEVICE) && (variant == Variant.X64));
90    }
91
92    /** The default command to use for the mode unless overridden by --vm-command */
93    public String defaultVmCommand(Variant variant) {
94        if (!supportsVariant(variant)) {
95            throw new AssertionError("Unsupported variant: " + variant + " for " + this);
96        }
97        switch (this) {
98            case DEVICE:
99            case HOST:
100                if (variant == Variant.X32) {
101                    return "dalvikvm32";
102                } else {
103                    return "dalvikvm64";
104                }
105
106            case JVM:
107                return "java";
108            case APP_PROCESS:
109                return "app_process";
110            case ACTIVITY:
111                return null;
112            default:
113                throw new IllegalArgumentException("Unknown mode: " + this);
114        }
115    }
116
117    /**
118     * Return the names of jars required to compile in this mode when android.jar is not being used.
119     * Also used to generated the classpath in HOST* and DEVICE* modes.
120     */
121    public String[] getJarNames() {
122        List<String> jarNames = new ArrayList<String>();
123        switch (this) {
124            case ACTIVITY:
125            case APP_PROCESS:
126            case DEVICE:
127                jarNames.addAll(Arrays.asList(DEVICE_JARS));
128                break;
129            case HOST:
130                jarNames.addAll(Arrays.asList(HOST_JARS));
131                break;
132            default:
133                throw new IllegalArgumentException("Unsupported mode: " + this);
134        }
135        return jarNames.toArray(new String[jarNames.size()]);
136    }
137}
138