• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 USB_FUNCTION_SWITCH_WINDOW_H
17 #define USB_FUNCTION_SWITCH_WINDOW_H
18 
19 #include <mutex>
20 
21 #include "ability_connect_callback_stub.h"
22 #include "bundle_mgr_interface.h"
23 #include "parameter.h"
24 #include "usb_common.h"
25 #include "usb_srv_support.h"
26 #include "timer.h"
27 
28 namespace OHOS {
29 namespace USB {
30 
31 enum UsbFunctionChoose : int32_t {
32     FUNCTION_CHOOSE_CHARGE_ONLY = -1,
33     FUNCTION_CHOOSE_TRANSFER_FILE = UsbSrvSupport::FUNCTION_MTP,
34     FUNCTION_CHOOSE_TRANSFER_PIC = UsbSrvSupport::FUNCTION_PTP,
35 };
36 
37 enum UsbFunctionSwitchWindowAction : int32_t {
38     FUNCTION_SWITCH_WINDOW_ACTION_DEFAULT = 0,
39     FUNCTION_SWITCH_WINDOW_ACTION_SHOW,
40     FUNCTION_SWITCH_WINDOW_ACTION_DISMISS,
41     FUNCTION_SWITCH_WINDOW_ACTION_FORBID,
42 };
43 
44 enum SUPPORTED_FUNC : int32_t {
45     SUPPORTED_FUNC_NONE = -1,
46     SUPPORTED_FUNC_CHARGE = 0,
47     SUPPORTED_FUNC_MTP = 8,
48     SUPPORTED_FUNC_PTP = 16,
49 };
50 
51 class UsbFunctionSwitchWindow {
52 public:
53     static std::shared_ptr<UsbFunctionSwitchWindow> GetInstance();
54     ~UsbFunctionSwitchWindow();
55     int32_t Init();
56     bool PopUpFunctionSwitchWindow();
57     bool DismissFunctionSwitchWindow();
58     int32_t SetCurrentFunctionLabel(int32_t func);
59     int32_t RemoveCurrentFunctionLabel();
60     bool CheckDialogInstallStatus();
61 private:
62     UsbFunctionSwitchWindow();
63     DISALLOW_COPY_AND_MOVE(UsbFunctionSwitchWindow);
64     class UsbFuncAbilityConn : public OHOS::AAFwk::AbilityConnectionStub {
65         void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
66             int32_t resultCode) override;
67         void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override;
68     public:
69         void CloseDialog();
70     private:
71         std::mutex remoteMutex_;
72         sptr<IRemoteObject> remoteObject_ = nullptr;
73     };
74     bool ShowFunctionSwitchWindow();
75     bool UnShowFunctionSwitchWindow();
76     void SubscribeCommonEvent();
77     bool ShouldRejectShowWindow();
78     static void BootCompletedEventCallback(const char *key, const char *value, void *context);
79     static std::shared_ptr<UsbFunctionSwitchWindow> instance_;
80     static int32_t GetSupportedFunctions();
81     sptr<UsbFuncAbilityConn> usbFuncAbilityConn = nullptr;
82     int32_t windowAction_ = UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_DEFAULT;
83     static std::mutex insMutex_;
84     std::mutex opMutex_;
85     const std::string functionSwitchBundleName_ = "com.usb.right";
86     const std::string functionSwitchExtAbility_ = "UsbFunctionSwitchExtAbility";
87     bool isDialogInstalled_ = false;
88     Utils::Timer checkDialogTimer_ {"checkDialogTimer"};
89     uint32_t checkDialogTimerId_ {UINT32_MAX};
90 };
91 } // namespace USB
92 } // namespace OHOS
93 
94 #endif
95