• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
12  * This class provides the path related methods for the native library.
13  */
14 public abstract class PathUtils {
15 
16     private static String sDataDirectorySuffix;
17 
18     // Prevent instantiation.
PathUtils()19     private PathUtils() {}
20 
21     /**
22      * Sets the suffix that should be used for the directory where private data is to be stored
23      * by the application.
24      * @param suffix The private data directory suffix.
25      * @see Context#getDir(String, int)
26      */
setPrivateDataDirectorySuffix(String suffix)27     public static void setPrivateDataDirectorySuffix(String suffix) {
28         sDataDirectorySuffix = suffix;
29     }
30 
31     /**
32      * @return the private directory that is used to store application data.
33      */
34     @CalledByNative
getDataDirectory(Context appContext)35     public static String getDataDirectory(Context appContext) {
36         if (sDataDirectorySuffix == null) {
37             throw new IllegalStateException(
38                     "setDataDirectorySuffix must be called before getDataDirectory");
39         }
40         return appContext.getDir(sDataDirectorySuffix, Context.MODE_PRIVATE).getPath();
41     }
42 
43     /**
44      * @return the private directory that is used to store application database.
45      */
46     @CalledByNative
getDatabaseDirectory(Context appContext)47     public static String getDatabaseDirectory(Context appContext) {
48         // Context.getDatabasePath() returns path for the provided filename.
49         return appContext.getDatabasePath("foo").getParent();
50     }
51 
52     /**
53      * @return the cache directory.
54      */
55     @SuppressWarnings("unused")
56     @CalledByNative
getCacheDirectory(Context appContext)57     public static String getCacheDirectory(Context appContext) {
58         return appContext.getCacheDir().getPath();
59     }
60 
61     /**
62      * @return the public downloads directory.
63      */
64     @SuppressWarnings("unused")
65     @CalledByNative
getDownloadsDirectory(Context appContext)66     private static String getDownloadsDirectory(Context appContext) {
67         return Environment.getExternalStoragePublicDirectory(
68                 Environment.DIRECTORY_DOWNLOADS).getPath();
69     }
70 
71     /**
72      * @return the path to native libraries.
73      */
74     @SuppressWarnings("unused")
75     @CalledByNative
getNativeLibraryDirectory(Context appContext)76     private static String getNativeLibraryDirectory(Context appContext) {
77         ApplicationInfo ai = appContext.getApplicationInfo();
78         if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ||
79             (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
80             return ai.nativeLibraryDir;
81         }
82 
83         return "/system/lib/";
84     }
85 
86     /**
87      * @return the external storage directory.
88      */
89     @SuppressWarnings("unused")
90     @CalledByNative
getExternalStorageDirectory()91     public static String getExternalStorageDirectory() {
92         return Environment.getExternalStorageDirectory().getAbsolutePath();
93     }
94 }
95