blob: 2315664f0d8a79d067d4dc3c5422044539caab9c [file] [log] [blame]
Jeff Sharkeye8d13ea2014-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 McKay7776aa52016-01-25 19:00:22 -080019import static com.android.documentsui.State.MODE_UNKNOWN;
Steve McKay7776aa52016-01-25 19:00:22 -080020
Felipe Leme0bdb7c32016-03-09 17:40:49 -080021import java.lang.annotation.Retention;
22import java.lang.annotation.RetentionPolicy;
23
24import android.annotation.IntDef;
25import android.annotation.Nullable;
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070026import android.content.Context;
Felipe Leme0bdb7c32016-03-09 17:40:49 -080027import android.content.SharedPreferences;
28import android.os.UserHandle;
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070029import android.preference.PreferenceManager;
30
Steve McKay7776aa52016-01-25 19:00:22 -080031import com.android.documentsui.State.ViewMode;
32import com.android.documentsui.model.RootInfo;
33
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070034public class LocalPreferences {
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070035 private static final String KEY_FILE_SIZE = "fileSize";
Steve McKay7776aa52016-01-25 19:00:22 -080036 private static final String ROOT_VIEW_MODE_PREFIX = "rootViewMode-";
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070037
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070038 public static boolean getDisplayFileSize(Context context) {
Felipe Leme0bdb7c32016-03-09 17:40:49 -080039 return getPrefs(context).getBoolean(KEY_FILE_SIZE, false);
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070040 }
41
Felipe Leme0bdb7c32016-03-09 17:40:49 -080042 public static @ViewMode int getViewMode(Context context, RootInfo root,
43 @ViewMode int fallback) {
44 return getPrefs(context).getInt(createKey(root), fallback);
Steve McKay7776aa52016-01-25 19:00:22 -080045 }
46
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070047 public static void setDisplayFileSize(Context context, boolean display) {
Felipe Leme0bdb7c32016-03-09 17:40:49 -080048 getPrefs(context).edit().putBoolean(KEY_FILE_SIZE, display).apply();
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -070049 }
Steve McKay7776aa52016-01-25 19:00:22 -080050
51 public static void setViewMode(Context context, RootInfo root, @ViewMode int viewMode) {
Steve McKay0af8afd2016-02-25 13:34:03 -080052 assert(viewMode != MODE_UNKNOWN);
53
Felipe Leme0bdb7c32016-03-09 17:40:49 -080054 getPrefs(context).edit().putInt(createKey(root), viewMode).apply();
55 }
56
57 private static SharedPreferences getPrefs(Context context) {
58 return PreferenceManager.getDefaultSharedPreferences(context);
Steve McKay7776aa52016-01-25 19:00:22 -080059 }
60
61 private static String createKey(RootInfo root) {
62 return ROOT_VIEW_MODE_PREFIX + root.authority + root.rootId;
63 }
Felipe Leme0bdb7c32016-03-09 17:40:49 -080064
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 Sharkeye8d13ea2014-08-08 15:10:03 -0700108}