From: Marcin Masternak Date: Tue, 9 Aug 2016 11:31:50 +0000 (+0200) Subject: [my-place] User consent starts/stops UserPlaces engine. X-Git-Tag: submit/tizen/20160907.123648^2~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=137c6d9636174aaa915f89a53fe187dc8816217e;p=platform%2Fcore%2Fcontext%2Fcontext-provider.git [my-place] User consent starts/stops UserPlaces engine. Change-Id: I54e8525e127efa5713e415beebb99d8775805a8c Signed-off-by: Marcin Masternak --- diff --git a/src/my-place/PlaceRecognitionProvider.cpp b/src/my-place/PlaceRecognitionProvider.cpp index 02b4071..1d17e84 100644 --- a/src/my-place/PlaceRecognitionProvider.cpp +++ b/src/my-place/PlaceRecognitionProvider.cpp @@ -39,28 +39,32 @@ int ctx::PlaceRecognitionProvider::unsubscribe(ctx::Json option) int ctx::PlaceRecognitionProvider::read(ctx::Json option, ctx::Json* requestResult) { - _I(BLUE("Read")); + _D(BLUE("Read")); _J("Option", option); std::string request; Json dataRead; option.get(NULL, PLACE_DETECTION_REQUEST, &request); + bool consent = __engine.getConsent(); + int ret = ERR_NONE; if (request == PLACE_DETECTION_REQUEST_CONSENT) { - dataRead.set(NULL, PLACE_DETECTION_REQUEST_CONSENT, UserPlaces::getConsent() ? MYPLACE_SETTING_VALUE_TRUE : MYPLACE_SETTING_VALUE_FALSE); + dataRead.set(NULL, PLACE_DETECTION_REQUEST_CONSENT, consent ? MYPLACE_SETTING_VALUE_TRUE : MYPLACE_SETTING_VALUE_FALSE); } else if (request == PLACE_DETECTION_REQUEST_LIST) { - dataRead = UserPlaces::getPlaces(); + if (consent) + dataRead = UserPlaces::getPlaces(); + else + ret = ERR_NOT_STARTED; } else { - replyToRead(option, ERR_INVALID_PARAMETER, dataRead); - return ERR_INVALID_PARAMETER; + ret = ERR_INVALID_PARAMETER; } - replyToRead(option, ERR_NONE, dataRead); - return ERR_NONE; + replyToRead(option, ret, dataRead); + return ret; } int ctx::PlaceRecognitionProvider::write(ctx::Json data, ctx::Json* requestResult) { - _I(BLUE("Write")); + _D(BLUE("Write")); _J("Data", data); std::string value; @@ -70,9 +74,9 @@ int ctx::PlaceRecognitionProvider::write(ctx::Json data, ctx::Json* requestResul _E, "Invalid write request"); if (value == MYPLACE_SETTING_VALUE_TRUE) { - UserPlaces::setConsent(true); + __engine.setConsent(true); } else if (value == MYPLACE_SETTING_VALUE_FALSE) { - UserPlaces::setConsent(false); + __engine.setConsent(false); } else { _E("Invalid write parameter %s = '%s'", PLACE_DETECTION_REQUEST_CONSENT, value.c_str()); return ERR_INVALID_PARAMETER; diff --git a/src/my-place/facade/UserPlaces.cpp b/src/my-place/facade/UserPlaces.cpp index 0e72436..0c593b4 100755 --- a/src/my-place/facade/UserPlaces.cpp +++ b/src/my-place/facade/UserPlaces.cpp @@ -57,37 +57,19 @@ typedef void (*places_detector_t)(); ctx::UserPlaces::UserPlaces(PlaceRecogMode energyMode): __visitDetector(nullptr), - __timerId(-1) + __timerId(-1), + __started(false), + __energyMode(energyMode) { + _D(""); __dbCreateMyPlaceSettingsTable(); - bool userConsent = getConsent(); - time_t now = std::time(nullptr); - __visitDetector = new(std::nothrow) VisitDetector(now, energyMode); - if (__visitDetector == nullptr) { - _E("Cannot initialize __visitDetector"); - return; - } - - __timerId = __timerManager.setAt( // execute once every night - PLACES_DETECTOR_TASK_START_HOUR, - PLACES_DETECTOR_TASK_START_MINUTE, - DayOfWeek::EVERYDAY, - this); - if (__timerId < 0) { - _E("timer set FAIL"); - return; - } else { - _D("timer set SUCCESS"); - } + if (getConsent()) + __start(); } ctx::UserPlaces::~UserPlaces() { - if (__timerId >= 0) { - __timerManager.remove(__timerId); - } - if (__visitDetector) - delete __visitDetector; + __stop(); }; ctx::Json ctx::UserPlaces::getPlaces() @@ -131,6 +113,7 @@ ctx::Json ctx::UserPlaces::__composeJson(std::vector> pla void ctx::UserPlaces::setMode(PlaceRecogMode energyMode) { + __energyMode = energyMode; if (__visitDetector) __visitDetector->setMode(energyMode); } @@ -255,16 +238,22 @@ void ctx::UserPlaces::__dbCreateMyPlaceSettingsTable() void ctx::UserPlaces::setConsent(bool consent) { + _D(""); DatabaseManager dbManager; std::vector records; std::stringstream query; query << "REPLACE INTO " MYPLACE_SETTINGS_TABLE \ " ( " MYPLACE_SETTINGS_COLUMN_KEY ", " MYPLACE_SETTINGS_COLUMN_VALUE " )" \ " VALUES ( '" MYPLACE_SETTING_KEY_USER_CONSENT "', '"; - if (consent) + if (consent) { query << MYPLACE_SETTING_VALUE_TRUE; - else + if (!__started) + __start(); + } else { query << MYPLACE_SETTING_VALUE_FALSE; + if (__started) + __stop(); + } query << "' );"; bool ret = dbManager.executeSync(query.str().c_str(), &records); @@ -273,6 +262,7 @@ void ctx::UserPlaces::setConsent(bool consent) bool ctx::UserPlaces::getConsent() { + _D(""); DatabaseManager dbManager; std::vector records; bool ret = dbManager.executeSync(__GET_USER_CONSENT_QUERY, &records); @@ -281,8 +271,10 @@ bool ctx::UserPlaces::getConsent() std::string consentStr; records[0].get(NULL, MYPLACE_SETTINGS_COLUMN_VALUE, &consentStr); if (consentStr == MYPLACE_SETTING_VALUE_TRUE) { + _D("User consent value from db: TRUE"); return true; } else if (consentStr == MYPLACE_SETTING_VALUE_FALSE) { + _D("User consent value from db: FALSE"); return false; } else { _E("Unknown user consent value \"%s\" from db, it should be \"%s\" or \"%s\"", @@ -295,3 +287,40 @@ bool ctx::UserPlaces::getConsent() setConsent(MYPLACE_SETTING_USER_CONSENT_DEFAULT); return MYPLACE_SETTING_USER_CONSENT_DEFAULT; } + +void ctx::UserPlaces::__start() +{ + _D(""); + time_t now = std::time(nullptr); + __visitDetector = new(std::nothrow) VisitDetector(now, __energyMode); + if (__visitDetector == nullptr) { + _E("Cannot initialize VisitDetector"); + return; + } + __timerId = __timerManager.setAt( // execute once every night + PLACES_DETECTOR_TASK_START_HOUR, + PLACES_DETECTOR_TASK_START_MINUTE, + DayOfWeek::EVERYDAY, + this); + if (__timerId < 0) { + _E("timer set FAIL"); + return; + } else { + _D("timer set SUCCESS"); + } + __started = true; +} + +void ctx::UserPlaces::__stop() +{ + _D(""); + if (__timerId >= 0) { + __timerManager.remove(__timerId); + __timerId = -1; + } + if (__visitDetector) { + delete __visitDetector; + __visitDetector = nullptr; + } + __started = false; +} diff --git a/src/my-place/facade/UserPlaces.h b/src/my-place/facade/UserPlaces.h index 5cade8f..a760a5f 100644 --- a/src/my-place/facade/UserPlaces.h +++ b/src/my-place/facade/UserPlaces.h @@ -35,6 +35,8 @@ namespace ctx { VisitDetector *__visitDetector; int __timerId; TimerManager __timerManager; + bool __started; + PlaceRecogMode __energyMode; static std::vector __dbGetPlaces(); static std::map __dbGetWifiAPsMap(); static std::shared_ptr __placeFromJson(Json &row, std::map> __getPlaces(); static Json __composeJson(std::vector> places); static void __dbCreateMyPlaceSettingsTable(); + void __start(); + void __stop(); bool onTimerExpired(int timerId); @@ -59,8 +63,8 @@ namespace ctx { void setMode(PlaceRecogMode energyMode); static ctx::Json getPlaces(); - static void setConsent(bool consent); - static bool getConsent(); + void setConsent(bool consent); + bool getConsent(); }; /* class UserPlaces */ diff --git a/src/my-place/visit-detector/VisitDetector.cpp b/src/my-place/visit-detector/VisitDetector.cpp index cde448e..f8af125 100644 --- a/src/my-place/visit-detector/VisitDetector.cpp +++ b/src/my-place/visit-detector/VisitDetector.cpp @@ -219,7 +219,7 @@ void ctx::VisitDetector::__visitStartDetected() } __representativesMacs = __selectRepresentatives(__historyFrames); __entranceTime = __historyFrames[0]->interval.start; - _I("Entrance detected, timestamp: %d", __entranceTime); + _D("Entrance detected, timestamp: %d", __entranceTime); __resetHistory(); } @@ -230,7 +230,7 @@ void ctx::VisitDetector::__visitEndDetected() listener->onVisitEnd(); } } - _I("Departure detected, timestamp: %d", __departureTime); + _D("Departure detected, timestamp: %d", __departureTime); Interval interval(__entranceTime, __departureTime); Visit visit(interval, __representativesMacs);