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 #include "location_policy_plugin.h"
16 #include "edm_ipc_interface_code.h"
17 #include "locator_impl.h"
18 #include "parameters.h"
19 #include "int_serializer.h"
20 #include "plugin_manager.h"
21 
22 namespace OHOS {
23 namespace EDM {
24 
25 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(LocationPolicyPlugin::GetPlugin());
26 const std::string PARAM_EDM_LOCATION_POLICY = "persist.edm.location_policy";
InitPlugin(std::shared_ptr<IPluginTemplate<LocationPolicyPlugin,int32_t>> ptr)27 void LocationPolicyPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<LocationPolicyPlugin, int32_t>> ptr)
28 {
29     EDMLOGI("LocationPolicyPlugin InitPlugin...");
30     ptr->InitAttribute(EdmInterfaceCode::LOCATION_POLICY, "location_policy",
31         "ohos.permission.ENTERPRISE_MANAGE_LOCATION", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
32     ptr->SetSerializer(IntSerializer::GetInstance());
33     ptr->SetOnHandlePolicyListener(&LocationPolicyPlugin::OnSetPolicy, FuncOperateType::SET);
34 }
35 
OnSetPolicy(int32_t & data)36 ErrCode LocationPolicyPlugin::OnSetPolicy(int32_t &data)
37 {
38     EDMLOGD("LocationPolicyPlugin set location policy value = %{public}d.", data);
39     switch (data) {
40         case static_cast<int32_t>(LocationPolicy::DEFAULT_LOCATION_SERVICE):
41             system::SetParameter(PARAM_EDM_LOCATION_POLICY, "none");
42             break;
43         case static_cast<int32_t>(LocationPolicy::DISALLOW_LOCATION_SERVICE):
44             system::SetParameter(PARAM_EDM_LOCATION_POLICY, "disallow");
45             Location::LocatorImpl::GetInstance()->EnableAbility(false);
46             break;
47         case static_cast<int32_t>(LocationPolicy::FORCE_OPEN_LOCATION_SERVICE):
48             system::SetParameter(PARAM_EDM_LOCATION_POLICY, "force_open");
49             Location::LocatorImpl::GetInstance()->EnableAbility(true);
50             break;
51         default:
52             EDMLOGD("LocationPolicyPlugin location policy illegal. Value = %{public}d.", data);
53             return EdmReturnErrCode::PARAM_ERROR;
54     }
55     return ERR_OK;
56 }
57 
OnGetPolicy(std::string & value,MessageParcel & data,MessageParcel & reply,int32_t userId)58 ErrCode LocationPolicyPlugin::OnGetPolicy(std::string &value, MessageParcel &data,
59     MessageParcel &reply, int32_t userId)
60 {
61     EDMLOGI("LocationPolicyPlugin OnGetPolicy");
62     std::string res = system::GetParameter(PARAM_EDM_LOCATION_POLICY, "");
63     EDMLOGI("LocationPolicyPlugin OnGetPolicy res = %{public}s", res.c_str());
64     if (res == "none") {
65         reply.WriteInt32(ERR_OK);
66         reply.WriteInt32(static_cast<int32_t>(LocationPolicy::DEFAULT_LOCATION_SERVICE));
67     } else if (res == "disallow") {
68         reply.WriteInt32(ERR_OK);
69         reply.WriteInt32(static_cast<int32_t>(LocationPolicy::DISALLOW_LOCATION_SERVICE));
70     } else if (res == "force_open") {
71         reply.WriteInt32(ERR_OK);
72         reply.WriteInt32(static_cast<int32_t>(LocationPolicy::FORCE_OPEN_LOCATION_SERVICE));
73     } else {
74         EDMLOGD("LocationPolicyPlugin get location policy illegal. Value = %{public}s.", res.c_str());
75         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
76         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
77     }
78     return ERR_OK;
79 }
80 } // namespace EDM
81 } // namespace OHOS
82