• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "call_earthquake_alarm_locator.h"
17 
18 using namespace std;
19 using namespace OHOS::Telephony;
20 using namespace OHOS::Location;
21 using namespace OHOS::EventFwk;
22 namespace OHOS {
23 namespace Telephony {
24 using namespace AppExecFwk;
25 OHOS::Uri MyLocationEngine::uri_q(string("datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?")
26     + string("Proxy=true&key=auto_send_earthquake_alarm_switch"));
27 const int MyLocationEngine::DISTANCE_INTERVAL = 10000; /** 10 kilometers */
28 const int MyLocationEngine::TIMER_INTERVAL = 0;
29 const std::string MyLocationEngine::EMERGENCY_DEVICE_ID = "";
30 const std::string MyLocationEngine::EMERGENCY_BUNDLE_NAME = "";
31 const std::string MyLocationEngine::EMERGENCY_ABILITY_NAME = "";
32 const std::string MyLocationEngine::PARAMETERS_VALUE = "call_manager_earthquake_alarm";
33 const char* MyLocationEngine::PARAMETERS_KEY = "callerName";
34 const std::string MyLocationEngine::ALARM_SWITCH_ON = "1";
35 const std::string MyLocationEngine::ALARM_SWITCH_OFF = "0";
36 std::string MyLocationEngine::INITIAL_FIRST_VALUE = "FIRST_NO_VALUE";
37 const int EmergencyCallConnectCallback::CONNECT_SUCCESS = 0;
38 std::shared_ptr<MyLocationEngine> MyLocationEngine::mylocator = std::make_shared<MyLocationEngine>();
GetInstance()39 std::shared_ptr<MyLocationEngine> MyLocationEngine::GetInstance()
40 {
41     if (MyLocationEngine::mylocator != nullptr) {
42         std::shared_ptr<MyLocationEngine> instance_ = MyLocationEngine::mylocator;
43         return instance_;
44     }
45     return nullptr;
46 }
47 
OnInit()48 void MyLocationEngine::OnInit()
49 {
50     if (this->requestConfig != nullptr)     this->requestConfig = nullptr;
51     if (this->locatorCallback_ != nullptr)  this->locatorCallback_ = nullptr;
52     if (this->locatorImpl != nullptr)       this->locatorImpl = nullptr;
53     if (this->switchCallback_ != nullptr)   this->switchCallback_ = nullptr;
54 }
55 
MyLocationEngine()56 MyLocationEngine::MyLocationEngine() {}
57 
~MyLocationEngine()58 MyLocationEngine::~MyLocationEngine()
59 {
60     UnRegisterSwitchCallback();
61     UnregisterLocationChange();
62     this->OnInit();
63 }
64 
SetValue()65 void MyLocationEngine::SetValue()
66 {
67     if (this->locatorImpl == nullptr) {
68         this->locatorImpl = Location::Locator::GetInstance();
69         if (this->locatorImpl == nullptr) {
70             TELEPHONY_LOGI("MyLocationEngine locatorCallback_ is null");
71             return;
72         }
73         locationEnabled_ = locatorImpl->IsLocationEnabled();
74         TELEPHONY_LOGI("MyLocationEngine locatorImpl is not nullptr");
75     }
76     if (this->requestConfig == nullptr) {
77     this->requestConfig = std::make_unique<RequestConfig>(Location::SCENE_NO_POWER);
78     this->requestConfig->SetTimeInterval(TIMER_INTERVAL);
79     this->requestConfig->SetDistanceInterval(DISTANCE_INTERVAL);
80     TELEPHONY_LOGI("MyLocationEngine requestConfig initial");
81     }
82 }
83 
RegisterLocationChange()84 void MyLocationEngine::RegisterLocationChange()
85 {
86     if (locatorImpl == nullptr) {
87         TELEPHONY_LOGI("MyLocationEngine locatorImpl is null");
88         return;
89     }
90     if (locatorCallback_ == nullptr) {
91         locatorCallback_ =
92             sptr<MyLocationEngine::MyLocationCallBack>(new (std::nothrow) MyLocationEngine::MyLocationCallBack());
93         if (locatorCallback_ == nullptr) {
94             TELEPHONY_LOGI("MyLocationEngine locatorCallback_ is null");
95             return;
96         }
97     }
98     bool IsLocationEnable;
99     locatorImpl->IsLocationEnabledV9(IsLocationEnable);
100     if (IsLocationEnable == false) {
101         return;
102     }
103     auto callback = sptr<Location::ILocatorCallback>(locatorCallback_);
104     int code = this->locatorImpl->StartLocatingV9(this->requestConfig, callback);
105     TELEPHONY_LOGI("MyLocationEngine startListencode = %{public}d.", code);
106 }
107 
UnregisterLocationChange()108 void MyLocationEngine::UnregisterLocationChange()
109 {
110     if (locatorImpl == nullptr || locatorCallback_ == nullptr) {
111         return;
112     }
113     auto callback = sptr<Location::ILocatorCallback>(locatorCallback_);
114     auto code = this->locatorImpl->StopLocatingV9(callback);
115     TELEPHONY_LOGI("MyLocationEngine stopListencdode = %{public}d.", code);
116 }
117 
RegisterSwitchCallback()118 void MyLocationEngine::RegisterSwitchCallback()
119 {
120     if (locatorImpl == nullptr) {
121         TELEPHONY_LOGI("MyLocationEnginek locatorImpl is null.");
122         return;
123     }
124     auto engine = MyLocationEngine::GetInstance();
125     if (switchCallback_ == nullptr) {
126         switchCallback_ = sptr<MyLocationEngine::MySwitchCallback>(
127             new (std::nothrow) MyLocationEngine::MySwitchCallback(engine));
128         if (switchCallback_ == nullptr) {
129             TELEPHONY_LOGI("MyLocationEngine callback is null.");
130             return;
131         }
132     }
133     auto code = locatorImpl->RegisterSwitchCallbackV9(switchCallback_->AsObject());
134     TELEPHONY_LOGI("MyLocationEngine code = %{public}d. success", code);
135 }
136 
UnRegisterSwitchCallback()137 void MyLocationEngine::UnRegisterSwitchCallback()
138 {
139     if (locatorImpl == nullptr) {
140         TELEPHONY_LOGI("MyLocationEngine locatorImpl is null.");
141         return;
142     }
143     if (switchCallback_ != nullptr) {
144         TELEPHONY_LOGI("MyLocationEngine lUnregisterSwitchCallback");
145         locatorImpl->UnregisterSwitchCallbackV9(switchCallback_->AsObject());
146     }
147 }
148 
LocationSwitchChange()149 void MyLocationEngine::LocationSwitchChange()
150 {
151     if (locatorImpl == nullptr) {
152         TELEPHONY_LOGI("MyLocationEngine locatorImpl is null.");
153         return;
154     }
155     bool locationEnabled = locationEnabled_;
156     locationEnabled_ = locatorImpl->IsLocationEnabled();
157     if (!locationEnabled && locationEnabled_) {
158         TELEPHONY_LOGI("MyLocationEngine Enable location.[%{public}d][%{public}d]", locationEnabled, locationEnabled_);
159         RegisterLocationChange();
160     } else if (locationEnabled && !locationEnabled_) {
161         TELEPHONY_LOGI("MyLocationEngine Disable location.[%{public}d][%{public}d]",
162             locationEnabled, locationEnabled_);
163         UnregisterLocationChange();
164     } else {
165         TELEPHONY_LOGI("MyLocationEngine Location switch not change[%{public}d]", locationEnabled_);
166     }
167 }
168 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)169 int32_t MyLocationEngine::MyLocationCallBack::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
170     MessageOption &option)
171 {
172     if (data.ReadInterfaceToken() != GetDescriptor()) {
173         TELEPHONY_LOGI("MyLocationEngine invalid token.");
174         return -1;
175     }
176     switch (code) {
177         case RECEIVE_LOCATION_INFO_EVENT: {
178             std::unique_ptr<OHOS::Location::Location> location = OHOS::Location::Location::Unmarshalling(data);
179             OnLocationReport(location);
180             break;
181         }
182         case RECEIVE_ERROR_INFO_EVENT: {
183             int32_t errorCode = data.ReadInt32();
184             OnErrorReport(errorCode);
185             break;
186         }
187         case RECEIVE_LOCATION_STATUS_EVENT: {
188             int32_t status = data.ReadInt32();
189             OnLocatingStatusChange(status);
190             break;
191         }
192         default: {
193             break;
194         }
195     }
196     TELEPHONY_LOGI("MyLocationEngine locationreport code = %{public}d.", code);
197     return 0;
198 }
199 
MyLocationCallBack()200 MyLocationEngine::MyLocationCallBack::MyLocationCallBack() {}
201 
OnLocatingStatusChange(const int status)202 void MyLocationEngine::MyLocationCallBack::OnLocatingStatusChange(const int status)
203 {
204     TELEPHONY_LOGI("MyLocationEngine ListenStatus = %{public}d.", status);
205 }
206 
OnErrorReport(const int errorCode)207 void MyLocationEngine::MyLocationCallBack::OnErrorReport(const int errorCode) {}
208 
OnLocationReport(const std::unique_ptr<Location::Location> & location)209 void MyLocationEngine::MyLocationCallBack::OnLocationReport(const std::unique_ptr<Location::Location>& location)
210 {
211     TELEPHONY_LOGI("MyLocationEngine ListenLocation is success");
212     MyLocationEngine::ConnectAbility();
213 }
214 
BootComplete()215 void MyLocationEngine::BootComplete()
216 {
217     if (!MyLocationEngine::IsSwitchOn()) {
218         TELEPHONY_LOGI("the switchstate is close MyLocationEngine");
219         return;
220     }
221     TELEPHONY_LOGI("the switchstate is open MyLocationEngine");
222     auto engine = MyLocationEngine::GetInstance();
223     if (engine == nullptr) {
224         TELEPHONY_LOGI("engine == nullptr MyLocationEngine");
225         return;
226     }
227     engine->SetValue();
228     engine->RegisterLocationChange();
229     engine->RegisterSwitchCallback();
230 }
231 
MySwitchCallback(std::shared_ptr<MyLocationEngine> locationUpdate)232 MyLocationEngine::MySwitchCallback::MySwitchCallback(std::shared_ptr<MyLocationEngine> locationUpdate)
233     : locationUpdate_(locationUpdate)
234 {}
235 
OnSwitchChange(const int state)236 void MyLocationEngine::MySwitchCallback::OnSwitchChange(const int state)
237 {
238     if (locationUpdate_ == nullptr) {
239         return;
240     }
241     locationUpdate_->LocationSwitchChange();
242 }
243 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)244 int MyLocationEngine::MySwitchCallback::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
245     MessageOption &option)
246 {
247     if (data.ReadInterfaceToken() != GetDescriptor()) {
248         TELEPHONY_LOGI("MyLocationEngine SwitchCallback invalid token.");
249         return -1;
250     }
251     switch (code) {
252         case RECEIVE_SWITCH_STATE_EVENT: {
253             int32_t status = data.ReadInt32();
254             OnSwitchChange(status);
255             break;
256         }
257         default: {
258             TELEPHONY_LOGI("MyLocationEngine  receive error code:%{public}u", code);
259             break;
260         }
261     }
262     return 0;
263 }
264 
IsSwitchOn()265 bool MyLocationEngine::IsSwitchOn()
266 {
267     auto datashareHelper = std::make_shared<DataShareSwitchState>();
268     std::string switch_state = INITIAL_FIRST_VALUE;
269     int resp = datashareHelper->QueryData(MyLocationEngine::uri_q, LocationSubscriber::SWITCH_STATE_KEY, switch_state);
270     TELEPHONY_LOGI("MyLocationEngine switch_state = %{public}s ", switch_state.c_str());
271     if (resp == DataShareSwitchState::TELEPHONY_SUCCESS && switch_state == ALARM_SWITCH_ON) {
272         return true;
273     }
274     if (resp == DataShareSwitchState::TELEPHONY_SUCCESS && switch_state == ALARM_SWITCH_OFF) {
275         return false;
276     }
277     return false;
278 }
279 
280 sptr<AAFwk::IAbilityConnection> EmergencyCallConnectCallback::connectCallback_ = nullptr;
ConnectAbility()281 void MyLocationEngine::ConnectAbility()
282 {
283     AAFwk::Want want;
284     AppExecFwk::ElementName element(EMERGENCY_DEVICE_ID, EMERGENCY_BUNDLE_NAME, EMERGENCY_ABILITY_NAME);
285     want.SetElement(element);
286     want.SetParam(PARAMETERS_KEY, PARAMETERS_VALUE);
287     if (EmergencyCallConnectCallback::connectCallback_ == nullptr) {
288         EmergencyCallConnectCallback::connectCallback_ = new EmergencyCallConnectCallback();
289     }
290     int32_t userId = -1;
291     AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, EmergencyCallConnectCallback::connectCallback_,
292         userId);
293     TELEPHONY_LOGI("Connect emergencycommunication ability MyLocationEngine");
294 }
295 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)296 void EmergencyCallConnectCallback::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
297     const sptr<IRemoteObject> &remoteObject, int resultCode)
298 {
299     TELEPHONY_LOGI("connect callui result code: %{public}d", resultCode);
300     if (resultCode == CONNECT_SUCCESS) {
301         TELEPHONY_LOGI("connect emergencycommunication result success");
302     }
303 }
304 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)305 void EmergencyCallConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
306 {
307     TELEPHONY_LOGI("disconnect emergencycommunication result code: %{public}d", resultCode);
308 }
309 } // namespace Telephony
310 } // namespace OHOS