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 package android.nfc;
17 
18 import android.annotation.NonNull;
19 import android.annotation.SystemApi;
20 import android.app.SystemServiceRegistry;
21 import android.content.Context;
22 
23 /**
24  * Class for performing registration for Nfc service.
25  *
26  * @hide
27  */
28 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
29 public class NfcFrameworkInitializer {
NfcFrameworkInitializer()30     private NfcFrameworkInitializer() {}
31 
32     private static volatile NfcServiceManager sNfcServiceManager;
33 
34     /**
35      * Sets an instance of {@link NfcServiceManager} that allows
36      * the nfc mainline module to register/obtain nfc binder services. This is called
37      * by the platform during the system initialization.
38      *
39      * @param nfcServiceManager instance of {@link NfcServiceManager} that allows
40      * the nfc mainline module to register/obtain nfcd binder services.
41      */
setNfcServiceManager( @onNull NfcServiceManager nfcServiceManager)42     public static void setNfcServiceManager(
43             @NonNull NfcServiceManager nfcServiceManager) {
44         if (sNfcServiceManager != null) {
45             throw new IllegalStateException("setNfcServiceManager called twice!");
46         }
47 
48         if (nfcServiceManager == null) {
49             throw new IllegalArgumentException("nfcServiceManager must not be null");
50         }
51 
52         sNfcServiceManager = nfcServiceManager;
53     }
54 
55     /** @hide */
getNfcServiceManager()56     public static NfcServiceManager getNfcServiceManager() {
57         return sNfcServiceManager;
58     }
59 
60     /**
61      * Called by {@link SystemServiceRegistry}'s static initializer and registers NFC service
62      * to {@link Context}, so that {@link Context#getSystemService} can return them.
63      *
64      * @throws IllegalStateException if this is called from anywhere besides
65      * {@link SystemServiceRegistry}
66      */
registerServiceWrappers()67     public static void registerServiceWrappers() {
68         SystemServiceRegistry.registerContextAwareService(Context.NFC_SERVICE,
69                 NfcManager.class, context -> new NfcManager(context));
70     }
71 }
72