| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
| 9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
| 10 import android.os.Environment; | 10 import android.os.Environment; |
| 11 | 11 |
| 12 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
| 13 | 13 |
| 14 import java.io.File; | 14 import java.io.File; |
| 15 import java.util.concurrent.ExecutionException; | 15 import java.util.concurrent.ExecutionException; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * This class provides the path related methods for the native library. | 18 * This class provides the path related methods for the native library. |
| 19 */ | 19 */ |
| 20 public abstract class PathUtils { | 20 public abstract class PathUtils { |
| 21 private static final String THUMBNAIL_DIRECTORY = "textures"; | 21 private static final String THUMBNAIL_DIRECTORY = "textures"; |
| 22 | 22 |
| 23 private static final int DATA_DIRECTORY = 0; | 23 private static final int DATA_DIRECTORY = 0; |
| 24 private static final int DATABASE_DIRECTORY = 1; | 24 private static final int DATABASE_DIRECTORY = 1; |
| 25 private static final int CACHE_DIRECTORY = 2; | 25 private static final int CACHE_DIRECTORY = 2; |
| 26 private static final int DOWNLOADS_DIRECTORY = 3; | 26 private static final int NUM_DIRECTORIES = 3; |
| 27 private static final int NUM_DIRECTORIES = 4; | |
| 28 private static AsyncTask<String, Void, String[]> sDirPathFetchTask; | 27 private static AsyncTask<String, Void, String[]> sDirPathFetchTask; |
| 29 | 28 |
| 30 private static File sThumbnailDirectory; | 29 private static File sThumbnailDirectory; |
| 31 | 30 |
| 32 // Prevent instantiation. | 31 // Prevent instantiation. |
| 33 private PathUtils() {} | 32 private PathUtils() {} |
| 34 | 33 |
| 35 /** | 34 /** |
| 36 * Starts an asynchronous task to fetch the path of the directory where priv
ate data is to be | 35 * Starts an asynchronous task to fetch the path of the directory where priv
ate data is to be |
| 37 * stored by the application. | 36 * stored by the application. |
| 38 * | 37 * |
| 39 * @param suffix The private data directory suffix. | 38 * @param suffix The private data directory suffix. |
| 40 * @see Context#getDir(String, int) | 39 * @see Context#getDir(String, int) |
| 41 */ | 40 */ |
| 42 public static void setPrivateDataDirectorySuffix(String suffix, Context cont
ext) { | 41 public static void setPrivateDataDirectorySuffix(String suffix, Context cont
ext) { |
| 43 final Context appContext = context.getApplicationContext(); | 42 final Context appContext = context.getApplicationContext(); |
| 44 sDirPathFetchTask = new AsyncTask<String, Void, String[]>() { | 43 sDirPathFetchTask = new AsyncTask<String, Void, String[]>() { |
| 45 @Override | 44 @Override |
| 46 protected String[] doInBackground(String... dataDirectorySuffix) { | 45 protected String[] doInBackground(String... dataDirectorySuffix) { |
| 47 String[] paths = new String[NUM_DIRECTORIES]; | 46 String[] paths = new String[NUM_DIRECTORIES]; |
| 48 paths[DATA_DIRECTORY] = | 47 paths[DATA_DIRECTORY] = |
| 49 appContext.getDir(dataDirectorySuffix[0], Context.MODE_P
RIVATE).getPath(); | 48 appContext.getDir(dataDirectorySuffix[0], Context.MODE_P
RIVATE).getPath(); |
| 50 paths[DATABASE_DIRECTORY] = appContext.getDatabasePath("foo").ge
tParent(); | 49 paths[DATABASE_DIRECTORY] = appContext.getDatabasePath("foo").ge
tParent(); |
| 51 // TODO(wnwen): Find a way to avoid calling this function in ren
derer process. | 50 // TODO(wnwen): Find a way to avoid calling this function in ren
derer process. |
| 52 if (appContext.getCacheDir() != null) { | 51 if (appContext.getCacheDir() != null) { |
| 53 // These paths are only available in the browser process. | |
| 54 paths[CACHE_DIRECTORY] = appContext.getCacheDir().getPath(); | 52 paths[CACHE_DIRECTORY] = appContext.getCacheDir().getPath(); |
| 55 paths[DOWNLOADS_DIRECTORY] = Environment | |
| 56 .getExternalStoragePubl
icDirectory( | |
| 57 Environment.DIR
ECTORY_DOWNLOADS) | |
| 58 .getPath(); | |
| 59 } | 53 } |
| 60 return paths; | 54 return paths; |
| 61 } | 55 } |
| 62 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, suffix); | 56 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, suffix); |
| 63 } | 57 } |
| 64 | 58 |
| 65 /** | 59 /** |
| 66 * @param index The index of the cached directory path. | 60 * @param index The index of the cached directory path. |
| 67 * @return The directory path requested, or null if not available. | 61 * @return The directory path requested, or null if not available. |
| 68 */ | 62 */ |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 public static String getThumbnailCacheDirectoryPath(Context appContext) { | 108 public static String getThumbnailCacheDirectoryPath(Context appContext) { |
| 115 return getThumbnailCacheDirectory(appContext).getAbsolutePath(); | 109 return getThumbnailCacheDirectory(appContext).getAbsolutePath(); |
| 116 } | 110 } |
| 117 | 111 |
| 118 /** | 112 /** |
| 119 * @return the public downloads directory. | 113 * @return the public downloads directory. |
| 120 */ | 114 */ |
| 121 @SuppressWarnings("unused") | 115 @SuppressWarnings("unused") |
| 122 @CalledByNative | 116 @CalledByNative |
| 123 private static String getDownloadsDirectory(Context appContext) { | 117 private static String getDownloadsDirectory(Context appContext) { |
| 124 assert sDirPathFetchTask != null : "setDataDirectorySuffix must be calle
d first."; | 118 return Environment.getExternalStoragePublicDirectory(Environment.DIRECTO
RY_DOWNLOADS) |
| 125 return getDirectoryPath(DOWNLOADS_DIRECTORY); | 119 .getPath(); |
| 126 } | 120 } |
| 127 | 121 |
| 128 /** | 122 /** |
| 129 * @return the path to native libraries. | 123 * @return the path to native libraries. |
| 130 */ | 124 */ |
| 131 @SuppressWarnings("unused") | 125 @SuppressWarnings("unused") |
| 132 @CalledByNative | 126 @CalledByNative |
| 133 private static String getNativeLibraryDirectory(Context appContext) { | 127 private static String getNativeLibraryDirectory(Context appContext) { |
| 134 ApplicationInfo ai = appContext.getApplicationInfo(); | 128 ApplicationInfo ai = appContext.getApplicationInfo(); |
| 135 if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 | 129 if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 |
| 136 || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { | 130 || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 137 return ai.nativeLibraryDir; | 131 return ai.nativeLibraryDir; |
| 138 } | 132 } |
| 139 | 133 |
| 140 return "/system/lib/"; | 134 return "/system/lib/"; |
| 141 } | 135 } |
| 142 | 136 |
| 143 /** | 137 /** |
| 144 * @return the external storage directory. | 138 * @return the external storage directory. |
| 145 */ | 139 */ |
| 146 @SuppressWarnings("unused") | 140 @SuppressWarnings("unused") |
| 147 @CalledByNative | 141 @CalledByNative |
| 148 public static String getExternalStorageDirectory() { | 142 public static String getExternalStorageDirectory() { |
| 149 return Environment.getExternalStorageDirectory().getAbsolutePath(); | 143 return Environment.getExternalStorageDirectory().getAbsolutePath(); |
| 150 } | 144 } |
| 151 } | 145 } |
| OLD | NEW |