2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <geofence_manager.h>
20 #include <types_internal.h>
21 #include "myplace_handle.h"
22 #include "place_geofence.h"
24 typedef std::map<int, ctx::myplace_handle*> handle_map_t;
25 static handle_map_t handle_map;
27 ctx::place_geofence_detector::place_geofence_detector()
31 ctx::place_geofence_detector::~place_geofence_detector()
33 for (handle_map_t::iterator it = handle_map.begin(); it != handle_map.end(); ++it) {
40 int ctx::place_geofence_detector::subscribe(ctx::json option, ctx::json* request_result, const char* zone)
43 option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
44 IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
46 handle_map_t::iterator it = handle_map.find(pid);
47 if (it != handle_map.end()) {
48 _D("Place ID %d is being monitored already", pid);
49 it->second->add_zone(zone);
53 myplace_handle *handle = new(std::nothrow) myplace_handle();
56 bool ret = handle->start_monitor(pid);
58 _E("Monitoring Place ID %d failed", pid);
60 return ERR_OPERATION_FAILED;
63 handle->add_zone(zone);
64 handle_map[pid] = handle;
69 int ctx::place_geofence_detector::unsubscribe(ctx::json option, const char* zone)
72 option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
73 IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
75 handle_map_t::iterator it = handle_map.find(pid);
76 if (it == handle_map.end()) {
77 _D("Place ID %d is not being monitored", pid);
81 it->second->remove_zone(zone);
82 if (it->second->zone_empty()) {
90 bool ctx::place_geofence_detector::is_supported(const char* zone)
92 bool supported = false;
94 int ret = geofence_manager_is_supported(&supported);
95 IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, false, _E, "geofence_manager_is_supported() failed");