1e1c44949Sopenharmony_ci/*
2e1c44949Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3e1c44949Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e1c44949Sopenharmony_ci * you may not use this file except in compliance with the License.
5e1c44949Sopenharmony_ci * You may obtain a copy of the License at
6e1c44949Sopenharmony_ci *
7e1c44949Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e1c44949Sopenharmony_ci *
9e1c44949Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e1c44949Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e1c44949Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e1c44949Sopenharmony_ci * See the License for the specific language governing permissions and
13e1c44949Sopenharmony_ci * limitations under the License.
14e1c44949Sopenharmony_ci */
15e1c44949Sopenharmony_ci
16e1c44949Sopenharmony_ci#include "fold_status_manager.h"
17e1c44949Sopenharmony_ci
18e1c44949Sopenharmony_ci#include "telephony_log_wrapper.h"
19e1c44949Sopenharmony_ci#include "syspara/parameters.h"
20e1c44949Sopenharmony_ci#include "call_dialog.h"
21e1c44949Sopenharmony_ci
22e1c44949Sopenharmony_cinamespace OHOS {
23e1c44949Sopenharmony_cinamespace Telephony {
24e1c44949Sopenharmony_ciconst std::string FOLD_PRODUCT_TYPE_KEY = "const.window.foldscreen.type";
25e1c44949Sopenharmony_ciconst std::string SMALL_FOLD_PRODUCT_TYPE = "2,0,0,0";
26e1c44949Sopenharmony_ci
27e1c44949Sopenharmony_ciFoldStatusManager::~FoldStatusManager() {};
28e1c44949Sopenharmony_ci
29e1c44949Sopenharmony_ciFoldStatusManager::FoldStatusManager()
30e1c44949Sopenharmony_ci{
31e1c44949Sopenharmony_ci    TELEPHONY_LOGI("Enter FoldStatusManager");
32e1c44949Sopenharmony_ci    mOldFoldStatus = Rosen::FoldStatus::UNKNOWN;
33e1c44949Sopenharmony_ci};
34e1c44949Sopenharmony_ci
35e1c44949Sopenharmony_civoid FoldStatusManager::RegisterFoldableListener()
36e1c44949Sopenharmony_ci{
37e1c44949Sopenharmony_ci    if (!IsSmallFoldDevice()) {
38e1c44949Sopenharmony_ci        TELEPHONY_LOGI("Is not small fold device");
39e1c44949Sopenharmony_ci        return;
40e1c44949Sopenharmony_ci    }
41e1c44949Sopenharmony_ci    if (foldStatusListener_ != nullptr) {
42e1c44949Sopenharmony_ci        TELEPHONY_LOGI("Foldable listener is already registed");
43e1c44949Sopenharmony_ci        return;
44e1c44949Sopenharmony_ci    }
45e1c44949Sopenharmony_ci    foldStatusListener_ = new (std::nothrow) FoldStatusListener();
46e1c44949Sopenharmony_ci    if (foldStatusListener_ == nullptr) {
47e1c44949Sopenharmony_ci        TELEPHONY_LOGE("RegisterFoldableListener new listener failed");
48e1c44949Sopenharmony_ci        return;
49e1c44949Sopenharmony_ci    }
50e1c44949Sopenharmony_ci    auto ret = Rosen::DisplayManager::GetInstance().RegisterFoldStatusListener(foldStatusListener_);
51e1c44949Sopenharmony_ci    if (ret != Rosen::DMError::DM_OK) {
52e1c44949Sopenharmony_ci        TELEPHONY_LOGE("Rosen::DisplayManager::RegisterFoldStatusListener failed");
53e1c44949Sopenharmony_ci        foldStatusListener_ = nullptr;
54e1c44949Sopenharmony_ci    } else {
55e1c44949Sopenharmony_ci        mOldFoldStatus = Rosen::DisplayManager::GetInstance().GetFoldStatus();
56e1c44949Sopenharmony_ci        TELEPHONY_LOGI("Rosen::DisplayManager::RegisterFoldStatusListener success");
57e1c44949Sopenharmony_ci    }
58e1c44949Sopenharmony_ci}
59e1c44949Sopenharmony_ci
60e1c44949Sopenharmony_civoid FoldStatusManager::UnregisterFoldableListener()
61e1c44949Sopenharmony_ci{
62e1c44949Sopenharmony_ci    if (foldStatusListener_ == nullptr) {
63e1c44949Sopenharmony_ci        TELEPHONY_LOGI("UnregisterFoldableListener listener is nullptr");
64e1c44949Sopenharmony_ci        return;
65e1c44949Sopenharmony_ci    }
66e1c44949Sopenharmony_ci    auto ret = Rosen::DisplayManager::GetInstance().UnregisterFoldStatusListener(foldStatusListener_);
67e1c44949Sopenharmony_ci    if (ret != Rosen::DMError::DM_OK) {
68e1c44949Sopenharmony_ci        TELEPHONY_LOGE("Rosen::DisplayManager::UnregisterFoldStatusListener failed");
69e1c44949Sopenharmony_ci    } else {
70e1c44949Sopenharmony_ci        mOldFoldStatus = Rosen::FoldStatus::UNKNOWN;
71e1c44949Sopenharmony_ci        foldStatusListener_ = nullptr;
72e1c44949Sopenharmony_ci        TELEPHONY_LOGI("Rosen::DisplayManager::UnregisterFoldStatusListener success");
73e1c44949Sopenharmony_ci    }
74e1c44949Sopenharmony_ci}
75e1c44949Sopenharmony_ci
76e1c44949Sopenharmony_civoid FoldStatusManager::FoldStatusListener::OnFoldStatusChanged(Rosen::FoldStatus foldStatus)
77e1c44949Sopenharmony_ci{
78e1c44949Sopenharmony_ci    auto oldFoldStatus = DelayedSingleton<FoldStatusManager>::GetInstance()->mOldFoldStatus;
79e1c44949Sopenharmony_ci    TELEPHONY_LOGI("OnFoldStatusChanged foldStatus is %{public}d, oldFoldStatus is %{public}d",
80e1c44949Sopenharmony_ci        static_cast<int32_t>(foldStatus), static_cast<int32_t>(oldFoldStatus));
81e1c44949Sopenharmony_ci    if (foldStatus == oldFoldStatus) {
82e1c44949Sopenharmony_ci        TELEPHONY_LOGE("Fold status don't change");
83e1c44949Sopenharmony_ci        return;
84e1c44949Sopenharmony_ci    }
85e1c44949Sopenharmony_ci    DelayedSingleton<CallDialog>::GetInstance()->DialogCallingPrivacyModeExtension(foldStatus);
86e1c44949Sopenharmony_ci    DelayedSingleton<FoldStatusManager>::GetInstance()->mOldFoldStatus = foldStatus;
87e1c44949Sopenharmony_ci}
88e1c44949Sopenharmony_ci
89e1c44949Sopenharmony_cibool FoldStatusManager::IsSmallFoldDevice()
90e1c44949Sopenharmony_ci{
91e1c44949Sopenharmony_ci    std::string foldType = system::GetParameter(FOLD_PRODUCT_TYPE_KEY, "0,0,0,0");
92e1c44949Sopenharmony_ci    TELEPHONY_LOGI("IsSmallFoldDevice foldType is %{public}s", foldType.c_str());
93e1c44949Sopenharmony_ci    return foldType == SMALL_FOLD_PRODUCT_TYPE;
94e1c44949Sopenharmony_ci}
95e1c44949Sopenharmony_ci} // namespace Telephony
96e1c44949Sopenharmony_ci} // namespace OHOS