Home | History | Annotate | Download | only in prefs
      1 /*
      2  * Copyright (C) 2016 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 package com.android.documentsui.prefs;
     17 
     18 import android.content.Context;
     19 import android.content.SharedPreferences;
     20 import android.preference.PreferenceManager;
     21 import android.text.TextUtils;
     22 
     23 import com.android.documentsui.R;
     24 
     25 /**
     26  * Provides an interface (and runtime implementation) for preferences that are
     27  * scoped (presumably to an activity). This eliminates the need to pass
     28  * scoping values into {@link LocalPreferences}, as well as eliminates
     29  * the static-coupling to {@link LocalPreferences} increasing testability.
     30  */
     31 public interface ScopedPreferences {
     32 
     33     static final String INCLUDE_DEVICE_ROOT = "includeDeviceRoot";
     34     static final String ENABLE_ARCHIVE_CREATION = "enableArchiveCreation-";
     35 
     36     boolean getShowDeviceRoot();
     37     void setShowDeviceRoot(boolean display);
     38 
     39     /**
     40      * @param scope An arbitrary string representitive of the scope
     41      *        for prefs that are set using this object.
     42      */
     43     public static ScopedPreferences create(Context context, String scope) {
     44         return new RuntimeScopedPreferences(context,
     45                 PreferenceManager.getDefaultSharedPreferences(context), scope);
     46     }
     47 
     48     static final class RuntimeScopedPreferences implements ScopedPreferences {
     49 
     50         private final SharedPreferences mSharedPrefs;
     51         private final String mScope;
     52         private final boolean mDefaultShowDeviceRoot;
     53 
     54         private RuntimeScopedPreferences(Context context, SharedPreferences sharedPrefs,
     55                 String scope)  {
     56             assert(!TextUtils.isEmpty(scope));
     57 
     58             mSharedPrefs = sharedPrefs;
     59             mScope = scope;
     60             mDefaultShowDeviceRoot = context.getResources()
     61                     .getBoolean(R.bool.config_default_show_device_root);
     62         }
     63 
     64         @Override
     65         public boolean getShowDeviceRoot() {
     66             return mSharedPrefs.getBoolean(INCLUDE_DEVICE_ROOT, mDefaultShowDeviceRoot);
     67         }
     68 
     69         @Override
     70         public void setShowDeviceRoot(boolean display) {
     71             mSharedPrefs.edit().putBoolean(INCLUDE_DEVICE_ROOT, display).apply();
     72         }
     73     }
     74 
     75     static boolean shouldBackup(String s) {
     76         return INCLUDE_DEVICE_ROOT.equals(s);
     77     }
     78 }
     79