Home | History | Annotate | Download | only in prefs
      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.annotation.IntDef;
     22 import android.content.Context;
     23 import android.content.SharedPreferences;
     24 import android.preference.PreferenceManager;
     25 
     26 import com.android.documentsui.base.RootInfo;
     27 import com.android.documentsui.base.State.ViewMode;
     28 
     29 import java.lang.annotation.Retention;
     30 import java.lang.annotation.RetentionPolicy;
     31 
     32 /**
     33  * Methods for accessing the local preferences.
     34  */
     35 public class LocalPreferences {
     36     private static final String ROOT_VIEW_MODE_PREFIX = "rootViewMode-";
     37 
     38     public static @ViewMode int getViewMode(Context context, RootInfo root,
     39             @ViewMode int fallback) {
     40         return getPrefs(context).getInt(createKey(root), fallback);
     41     }
     42 
     43     public static void setViewMode(Context context, RootInfo root, @ViewMode int viewMode) {
     44         assert(viewMode != MODE_UNKNOWN);
     45         getPrefs(context).edit().putInt(createKey(root), viewMode).apply();
     46     }
     47 
     48     private static SharedPreferences getPrefs(Context context) {
     49         return PreferenceManager.getDefaultSharedPreferences(context);
     50     }
     51 
     52     private static String createKey(RootInfo root) {
     53         return ROOT_VIEW_MODE_PREFIX + root.authority + root.rootId;
     54     }
     55 
     56     public static final int PERMISSION_ASK = 0;
     57     public static final int PERMISSION_ASK_AGAIN = 1;
     58     public static final int PERMISSION_NEVER_ASK = -1;
     59 
     60     @IntDef(flag = true, value = {
     61             PERMISSION_ASK,
     62             PERMISSION_ASK_AGAIN,
     63             PERMISSION_NEVER_ASK,
     64     })
     65     @Retention(RetentionPolicy.SOURCE)
     66     public @interface PermissionStatus {}
     67 
     68     public static boolean shouldBackup(String s) {
     69         return (s != null) ? s.startsWith(ROOT_VIEW_MODE_PREFIX) : false;
     70     }
     71 }
     72