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