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 isArchiveCreationEnabled()35 boolean isArchiveCreationEnabled(); isCommandInterceptorEnabled()36 boolean isCommandInterceptorEnabled(); isContentPagingEnabled()37 boolean isContentPagingEnabled(); isContentRefreshEnabled()38 boolean isContentRefreshEnabled(); isDebugSupportEnabled()39 boolean isDebugSupportEnabled(); isFoldersInSearchResultsEnabled()40 boolean isFoldersInSearchResultsEnabled(); isGestureScaleEnabled()41 boolean isGestureScaleEnabled(); isInspectorEnabled()42 boolean isInspectorEnabled(); isJobProgressDialogEnabled()43 boolean isJobProgressDialogEnabled(); isLaunchToDocumentEnabled()44 boolean isLaunchToDocumentEnabled(); isNotificationChannelEnabled()45 boolean isNotificationChannelEnabled(); isOverwriteConfirmationEnabled()46 boolean isOverwriteConfirmationEnabled(); isRemoteActionsEnabled()47 boolean isRemoteActionsEnabled(); isSystemKeyboardNavigationEnabled()48 boolean isSystemKeyboardNavigationEnabled(); isVirtualFilesSharingEnabled()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 */ forceFeature(@oolRes int feature, boolean enabled)61 void forceFeature(@BoolRes int feature, boolean enabled); 62 create(Context context)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 RuntimeFeatures(Resources resources, UserManager userMgr)74 public RuntimeFeatures(Resources resources, UserManager userMgr) { 75 mRes = resources; 76 mUserMgr = userMgr; 77 } 78 79 @Override forceFeature(@oolRes int feature, boolean enabled)80 public void forceFeature(@BoolRes int feature, boolean enabled) { 81 mDebugEnabled.put(feature, enabled); 82 } 83 isEnabled(@oolRes int feature)84 private boolean isEnabled(@BoolRes int feature) { 85 return mDebugEnabled.get(feature, mRes.getBoolean(feature)); 86 } 87 88 @Override isArchiveCreationEnabled()89 public boolean isArchiveCreationEnabled() { 90 return isEnabled(R.bool.feature_archive_creation); 91 } 92 93 @Override isCommandInterceptorEnabled()94 public boolean isCommandInterceptorEnabled() { 95 assert(isDebugPolicyEnabled()); 96 return isEnabled(R.bool.feature_command_interceptor); 97 } 98 99 @Override isContentPagingEnabled()100 public boolean isContentPagingEnabled() { 101 return isEnabled(R.bool.feature_content_paging); 102 } 103 104 @Override isContentRefreshEnabled()105 public boolean isContentRefreshEnabled() { 106 return isEnabled(R.bool.feature_content_refresh); 107 } 108 isDebugPolicyEnabled()109 private boolean isDebugPolicyEnabled() { 110 return !mUserMgr.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES); 111 } 112 113 @Override isDebugSupportEnabled()114 public boolean isDebugSupportEnabled() { 115 return isDebugPolicyEnabled() && isFunPolicyEnabled(); 116 } 117 118 @Override isFoldersInSearchResultsEnabled()119 public boolean isFoldersInSearchResultsEnabled() { 120 return isEnabled(R.bool.feature_folders_in_search_results); 121 } 122 isFunPolicyEnabled()123 private boolean isFunPolicyEnabled() { 124 return !mUserMgr.hasUserRestriction(UserManager.DISALLOW_FUN); 125 } 126 127 @Override isGestureScaleEnabled()128 public boolean isGestureScaleEnabled() { 129 return isEnabled(R.bool.feature_gesture_scale); 130 } 131 isInspectorEnabled()132 public boolean isInspectorEnabled() { 133 return isEnabled(R.bool.feature_inspector); 134 } 135 136 @Override isJobProgressDialogEnabled()137 public boolean isJobProgressDialogEnabled() { 138 return isEnabled(R.bool.feature_job_progress_dialog); 139 } 140 141 @Override isLaunchToDocumentEnabled()142 public boolean isLaunchToDocumentEnabled() { 143 return isEnabled(R.bool.feature_launch_to_document); 144 } 145 146 @Override isNotificationChannelEnabled()147 public boolean isNotificationChannelEnabled() { 148 return isEnabled(R.bool.feature_notification_channel); 149 } 150 151 @Override isOverwriteConfirmationEnabled()152 public boolean isOverwriteConfirmationEnabled() { 153 return isEnabled(R.bool.feature_overwrite_confirmation); 154 } 155 156 @Override isRemoteActionsEnabled()157 public boolean isRemoteActionsEnabled() { 158 return isEnabled(R.bool.feature_remote_actions); 159 } 160 161 @Override isSystemKeyboardNavigationEnabled()162 public boolean isSystemKeyboardNavigationEnabled() { 163 return isEnabled(R.bool.feature_system_keyboard_navigation); 164 } 165 166 @Override isVirtualFilesSharingEnabled()167 public boolean isVirtualFilesSharingEnabled() { 168 return isEnabled(R.bool.feature_virtual_files_sharing); 169 } 170 } 171 } 172