Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2017 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.base;
     17 
     18 import android.annotation.BoolRes;
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.os.UserManager;
     22 import android.util.SparseBooleanArray;
     23 
     24 import com.android.documentsui.R;
     25 
     26 /**
     27  * Provides access to feature flags configured in config.xml.
     28  */
     29 public interface Features {
     30 
     31     // technically we want to check >= O, but we'd need to patch back the O version code :|
     32     public static final boolean OMC_RUNTIME =
     33             android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.N_MR1;
     34 
     35     boolean isArchiveCreationEnabled();
     36     boolean isCommandInterceptorEnabled();
     37     boolean isContentPagingEnabled();
     38     boolean isContentRefreshEnabled();
     39     boolean isDebugSupportEnabled();
     40     boolean isFoldersInSearchResultsEnabled();
     41     boolean isGestureScaleEnabled();
     42     boolean isLaunchToDocumentEnabled();
     43     boolean isNotificationChannelEnabled();
     44     boolean isRemoteActionsEnabled();
     45     boolean isSystemKeyboardNavigationEnabled();
     46     boolean isVirtualFilesSharingEnabled();
     47 
     48     /**
     49      * Call this to force-enable any particular feature known by this instance.
     50      * Note that all feature may not support being enabled at runtime as
     51      * they may depend on runtime initialization guarded by feature check.
     52      *
     53      * <p>Feature changes will be persisted across activities, but not app restarts.
     54      *
     55      * @param feature int reference to a boolean feature resource.
     56      */
     57     void forceFeature(@BoolRes int feature, boolean enabled);
     58 
     59     public static Features create(Context context) {
     60         return new RuntimeFeatures(context.getResources(), UserManager.get(context));
     61     }
     62 
     63     final class RuntimeFeatures implements Features {
     64 
     65         private final SparseBooleanArray mDebugEnabled = new SparseBooleanArray();
     66 
     67         private final Resources mRes;
     68         private final UserManager mUserMgr;
     69 
     70         public RuntimeFeatures(Resources resources, UserManager userMgr) {
     71             mRes = resources;
     72             mUserMgr = userMgr;
     73         }
     74 
     75         @Override
     76         public void forceFeature(@BoolRes int feature, boolean enabled) {
     77             mDebugEnabled.put(feature, enabled);
     78         }
     79 
     80         private boolean isEnabled(@BoolRes int feature) {
     81             return mDebugEnabled.get(feature, mRes.getBoolean(feature));
     82         }
     83 
     84         @Override
     85         public boolean isArchiveCreationEnabled() {
     86             return isEnabled(R.bool.feature_archive_creation);
     87         }
     88 
     89         @Override
     90         public boolean isCommandInterceptorEnabled() {
     91             return !mUserMgr.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
     92                     && isEnabled(R.bool.feature_command_interceptor);
     93         }
     94 
     95         @Override
     96         public boolean isContentPagingEnabled() {
     97             return isEnabled(R.bool.feature_content_paging);
     98         }
     99 
    100         @Override
    101         public boolean isContentRefreshEnabled() {
    102             return isEnabled(R.bool.feature_content_refresh);
    103         }
    104 
    105         @Override
    106         public boolean isDebugSupportEnabled() {
    107             return !mUserMgr.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
    108                     && !mUserMgr.hasUserRestriction(UserManager.DISALLOW_FUN);
    109         }
    110 
    111         @Override
    112         public boolean isFoldersInSearchResultsEnabled() {
    113             return isEnabled(R.bool.feature_folders_in_search_results);
    114         }
    115 
    116         @Override
    117         public boolean isGestureScaleEnabled() {
    118             return isEnabled(R.bool.feature_gesture_scale);
    119         }
    120 
    121         @Override
    122         public boolean isLaunchToDocumentEnabled() {
    123             return isEnabled(R.bool.feature_launch_to_document);
    124         }
    125 
    126         @Override
    127         public boolean isNotificationChannelEnabled() {
    128             return isEnabled(R.bool.feature_notification_channel);
    129         }
    130 
    131         @Override
    132         public boolean isRemoteActionsEnabled() {
    133             return isEnabled(R.bool.feature_remote_actions);
    134         }
    135 
    136         @Override
    137         public boolean isSystemKeyboardNavigationEnabled() {
    138             return isEnabled(R.bool.feature_system_keyboard_navigation);
    139         }
    140 
    141         @Override
    142         public boolean isVirtualFilesSharingEnabled() {
    143             return isEnabled(R.bool.feature_virtual_files_sharing);
    144         }
    145     }
    146 }
    147