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 <cstdint>
20 #include <cstring>
21 #include <memory>
22 #include <vector>
23 
24 #include "app_mgr_client.h"
25 #include "datashare_helper.h"
26 #include "iremote_object.h"
27 #include "singleton.h"
28 
29 #include "miscdevice_observer.h"
30 #include "vibrator_infos.h"
31 #include "vibrator_thread.h"
32 
33 namespace OHOS {
34 namespace Sensors {
35 enum VibrateStatus {
36     VIBRATION = 0,
37     IGNORE_BACKGROUND = 1,
38     IGNORE_LOW_POWER = 2,
39     IGNORE_GLOBAL_SETTINGS = 3,
40     IGNORE_RINGTONE = 4,
41     IGNORE_REPEAT = 5,
42     IGNORE_ALARM = 6,
43     IGNORE_UNKNOWN = 7,
44     IGNORE_RINGER_MODE = 8,
45     IGNORE_FEEDBACK = 9,
46 };
47 
48 enum RingerMode {
49     RINGER_MODE_INVALID = -1,
50     RINGER_MODE_SILENT = 0,
51     RINGER_MODE_VIBRATE = 1,
52     RINGER_MODE_NORMAL = 2
53 };
54 
55 enum FeedbackMode {
56     FEEDBACK_MODE_INVALID = -1,
57     FEEDBACK_MODE_OFF = 0,
58     FEEDBACK_MODE_ON = 1
59 };
60 
61 class VibrationPriorityManager {
62     DECLARE_DELAYED_SINGLETON(VibrationPriorityManager);
63 public:
64     DISALLOW_COPY_AND_MOVE(VibrationPriorityManager);
65     bool Init();
66     VibrateStatus ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo, std::shared_ptr<VibratorThread> vibratorThread);
67 
68 private:
69     bool IsCurrentVibrate(std::shared_ptr<VibratorThread> vibratorThread) const;
70     bool IsLoopVibrate(const VibrateInfo &vibrateInfo) const;
71     VibrateStatus ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo, VibrateInfo currentVibrateInfo) const;
72     bool ShouldIgnoreInputMethod(const VibrateInfo &vibrateInfo);
73     static void ExecRegisterCb(const sptr<MiscDeviceObserver> &observer);
74     int32_t RegisterObserver(const sptr<MiscDeviceObserver> &observer);
75     int32_t UnregisterObserver(const sptr<MiscDeviceObserver> &observer);
76     int32_t GetIntValue(const std::string &key, int32_t &value);
77     int32_t GetLongValue(const std::string &key, int64_t &value);
78     int32_t GetStringValue(const std::string &key, std::string &value);
79     Uri AssembleUri(const std::string &key);
80     std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper();
81     bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper);
82     sptr<MiscDeviceObserver> CreateObserver(const MiscDeviceObserver::UpdateFunc &func);
83     void UpdateStatus();
84     bool IsSystemServiceCalling();
85     bool IsSystemCalling();
86     sptr<IRemoteObject> remoteObj_ { nullptr };
87     sptr<MiscDeviceObserver> observer_ { nullptr };
88     std::shared_ptr<AppExecFwk::AppMgrClient> appMgrClientPtr_ {nullptr};
89     std::atomic_int32_t miscFeedback_ = FEEDBACK_MODE_INVALID;
90     std::atomic_int32_t miscAudioRingerMode_ = RINGER_MODE_INVALID;
91 };
92 #define PriorityManager DelayedSingleton<VibrationPriorityManager>::GetInstance()
93 }  // namespace Sensors
94 }  // namespace OHOS
95 #endif  // VIBRATION_PRIORITY_MANAGER_H