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 OHOS_JS_DISPLAY_LISTENER_H 17 #define OHOS_JS_DISPLAY_LISTENER_H 18 19 #include <mutex> 20 #include "native_engine/native_engine.h" 21 #include "native_engine/native_value.h" 22 #include "refbase.h" 23 #include "display_manager.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 class JsDisplayListener : public DisplayManager::IDisplayListener, 28 public DisplayManager::IPrivateWindowListener, 29 public DisplayManager::IFoldStatusListener, 30 public DisplayManager::IDisplayModeListener { 31 public: JsDisplayListener(NativeEngine * engine)32 explicit JsDisplayListener(NativeEngine* engine) : engine_(engine) {} 33 ~JsDisplayListener() override = default; 34 void AddCallback(const std::string& type, NativeValue* jsListenerObject); 35 void RemoveAllCallback(); 36 void RemoveCallback(const std::string& type, NativeValue* jsListenerObject); 37 void OnCreate(DisplayId id) override; 38 void OnDestroy(DisplayId id) override; 39 void OnChange(DisplayId id) override; 40 void OnPrivateWindow(bool hasPrivate) override; 41 void OnFoldStatusChanged(FoldStatus foldStatus) override; 42 void OnDisplayModeChanged(FoldDisplayMode displayMode) override; 43 44 private: 45 void CallJsMethod(const std::string& methodName, NativeValue* const* argv = nullptr, size_t argc = 0); 46 NativeEngine* engine_ = nullptr; 47 std::mutex mtx_; 48 std::map<std::string, std::vector<std::unique_ptr<NativeReference>>> jsCallBack_; 49 NativeValue* CreateDisplayIdArray(NativeEngine& engine, const std::vector<DisplayId>& data); 50 }; 51 const std::string EVENT_ADD = "add"; 52 const std::string EVENT_REMOVE = "remove"; 53 const std::string EVENT_CHANGE = "change"; 54 const std::string EVENT_PRIVATE_MODE_CHANGE = "privateModeChange"; 55 const std::string 56 EVENT_FOLD_STATUS_CHANGED = "foldStatusChange"; 57 const std::string EVENT_DISPLAY_MODE_CHANGED = "foldDisplayModeChange"; 58 } // namespace Rosen 59 } // namespace OHOS 60 #endif /* OHOS_JS_DISPLAY_LISTENER_H */