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 "edm_data_ability_utils.h"
19 #include "edm_ipc_interface_code.h"
20 #include "int_serializer.h"
21 #include "plugin_manager.h"
22 
23 namespace OHOS {
24 namespace EDM {
25 static constexpr int32_t SCREEN_OFF_TIME_MIN_VALUE = 15000;
26 static constexpr int32_t SCREEN_OFF_TIME_NEVER_VALUE = -1;
27 
28 const std::string KEY_SCREEN_OFF_TIME = "settings.display.screen_off_timeout";
29 
30 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(ScreenOffTimePlugin::GetPlugin());
31 
InitPlugin(std::shared_ptr<IPluginTemplate<ScreenOffTimePlugin,int32_t>> ptr)32 void ScreenOffTimePlugin::InitPlugin(std::shared_ptr<IPluginTemplate<ScreenOffTimePlugin, int32_t>> ptr)
33 {
34     EDMLOGI("ScreenOffTimePlugin InitPlugin...");
35     ptr->InitAttribute(EdmInterfaceCode::SCREEN_OFF_TIME, "screen_off_time", false);
36     std::map<std::string, std::string> setPerms;
37     setPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11,
38         "ohos.permission.ENTERPRISE_SET_SCREENOFF_TIME"));
39     setPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12,
40         "ohos.permission.ENTERPRISE_MANAGE_SETTINGS"));
41     IPlugin::PolicyPermissionConfig setConfig = IPlugin::PolicyPermissionConfig(setPerms,
42         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
43     ptr->InitPermission(FuncOperateType::SET, setConfig);
44     std::map<std::string, std::string> getPerms;
45     getPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11,
46         "ohos.permission.ENTERPRISE_GET_SETTINGS"));
47     getPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12,
48         "ohos.permission.ENTERPRISE_MANAGE_SETTINGS"));
49     IPlugin::PolicyPermissionConfig getConfig = IPlugin::PolicyPermissionConfig(getPerms,
50         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
51     ptr->InitPermission(FuncOperateType::GET, getConfig);
52     ptr->SetSerializer(IntSerializer::GetInstance());
53     ptr->SetOnHandlePolicyListener(&ScreenOffTimePlugin::OnSetPolicy, FuncOperateType::SET);
54 }
55 
OnSetPolicy(int32_t & data)56 ErrCode ScreenOffTimePlugin::OnSetPolicy(int32_t &data)
57 {
58     EDMLOGD("ScreenOffTimePlugin start set screen off time value = %{public}d.", data);
59     if (data >= SCREEN_OFF_TIME_MIN_VALUE || data == SCREEN_OFF_TIME_NEVER_VALUE) {
60         ErrCode code =
61             EdmDataAbilityUtils::UpdateSettingsData(KEY_SCREEN_OFF_TIME, std::to_string(data));
62         if (FAILED(code)) {
63             EDMLOGE("ScreenOffTimePlugin::set screen off time failed : %{public}d.", code);
64             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
65         }
66         return ERR_OK;
67     }
68     return EdmReturnErrCode::PARAM_ERROR;
69 }
70 
OnGetPolicy(std::string & value,MessageParcel & data,MessageParcel & reply,int32_t userId)71 ErrCode ScreenOffTimePlugin::OnGetPolicy(std::string &value, MessageParcel &data, MessageParcel &reply, int32_t userId)
72 {
73     EDMLOGI("ScreenOffTimePlugin OnGetPolicy");
74     int32_t result = 0;
75     ErrCode code = EdmDataAbilityUtils::GetIntFromSettingsDataShare(KEY_SCREEN_OFF_TIME, result);
76     if (code != ERR_OK) {
77         EDMLOGE("ScreenOffTimePlugin::get screen off time from database failed : %{public}d.", code);
78         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
79         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
80     }
81     reply.WriteInt32(ERR_OK);
82     reply.WriteInt32(result);
83     return ERR_OK;
84 }
85 } // namespace EDM
86 } // namespace OHOS
87