PathUtils.java revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
5package org.chromium.base;
6
7import android.content.Context;
8import android.content.pm.ApplicationInfo;
9import android.os.Environment;
10
11/**
12 * This class provides the path related methods for the native library.
13 */
14public abstract class PathUtils {
15
16    private static String sDataDirectorySuffix;
17
18    // Prevent instantiation.
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     */
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
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 cache directory.
45     */
46    @SuppressWarnings("unused")
47    @CalledByNative
48    private static String getCacheDirectory(Context appContext) {
49        return appContext.getCacheDir().getPath();
50    }
51
52    /**
53     * @return the public downloads directory.
54     */
55    @SuppressWarnings("unused")
56    @CalledByNative
57    private static String getDownloadsDirectory(Context appContext) {
58        return Environment.getExternalStoragePublicDirectory(
59                Environment.DIRECTORY_DOWNLOADS).getPath();
60    }
61
62    /**
63     * @return the path to native libraries.
64     */
65    @SuppressWarnings("unused")
66    @CalledByNative
67    private static String getNativeLibraryDirectory(Context appContext) {
68        ApplicationInfo ai = appContext.getApplicationInfo();
69        if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ||
70            (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
71            return ai.nativeLibraryDir;
72        }
73
74        return "/system/lib/";
75    }
76
77    /**
78     * @return the external storage directory.
79     */
80    @SuppressWarnings("unused")
81    @CalledByNative
82    public static String getExternalStorageDirectory() {
83        return Environment.getExternalStorageDirectory().getAbsolutePath();
84    }
85}
86