2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.prefs;
22 * Manages the location of the android files (including emulator files, ddms config, debug keystore)
24 public final class AndroidLocation {
26 * Virtual Device folder inside the path returned by {@link #getFolder()}
28 public static final String FOLDER_AVD = "avd";
31 * Throw when the location of the android folder couldn't be found.
33 public static final class AndroidLocationException extends Exception {
34 private static final long serialVersionUID = 1L;
36 public AndroidLocationException(String string) {
41 private static String sPrefsLocation = null;
44 * Returns the folder used to store android related files.
45 * @return an OS specific path, terminated by a separator.
46 * @throws AndroidLocationException
48 public final static String getFolder() throws AndroidLocationException {
49 if (sPrefsLocation == null) {
50 String home = findValidPath("ANDROID_SDK_HOME", "user.home", "HOME");
52 // if the above failed, we throw an exception.
54 throw new AndroidLocationException(
55 "Unable to get the Android SDK home directory.\n" +
56 "Make sure the environment variable ANDROID_SDK_HOME is set up.");
58 sPrefsLocation = home + File.separator + ".android" + File.separator;
62 // make sure the folder exists!
63 File f = new File(sPrefsLocation);
64 if (f.exists() == false) {
67 } catch (SecurityException e) {
68 AndroidLocationException e2 = new AndroidLocationException(String.format(
69 "Unable to create folder '%1$s'. " +
70 "This is the path of preference folder expected by the Android tools.",
75 } else if (f.isFile()) {
76 throw new AndroidLocationException(sPrefsLocation +
77 " is not a directory! " +
78 "This is the path of preference folder expected by the Android tools.");
81 return sPrefsLocation;
85 * Checks a list of system properties and/or system environment variables for validity, and
86 * existing director, and returns the first one.
88 * @return the content of the first property/variable.
90 private static String findValidPath(String... names) {
91 for (String name : names) {
93 if (name.indexOf('.') != -1) {
94 path = System.getProperty(name);
96 path = System.getenv(name);
100 File f = new File(path);
101 if (f.isDirectory()) {