• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dm_common.h"
21 #include "native_engine/native_engine.h"
22 #include "native_engine/native_value.h"
23 #include "refbase.h"
24 #include "display_manager.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class JsDisplayListener : public DisplayManager::IDisplayListener,
29                           public DisplayManager::IPrivateWindowListener,
30                           public DisplayManager::IFoldStatusListener,
31                           public DisplayManager::IFoldAngleListener,
32                           public DisplayManager::ICaptureStatusListener,
33                           public DisplayManager::IDisplayModeListener,
34                           public DisplayManager::IAvailableAreaListener {
35 public:
36     explicit JsDisplayListener(napi_env env);
37     ~JsDisplayListener() override;
38     void AddCallback(const std::string& type, napi_value jsListenerObject);
39     void RemoveAllCallback();
40     void RemoveCallback(napi_env env, const std::string& type, napi_value jsListenerObject);
41     void OnCreate(DisplayId id) override;
42     void OnDestroy(DisplayId id) override;
43     void OnChange(DisplayId id) override;
44     void OnPrivateWindow(bool hasPrivate) override;
45     void OnFoldStatusChanged(FoldStatus foldStatus) override;
46     void OnFoldAngleChanged(std::vector<float> foldAngles) override;
47     void OnCaptureStatusChanged(bool isCapture) override;
48     void OnDisplayModeChanged(FoldDisplayMode displayMode) override;
49     void OnAvailableAreaChanged(DMRect area) override;
50 
51 private:
52     void CallJsMethod(const std::string& methodName, napi_value const * argv = nullptr, size_t argc = 0);
53     napi_env env_ = nullptr;
54     std::mutex mtx_;
55     wptr<JsDisplayListener> weakRef_  = nullptr;
56     std::map<std::string, std::vector<std::unique_ptr<NativeReference>>> jsCallBack_;
57     napi_value CreateDisplayIdArray(napi_env env, const std::vector<DisplayId>& data);
58     static void CleanEnv(void* obj);
59 };
60 const std::string EVENT_ADD = "add";
61 const std::string EVENT_REMOVE = "remove";
62 const std::string EVENT_CHANGE = "change";
63 const std::string EVENT_PRIVATE_MODE_CHANGE = "privateModeChange";
64 const std::string EVENT_FOLD_STATUS_CHANGED = "foldStatusChange";
65 const std::string EVENT_FOLD_ANGLE_CHANGED = "foldAngleChange";
66 const std::string EVENT_CAPTURE_STATUS_CHANGED = "captureStatusChange";
67 const std::string EVENT_DISPLAY_MODE_CHANGED = "foldDisplayModeChange";
68 const std::string EVENT_AVAILABLE_AREA_CHANGED = "availableAreaChange";
69 }  // namespace Rosen
70 }  // namespace OHOS
71 #endif /* OHOS_JS_DISPLAY_LISTENER_H */