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