• 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 #include "shutdown_dialog.h"
17 
18 #include <atomic>
19 #include <fstream>
20 #include <memory>
21 #include <set>
22 #include <string_ex.h>
23 
24 #include <ability_manager_client.h>
25 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
26 #include <input_manager.h>
27 #include <key_event.h>
28 #include <key_option.h>
29 #endif
30 #include <message_parcel.h>
31 
32 #include "config_policy_utils.h"
33 #include "json/reader.h"
34 #include "json/value.h"
35 #include "power_log.h"
36 #include "power_mgr_service.h"
37 #include "power_vibrator.h"
38 
39 using namespace OHOS::MMI;
40 using namespace OHOS::AAFwk;
41 
42 namespace OHOS {
43 namespace PowerMgr {
44 namespace {
45 static constexpr int32_t LONG_PRESS_DELAY_MS = 3000;
46 static constexpr int32_t INVALID_USERID = -1;
47 static constexpr int32_t MESSAGE_PARCEL_KEY_SIZE = 3;
48 static constexpr int32_t INIT_LONG_PRESS_RETRY = 3;
49 static constexpr uint32_t RETRY_TIME = 1000;
50 std::atomic_bool g_isDialogShow = false;
51 std::atomic_bool g_longPressShow = false;
52 int32_t g_retryCount = 1;
53 sptr<IRemoteObject> g_remoteObject = nullptr;
54 const std::string DIALOG_CONFIG_PATH = "etc/systemui/poweroff_config.json";
55 } // namespace
56 
57 std::string ShutdownDialog::bundleName_ = "com.ohos.powerdialog";
58 std::string ShutdownDialog::abilityName_ = "PowerUiExtensionAbility";
59 std::string ShutdownDialog::uiExtensionType_ = "sysDialog/power";
60 std::string ShutdownDialog::dialogBundleName_ = "com.ohos.systemui";
61 std::string ShutdownDialog::dialogAbilityName_ = "com.ohos.systemui.dialog";
62 
ShutdownDialog()63 ShutdownDialog::ShutdownDialog() : dialogConnectionCallback_(new DialogAbilityConnection()) {}
64 
~ShutdownDialog()65 ShutdownDialog::~ShutdownDialog()
66 {
67     dialogConnectionCallback_ = nullptr;
68 }
69 
KeyMonitorInit()70 void ShutdownDialog::KeyMonitorInit()
71 {
72 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
73     POWER_HILOGD(FEATURE_SHUTDOWN, "Initialize the long press powerkey");
74     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
75     std::set<int32_t> preKeys;
76 
77     keyOption->SetPreKeys(preKeys);
78     keyOption->SetFinalKey(KeyEvent::KEYCODE_POWER);
79     keyOption->SetFinalKeyDown(true);
80     keyOption->SetFinalKeyDownDuration(LONG_PRESS_DELAY_MS);
81     longPressId_ =
82         InputManager::GetInstance()->SubscribeKeyEvent(keyOption, [this](std::shared_ptr<KeyEvent> keyEvent) {
83             POWER_HILOGI(FEATURE_SHUTDOWN, "Receive long press powerkey");
84             ConnectSystemUi();
85             StartVibrator();
86         });
87     if (longPressId_ < ERR_OK) {
88         if (g_retryCount <= INIT_LONG_PRESS_RETRY) {
89             POWER_HILOGI(FEATURE_SHUTDOWN, "SubscribeKey long press failed errcode = %{public}d, Try again in 1 second",
90                 longPressId_);
91             FFRTTask task = [this] {
92                 KeyMonitorInit();
93             };
94             FFRTUtils::SubmitDelayTask(task, RETRY_TIME, queue_);
95             g_retryCount++;
96         }
97         return;
98     }
99     POWER_HILOGI(FEATURE_SHUTDOWN, "SubscribeKey long press success");
100 #endif
101 }
102 
KeyMonitorCancel()103 void ShutdownDialog::KeyMonitorCancel()
104 {
105 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
106     InputManager* inputManager = InputManager::GetInstance();
107     if (inputManager == nullptr) {
108         POWER_HILOGW(FEATURE_SHUTDOWN, "InputManager is null");
109         return;
110     }
111     if (longPressId_ >= ERR_OK) {
112         inputManager->UnsubscribeKeyEvent(longPressId_);
113     }
114     longPressId_ = 0;
115 #endif
116 }
117 
ConnectSystemUi()118 bool ShutdownDialog::ConnectSystemUi()
119 {
120     if (g_isDialogShow) {
121         AppExecFwk::ElementName element;
122         dialogConnectionCallback_->OnAbilityConnectDone(element, g_remoteObject, INVALID_USERID);
123         POWER_HILOGW(FEATURE_SHUTDOWN, "power dialog has been show");
124         return true;
125     }
126     auto ams = AbilityManagerClient::GetInstance();
127     if (ams == nullptr) {
128         POWER_HILOGW(FEATURE_SHUTDOWN, "AbilityManagerClient is nullptr");
129         return false;
130     }
131 
132     Want want;
133     want.SetElementName(dialogBundleName_, dialogAbilityName_);
134     ErrCode result = ams->ConnectAbility(want, dialogConnectionCallback_, INVALID_USERID);
135     if (result != ERR_OK) {
136         POWER_HILOGW(FEATURE_SHUTDOWN, "ConnectAbility systemui dialog failed, result = %{public}d", result);
137         return false;
138     }
139     POWER_HILOGI(FEATURE_SHUTDOWN, "ConnectAbility systemui dialog success.");
140     return true;
141 }
142 
IsLongPress() const143 bool ShutdownDialog::IsLongPress() const
144 {
145     return g_longPressShow;
146 }
147 
ResetLongPressFlag()148 void ShutdownDialog::ResetLongPressFlag()
149 {
150     g_longPressShow = false;
151 }
152 
LoadDialogConfig()153 void ShutdownDialog::LoadDialogConfig()
154 {
155     char buf[MAX_PATH_LEN];
156     char* configPath = GetOneCfgFile(DIALOG_CONFIG_PATH.c_str(), buf, MAX_PATH_LEN);
157     if (configPath == nullptr || *configPath == '\0') {
158         POWER_HILOGI(COMP_UTILS, "do not find shutdown off json");
159         return;
160     }
161 
162     std::ifstream inputStream(configPath, std::ios::in | std::ios::binary);
163     std::string contentStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
164     Json::Reader reader;
165     Json::Value root;
166     if (!reader.parse(contentStr.data(), contentStr.data() + contentStr.size(), root)) {
167         POWER_HILOGE(COMP_UTILS, "json parse error");
168         return;
169     }
170 
171     if (!root["bundleName"].isString() ||
172         !root["abilityName"].isString() || !root["uiExtensionType"].isString()) {
173         POWER_HILOGE(COMP_UTILS, "json varibale not support");
174         return;
175     }
176 
177     bundleName_ = root["bundleName"].asString();
178     abilityName_ = root["abilityName"].asString();
179     uiExtensionType_ = root["uiExtensionType"].asString();
180     dialogBundleName_ = root["bundleName"].asString();
181     dialogAbilityName_ = "com.ohos.sceneboard.systemdialog";
182     POWER_HILOGI(COMP_UTILS, "PowerOff variables have changed");
183 }
184 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)185 void ShutdownDialog::DialogAbilityConnection::OnAbilityConnectDone(
186     const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
187 {
188     POWER_HILOGI(FEATURE_SHUTDOWN, "OnAbilityConnectDone");
189     std::lock_guard lock(mutex_);
190     if (remoteObject == nullptr) {
191         POWER_HILOGW(FEATURE_SHUTDOWN, "remoteObject is nullptr");
192         return;
193     }
194     if (g_remoteObject == nullptr) {
195         g_remoteObject = remoteObject;
196     }
197     FFRTUtils::SubmitTask([remoteObject] {
198         MessageParcel data;
199         MessageParcel reply;
200         MessageOption option;
201         data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE);
202         data.WriteString16(u"bundleName");
203         data.WriteString16(Str8ToStr16(ShutdownDialog::GetBundleName()));
204         data.WriteString16(u"abilityName");
205         data.WriteString16(Str8ToStr16(ShutdownDialog::GetAbilityName()));
206         data.WriteString16(u"parameters");
207         std::string midStr = "\"";
208         // sysDialogZOrder = 2 displayed on the lock screen
209         std::string paramStr = "{\"ability.want.params.uiExtensionType\":" + midStr +
210             ShutdownDialog::GetUiExtensionType() + midStr + ",\"sysDialogZOrder\":2}";
211         data.WriteString16(Str8ToStr16(paramStr));
212         POWER_HILOGI(FEATURE_SHUTDOWN, "show power dialog is begin");
213         const uint32_t cmdCode = 1;
214         int32_t ret = remoteObject->SendRequest(cmdCode, data, reply, option);
215         if (ret != ERR_OK) {
216             POWER_HILOGW(FEATURE_SHUTDOWN, "show power dialog is failed: %{public}d", ret);
217             return;
218         }
219         g_isDialogShow = true;
220         g_longPressShow = true;
221         POWER_HILOGI(FEATURE_SHUTDOWN, "show power dialog is success");
222         auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
223         if (pms == nullptr) {
224             return;
225         }
226         pms->RefreshActivityInner(
227             static_cast<int64_t>(time(nullptr)), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, false);
228     });
229 }
230 
StartVibrator()231 void ShutdownDialog::StartVibrator()
232 {
233     std::shared_ptr<PowerVibrator> vibrator = PowerVibrator::GetInstance();
234     std::string scene = "shutdown_diag";
235     vibrator->StartVibrator(scene);
236 }
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)237 void ShutdownDialog::DialogAbilityConnection::OnAbilityDisconnectDone(
238     const AppExecFwk::ElementName& element, int resultCode)
239 {
240     POWER_HILOGD(FEATURE_SHUTDOWN, "OnAbilityDisconnectDone");
241     std::lock_guard lock(mutex_);
242     g_isDialogShow = false;
243     g_longPressShow = false;
244     g_remoteObject = nullptr;
245 }
246 } // namespace PowerMgr
247 } // namespace OHOS
248