Jeff Sharkey | 669f8e7 | 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 | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.State.MODE_UNKNOWN; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 20 | |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.Nullable; |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 24 | import android.content.SharedPreferences; |
| 25 | import android.os.UserHandle; |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 26 | import android.preference.PreferenceManager; |
| 27 | |
Steve McKay | dd27444 | 2016-06-20 11:46:56 -0700 | [diff] [blame] | 28 | import com.android.documentsui.State.ActionType; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 29 | import com.android.documentsui.State.ViewMode; |
| 30 | import com.android.documentsui.model.RootInfo; |
| 31 | |
Steve McKay | dd27444 | 2016-06-20 11:46:56 -0700 | [diff] [blame] | 32 | import java.lang.annotation.Retention; |
| 33 | import java.lang.annotation.RetentionPolicy; |
| 34 | |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 35 | public class LocalPreferences { |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 36 | private static final String KEY_FILE_SIZE = "fileSize"; |
Steve McKay | dd27444 | 2016-06-20 11:46:56 -0700 | [diff] [blame] | 37 | private static final String INCLUDE_DEVICE_ROOT = "includeDeviceRoot-"; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 38 | private static final String ROOT_VIEW_MODE_PREFIX = "rootViewMode-"; |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 39 | |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 40 | public static boolean getDisplayFileSize(Context context) { |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 41 | return getPrefs(context).getBoolean(KEY_FILE_SIZE, false); |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 44 | public static @ViewMode int getViewMode(Context context, RootInfo root, |
| 45 | @ViewMode int fallback) { |
| 46 | return getPrefs(context).getInt(createKey(root), fallback); |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 49 | public static void setDisplayFileSize(Context context, boolean display) { |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 50 | getPrefs(context).edit().putBoolean(KEY_FILE_SIZE, display).apply(); |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 51 | } |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 52 | |
Steve McKay | dd27444 | 2016-06-20 11:46:56 -0700 | [diff] [blame] | 53 | public static boolean getShowDeviceRoot(Context context, @ActionType int action) { |
| 54 | return getPrefs(context).getBoolean(INCLUDE_DEVICE_ROOT + action, false); |
| 55 | } |
| 56 | |
| 57 | public static void setShowDeviceRoot( |
| 58 | Context context, @ActionType int action, boolean display) { |
| 59 | getPrefs(context).edit().putBoolean(INCLUDE_DEVICE_ROOT + action, display).apply(); |
| 60 | } |
| 61 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 62 | public static void setViewMode(Context context, RootInfo root, @ViewMode int viewMode) { |
Steve McKay | a1f7680 | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 63 | assert(viewMode != MODE_UNKNOWN); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 64 | getPrefs(context).edit().putInt(createKey(root), viewMode).apply(); |
| 65 | } |
| 66 | |
| 67 | private static SharedPreferences getPrefs(Context context) { |
| 68 | return PreferenceManager.getDefaultSharedPreferences(context); |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private static String createKey(RootInfo root) { |
| 72 | return ROOT_VIEW_MODE_PREFIX + root.authority + root.rootId; |
| 73 | } |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 74 | |
| 75 | public static final int PERMISSION_ASK = 0; |
| 76 | public static final int PERMISSION_ASK_AGAIN = 1; |
| 77 | public static final int PERMISSION_NEVER_ASK = -1; |
| 78 | |
| 79 | @IntDef(flag = true, value = { |
| 80 | PERMISSION_ASK, |
| 81 | PERMISSION_ASK_AGAIN, |
| 82 | PERMISSION_NEVER_ASK, |
| 83 | }) |
| 84 | @Retention(RetentionPolicy.SOURCE) |
| 85 | public @interface PermissionStatus {} |
| 86 | |
| 87 | /** |
| 88 | * Methods below are used to keep track of denied user requests on scoped directory access so |
| 89 | * the dialog is not offered when user checked the 'Do not ask again' box |
| 90 | * |
| 91 | * <p>It uses a shared preferences, whose key is: |
| 92 | * <ol> |
| 93 | * <li>{@code USER_ID|PACKAGE_NAME|VOLUME_UUID|DIRECTORY} for storage volumes that have a UUID |
| 94 | * (typically physical volumes like SD cards). |
| 95 | * <li>{@code USER_ID|PACKAGE_NAME||DIRECTORY} for storage volumes that do not have a UUID |
| 96 | * (typically the emulated volume used for primary storage |
| 97 | * </ol> |
| 98 | */ |
| 99 | static @PermissionStatus int getScopedAccessPermissionStatus(Context context, |
| 100 | String packageName, @Nullable String uuid, String directory) { |
| 101 | final String key = getScopedAccessDenialsKey(packageName, uuid, directory); |
| 102 | return getPrefs(context).getInt(key, PERMISSION_ASK); |
| 103 | } |
| 104 | |
| 105 | static void setScopedAccessPermissionStatus(Context context, String packageName, |
| 106 | @Nullable String uuid, String directory, @PermissionStatus int status) { |
| 107 | final String key = getScopedAccessDenialsKey(packageName, uuid, directory); |
| 108 | getPrefs(context).edit().putInt(key, status).apply(); |
| 109 | } |
| 110 | |
| 111 | private static String getScopedAccessDenialsKey(String packageName, String uuid, |
| 112 | String directory) { |
| 113 | final int userId = UserHandle.myUserId(); |
| 114 | return uuid == null |
| 115 | ? userId + "|" + packageName + "||" + directory |
| 116 | : userId + "|" + packageName + "|" + uuid + "|" + directory; |
| 117 | } |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 118 | } |