• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.os.IBinder;
23 import android.os.ServiceManager;
24 
25 /**
26  * Service Manager for the {@code android.provider.DeviceConfig} service.
27  *
28  * <p>Used to be able to get an instance of the service in places that don't have access to a
29  * {@code Context}
30  *
31  * @hide
32  */
33 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
34 public class DeviceConfigServiceManager {
35 
36     /**
37      * @hide
38      */
DeviceConfigServiceManager()39     public DeviceConfigServiceManager() {
40     }
41 
42     /**
43      * @hide
44      */
45     @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
46     public static final class ServiceRegisterer {
47         private final String mServiceName;
48 
49         /**
50          * @hide
51          */
ServiceRegisterer(String serviceName)52         public ServiceRegisterer(String serviceName) {
53             mServiceName = serviceName;
54         }
55 
56         /**
57          * Register a system server binding object for a service.
58          * @hide
59          */
60         @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
register(@onNull IBinder service)61         public void register(@NonNull IBinder service) {
62             ServiceManager.addService(mServiceName, service);
63         }
64 
65         /**
66          * Get the system server binding object for a service.
67          *
68          * <p>This blocks until the service instance is ready,
69          * or a timeout happens, in which case it returns null.
70          *
71          * @hide
72          */
73         @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
74         @Nullable
get()75         public IBinder get() {
76             return ServiceManager.getService(mServiceName);
77         }
78 
79         /**
80          * Get the system server binding object for a service.
81          *
82          * <p>This blocks until the service instance is ready,
83          * or a timeout happens, in which case it throws {@link ServiceNotFoundException}.
84          *
85          * @hide
86          */
87         @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
88         @NonNull
getOrThrow()89         public IBinder getOrThrow() throws ServiceNotFoundException {
90             try {
91                 return ServiceManager.getServiceOrThrow(mServiceName);
92             } catch (ServiceManager.ServiceNotFoundException e) {
93                 throw new ServiceNotFoundException(mServiceName);
94             }
95         }
96 
97         /**
98          * Get the system server binding object for a service. If the specified service is
99          * not available, it returns null.
100          *
101          * @hide
102          */
103         @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
104         @Nullable
tryGet()105         public IBinder tryGet() {
106             return ServiceManager.checkService(mServiceName);
107         }
108 
109     }
110 
111     /**
112      * See {@link ServiceRegisterer#getOrThrow}.
113      *
114      * @hide
115      */
116     @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
117     public static class ServiceNotFoundException extends ServiceManager.ServiceNotFoundException {
118         /**
119          * Constructor.
120          *
121          * @param name the name of the binder service that cannot be found.
122          *
123          * @hide
124          */
ServiceNotFoundException(@onNull String name)125         public ServiceNotFoundException(@NonNull String name) {
126             super(name);
127         }
128     }
129 
130     /**
131      * Returns {@link ServiceRegisterer} for the device config service that
132      * is updatable via mainline.
133      *
134      * @hide
135      */
136     @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
137     @NonNull
getDeviceConfigUpdatableServiceRegisterer()138     public ServiceRegisterer getDeviceConfigUpdatableServiceRegisterer() {
139         return new ServiceRegisterer("device_config_updatable");
140     }
141 }
142