1 // Copyright (c) 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.
5 package org.chromium.base;
7 import android.content.Context;
8 import android.content.pm.ApplicationInfo;
9 import android.os.Environment;
14 * This class provides the path related methods for the native library.
16 public abstract class PathUtils {
18 private static String sDataDirectorySuffix;
19 private static String sWebappDirectorySuffix;
20 private static String sWebappCacheDirectory;
22 // Prevent instantiation.
23 private PathUtils() {}
26 * Sets the suffix that should be used for the directory where private data is to be stored
28 * @param suffix The private data directory suffix.
29 * @see Context#getDir(String, int)
31 public static void setPrivateDataDirectorySuffix(String suffix) {
32 sDataDirectorySuffix = suffix;
36 * Sets the directory info used for chrome process running in application mode.
38 * @param webappSuffix The suffix of the directory used for storing webapp-specific profile
39 * @param cacheDir Cache directory name for web apps.
41 public static void setWebappDirectoryInfo(String webappSuffix, String cacheDir) {
42 sWebappDirectorySuffix = webappSuffix;
43 sWebappCacheDirectory = cacheDir;
47 * @return the private directory that is used to store application data.
50 public static String getDataDirectory(Context appContext) {
51 if (sDataDirectorySuffix == null) {
52 throw new IllegalStateException(
53 "setDataDirectorySuffix must be called before getDataDirectory");
55 return appContext.getDir(sDataDirectorySuffix, Context.MODE_PRIVATE).getPath();
59 * @return the private directory that is used to store application database.
62 public static String getDatabaseDirectory(Context appContext) {
63 // Context.getDatabasePath() returns path for the provided filename.
64 return appContext.getDatabasePath("foo").getParent();
68 * @return the cache directory.
70 @SuppressWarnings("unused")
72 public static String getCacheDirectory(Context appContext) {
73 if (ContextTypes.getInstance().getType(appContext) == ContextTypes.CONTEXT_TYPE_NORMAL) {
74 return appContext.getCacheDir().getPath();
76 if (sWebappDirectorySuffix == null || sWebappCacheDirectory == null) {
77 throw new IllegalStateException(
78 "setWebappDirectoryInfo must be called before getCacheDirectory");
80 return new File(appContext.getDir(sWebappDirectorySuffix, appContext.MODE_PRIVATE),
81 sWebappCacheDirectory).getPath();
85 * @return the public downloads directory.
87 @SuppressWarnings("unused")
89 private static String getDownloadsDirectory(Context appContext) {
90 return Environment.getExternalStoragePublicDirectory(
91 Environment.DIRECTORY_DOWNLOADS).getPath();
95 * @return the path to native libraries.
97 @SuppressWarnings("unused")
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;
106 return "/system/lib/";
110 * @return the external storage directory.
112 @SuppressWarnings("unused")
114 public static String getExternalStorageDirectory() {
115 return Environment.getExternalStorageDirectory().getAbsolutePath();