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 "screen_off_time_plugin.h"
17
18 #include "battery_utils.h"
19 #include "edm_data_ability_utils.h"
20 #include "edm_ipc_interface_code.h"
21 #include "int_serializer.h"
22 #include "iplugin_manager.h"
23
24 namespace OHOS {
25 namespace EDM {
26 static constexpr int32_t SCREEN_OFF_TIME_MIN_VALUE = 15000;
27 static constexpr int32_t SCREEN_OFF_TIME_NEVER_VALUE = -1;
28
29 const std::string KEY_SCREEN_OFF_TIME = "settings.display.screen_off_timeout";
30 const std::string KEY_AC_SCREEN_OFF_TIME = "settings.display.ac.screen_off_timeout";
31 const std::string KEY_DC_SCREEN_OFF_TIME = "settings.display.dc.screen_off_timeout";
32
33 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(ScreenOffTimePlugin::GetPlugin());
34
InitPlugin(std::shared_ptr<IPluginTemplate<ScreenOffTimePlugin,int32_t>> ptr)35 void ScreenOffTimePlugin::InitPlugin(std::shared_ptr<IPluginTemplate<ScreenOffTimePlugin, int32_t>> ptr)
36 {
37 EDMLOGI("ScreenOffTimePlugin InitPlugin...");
38 ptr->InitAttribute(EdmInterfaceCode::SCREEN_OFF_TIME, PolicyName::POLICY_SCREEN_OFF_TIME, false);
39 std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissionsForSet;
40 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11ForSet;
41 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12ForSet;
42 typePermissionsForTag11ForSet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
43 EdmPermission::PERMISSION_ENTERPRISE_SET_SCREEN_OFF_TIME);
44 typePermissionsForTag12ForSet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
45 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_SETTINGS);
46 tagPermissionsForSet.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11ForSet);
47 tagPermissionsForSet.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12ForSet);
48 IPlugin::PolicyPermissionConfig setConfig = IPlugin::PolicyPermissionConfig(tagPermissionsForSet,
49 IPlugin::ApiType::PUBLIC);
50 ptr->InitPermission(FuncOperateType::SET, setConfig);
51
52 std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissionsForGet;
53 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11ForGet;
54 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12ForGet;
55 typePermissionsForTag11ForGet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
56 EdmPermission::PERMISSION_ENTERPRISE_GET_SETTINGS);
57 typePermissionsForTag12ForGet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
58 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_SETTINGS);
59 tagPermissionsForGet.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11ForGet);
60 tagPermissionsForGet.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12ForGet);
61 IPlugin::PolicyPermissionConfig getConfig = IPlugin::PolicyPermissionConfig(tagPermissionsForGet,
62 IPlugin::ApiType::PUBLIC);
63 ptr->InitPermission(FuncOperateType::GET, getConfig);
64 ptr->SetSerializer(IntSerializer::GetInstance());
65 ptr->SetOnHandlePolicyListener(&ScreenOffTimePlugin::OnSetPolicy, FuncOperateType::SET);
66 }
67
OnSetPolicy(int32_t & data)68 ErrCode ScreenOffTimePlugin::OnSetPolicy(int32_t &data)
69 {
70 EDMLOGD("ScreenOffTimePlugin start set screen off time value = %{public}d.", data);
71 if (data >= SCREEN_OFF_TIME_MIN_VALUE || data == SCREEN_OFF_TIME_NEVER_VALUE) {
72 #ifdef FEATURE_CHARGING_TYPE_SETTING
73 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(
74 BatteryUtils::GetSubUserTableUri(), KEY_AC_SCREEN_OFF_TIME, std::to_string(data)))) {
75 EDMLOGE("ScreenOffTimePlugin::set ac screen off time failed");
76 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
77 }
78 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(
79 BatteryUtils::GetSubUserTableUri(), KEY_DC_SCREEN_OFF_TIME, std::to_string(data)))) {
80 EDMLOGE("ScreenOffTimePlugin::set dc screen off time failed");
81 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
82 }
83 #else
84 ErrCode code = EdmDataAbilityUtils::UpdateSettingsData(KEY_SCREEN_OFF_TIME, std::to_string(data));
85 if (FAILED(code)) {
86 EDMLOGE("ScreenOffTimePlugin::set screen off time failed : %{public}d.", code);
87 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
88 }
89 #endif
90 return ERR_OK;
91 }
92 return EdmReturnErrCode::PARAM_ERROR;
93 }
94
OnGetPolicy(std::string & value,MessageParcel & data,MessageParcel & reply,int32_t userId)95 ErrCode ScreenOffTimePlugin::OnGetPolicy(std::string &value, MessageParcel &data, MessageParcel &reply, int32_t userId)
96 {
97 EDMLOGI("ScreenOffTimePlugin OnGetPolicy");
98 int32_t result = 0;
99 ErrCode code;
100 #ifdef FEATURE_CHARGING_TYPE_SETTING
101 std::string newKey;
102 if (BatteryUtils::GetBatteryPluggedType() == BatteryUtils::PLUGGED_TYPE_AC) {
103 newKey = KEY_AC_SCREEN_OFF_TIME;
104 } else {
105 newKey = KEY_DC_SCREEN_OFF_TIME;
106 }
107 code = EdmDataAbilityUtils::GetIntFromSettingsDataShare(BatteryUtils::GetSubUserTableUri(), newKey, result);
108 #else
109 code = EdmDataAbilityUtils::GetIntFromSettingsDataShare(KEY_SCREEN_OFF_TIME, result);
110 #endif
111 if (code != ERR_OK) {
112 EDMLOGE("ScreenOffTimePlugin::get screen off time from database failed : %{public}d.", code);
113 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
114 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
115 }
116 reply.WriteInt32(ERR_OK);
117 reply.WriteInt32(result);
118 return ERR_OK;
119 }
120 } // namespace EDM
121 } // namespace OHOS
122