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 Android SDK home directory.\n" + 56 "Make sure the environment variable ANDROID_SDK_HOME is set up."); 57 } else { 58 sPrefsLocation = home + File.separator + ".android" + File.separator; 59 } 60 } 61 62 // make sure the folder exists! 63 File f = new File(sPrefsLocation); 64 if (f.exists() == false) { 65 try { 66 f.mkdir(); 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.", 71 sPrefsLocation)); 72 e2.initCause(e); 73 throw e2; 74 } 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."); 79 } 80 81 return sPrefsLocation; 82 } 83 84 /** 85 * Checks a list of system properties and/or system environment variables for validity, and 86 * existing director, and returns the first one. 87 * @param names 88 * @return the content of the first property/variable. 89 */ 90 private static String findValidPath(String... names) { 91 for (String name : names) { 92 String path; 93 if (name.indexOf('.') != -1) { 94 path = System.getProperty(name); 95 } else { 96 path = System.getenv(name); 97 } 98 99 if (path != null) { 100 File f = new File(path); 101 if (f.isDirectory()) { 102 return path; 103 } 104 } 105 } 106 107 return null; 108 } 109 } 110