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 android.provider;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 
23 import java.util.Objects;
24 
25 /**
26  * Class that will hold an instance of {@link DeviceConfigServiceManager}
27  * which is used by {@link DeviceConfig} to retrieve an instance of the service.
28  *
29  * @hide
30  */
31 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
32 public class DeviceConfigInitializer {
33     private static DeviceConfigServiceManager sDeviceConfigServiceManager;
34 
35     private static final Object sLock = new Object();
36 
DeviceConfigInitializer()37     private DeviceConfigInitializer() {
38         // fully static class
39     }
40 
41     /**
42      * Setter for {@link DeviceConfigServiceManager}. Should be called only once.
43      *
44      */
setDeviceConfigServiceManager( @onNull DeviceConfigServiceManager serviceManager)45     public static void setDeviceConfigServiceManager(
46             @NonNull DeviceConfigServiceManager serviceManager) {
47         synchronized (sLock) {
48             if (sDeviceConfigServiceManager != null) {
49                 throw new IllegalStateException("setDeviceConfigServiceManager called twice!");
50             }
51             Objects.requireNonNull(serviceManager, "serviceManager must not be null");
52 
53             sDeviceConfigServiceManager = serviceManager;
54         }
55     }
56 
57     /**
58      * Getter for {@link DeviceConfigServiceManager}.
59      *
60      */
61     @Nullable
getDeviceConfigServiceManager()62     public static DeviceConfigServiceManager getDeviceConfigServiceManager() {
63         synchronized (sLock) {
64             return sDeviceConfigServiceManager;
65         }
66     }
67 }
68