1 /* 2 * Copyright (C) 2023 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 17 package com.android.ondevicepersonalization.services; 18 19 import android.os.Binder; 20 21 import com.android.internal.annotations.VisibleForTesting; 22 23 import java.util.HashMap; 24 import java.util.Map; 25 26 /** 27 * Container of process-stable flags. 28 */ 29 public class StableFlags { 30 private static final Object sLock = new Object(); 31 private static volatile StableFlags sStableFlags = null; 32 33 private final Map<String, Object> mStableFlagsMap = new HashMap<>(); 34 35 /** Returns the value of the named stable flag. */ get(String flagName)36 public static Object get(String flagName) { 37 return getInstance().getStableFlag(flagName); 38 39 } 40 41 /** Returns the singleton instance of StableFlags. */ 42 @VisibleForTesting getInstance()43 public static StableFlags getInstance() { 44 if (sStableFlags == null) { 45 synchronized (sLock) { 46 if (sStableFlags == null) { 47 long origId = Binder.clearCallingIdentity(); 48 sStableFlags = new StableFlags(FlagsFactory.getFlags()); 49 Binder.restoreCallingIdentity(origId); 50 } 51 } 52 } 53 return sStableFlags; 54 } 55 56 @VisibleForTesting StableFlags(Flags flags)57 StableFlags(Flags flags) { 58 mStableFlagsMap.put( 59 FlagsConstants.KEY_APP_REQUEST_FLOW_DEADLINE_SECONDS, 60 flags.getAppRequestFlowDeadlineSeconds()); 61 mStableFlagsMap.put( 62 FlagsConstants.KEY_RENDER_FLOW_DEADLINE_SECONDS, 63 flags.getRenderFlowDeadlineSeconds()); 64 mStableFlagsMap.put( 65 FlagsConstants.KEY_WEB_TRIGGER_FLOW_DEADLINE_SECONDS, 66 flags.getWebTriggerFlowDeadlineSeconds()); 67 mStableFlagsMap.put( 68 FlagsConstants.KEY_WEB_VIEW_FLOW_DEADLINE_SECONDS, 69 flags.getWebViewFlowDeadlineSeconds()); 70 mStableFlagsMap.put( 71 FlagsConstants.KEY_EXAMPLE_STORE_FLOW_DEADLINE_SECONDS, 72 flags.getExampleStoreFlowDeadlineSeconds()); 73 mStableFlagsMap.put( 74 FlagsConstants.KEY_DOWNLOAD_FLOW_DEADLINE_SECONDS, 75 flags.getDownloadFlowDeadlineSeconds()); 76 mStableFlagsMap.put( 77 FlagsConstants.KEY_SHARED_ISOLATED_PROCESS_FEATURE_ENABLED, 78 flags.isSharedIsolatedProcessFeatureEnabled()); 79 mStableFlagsMap.put( 80 FlagsConstants.KEY_TRUSTED_PARTNER_APPS_LIST, flags.getTrustedPartnerAppsList()); 81 mStableFlagsMap.put( 82 FlagsConstants.KEY_IS_ART_IMAGE_LOADING_OPTIMIZATION_ENABLED, 83 flags.isArtImageLoadingOptimizationEnabled()); 84 mStableFlagsMap.put( 85 FlagsConstants.KEY_ENABLE_PERSONALIZATION_STATUS_OVERRIDE, 86 flags.isPersonalizationStatusOverrideEnabled()); 87 mStableFlagsMap.put( 88 FlagsConstants.KEY_PERSONALIZATION_STATUS_OVERRIDE_VALUE, 89 flags.getPersonalizationStatusOverrideValue()); 90 mStableFlagsMap.put( 91 FlagsConstants.KEY_USER_CONTROL_CACHE_IN_MILLIS, 92 flags.getUserControlCacheInMillis()); 93 mStableFlagsMap.put( 94 FlagsConstants.KEY_PLUGIN_PROCESS_RUNNER_ENABLED, 95 flags.isPluginProcessRunnerEnabled()); 96 } 97 getStableFlag(String flagName)98 private Object getStableFlag(String flagName) { 99 if (!mStableFlagsMap.containsKey(flagName)) { 100 throw new IllegalArgumentException("Flag " + flagName + " is not stable."); 101 } 102 return mStableFlagsMap.get(flagName); 103 } 104 } 105