1 /*
2  * Copyright (c) 2023 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 INPUTMETHOD_IMF_INPUT_TYPE_MANAGER_H
17 #define INPUTMETHOD_IMF_INPUT_TYPE_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "block_data.h"
26 #include "serializable.h"
27 #include "sys_cfg_parser.h"
28 
29 namespace OHOS {
30 namespace MiscServices {
31 struct ImeIdentification {
32     std::string bundleName;
33     std::string subName;
34     bool operator==(const ImeIdentification &ime) const
35     {
36         return (bundleName == ime.bundleName && subName == ime.subName);
37     }
38     bool operator<(const ImeIdentification &ime) const
39     {
40         return bundleName == ime.bundleName ? (subName < ime.subName) : (bundleName < ime.bundleName);
41     }
42 };
43 
44 class InputTypeManager {
45 public:
46     static InputTypeManager &GetInstance();
47     bool IsSupported(InputType type);
48     bool IsInputType(const ImeIdentification &ime);
49     bool IsStarted();
50     bool IsSecurityImeStarted();
51     bool IsCameraImeStarted();
52     bool IsVoiceImeStarted();
53     InputType GetCurrentInputType();
54     void Set(bool isStarted, const ImeIdentification &currentIme = {});
55     ImeIdentification GetCurrentIme();
56     int32_t GetImeByInputType(InputType type, ImeIdentification &ime);
57 
58 private:
59     bool Init();
60     std::mutex stateLock_;
61     bool isStarted_{ false };
62     ImeIdentification currentTypeIme_;
63 
64     std::mutex typesLock_;
65     std::map<InputType, ImeIdentification> inputTypes_;
66     std::mutex listLock_;
67     std::set<ImeIdentification> inputTypeImeList_;
68 
69     std::atomic_bool isTypeCfgReady_{ false };
70     std::atomic_bool isInitInProgress_{ false };
71     BlockData<bool> isInitSuccess_{ false };
72 };
73 } // namespace MiscServices
74 } // namespace OHOS
75 
76 #endif //INPUTMETHOD_IMF_INPUT_TYPE_MANAGER_H
77