blob: d2e9885edf1456b29639a4bc5725daf07431f99b [file] [log] [blame]
Jeff Sharkey669f8e72014-08-08 15:10:03 -07001/*
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
17package com.android.documentsui;
18
Steve McKay3eb2d072016-01-25 19:00:22 -080019import static com.android.documentsui.State.MODE_UNKNOWN;
Steve McKay3eb2d072016-01-25 19:00:22 -080020
Felipe Lemeadccb992016-03-09 17:40:49 -080021import android.annotation.IntDef;
22import android.annotation.Nullable;
Jeff Sharkey669f8e72014-08-08 15:10:03 -070023import android.content.Context;
Felipe Lemeadccb992016-03-09 17:40:49 -080024import android.content.SharedPreferences;
25import android.os.UserHandle;
Jeff Sharkey669f8e72014-08-08 15:10:03 -070026import android.preference.PreferenceManager;
27
Steve McKaydd274442016-06-20 11:46:56 -070028import com.android.documentsui.State.ActionType;
Steve McKay3eb2d072016-01-25 19:00:22 -080029import com.android.documentsui.State.ViewMode;
30import com.android.documentsui.model.RootInfo;
31
Steve McKaydd274442016-06-20 11:46:56 -070032import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
34
Jeff Sharkey669f8e72014-08-08 15:10:03 -070035public class LocalPreferences {
Jeff Sharkey669f8e72014-08-08 15:10:03 -070036 private static final String KEY_FILE_SIZE = "fileSize";
Steve McKaydd274442016-06-20 11:46:56 -070037 private static final String INCLUDE_DEVICE_ROOT = "includeDeviceRoot-";
Steve McKay3eb2d072016-01-25 19:00:22 -080038 private static final String ROOT_VIEW_MODE_PREFIX = "rootViewMode-";
Jeff Sharkey669f8e72014-08-08 15:10:03 -070039
Jeff Sharkey669f8e72014-08-08 15:10:03 -070040 public static boolean getDisplayFileSize(Context context) {
Felipe Lemeadccb992016-03-09 17:40:49 -080041 return getPrefs(context).getBoolean(KEY_FILE_SIZE, false);
Jeff Sharkey669f8e72014-08-08 15:10:03 -070042 }
43
Felipe Lemeadccb992016-03-09 17:40:49 -080044 public static @ViewMode int getViewMode(Context context, RootInfo root,
45 @ViewMode int fallback) {
46 return getPrefs(context).getInt(createKey(root), fallback);
Steve McKay3eb2d072016-01-25 19:00:22 -080047 }
48
Jeff Sharkey669f8e72014-08-08 15:10:03 -070049 public static void setDisplayFileSize(Context context, boolean display) {
Felipe Lemeadccb992016-03-09 17:40:49 -080050 getPrefs(context).edit().putBoolean(KEY_FILE_SIZE, display).apply();
Jeff Sharkey669f8e72014-08-08 15:10:03 -070051 }
Steve McKay3eb2d072016-01-25 19:00:22 -080052
Steve McKaydd274442016-06-20 11:46:56 -070053 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 McKay3eb2d072016-01-25 19:00:22 -080062 public static void setViewMode(Context context, RootInfo root, @ViewMode int viewMode) {
Steve McKaya1f76802016-02-25 13:34:03 -080063 assert(viewMode != MODE_UNKNOWN);
Felipe Lemeadccb992016-03-09 17:40:49 -080064 getPrefs(context).edit().putInt(createKey(root), viewMode).apply();
65 }
66
67 private static SharedPreferences getPrefs(Context context) {
68 return PreferenceManager.getDefaultSharedPreferences(context);
Steve McKay3eb2d072016-01-25 19:00:22 -080069 }
70
71 private static String createKey(RootInfo root) {
72 return ROOT_VIEW_MODE_PREFIX + root.authority + root.rootId;
73 }
Felipe Lemeadccb992016-03-09 17:40:49 -080074
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 Sharkey669f8e72014-08-08 15:10:03 -0700118}