• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #ifndef CHRE_CORE_SETTINGS_H_
18 #define CHRE_CORE_SETTINGS_H_
19 
20 #include <cinttypes>
21 
22 #include "chre/util/system/debug_dump.h"
23 
24 namespace chre {
25 
26 enum class Setting : uint8_t {
27   LOCATION = 0,
28   WIFI_AVAILABLE,
29   AIRPLANE_MODE,
30   MICROPHONE,
31   BLE_AVAILABLE,
32   SETTING_MAX,
33 };
34 
35 /**
36  * Stores latest setting state and is responsible for sending setting updates
37  * to nanoapps.
38  */
39 class SettingManager {
40  public:
41   SettingManager();
42 
43   /**
44    * Updates the state of a given setting.
45    *
46    * @param setting The setting to update.
47    * @param state The state of the setting.
48    */
49   void postSettingChange(Setting setting, bool enabled);
50 
51   /**
52    * Gets the current state of a given setting. Must be called from the context
53    * of the main CHRE thread.
54    *
55    * @param setting The setting to check the current state of.
56    *
57    * @return True if the setting is enabled.
58    */
59   bool getSettingEnabled(Setting setting);
60 
61   /**
62    * Gets the current state of a given setting, but returns the state as an
63    * int8_t. The state is guaranteed to be a member of enum
64    * chreUserSettingState.
65    *
66    * @param setting The setting to check the current state of (see
67    * CHRE_USER_SETTINGS).
68    *
69    * @return The current state of the setting (see enum chreUserSettingState)
70    */
71   int8_t getSettingStateAsInt8(uint8_t setting);
72 
73   /**
74    * Logs the settings related stats in the debug dump. Must be called from the
75    * context of the main CHRE thread.
76    *
77    * @param debugDump The object that is printed into for debug dump logs.
78    */
79   void logStateToBuffer(DebugDumpWrapper &debugDump);
80 
81  private:
82   static constexpr size_t kNumSettings =
83       static_cast<size_t>(Setting::SETTING_MAX);
84 
85   //! The current state for each setting.
86   bool mSettingStateList[kNumSettings];
87 
88   void setSettingState(Setting setting, bool enabled);
89 
90   const char *getSettingEnabledString(Setting setting);
91 
92   static void settingChangedCallback(uint16_t type, void *data,
93                                      void *extraData);
94 };
95 
96 }  // namespace chre
97 
98 #endif  // CHRE_CORE_SETTINGS_H_
99