• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VIBRATION_PRIORITY_MANAGER_H
17 #define VIBRATION_PRIORITY_MANAGER_H
18 
19 #include "app_mgr_client.h"
20 #ifdef OHOS_BUILD_ENABLE_DO_NOT_DISTURB
21 #include "cJSON.h"
22 #endif // OHOS_BUILD_ENABLE_DO_NOT_DISTURB
23 #include "datashare_helper.h"
24 
25 #include "miscdevice_observer.h"
26 #include "vibrator_thread.h"
27 
28 namespace OHOS {
29 namespace Sensors {
30 enum VibrateStatus {
31     VIBRATION = 0,
32     IGNORE_BACKGROUND = 1,
33     IGNORE_LOW_POWER = 2,
34     IGNORE_GLOBAL_SETTINGS = 3,
35     IGNORE_RINGTONE = 4,
36     IGNORE_REPEAT = 5,
37     IGNORE_ALARM = 6,
38     IGNORE_UNKNOWN = 7,
39     IGNORE_RINGER_MODE = 8,
40     IGNORE_FEEDBACK = 9,
41 };
42 
43 enum RingerMode {
44     RINGER_MODE_INVALID = -1,
45     RINGER_MODE_SILENT = 0,
46     RINGER_MODE_VIBRATE = 1,
47     RINGER_MODE_NORMAL = 2
48 };
49 
50 enum FeedbackMode {
51     FEEDBACK_MODE_INVALID = -1,
52     FEEDBACK_MODE_OFF = 0,
53     FEEDBACK_MODE_ON = 1
54 };
55 
56 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CROWN
57 enum FeedbackIntensity {
58     FEEDBACK_INTENSITY_INVALID = -1,
59     FEEDBACK_INTENSITY_STRONGE = 0,
60     FEEDBACK_INTENSITY_WEAK = 1,
61     FEEDBACK_INTENSITY_NONE = 2,
62 };
63 #endif
64 
65 #ifdef OHOS_BUILD_ENABLE_DO_NOT_DISTURB
66 enum DoNotDisturbSwitch {
67     DONOTDISTURB_SWITCH_INVALID = -1,
68     DONOTDISTURB_SWITCH_OFF = 0,
69     DONOTDISTURB_SWITCH_ON = 1
70 };
71 
72 struct WhiteListAppInfo {
73     std::string bundle;
74     int64_t uid;
75 };
76 #endif // OHOS_BUILD_ENABLE_DO_NOT_DISTURB
77 
78 class VibrationPriorityManager {
79     DECLARE_DELAYED_SINGLETON(VibrationPriorityManager);
80 public:
81     DISALLOW_COPY_AND_MOVE(VibrationPriorityManager);
82     bool Init();
83     VibrateStatus ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo,
84 		const std::shared_ptr<VibratorThread> &vibratorThread, const VibratorIdentifierIPC& identifier);
85 #ifdef OHOS_BUILD_ENABLE_DO_NOT_DISTURB
86     void InitDoNotDisturbData();
87     void ReregisterCurrentUserObserver();
88 #endif // OHOS_BUILD_ENABLE_DO_NOT_DISTURB
89 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CROWN
90     bool ShouldIgnoreByIntensity(const VibrateInfo &vibrateInfo);
91     void MiscCrownIntensityFeedbackInit(void);
92 #endif
93 
94 private:
95     bool IsCurrentVibrate(const std::shared_ptr<VibratorThread> &vibratorThread,
96         const VibratorIdentifierIPC& identifier) const;
97     bool IsLoopVibrate(const VibrateInfo &vibrateInfo) const;
98     VibrateStatus ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo, VibrateInfo currentVibrateInfo) const;
99 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_INPUT_METHOD
100     bool ShouldIgnoreInputMethod(const VibrateInfo &vibrateInfo);
101 #endif // OHOS_BUILD_ENABLE_VIBRATOR_INPUT_METHOD
102     static void ExecRegisterCb(const sptr<MiscDeviceObserver> &observer);
103     int32_t RegisterObserver(const sptr<MiscDeviceObserver> &observer);
104     int32_t UnregisterObserver(const sptr<MiscDeviceObserver> &observer);
105     int32_t GetIntValue(const std::string &key, int32_t &value);
106     int32_t GetLongValue(const std::string &key, int64_t &value);
107     int32_t GetStringValue(const std::string &key, std::string &value);
108 #ifdef OHOS_BUILD_ENABLE_DO_NOT_DISTURB
109     int32_t GetDoNotDisturbStringValue(const std::string &key, std::string &value);
110     int32_t GetDoNotDisturbIntValue(const std::string &key, int32_t &value);
111     int32_t GetDoNotDisturbLongValue(const std::string &key, int64_t &value);
112     int32_t GetWhiteListValue(const std::string &key, std::vector<WhiteListAppInfo> &value);
113     void DeleteCJSONValue(cJSON *jsonValue);
114     bool IgnoreAppVibrations(const VibrateInfo &vibrateInfo);
115     void UpdateCurrentUserId();
116     int32_t RegisterUserObserver();
117     int32_t UnregisterUserObserver();
118     std::string ReplaceUserIdForUri(std::string uri, int32_t userId);
119     Uri DoNotDisturbAssembleUri(const std::string &key);
120 #endif // OHOS_BUILD_ENABLE_DO_NOT_DISTURB
121     Uri AssembleUri(const std::string &key);
122     std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string &tableUrl);
123     bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper);
124     sptr<MiscDeviceObserver> CreateObserver(const MiscDeviceObserver::UpdateFunc &func);
125     void UpdateStatus();
126     bool IsSystemServiceCalling();
127     bool IsSystemCalling();
128     sptr<IRemoteObject> remoteObj_ { nullptr };
129     sptr<MiscDeviceObserver> observer_ { nullptr };
130     std::shared_ptr<AppExecFwk::AppMgrClient> appMgrClientPtr_ {nullptr};
131     std::atomic_int32_t miscFeedback_ = FEEDBACK_MODE_INVALID;
132     std::atomic_int32_t miscAudioRingerMode_ = RINGER_MODE_INVALID;
133 #ifdef OHOS_BUILD_ENABLE_DO_NOT_DISTURB
134     std::atomic_int32_t doNotDisturbSwitch_ = DONOTDISTURB_SWITCH_INVALID;
135     std::vector<WhiteListAppInfo> doNotDisturbWhiteList_;
136     sptr<MiscDeviceObserver> currentUserObserver_;
137     std::mutex currentUserObserverMutex_;
138     std::mutex whiteListMutex_;
139 #endif // OHOS_BUILD_ENABLE_DO_NOT_DISTURB
140 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CROWN
141     std::atomic_int32_t miscCrownFeedback_ = FEEDBACK_MODE_INVALID;
142     std::atomic_int32_t miscIntensity_ = FEEDBACK_INTENSITY_INVALID;
143 #endif
144 };
145 #define PriorityManager DelayedSingleton<VibrationPriorityManager>::GetInstance()
146 }  // namespace Sensors
147 }  // namespace OHOS
148 #endif  // VIBRATION_PRIORITY_MANAGER_H