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 isInspectorEnabled();
     43     boolean isJobProgressDialogEnabled();
     44     boolean isLaunchToDocumentEnabled();
     45     boolean isNotificationChannelEnabled();
     46     boolean isOverwriteConfirmationEnabled();
     47     boolean isRemoteActionsEnabled();
     48     boolean isSystemKeyboardNavigationEnabled();
     49     boolean isVirtualFilesSharingEnabled();
     50 
     51 
     52     /**
     53      * Call this to force-enable any particular feature known by this instance.
     54      * Note that all feature may not support being enabled at runtime as
     55      * they may depend on runtime initialization guarded by feature check.
     56      *
     57      * <p>Feature changes will be persisted across activities, but not app restarts.
     58      *
     59      * @param feature int reference to a boolean feature resource.
     60      */
     61     void forceFeature(@BoolRes int feature, boolean enabled);
     62 
     63     public static Features create(Context context) {
     64         return new RuntimeFeatures(context.getResources(), UserManager.get(context));
     65     }
     66 
     67     final class RuntimeFeatures implements Features {
     68 
     69         private final SparseBooleanArray mDebugEnabled = new SparseBooleanArray();
     70 
     71         private final Resources mRes;
     72         private final UserManager mUserMgr;
     73 
     74         public RuntimeFeatures(Resources resources, UserManager userMgr) {
     75             mRes = resources;
     76             mUserMgr = userMgr;
     77         }
     78 
     79         @Override
     80         public void forceFeature(@BoolRes int feature, boolean enabled) {
     81             mDebugEnabled.put(feature, enabled);
     82         }
     83 
     84         private boolean isEnabled(@BoolRes int feature) {
     85             return mDebugEnabled.get(feature, mRes.getBoolean(feature));
     86         }
     87 
     88         @Override
     89         public boolean isArchiveCreationEnabled() {
     90             return isEnabled(R.bool.feature_archive_creation);
     91         }
     92 
     93         @Override
     94         public boolean isCommandInterceptorEnabled() {
     95             assert(isDebugPolicyEnabled());
     96             return isEnabled(R.bool.feature_command_interceptor);
     97         }
     98 
     99         @Override
    100         public boolean isContentPagingEnabled() {
    101             return isEnabled(R.bool.feature_content_paging);
    102         }
    103 
    104         @Override
    105         public boolean isContentRefreshEnabled() {
    106             return isEnabled(R.bool.feature_content_refresh);
    107         }
    108 
    109         private boolean isFunPolicyEnabled() {
    110             return !mUserMgr.hasUserRestriction(UserManager.DISALLOW_FUN);
    111         }
    112 
    113         private boolean isDebugPolicyEnabled() {
    114             return !mUserMgr.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
    115         }
    116 
    117         @Override
    118         public boolean isDebugSupportEnabled() {
    119             return isDebugPolicyEnabled() && isFunPolicyEnabled();
    120         }
    121 
    122         @Override
    123         public boolean isFoldersInSearchResultsEnabled() {
    124             return isEnabled(R.bool.feature_folders_in_search_results);
    125         }
    126 
    127         @Override
    128         public boolean isGestureScaleEnabled() {
    129             return isEnabled(R.bool.feature_gesture_scale);
    130         }
    131 
    132         @Override
    133         public boolean isInspectorEnabled() {
    134             return isEnabled(R.bool.feature_inspector);
    135         }
    136 
    137         @Override
    138         public boolean isJobProgressDialogEnabled() {
    139             return isEnabled(R.bool.feature_job_progress_dialog);
    140         }
    141 
    142         @Override
    143         public boolean isLaunchToDocumentEnabled() {
    144             return isEnabled(R.bool.feature_launch_to_document);
    145         }
    146 
    147         @Override
    148         public boolean isNotificationChannelEnabled() {
    149             return isEnabled(R.bool.feature_notification_channel);
    150         }
    151 
    152         @Override
    153         public boolean isOverwriteConfirmationEnabled() {
    154             return isEnabled(R.bool.feature_overwrite_confirmation);
    155         }
    156 
    157         @Override
    158         public boolean isRemoteActionsEnabled() {
    159             return isEnabled(R.bool.feature_remote_actions);
    160         }
    161 
    162         @Override
    163         public boolean isSystemKeyboardNavigationEnabled() {
    164             return isEnabled(R.bool.feature_system_keyboard_navigation);
    165         }
    166 
    167         @Override
    168         public boolean isVirtualFilesSharingEnabled() {
    169             return isEnabled(R.bool.feature_virtual_files_sharing);
    170         }
    171     }
    172 }
    173