Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.State.MODE_UNKNOWN; |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 20 | |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 21 | import java.lang.annotation.Retention; |
| 22 | import java.lang.annotation.RetentionPolicy; |
| 23 | |
| 24 | import android.annotation.IntDef; |
| 25 | import android.annotation.Nullable; |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 27 | import android.content.SharedPreferences; |
| 28 | import android.os.UserHandle; |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 29 | import android.preference.PreferenceManager; |
| 30 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 31 | import com.android.documentsui.State.ViewMode; |
| 32 | import com.android.documentsui.model.RootInfo; |
| 33 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 34 | public class LocalPreferences { |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 35 | private static final String KEY_FILE_SIZE = "fileSize"; |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 36 | private static final String ROOT_VIEW_MODE_PREFIX = "rootViewMode-"; |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 37 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 38 | public static boolean getDisplayFileSize(Context context) { |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 39 | return getPrefs(context).getBoolean(KEY_FILE_SIZE, false); |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 42 | public static @ViewMode int getViewMode(Context context, RootInfo root, |
| 43 | @ViewMode int fallback) { |
| 44 | return getPrefs(context).getInt(createKey(root), fallback); |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 47 | public static void setDisplayFileSize(Context context, boolean display) { |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 48 | getPrefs(context).edit().putBoolean(KEY_FILE_SIZE, display).apply(); |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 49 | } |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 50 | |
| 51 | public static void setViewMode(Context context, RootInfo root, @ViewMode int viewMode) { |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 52 | assert(viewMode != MODE_UNKNOWN); |
| 53 | |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 54 | getPrefs(context).edit().putInt(createKey(root), viewMode).apply(); |
| 55 | } |
| 56 | |
| 57 | private static SharedPreferences getPrefs(Context context) { |
| 58 | return PreferenceManager.getDefaultSharedPreferences(context); |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | private static String createKey(RootInfo root) { |
| 62 | return ROOT_VIEW_MODE_PREFIX + root.authority + root.rootId; |
| 63 | } |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 64 | |
| 65 | public static final int PERMISSION_ASK = 0; |
| 66 | public static final int PERMISSION_ASK_AGAIN = 1; |
| 67 | public static final int PERMISSION_NEVER_ASK = -1; |
| 68 | |
| 69 | @IntDef(flag = true, value = { |
| 70 | PERMISSION_ASK, |
| 71 | PERMISSION_ASK_AGAIN, |
| 72 | PERMISSION_NEVER_ASK, |
| 73 | }) |
| 74 | @Retention(RetentionPolicy.SOURCE) |
| 75 | public @interface PermissionStatus {} |
| 76 | |
| 77 | /** |
| 78 | * Methods below are used to keep track of denied user requests on scoped directory access so |
| 79 | * the dialog is not offered when user checked the 'Do not ask again' box |
| 80 | * |
| 81 | * <p>It uses a shared preferences, whose key is: |
| 82 | * <ol> |
| 83 | * <li>{@code USER_ID|PACKAGE_NAME|VOLUME_UUID|DIRECTORY} for storage volumes that have a UUID |
| 84 | * (typically physical volumes like SD cards). |
| 85 | * <li>{@code USER_ID|PACKAGE_NAME||DIRECTORY} for storage volumes that do not have a UUID |
| 86 | * (typically the emulated volume used for primary storage |
| 87 | * </ol> |
| 88 | */ |
| 89 | static @PermissionStatus int getScopedAccessPermissionStatus(Context context, |
| 90 | String packageName, @Nullable String uuid, String directory) { |
| 91 | final String key = getScopedAccessDenialsKey(packageName, uuid, directory); |
| 92 | return getPrefs(context).getInt(key, PERMISSION_ASK); |
| 93 | } |
| 94 | |
| 95 | static void setScopedAccessPermissionStatus(Context context, String packageName, |
| 96 | @Nullable String uuid, String directory, @PermissionStatus int status) { |
| 97 | final String key = getScopedAccessDenialsKey(packageName, uuid, directory); |
| 98 | getPrefs(context).edit().putInt(key, status).apply(); |
| 99 | } |
| 100 | |
| 101 | private static String getScopedAccessDenialsKey(String packageName, String uuid, |
| 102 | String directory) { |
| 103 | final int userId = UserHandle.myUserId(); |
| 104 | return uuid == null |
| 105 | ? userId + "|" + packageName + "||" + directory |
| 106 | : userId + "|" + packageName + "|" + uuid + "|" + directory; |
| 107 | } |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 108 | } |