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 sptr<IRemoteObject> remoteObject_ = nullptr; 72 }; 73 bool ShowFunctionSwitchWindow(); 74 bool UnShowFunctionSwitchWindow(); 75 void SubscribeCommonEvent(); 76 bool ShouldRejectShowWindow(); 77 static void BootCompletedEventCallback(const char *key, const char *value, void *context); 78 static std::shared_ptr<UsbFunctionSwitchWindow> instance_; 79 static int32_t GetSupportedFunctions(); 80 sptr<UsbFuncAbilityConn> usbFuncAbilityConn = nullptr; 81 int32_t windowAction_ = UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_DEFAULT; 82 static std::mutex insMutex_; 83 std::mutex opMutex_; 84 const std::string functionSwitchBundleName_ = "com.usb.right"; 85 const std::string functionSwitchExtAbility_ = "UsbFunctionSwitchExtAbility"; 86 bool isDialogInstalled_ = false; 87 Utils::Timer checkDialogTimer_ {"checkDialogTimer"}; 88 uint32_t checkDialogTimerId_ {UINT32_MAX}; 89 }; 90 } // namespace USB 91 } // namespace OHOS 92 93 #endif 94