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. |
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 cache directory. |
60 | */ |
61 | @SuppressWarnings("unused") |
62 | @CalledByNative |
63 | public static String getCacheDirectory(Context appContext) { |
64 | if (ContextTypes.getInstance().getType(appContext) == ContextTypes.CONTEXT_TYPE_NORMAL) { |
65 | return appContext.getCacheDir().getPath(); |
66 | } |
67 | if (sWebappDirectorySuffix == null || sWebappCacheDirectory == null) { |
68 | throw new IllegalStateException( |
69 | "setWebappDirectoryInfo must be called before getCacheDirectory"); |
70 | } |
71 | return new File(appContext.getDir(sWebappDirectorySuffix, appContext.MODE_PRIVATE), |
72 | sWebappCacheDirectory).getPath(); |
73 | } |
74 | |
75 | /** |
76 | * @return the public downloads directory. |
77 | */ |
78 | @SuppressWarnings("unused") |
79 | @CalledByNative |
80 | private static String getDownloadsDirectory(Context appContext) { |
81 | return Environment.getExternalStoragePublicDirectory( |
82 | Environment.DIRECTORY_DOWNLOADS).getPath(); |
83 | } |
84 | |
85 | /** |
86 | * @return the path to native libraries. |
87 | */ |
88 | @SuppressWarnings("unused") |
89 | @CalledByNative |
90 | private static String getNativeLibraryDirectory(Context appContext) { |
91 | ApplicationInfo ai = appContext.getApplicationInfo(); |
92 | if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 || |
93 | (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
94 | return ai.nativeLibraryDir; |
95 | } |
96 | |
97 | return "/system/lib/"; |
98 | } |
99 | |
100 | /** |
101 | * @return the external storage directory. |
102 | */ |
103 | @SuppressWarnings("unused") |
104 | @CalledByNative |
105 | public static String getExternalStorageDirectory() { |
106 | return Environment.getExternalStorageDirectory().getAbsolutePath(); |
107 | } |
108 | } |