OSDN Git Service

SDK Manager: correctly handle lack of AvdManager
[android-x86/sdk.git] / androidprefs / src / com / android / prefs / AndroidLocation.java
1 /*
2  * Copyright (C) 2008 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
17 package com.android.prefs;
18
19 import java.io.File;
20
21 /**
22  * Manages the location of the android files (including emulator files, ddms config, debug keystore)
23  */
24 public final class AndroidLocation {
25     /**
26      * Virtual Device folder inside the path returned by {@link #getFolder()}
27      */
28     public static final String FOLDER_AVD = "avd";
29
30     /**
31      * Throw when the location of the android folder couldn't be found.
32      */
33     public static final class AndroidLocationException extends Exception {
34         private static final long serialVersionUID = 1L;
35
36         public AndroidLocationException(String string) {
37             super(string);
38         }
39     }
40
41     private static String sPrefsLocation = null;
42
43     /**
44      * Returns the folder used to store android related files.
45      * @return an OS specific path, terminated by a separator.
46      * @throws AndroidLocationException
47      */
48     public final static String getFolder() throws AndroidLocationException {
49         if (sPrefsLocation == null) {
50             String home = findValidPath("ANDROID_SDK_HOME", "user.home", "HOME");
51
52             // if the above failed, we throw an exception.
53             if (home == null) {
54                 throw new AndroidLocationException(
55                         "Unable to get the home directory. Make sure the environment variable ANDROID_SDK_HOME is set up");
56             } else {
57                 sPrefsLocation = home + File.separator + ".android" + File.separator;
58             }
59         }
60
61         // make sure the folder exists!
62         File f = new File(sPrefsLocation);
63         if (f.exists() == false) {
64             try {
65                 f.mkdir();
66             } catch (SecurityException e) {
67                 AndroidLocationException e2 = new AndroidLocationException(String.format(
68                         "Unable to create folder '%1$s'. " +
69                         "This is the path of preference folder expected by the Android tools.",
70                         sPrefsLocation));
71                 e2.initCause(e);
72                 throw e2;
73             }
74         } else if (f.isFile()) {
75             throw new AndroidLocationException(sPrefsLocation +
76                     " is not a directory! " +
77                     "This is the path of preference folder expected by the Android tools.");
78         }
79
80         return sPrefsLocation;
81     }
82
83     /**
84      * Checks a list of system properties and/or system environment variables for validity, and
85      * existing director, and returns the first one.
86      * @param names
87      * @return the content of the first property/variable.
88      */
89     private static String findValidPath(String... names) {
90         for (String name : names) {
91             String path;
92             if (name.indexOf('.') != -1) {
93                 path = System.getProperty(name);
94             } else {
95                 path = System.getenv(name);
96             }
97
98             if (path != null) {
99                 File f = new File(path);
100                 if (f.isDirectory()) {
101                     return path;
102                 }
103             }
104         }
105
106         return null;
107     }
108 }