1145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey/*
2145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project
3145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey *
4145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
5145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * you may not use this file except in compliance with the License.
6145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * You may obtain a copy of the License at
7145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey *
8145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
9145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey *
10145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
11145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
12145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * See the License for the specific language governing permissions and
14145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey * limitations under the License.
15145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey */
16145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
17145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkeypackage android.support.v4.os;
18145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
19145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkeyimport android.os.Build;
20145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkeyimport android.os.Environment;
21145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkeyimport android.util.Log;
22145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
23145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkeyimport java.io.File;
24145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkeyimport java.io.IOException;
25145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
26145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey/**
27c76e59bb75def1cb5e2084fd719681647678283cAurimas Liutikas * Helper for accessing features in {@link Environment} in a backwards compatible fashion.
28145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey */
29c5847d13e40f5d52459f5c0dab32dc08f1a9a683Chris Banespublic final class EnvironmentCompat {
30145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey    private static final String TAG = "EnvironmentCompat";
31145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
32145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey    /**
33145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     * Unknown storage state, such as when a path isn't backed by known storage
34145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     * media.
35145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *
36145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     * @see #getStorageState(File)
37145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     */
38145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey    public static final String MEDIA_UNKNOWN = "unknown";
39145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
40145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey    /**
41145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     * Returns the current state of the storage device that provides the given
42145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     * path.
43145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *
44145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     * @return one of {@link #MEDIA_UNKNOWN}, {@link Environment#MEDIA_REMOVED},
45145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_UNMOUNTED},
46145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_CHECKING},
47145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_NOFS},
48145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_MOUNTED},
49145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_MOUNTED_READ_ONLY},
50145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_SHARED},
51145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_BAD_REMOVAL}, or
52145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     *         {@link Environment#MEDIA_UNMOUNTABLE}.
53145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey     */
54145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey    public static String getStorageState(File path) {
55c69882cb9b130902c1554ef5d3e3b06d776cd796Alan Viverette        if (Build.VERSION.SDK_INT >= 19) {
56c76e59bb75def1cb5e2084fd719681647678283cAurimas Liutikas            return Environment.getStorageState(path);
57145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey        }
58145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
59145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey        try {
60145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey            final String canonicalPath = path.getCanonicalPath();
61145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey            final String canonicalExternal = Environment.getExternalStorageDirectory()
62145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey                    .getCanonicalPath();
63145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
64145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey            if (canonicalPath.startsWith(canonicalExternal)) {
65145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey                return Environment.getExternalStorageState();
66145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey            }
67145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey        } catch (IOException e) {
68145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey            Log.w(TAG, "Failed to resolve canonical path: " + e);
69145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey        }
70145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey
71145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey        return MEDIA_UNKNOWN;
72145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey    }
73c5847d13e40f5d52459f5c0dab32dc08f1a9a683Chris Banes
74c5847d13e40f5d52459f5c0dab32dc08f1a9a683Chris Banes    private EnvironmentCompat() {}
75145d27f9dd22e953d05e01327c9c2dac68634ae8Jeff Sharkey}
76