• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.prefs;
18 
19 import static com.android.documentsui.base.State.MODE_UNKNOWN;
20 
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.preference.PreferenceManager;
24 
25 import androidx.annotation.IntDef;
26 import androidx.annotation.VisibleForTesting;
27 
28 import com.android.documentsui.base.RootInfo;
29 import com.android.documentsui.base.State.ViewMode;
30 
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 
34 /**
35  * Methods for accessing the local preferences.
36  */
37 public class LocalPreferences {
38     private static final String ROOT_VIEW_MODE_PREFIX = "rootViewMode-";
39     private static final String SHOW_HIDDEN_FILES = "showHiddenFiles";
40 
getViewMode(Context context, RootInfo root, @ViewMode int fallback)41     public static @ViewMode int getViewMode(Context context, RootInfo root,
42             @ViewMode int fallback) {
43         return getPrefs(context).getInt(createKey(ROOT_VIEW_MODE_PREFIX, root), fallback);
44     }
45 
46     /** Returns if hidden files should be shown. */
getShowHiddenFiles(Context context, boolean fallback)47     public static boolean getShowHiddenFiles(Context context, boolean fallback) {
48         return getPrefs(context).getBoolean(SHOW_HIDDEN_FILES, fallback);
49     }
50 
setViewMode(Context context, RootInfo root, @ViewMode int viewMode)51     public static void setViewMode(Context context, RootInfo root, @ViewMode int viewMode) {
52         assert(viewMode != MODE_UNKNOWN);
53         getPrefs(context).edit().putInt(createKey(ROOT_VIEW_MODE_PREFIX, root), viewMode).apply();
54     }
55 
56     /** Sets if hidden files should be shown. */
57     @VisibleForTesting
setShowHiddenFiles(Context context, boolean showHiddenFiles)58     public static void setShowHiddenFiles(Context context, boolean showHiddenFiles) {
59         getPrefs(context).edit()
60                 .putBoolean(SHOW_HIDDEN_FILES, showHiddenFiles)
61                 .apply();
62     }
63 
getPrefs(Context context)64     private static SharedPreferences getPrefs(Context context) {
65         return PreferenceManager.getDefaultSharedPreferences(context);
66     }
67 
createKey(String prefix, RootInfo root)68     private static String createKey(String prefix, RootInfo root) {
69         return prefix + root.authority + root.rootId;
70     }
71 
72     public static final int PERMISSION_ASK = 0;
73     public static final int PERMISSION_ASK_AGAIN = 1;
74     public static final int PERMISSION_NEVER_ASK = -1;
75 
76     @IntDef(flag = true, value = {
77             PERMISSION_ASK,
78             PERMISSION_ASK_AGAIN,
79             PERMISSION_NEVER_ASK,
80     })
81     @Retention(RetentionPolicy.SOURCE)
82     public @interface PermissionStatus {}
83 
shouldBackup(String s)84     public static boolean shouldBackup(String s) {
85         return (s != null) ? s.startsWith(ROOT_VIEW_MODE_PREFIX) : false;
86     }
87 }
88