1 // Copyright 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base; 6 7 import android.content.Context; 8 import android.content.pm.ApplicationInfo; 9 import android.os.Environment; 10 11 import java.io.File; 12 13 /** 14 * This class provides the path related methods for the native library. 15 */ 16 public abstract class PathUtils { 17 18 private static String sDataDirectorySuffix; 19 private static String sWebappDirectorySuffix; 20 private static String sWebappCacheDirectory; 21 22 // Prevent instantiation. 23 private PathUtils() {} 24 25 /** 26 * Sets the suffix that should be used for the directory where private data is to be stored 27 * by the application. 28 * @param suffix The private data directory suffix. 29 * @see Context#getDir(String, int) 30 */ 31 public static void setPrivateDataDirectorySuffix(String suffix) { 32 sDataDirectorySuffix = suffix; 33 } 34 35 /** 36 * Sets the directory info used for chrome process running in application mode. 37 * 38 * @param webappSuffix The suffix of the directory used for storing webapp-specific profile 39 * @param cacheDir Cache directory name for web apps. 40 */ 41 public static void setWebappDirectoryInfo(String webappSuffix, String cacheDir) { 42 sWebappDirectorySuffix = webappSuffix; 43 sWebappCacheDirectory = cacheDir; 44 } 45 46 /** 47 * @return the private directory that is used to store application data. 48 */ 49 @CalledByNative 50 public static String getDataDirectory(Context appContext) { 51 if (sDataDirectorySuffix == null) { 52 throw new IllegalStateException( 53 "setDataDirectorySuffix must be called before getDataDirectory"); 54 } 55 return appContext.getDir(sDataDirectorySuffix, Context.MODE_PRIVATE).getPath(); 56 } 57 58 /** 59 * @return the private directory that is used to store application database. 60 */ 61 @CalledByNative 62 public static String getDatabaseDirectory(Context appContext) { 63 // Context.getDatabasePath() returns path for the provided filename. 64 return appContext.getDatabasePath("foo").getParent(); 65 } 66 67 /** 68 * @return the cache directory. 69 */ 70 @SuppressWarnings("unused") 71 @CalledByNative 72 public static String getCacheDirectory(Context appContext) { 73 if (ContextTypes.getInstance().getType(appContext) == ContextTypes.CONTEXT_TYPE_NORMAL) { 74 return appContext.getCacheDir().getPath(); 75 } 76 if (sWebappDirectorySuffix == null || sWebappCacheDirectory == null) { 77 throw new IllegalStateException( 78 "setWebappDirectoryInfo must be called before getCacheDirectory"); 79 } 80 return new File(appContext.getDir(sWebappDirectorySuffix, appContext.MODE_PRIVATE), 81 sWebappCacheDirectory).getPath(); 82 } 83 84 /** 85 * @return the public downloads directory. 86 */ 87 @SuppressWarnings("unused") 88 @CalledByNative 89 private static String getDownloadsDirectory(Context appContext) { 90 return Environment.getExternalStoragePublicDirectory( 91 Environment.DIRECTORY_DOWNLOADS).getPath(); 92 } 93 94 /** 95 * @return the path to native libraries. 96 */ 97 @SuppressWarnings("unused") 98 @CalledByNative 99 private static String getNativeLibraryDirectory(Context appContext) { 100 ApplicationInfo ai = appContext.getApplicationInfo(); 101 if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 || 102 (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { 103 return ai.nativeLibraryDir; 104 } 105 106 return "/system/lib/"; 107 } 108 109 /** 110 * @return the external storage directory. 111 */ 112 @SuppressWarnings("unused") 113 @CalledByNative 114 public static String getExternalStorageDirectory() { 115 return Environment.getExternalStorageDirectory().getAbsolutePath(); 116 } 117 } 118