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.nfc;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.provider.DeviceConfig;
22 import androidx.annotation.VisibleForTesting;
23 
24 /**
25  * This class allows getting all configurable flags from DeviceConfig.
26  */
27 public class DeviceConfigFacade {
28     // TODO: Temporary hack to copy string from DeviceConfig.NAMESPACE_NFC. Use API constant
29     // once build problems are resolved.
30     private static final String DEVICE_CONFIG_NAMESPACE_NFC = "nfc";
31 
32     private final Context mContext;
33 
34     // Cached values of fields updated via updateDeviceConfigFlags()
35     private boolean mAntennaBlockedAlertEnabled;
36 
37     private static DeviceConfigFacade sInstance;
getInstance(Context context, Handler handler)38     public static DeviceConfigFacade getInstance(Context context, Handler handler) {
39         if (sInstance == null) {
40             sInstance = new DeviceConfigFacade(context, handler);
41         }
42         return sInstance;
43     }
44 
45     @VisibleForTesting
DeviceConfigFacade(Context context, Handler handler)46     public DeviceConfigFacade(Context context, Handler handler) {
47         mContext = context;
48         updateDeviceConfigFlags();
49         DeviceConfig.addOnPropertiesChangedListener(
50                 DEVICE_CONFIG_NAMESPACE_NFC,
51                 command -> handler.post(command),
52                 properties -> {
53                     updateDeviceConfigFlags();
54                 });
55 
56         sInstance = this;
57     }
58 
updateDeviceConfigFlags()59     private void updateDeviceConfigFlags() {
60         mAntennaBlockedAlertEnabled = DeviceConfig.getBoolean(DEVICE_CONFIG_NAMESPACE_NFC,
61                 "enable_antenna_blocked_alert",
62                 mContext.getResources().getBoolean(R.bool.enable_antenna_blocked_alert));
63     }
64 
65     /**
66      * Get whether antenna blocked alert is enabled or not.
67      */
isAntennaBlockedAlertEnabled()68     public boolean isAntennaBlockedAlertEnabled() {
69         return mAntennaBlockedAlertEnabled;
70     }
71 }
72