• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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.server.bluetooth;
18 
19 import android.provider.DeviceConfig;
20 import android.provider.DeviceConfig.Properties;
21 import android.util.Log;
22 
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 
26 /**
27  * The BluetoothDeviceConfigChangeTracker receives changes to the DeviceConfig for
28  * NAMESPACE_BLUETOOTH, and determines whether we should queue a restart, if any Bluetooth-related
29  * INIT_ flags have been changed.
30  *
31  * <p>The initialProperties should be fetched from the BLUETOOTH namespace in DeviceConfig
32  */
33 public final class BluetoothDeviceConfigChangeTracker {
34     private static final String TAG = "BluetoothDeviceConfigChangeTracker";
35 
36     private final HashMap<String, String> mCurrFlags;
37 
BluetoothDeviceConfigChangeTracker(Properties initialProperties)38     public BluetoothDeviceConfigChangeTracker(Properties initialProperties) {
39         mCurrFlags = getFlags(initialProperties);
40     }
41 
42     /**
43      * Updates the instance state tracking the latest init flag values, and determines whether an
44      * init flag has changed (requiring a restart at some point)
45      */
shouldRestartWhenPropertiesUpdated(Properties newProperties)46     public boolean shouldRestartWhenPropertiesUpdated(Properties newProperties) {
47         if (!newProperties.getNamespace().equals(DeviceConfig.NAMESPACE_BLUETOOTH)) {
48             return false;
49         }
50         ArrayList<String> flags = new ArrayList<>();
51         for (String name : newProperties.getKeyset()) {
52             flags.add(name + "='" + newProperties.getString(name, "") + "'");
53         }
54         Log.d(TAG, "shouldRestartWhenPropertiesUpdated: " + String.join(",", flags));
55         boolean shouldRestart = false;
56         for (String name : newProperties.getKeyset()) {
57             if (!isInitFlag(name)) {
58                 continue;
59             }
60             var oldValue = mCurrFlags.get(name);
61             var newValue = newProperties.getString(name, "");
62             if (newValue.equals(oldValue)) {
63                 continue;
64             }
65             Log.d(TAG, "Property " + name + " changed from " + oldValue + " -> " + newValue);
66             mCurrFlags.put(name, newValue);
67             shouldRestart = true;
68         }
69         return shouldRestart;
70     }
71 
getFlags(Properties initialProperties)72     private HashMap<String, String> getFlags(Properties initialProperties) {
73         var out = new HashMap();
74         for (var name : initialProperties.getKeyset()) {
75             if (isInitFlag(name)) {
76                 out.put(name, initialProperties.getString(name, ""));
77             }
78         }
79         return out;
80     }
81 
isInitFlag(String flagName)82     private Boolean isInitFlag(String flagName) {
83         return flagName.startsWith("INIT_");
84     }
85 }
86