From 1e697e4596f5a1541d9a4561be9fcb588b484f6b Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Mon, 16 May 2022 15:15:40 +0900 Subject: [PATCH 01/17] [geolocator] Refactor the C++ code - Remove unused header - Change header's macro name - Fix wrong file name - Use typedefs of long commonly-used types - Cleanup error message --- .../tizen/inc/geolocator_tizen_plugin.h | 4 + .../tizen/src/geolocator_tizen_plugin.cc | 121 ++++++++++-------- packages/geolocator/tizen/src/location.h | 6 +- ...locaton_manager.cc => location_manager.cc} | 3 +- .../{locaton_manager.h => location_manager.h} | 11 +- .../tizen/src/permission_manager.cc | 5 - .../geolocator/tizen/src/permission_manager.h | 11 +- packages/geolocator/tizen/src/setting.cc | 6 +- packages/geolocator/tizen/src/setting.h | 6 +- packages/geolocator/tizen/src/tizen_result.h | 6 +- 10 files changed, 96 insertions(+), 83 deletions(-) rename packages/geolocator/tizen/src/{locaton_manager.cc => location_manager.cc} (99%) rename packages/geolocator/tizen/src/{locaton_manager.h => location_manager.h} (86%) diff --git a/packages/geolocator/tizen/inc/geolocator_tizen_plugin.h b/packages/geolocator/tizen/inc/geolocator_tizen_plugin.h index ad06c03df..4cac3ff67 100644 --- a/packages/geolocator/tizen/inc/geolocator_tizen_plugin.h +++ b/packages/geolocator/tizen/inc/geolocator_tizen_plugin.h @@ -1,3 +1,7 @@ +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + #ifndef FLUTTER_PLUGIN_GEOLOCATOR_TIZEN_PLUGIN_H_ #define FLUTTER_PLUGIN_GEOLOCATOR_TIZEN_PLUGIN_H_ diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 0c5035012..17bf31f8d 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -11,21 +11,24 @@ #include #include -#include -#include - -#include "locaton_manager.h" +#include "location_manager.h" #include "log.h" #include "permission_manager.h" #include "setting.h" namespace { +typedef flutter::MethodChannel FlMethodChannel; +typedef flutter::EventChannel FlEventChannel; +typedef flutter::MethodCall FlMethodCall; +typedef flutter::EventSink FlEventSink; +typedef flutter::MethodResult FlMethodResult; + class GeolocatorTizenPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { auto channel = - std::make_unique>( + std::make_unique( registrar->messenger(), "flutter.baseflow.com/geolocator", &flutter::StandardMethodCodec::GetInstance()); @@ -35,8 +38,8 @@ class GeolocatorTizenPlugin : public flutter::Plugin { plugin->SetupGeolocatorUpdatesChannel(registrar->messenger()); channel->SetMethodCallHandler( - [plugin_pointer = plugin.get()](const auto &call, auto result) { - plugin_pointer->HandleMethodCall(call, std::move(result)); + [plugin_ptr = plugin.get()](const auto &call, auto result) { + plugin_ptr->HandleMethodCall(call, std::move(result)); }); registrar->AddPlugin(std::move(plugin)); @@ -52,7 +55,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { void SetupGeolocatorServiceUpdatesChannel( flutter::BinaryMessenger *messenger) { geolocator_service_updates_channel_ = - std::make_unique>( + std::make_unique( messenger, "flutter.baseflow.com/geolocator_service_updates", &flutter::StandardMethodCodec::GetInstance()); auto handler = std::make_unique>( @@ -72,7 +75,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { void SetupGeolocatorUpdatesChannel(flutter::BinaryMessenger *messenger) { geolocator_updates_channel_ = - std::make_unique>( + std::make_unique( messenger, "flutter.baseflow.com/geolocator_updates", &flutter::StandardMethodCodec::GetInstance()); auto handler = std::make_unique>( @@ -90,110 +93,100 @@ class GeolocatorTizenPlugin : public flutter::Plugin { geolocator_updates_channel_->SetStreamHandler(std::move(handler)); } + void HandleMethodCall( - const flutter::MethodCall &method_call, - std::unique_ptr> result) { + const FlMethodCall &method_call, + std::unique_ptr result) { std::string method_name = method_call.method_name(); + + result_ = std::move(result); + if (method_name == "checkPermission") { - OnCheckPermission(std::move(result)); + OnCheckPermission(); } else if (method_name == "isLocationServiceEnabled") { - OnIsLocationServiceEnabled(std::move(result)); + OnIsLocationServiceEnabled(); } else if (method_name == "requestPermission") { - OnRequestPermission(std::move(result)); + OnRequestPermission(); } else if (method_name == "getLastKnownPosition") { - OnGetLastKnownPosition(std::move(result)); + OnGetLastKnownPosition(); } else if (method_name == "getCurrentPosition") { - OnGetCurrentPosition(std::move(result)); + OnGetCurrentPosition(); } else if (method_name == "openAppSettings") { TizenResult ret = Setting::LaunchAppSetting(); - result->Success(flutter::EncodableValue(static_cast(ret))); + SendResult(flutter::EncodableValue(static_cast(ret))); } else if (method_name == "openLocationSettings") { TizenResult ret = Setting::LaunchLocationSetting(); - result->Success(flutter::EncodableValue(static_cast(ret))); + SendResult(flutter::EncodableValue(static_cast(ret))); } else { result->NotImplemented(); } } - void OnCheckPermission( - std::unique_ptr> result) { + void OnCheckPermission() { PermissionStatus permission_status; TizenResult ret = permission_manager_->CheckPermissionStatus(&permission_status); if (!ret) { - result->Error("Failed to check permssion status.", ret.message()); + SendErrorResult(ret.message(), "Failed to check permssion status."); return; } LOG_INFO("permission_status is %d", permission_status); - result->Success( + SendResult( flutter::EncodableValue(static_cast(permission_status))); } - void OnIsLocationServiceEnabled( - std::unique_ptr> result) { + void OnIsLocationServiceEnabled() { bool is_enabled = false; TizenResult ret = location_manager_->IsLocationServiceEnabled(&is_enabled); // TODO : add location service listener if (!ret) { - result->Error("Failed to check service enabled.", ret.message()); + SendErrorResult(ret.message(), "Failed to check service enabled."); } - result->Success(flutter::EncodableValue(is_enabled)); + SendResult(flutter::EncodableValue(is_enabled)); } - void OnRequestPermission( - std::unique_ptr> result) { - auto result_ptr = result.release(); + void OnRequestPermission() { + auto result_ptr = result_.get(); permission_manager_->RequestPermssion( [result_ptr](PermissionStatus permission_status) { result_ptr->Success( flutter::EncodableValue(static_cast(permission_status))); - delete result_ptr; }, [result_ptr](TizenResult tizen_result) { - result_ptr->Error("Failed to request permssion.", - tizen_result.message()); - delete result_ptr; + result_ptr->Error(tizen_result.message(), "Failed to request permssion."); }); } - void OnGetLastKnownPosition( - std::unique_ptr> result) { + void OnGetLastKnownPosition() { Location location; TizenResult tizen_result = location_manager_->GetLastKnownLocation(&location); if (!tizen_result) { - result->Error("Failed to get last known position.", - tizen_result.message()); + SendErrorResult(tizen_result.message(), "Failed to get last known position."); return; } - result->Success(location.ToEncodableValue()); + SendResult(location.ToEncodableValue()); } - void OnGetCurrentPosition( - std::unique_ptr> result) { - auto result_ptr = result.release(); + void OnGetCurrentPosition() { + auto result_ptr = result_.get(); TizenResult tizen_result = location_manager_->RequestCurrentLocationOnce( [result_ptr](Location location) { result_ptr->Success(location.ToEncodableValue()); - delete result_ptr; }, [result_ptr](TizenResult error) { - result_ptr->Error( - "An error occurred while requesting current location.", - error.message()); - delete result_ptr; + result_ptr->Error(error.message(), + "An error occurred while requesting current location."); }); if (!tizen_result) { - result_ptr->Error("Failed to call RequestCurrentLocationOnce.", - tizen_result.message()); - delete result_ptr; + SendErrorResult(tizen_result.message(), "Failed to call RequestCurrentLocationOnce."); } } void OnListenGeolocatorServiceUpdates( - std::unique_ptr> + std::unique_ptr &&event_sink) { geolocator_service_updates_event_sink_ = std::move(event_sink); TizenResult tizen_result = location_manager_->SetOnServiceStateChanged( @@ -214,7 +207,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnListenGeolocatorUpdates( - std::unique_ptr> + std::unique_ptr &&event_sink) { geolocator_updates_event_sink_ = std::move(event_sink); TizenResult tizen_result = @@ -233,15 +226,33 @@ class GeolocatorTizenPlugin : public flutter::Plugin { location_manager_->UnsetOnLocationUpdated(); } + void SendResult(const flutter::EncodableValue &result) { + if (!result_) { + return; + } + result_->Success(result); + result_ = nullptr; + } + + void SendErrorResult(const std::string &error_code, + const std::string &error_message) { + if (!result_) { + return; + } + result_->Error(error_code, error_message); + result_ = nullptr; + } + + std::unique_ptr result_; std::unique_ptr permission_manager_; std::unique_ptr location_manager_; - std::unique_ptr> + std::unique_ptr geolocator_service_updates_channel_; - std::unique_ptr> + std::unique_ptr geolocator_service_updates_event_sink_; - std::unique_ptr> + std::unique_ptr geolocator_updates_channel_; - std::unique_ptr> + std::unique_ptr geolocator_updates_event_sink_; }; diff --git a/packages/geolocator/tizen/src/location.h b/packages/geolocator/tizen/src/location.h index b9f2eda15..b9817c5b1 100644 --- a/packages/geolocator/tizen/src/location.h +++ b/packages/geolocator/tizen/src/location.h @@ -2,9 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef FLUTTER_PLUGIN_LOCATION_H_ +#define FLUTTER_PLUGIN_LOCATION_H_ + #include #include - #include // Defined in: @@ -50,3 +52,5 @@ struct Location { std::optional speed; std::optional speedAccuracy; }; + +#endif // FLUTTER_PLUGIN_LOCATION_H_ diff --git a/packages/geolocator/tizen/src/locaton_manager.cc b/packages/geolocator/tizen/src/location_manager.cc similarity index 99% rename from packages/geolocator/tizen/src/locaton_manager.cc rename to packages/geolocator/tizen/src/location_manager.cc index 9daf331a2..bc06bab09 100644 --- a/packages/geolocator/tizen/src/locaton_manager.cc +++ b/packages/geolocator/tizen/src/location_manager.cc @@ -2,8 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "locaton_manager.h" - +#include "location_manager.h" #include "log.h" LocationManager::LocationManager() { diff --git a/packages/geolocator/tizen/src/locaton_manager.h b/packages/geolocator/tizen/src/location_manager.h similarity index 86% rename from packages/geolocator/tizen/src/locaton_manager.h rename to packages/geolocator/tizen/src/location_manager.h index 12d14ba98..3006754bc 100644 --- a/packages/geolocator/tizen/src/locaton_manager.h +++ b/packages/geolocator/tizen/src/location_manager.h @@ -1,12 +1,11 @@ -// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef LOCATON_MANAGER_H_ -#define LOCATON_MANAGER_H_ +#ifndef FLUTTER_PLUGIN_LOCATION_MANAGER_H_ +#define FLUTTER_PLUGIN_LOCATION_MANAGER_H_ #include - #include #include "location.h" @@ -30,7 +29,7 @@ class LocationManager { TizenResult RequestCurrentLocationOnce(OnLocationUpdated on_success, OnError on_error); - TizenResult GetLastKnownLocation(Location* locaton); + TizenResult GetLastKnownLocation(Location* LOCATION); TizenResult SetOnServiceStateChanged( OnServiceStateChanged on_service_state_changed); @@ -57,4 +56,4 @@ class LocationManager { OnServiceStateChanged on_service_state_changed_; OnLocationUpdated on_location_updated_; }; -#endif // LOCATON_MANAGER_H_ +#endif // FLUTTER_PLUGIN_LOCATION_MANAGER_H_ diff --git a/packages/geolocator/tizen/src/permission_manager.cc b/packages/geolocator/tizen/src/permission_manager.cc index 3a70fdc5a..42cb66517 100644 --- a/packages/geolocator/tizen/src/permission_manager.cc +++ b/packages/geolocator/tizen/src/permission_manager.cc @@ -7,8 +7,6 @@ #include #include -#include - #include "log.h" namespace { @@ -23,9 +21,6 @@ struct PermissionResponse { } // namespace -PermissionManager::PermissionManager() {} -PermissionManager::~PermissionManager() {} - TizenResult PermissionManager::CheckPermissionStatus( PermissionStatus *permission_status) { ppm_check_result_e check_result; diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index 0a92465b8..39942b62b 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -2,11 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef PERMISSION_MANAGER_H_ -#define PERMISSION_MANAGER_H_ +#ifndef FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ +#define FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ #include -#include #include "tizen_result.h" @@ -24,8 +23,8 @@ using OnSuccess = std::function; using OnFailure = std::function; class PermissionManager { public: - PermissionManager(); - ~PermissionManager(); + PermissionManager() {}; + ~PermissionManager() {}; TizenResult CheckPermissionStatus(PermissionStatus *permission_status); @@ -33,4 +32,4 @@ class PermissionManager { const OnFailure &on_failure); }; -#endif // PERMISSION_MANAGER_H_ +#endif // FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ diff --git a/packages/geolocator/tizen/src/setting.cc b/packages/geolocator/tizen/src/setting.cc index 507984819..52b859daf 100644 --- a/packages/geolocator/tizen/src/setting.cc +++ b/packages/geolocator/tizen/src/setting.cc @@ -9,6 +9,8 @@ #include namespace { + constexpr char kSettingAppId[] = "com.samsung.clocksetting.apps"; + constexpr char kLocationSettingAppId[] = "com.samsung.setting-location"; class AppControl { public: @@ -79,7 +81,7 @@ TizenResult Setting::LaunchAppSetting() { PackageName name; AppControl app_ctrl; - app_ctrl.SetAppId("com.samsung.clocksetting.apps"); + app_ctrl.SetAppId(kSettingAppId); app_ctrl.AddExtraData("pkgId", name); return app_ctrl.SendLauchRequest(); @@ -88,7 +90,7 @@ TizenResult Setting::LaunchAppSetting() { TizenResult Setting::LaunchLocationSetting() { AppControl app_ctrl; - app_ctrl.SetAppId("com.samsung.setting-location"); + app_ctrl.SetAppId(kLocationSettingAppId); app_ctrl.SetOperation( "http://tizen.org/appcontrol/operation/configure/location"); diff --git a/packages/geolocator/tizen/src/setting.h b/packages/geolocator/tizen/src/setting.h index c9bbd6e1c..70fc598e5 100644 --- a/packages/geolocator/tizen/src/setting.h +++ b/packages/geolocator/tizen/src/setting.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef SETTING_H_ -#define SETTING_H_ +#ifndef FLUTTER_PLUGIN_SETTING_H_ +#define FLUTTER_PLUGIN_SETTING_H_ #include "tizen_result.h" @@ -14,4 +14,4 @@ TizenResult LaunchLocationSetting(); }; // namespace Setting -#endif +#endif // FLUTTER_PLUGIN_SETTING_H_ diff --git a/packages/geolocator/tizen/src/tizen_result.h b/packages/geolocator/tizen/src/tizen_result.h index 1841320b6..d164cd202 100644 --- a/packages/geolocator/tizen/src/tizen_result.h +++ b/packages/geolocator/tizen/src/tizen_result.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef TIZEN_RESULT_H_ -#define TIZEN_RESULT_H_ +#ifndef FLUTTER_PLUGIN_TIZEN_RESULT_H_ +#define FLUTTER_PLUGIN_TIZEN_RESULT_H_ #include @@ -21,4 +21,4 @@ struct TizenResult { int error_code = TIZEN_ERROR_NONE; }; -#endif // TIZEN_RESULT_H_ +#endif // FLUTTER_PLUGIN_TIZEN_RESULT_H_ From 6a9743576457fa642095181894b32dbe8b473a2b Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Mon, 16 May 2022 18:29:00 +0900 Subject: [PATCH 02/17] [geolocator] Clean up code foramt --- .../tizen/src/geolocator_tizen_plugin.cc | 59 ++++++++----------- packages/geolocator/tizen/src/location.h | 1 + .../geolocator/tizen/src/location_manager.cc | 1 + .../geolocator/tizen/src/location_manager.h | 1 + .../geolocator/tizen/src/permission_manager.h | 4 +- packages/geolocator/tizen/src/setting.cc | 4 +- packages/geolocator/tizen/src/setting.h | 2 +- 7 files changed, 34 insertions(+), 38 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 17bf31f8d..3d05f16d7 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -27,10 +27,9 @@ typedef flutter::MethodResult FlMethodResult; class GeolocatorTizenPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { - auto channel = - std::make_unique( - registrar->messenger(), "flutter.baseflow.com/geolocator", - &flutter::StandardMethodCodec::GetInstance()); + auto channel = std::make_unique( + registrar->messenger(), "flutter.baseflow.com/geolocator", + &flutter::StandardMethodCodec::GetInstance()); auto plugin = std::make_unique(); @@ -54,10 +53,9 @@ class GeolocatorTizenPlugin : public flutter::Plugin { private: void SetupGeolocatorServiceUpdatesChannel( flutter::BinaryMessenger *messenger) { - geolocator_service_updates_channel_ = - std::make_unique( - messenger, "flutter.baseflow.com/geolocator_service_updates", - &flutter::StandardMethodCodec::GetInstance()); + geolocator_service_updates_channel_ = std::make_unique( + messenger, "flutter.baseflow.com/geolocator_service_updates", + &flutter::StandardMethodCodec::GetInstance()); auto handler = std::make_unique>( [this](const flutter::EncodableValue *arguments, std::unique_ptr> &&events) @@ -74,10 +72,9 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void SetupGeolocatorUpdatesChannel(flutter::BinaryMessenger *messenger) { - geolocator_updates_channel_ = - std::make_unique( - messenger, "flutter.baseflow.com/geolocator_updates", - &flutter::StandardMethodCodec::GetInstance()); + geolocator_updates_channel_ = std::make_unique( + messenger, "flutter.baseflow.com/geolocator_updates", + &flutter::StandardMethodCodec::GetInstance()); auto handler = std::make_unique>( [this](const flutter::EncodableValue *arguments, std::unique_ptr> &&events) @@ -94,9 +91,8 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } - void HandleMethodCall( - const FlMethodCall &method_call, - std::unique_ptr result) { + void HandleMethodCall(const FlMethodCall &method_call, + std::unique_ptr result) { std::string method_name = method_call.method_name(); result_ = std::move(result); @@ -154,7 +150,8 @@ class GeolocatorTizenPlugin : public flutter::Plugin { flutter::EncodableValue(static_cast(permission_status))); }, [result_ptr](TizenResult tizen_result) { - result_ptr->Error(tizen_result.message(), "Failed to request permssion."); + result_ptr->Error(tizen_result.message(), + "Failed to request permssion."); }); } @@ -163,7 +160,8 @@ class GeolocatorTizenPlugin : public flutter::Plugin { TizenResult tizen_result = location_manager_->GetLastKnownLocation(&location); if (!tizen_result) { - SendErrorResult(tizen_result.message(), "Failed to get last known position."); + SendErrorResult(tizen_result.message(), + "Failed to get last known position."); return; } SendResult(location.ToEncodableValue()); @@ -176,18 +174,19 @@ class GeolocatorTizenPlugin : public flutter::Plugin { result_ptr->Success(location.ToEncodableValue()); }, [result_ptr](TizenResult error) { - result_ptr->Error(error.message(), - "An error occurred while requesting current location."); + result_ptr->Error( + error.message(), + "An error occurred while requesting current location."); }); if (!tizen_result) { - SendErrorResult(tizen_result.message(), "Failed to call RequestCurrentLocationOnce."); + SendErrorResult(tizen_result.message(), + "Failed to call RequestCurrentLocationOnce."); } } void OnListenGeolocatorServiceUpdates( - std::unique_ptr - &&event_sink) { + std::unique_ptr &&event_sink) { geolocator_service_updates_event_sink_ = std::move(event_sink); TizenResult tizen_result = location_manager_->SetOnServiceStateChanged( [this](ServiceState service_state) { @@ -206,9 +205,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { location_manager_->UnsetOnServiceStateChanged(); } - void OnListenGeolocatorUpdates( - std::unique_ptr - &&event_sink) { + void OnListenGeolocatorUpdates(std::unique_ptr &&event_sink) { geolocator_updates_event_sink_ = std::move(event_sink); TizenResult tizen_result = location_manager_->SetOnLocationUpdated([this](Location location) { @@ -246,14 +243,10 @@ class GeolocatorTizenPlugin : public flutter::Plugin { std::unique_ptr result_; std::unique_ptr permission_manager_; std::unique_ptr location_manager_; - std::unique_ptr - geolocator_service_updates_channel_; - std::unique_ptr - geolocator_service_updates_event_sink_; - std::unique_ptr - geolocator_updates_channel_; - std::unique_ptr - geolocator_updates_event_sink_; + std::unique_ptr geolocator_service_updates_channel_; + std::unique_ptr geolocator_service_updates_event_sink_; + std::unique_ptr geolocator_updates_channel_; + std::unique_ptr geolocator_updates_event_sink_; }; } // namespace diff --git a/packages/geolocator/tizen/src/location.h b/packages/geolocator/tizen/src/location.h index b9817c5b1..e30a8c6c9 100644 --- a/packages/geolocator/tizen/src/location.h +++ b/packages/geolocator/tizen/src/location.h @@ -7,6 +7,7 @@ #include #include + #include // Defined in: diff --git a/packages/geolocator/tizen/src/location_manager.cc b/packages/geolocator/tizen/src/location_manager.cc index bc06bab09..8b44609d4 100644 --- a/packages/geolocator/tizen/src/location_manager.cc +++ b/packages/geolocator/tizen/src/location_manager.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "location_manager.h" + #include "log.h" LocationManager::LocationManager() { diff --git a/packages/geolocator/tizen/src/location_manager.h b/packages/geolocator/tizen/src/location_manager.h index 3006754bc..63407a70b 100644 --- a/packages/geolocator/tizen/src/location_manager.h +++ b/packages/geolocator/tizen/src/location_manager.h @@ -6,6 +6,7 @@ #define FLUTTER_PLUGIN_LOCATION_MANAGER_H_ #include + #include #include "location.h" diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index 39942b62b..21c51a0c1 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -23,8 +23,8 @@ using OnSuccess = std::function; using OnFailure = std::function; class PermissionManager { public: - PermissionManager() {}; - ~PermissionManager() {}; + PermissionManager(){}; + ~PermissionManager(){}; TizenResult CheckPermissionStatus(PermissionStatus *permission_status); diff --git a/packages/geolocator/tizen/src/setting.cc b/packages/geolocator/tizen/src/setting.cc index 52b859daf..71b405d7b 100644 --- a/packages/geolocator/tizen/src/setting.cc +++ b/packages/geolocator/tizen/src/setting.cc @@ -9,8 +9,8 @@ #include namespace { - constexpr char kSettingAppId[] = "com.samsung.clocksetting.apps"; - constexpr char kLocationSettingAppId[] = "com.samsung.setting-location"; +constexpr char kSettingAppId[] = "com.samsung.clocksetting.apps"; +constexpr char kLocationSettingAppId[] = "com.samsung.setting-location"; class AppControl { public: diff --git a/packages/geolocator/tizen/src/setting.h b/packages/geolocator/tizen/src/setting.h index 70fc598e5..eb72f2de5 100644 --- a/packages/geolocator/tizen/src/setting.h +++ b/packages/geolocator/tizen/src/setting.h @@ -14,4 +14,4 @@ TizenResult LaunchLocationSetting(); }; // namespace Setting -#endif // FLUTTER_PLUGIN_SETTING_H_ +#endif // FLUTTER_PLUGIN_SETTING_H_ From 7cddeb6b86b3c8358bcafb2a1d64a3cb0d27f7e5 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Mon, 16 May 2022 18:41:07 +0900 Subject: [PATCH 03/17] [geolocator] Update ChangeLog --- packages/geolocator/CHANGELOG.md | 4 ++++ packages/geolocator/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/geolocator/CHANGELOG.md b/packages/geolocator/CHANGELOG.md index 342f20105..e5ff72c16 100644 --- a/packages/geolocator/CHANGELOG.md +++ b/packages/geolocator/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.3 + +* Refactor the C++ code + ## 1.0.2 * Update geolocator to 8.0.0 diff --git a/packages/geolocator/pubspec.yaml b/packages/geolocator/pubspec.yaml index 7cddfc1ca..82fa2d975 100644 --- a/packages/geolocator/pubspec.yaml +++ b/packages/geolocator/pubspec.yaml @@ -2,7 +2,7 @@ name: geolocator_tizen description: Geolocation plugin for Flutter. This plugin provides the Tizen implementation for the geolocator. homepage: https://github.com/flutter-tizen/plugins repository: https://github.com/flutter-tizen/plugins/tree/master/packages/geolocator -version: 1.0.2 +version: 1.0.3 flutter: plugin: From a08a6440d5495b91200d931ae235f97ce94e988c Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Mon, 16 May 2022 18:44:51 +0900 Subject: [PATCH 04/17] [geolocator] Fix code format --- packages/geolocator/tizen/src/geolocator_tizen_plugin.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 3d05f16d7..1e2ff6936 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -90,7 +90,6 @@ class GeolocatorTizenPlugin : public flutter::Plugin { geolocator_updates_channel_->SetStreamHandler(std::move(handler)); } - void HandleMethodCall(const FlMethodCall &method_call, std::unique_ptr result) { std::string method_name = method_call.method_name(); @@ -127,8 +126,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { return; } LOG_INFO("permission_status is %d", permission_status); - SendResult( - flutter::EncodableValue(static_cast(permission_status))); + SendResult(flutter::EncodableValue(static_cast(permission_status))); } void OnIsLocationServiceEnabled() { @@ -176,7 +174,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { [result_ptr](TizenResult error) { result_ptr->Error( error.message(), - "An error occurred while requesting current location."); + "An error occurred while requesting current location."); }); if (!tizen_result) { From 5fee8d58ac209c1e2a64f038d6dcd24c0e76e852 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Tue, 17 May 2022 17:04:05 +0900 Subject: [PATCH 05/17] [geolocator] Replace PermissionManager code --- packages/geolocator/CHANGELOG.md | 2 +- .../tizen/src/geolocator_tizen_plugin.cc | 82 ++++++++++---- .../tizen/src/permission_manager.cc | 103 +++++++++--------- .../geolocator/tizen/src/permission_manager.h | 29 ++--- 4 files changed, 123 insertions(+), 93 deletions(-) diff --git a/packages/geolocator/CHANGELOG.md b/packages/geolocator/CHANGELOG.md index e5ff72c16..f8e0e9b66 100644 --- a/packages/geolocator/CHANGELOG.md +++ b/packages/geolocator/CHANGELOG.md @@ -1,6 +1,6 @@ ## 1.0.3 -* Refactor the C++ code +* Refactor the C++ code. ## 1.0.2 diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 1e2ff6936..616392106 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -24,6 +24,18 @@ typedef flutter::MethodCall FlMethodCall; typedef flutter::EventSink FlEventSink; typedef flutter::MethodResult FlMethodResult; +constexpr char kPrivilegeLocation[] = "http://tizen.org/privilege/location"; + +// Keep in sync with the enum values implemented in: +// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/enums/location_permission.dart +// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_android/android/src/main/java/com/baseflow/geolocator/permission/LocationPermission.java +enum class LocationPermission { + kDenied = 0, + kDeniedForever = 1, + kWhileInUse = 2, + kAlways = 3, +}; + class GeolocatorTizenPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { @@ -37,8 +49,8 @@ class GeolocatorTizenPlugin : public flutter::Plugin { plugin->SetupGeolocatorUpdatesChannel(registrar->messenger()); channel->SetMethodCallHandler( - [plugin_ptr = plugin.get()](const auto &call, auto result) { - plugin_ptr->HandleMethodCall(call, std::move(result)); + [plugin_pointer = plugin.get()](const auto &call, auto result) { + plugin_pointer->HandleMethodCall(call, std::move(result)); }); registrar->AddPlugin(std::move(plugin)); @@ -118,15 +130,32 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnCheckPermission() { - PermissionStatus permission_status; - TizenResult ret = - permission_manager_->CheckPermissionStatus(&permission_status); - if (!ret) { - SendErrorResult(ret.message(), "Failed to check permssion status."); + PermissionStatus result = + permission_manager_->CheckPermission(kPrivilegeLocation); + + if (result == PermissionStatus::kDeny) { + SendErrorResult("Permission denied", "Permission denied by user."); return; + } else if (result == PermissionStatus::kError) { + SendErrorResult("Operation failed", "Failed to request permission."); + return; + } + LOG_INFO("permission_status is %d", result); + LocationPermission location_permission = + ChangePermissionStatustoLocationPermission(result); + SendResult(flutter::EncodableValue(static_cast(location_permission))); + } + + LocationPermission ChangePermissionStatustoLocationPermission( + PermissionStatus permission) { + switch (permission) { + case PermissionStatus::kDeny: + case PermissionStatus::kAsk: + return LocationPermission::kDenied; + case PermissionStatus::kAllow: + default: + return LocationPermission::kAlways; } - LOG_INFO("permission_status is %d", permission_status); - SendResult(flutter::EncodableValue(static_cast(permission_status))); } void OnIsLocationServiceEnabled() { @@ -141,16 +170,31 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnRequestPermission() { - auto result_ptr = result_.get(); - permission_manager_->RequestPermssion( - [result_ptr](PermissionStatus permission_status) { - result_ptr->Success( - flutter::EncodableValue(static_cast(permission_status))); - }, - [result_ptr](TizenResult tizen_result) { - result_ptr->Error(tizen_result.message(), - "Failed to request permssion."); - }); + PermissionResult result = + permission_manager_->RequestPermssion(kPrivilegeLocation); + if (result == PermissionResult::kDenyForever || + result == PermissionResult::kDenyOnce) { + SendErrorResult("Permission denied", "Permission denied by user."); + } else if (result == PermissionResult::kError) { + SendErrorResult("Operation failed", "Failed to request permission."); + return; + } + LocationPermission location_permission = + ChangePermissionResulttoLocationPermission(result); + SendResult(flutter::EncodableValue(static_cast(location_permission))); + } + + LocationPermission ChangePermissionResulttoLocationPermission( + PermissionResult permission) { + switch (permission) { + case PermissionResult::kDenyOnce: + return LocationPermission::kDenied; + case PermissionResult::kDenyForever: + return LocationPermission::kDeniedForever; + case PermissionResult::kAllowForever: + default: + return LocationPermission::kAlways; + } } void OnGetLastKnownPosition() { diff --git a/packages/geolocator/tizen/src/permission_manager.cc b/packages/geolocator/tizen/src/permission_manager.cc index 42cb66517..435b362f9 100644 --- a/packages/geolocator/tizen/src/permission_manager.cc +++ b/packages/geolocator/tizen/src/permission_manager.cc @@ -1,92 +1,87 @@ -// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "permission_manager.h" +#ifndef TV_PROFILE #include #include +#include +#endif #include "log.h" -namespace { - -constexpr char kPrivilegeLocation[] = "http://tizen.org/privilege/location"; - -struct PermissionResponse { - ppm_call_cause_e cause; - ppm_request_result_e result; - bool received = false; -}; - -} // namespace - -TizenResult PermissionManager::CheckPermissionStatus( - PermissionStatus *permission_status) { - ppm_check_result_e check_result; - - int result = ppm_check_permission(kPrivilegeLocation, &check_result); - if (result != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { - return TizenResult(result); +PermissionStatus PermissionManager::CheckPermission( + const std::string &privilege) { +#ifdef TV_PROFILE + return PermissionStatus::kAllow; +#else + ppm_check_result_e result; + int ret = ppm_check_permission(privilege.c_str(), &result); + if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { + LOG_ERROR("Permission check failed [%s]: %s", privilege.c_str(), + get_error_message(ret)); + return PermissionStatus::kError; } - switch (check_result) { - case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_DENY: - case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ASK: - *permission_status = PermissionStatus::kDenied; - break; + switch (result) { case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ALLOW: + return PermissionStatus::kAllow; + case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ASK: + return PermissionStatus::kAsk; + case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_DENY: default: - *permission_status = PermissionStatus::kAlways; - break; + return PermissionStatus::kDeny; } - return TizenResult(); +#endif } -void PermissionManager::RequestPermssion(const OnSuccess &on_success, - const OnFailure &on_failure) { - const char *permission = kPrivilegeLocation; - PermissionResponse response; +PermissionResult PermissionManager::RequestPermssion( + const std::string &privilege) { +#ifdef TV_PROFILE + return PermissionResult::kAllowForever; +#else + struct Response { + bool received = false; + ppm_call_cause_e cause; + ppm_request_result_e result; + } response; + int ret = ppm_request_permission( - permission, + privilege.c_str(), [](ppm_call_cause_e cause, ppm_request_result_e result, - const char *privilege, void *data) { - PermissionResponse *response = static_cast(data); + const char *privilege, void *user_data) { + auto *response = static_cast(user_data); + response->received = true; response->cause = cause; response->result = result; - response->received = true; }, &response); if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { - LOG_ERROR("Failed to call ppm_request_permission with [%s].", permission); - on_failure(TizenResult(ret)); - return; + LOG_ERROR("Permission request failed [%s]: %s", privilege.c_str(), + get_error_message(ret)); + return PermissionResult::kError; } - // Wait until ppm_request_permission is done. + // Wait until ppm_request_permission() completes with a response. while (!response.received) { ecore_main_loop_iterate(); } - if (response.cause != PRIVACY_PRIVILEGE_MANAGER_CALL_CAUSE_ANSWER) { - LOG_ERROR("permission[%s] request failed with an error.", permission); - on_failure(TizenResult(ret)); - return; + if (response.cause == PRIVACY_PRIVILEGE_MANAGER_CALL_CAUSE_ERROR) { + LOG_ERROR("Received an error response [%s].", privilege.c_str()); + return PermissionResult::kError; } switch (response.result) { case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_ALLOW_FOREVER: - on_success(PermissionStatus::kAlways); - break; - case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_ONCE: - on_success(PermissionStatus::kDenied); - break; + return PermissionResult::kAllowForever; case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_FOREVER: - on_success(PermissionStatus::kDeniedForever); - break; + return PermissionResult::kDenyForever; + case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_ONCE: default: - LOG_ERROR("Unknown ppm_request_result_e."); - on_failure(TizenResult(ret)); - break; + return PermissionResult::kDenyOnce; } +#endif // TV_PROFILE } diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index 21c51a0c1..b14322fa7 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -1,35 +1,26 @@ -// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ #define FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ -#include +#include -#include "tizen_result.h" +// The result of permission check. +enum class PermissionStatus { kAllow, kDeny, kAsk, kError }; -// Keep in sync with the enum values implemented in: -// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/enums/location_permission.dart -// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_android/android/src/main/java/com/baseflow/geolocator/permission/LocationPermission.java -enum class PermissionStatus { - kDenied = 0, - kDeniedForever = 1, - kWhileInUse = 2, - kAlways = 3, -}; +// The result of permission request. +enum class PermissionResult { kAllowForever, kDenyForever, kDenyOnce, kError }; -using OnSuccess = std::function; -using OnFailure = std::function; class PermissionManager { public: - PermissionManager(){}; - ~PermissionManager(){}; + PermissionManager() {} + ~PermissionManager() {} - TizenResult CheckPermissionStatus(PermissionStatus *permission_status); + PermissionStatus CheckPermission(const std::string &privilege); - void RequestPermssion(const OnSuccess &on_success, - const OnFailure &on_failure); + PermissionResult RequestPermssion(const std::string &privilege); }; #endif // FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ From 74e215f0a7637b1b23b26fec245972e57671b9e5 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Wed, 18 May 2022 09:41:30 +0900 Subject: [PATCH 06/17] [geolocator] Enhance app setting manager --- .../tizen/src/app_settings_manager.cc | 155 ++++++++++++++++++ .../tizen/src/app_settings_manager.h | 31 ++++ .../tizen/src/geolocator_tizen_plugin.cc | 43 ++--- packages/geolocator/tizen/src/setting.cc | 98 ----------- packages/geolocator/tizen/src/setting.h | 17 -- 5 files changed, 208 insertions(+), 136 deletions(-) create mode 100644 packages/geolocator/tizen/src/app_settings_manager.cc create mode 100644 packages/geolocator/tizen/src/app_settings_manager.h delete mode 100644 packages/geolocator/tizen/src/setting.cc delete mode 100644 packages/geolocator/tizen/src/setting.h diff --git a/packages/geolocator/tizen/src/app_settings_manager.cc b/packages/geolocator/tizen/src/app_settings_manager.cc new file mode 100644 index 000000000..a18d99399 --- /dev/null +++ b/packages/geolocator/tizen/src/app_settings_manager.cc @@ -0,0 +1,155 @@ +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "app_settings_manager.h" + +#include +#include + +#include "log.h" + +namespace { + +constexpr char kSettingAppId[] = "com.samsung.clocksetting.apps"; +constexpr char kLocationSettingAppId[] = "com.samsung.setting-location"; +constexpr char kLocationSettingOperationId[] = + "http://tizen.org/appcontrol/operation/configure/location"; + +std::string GetPackageName() { + char* app_id; + int ret = app_get_id(&app_id); + if (ret != APP_ERROR_NONE) { + LOG_ERROR("The app ID is not found."); + return ""; + } + + package_info_h package_info; + ret = package_info_create(app_id, &package_info); + free(app_id); + if (ret != PACKAGE_MANAGER_ERROR_NONE) { + LOG_ERROR("Failed to create a package info handle."); + return ""; + } + + char* package_name; + ret = package_info_get_package(package_info, &package_name); + package_info_destroy(package_info); + if (ret != PACKAGE_MANAGER_ERROR_NONE) { + LOG_ERROR("Failed to get the package name."); + return ""; + } + + std::string result = std::string(package_name); + free(package_name); + + return result; +} + +} // namespace + +bool AppSettingsManager::OpenAppSettings() { + std::string package_name = GetPackageName(); + if (package_name.empty()) { + return false; + } + + if (!CreateAppControl()) { + DestroyAppControl(); + return false; + } + + if (!SetAppId(kSettingAppId)) { + DestroyAppControl(); + return false; + } + + if (!AddExtraData(package_name)) { + DestroyAppControl(); + return false; + } + + if (!SendLauchRequest()) { + DestroyAppControl(); + return false; + } + + DestroyAppControl(); + return true; +} + +bool AppSettingsManager::OpenLocationSetting() { + if (!CreateAppControl()) { + DestroyAppControl(); + return false; + } + + if (!SetAppId(kLocationSettingAppId)) { + DestroyAppControl(); + return false; + } + + if (!SetOperation(kLocationSettingOperationId)) { + DestroyAppControl(); + return false; + } + + if (!SendLauchRequest()) { + DestroyAppControl(); + return false; + } + + DestroyAppControl(); + return true; +} + +bool AppSettingsManager::CreateAppControl() { + int ret = app_control_create(&app_control_); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to create an app control handle. (%s)", + get_error_message(ret)); + return false; + } + return true; +} + +bool AppSettingsManager::SetAppId(const char* app_id) { + int ret = app_control_set_app_id(app_control_, app_id); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to set an app ID. (%s)", get_error_message(ret)); + return false; + } + return true; +} + +bool AppSettingsManager::SetOperation(const char* operation) { + int ret = app_control_set_operation(app_control_, operation); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to set operation. (%s)", get_error_message(ret)); + return false; + } + return true; +} + +bool AppSettingsManager::AddExtraData(std::string package_name) { + int ret = + app_control_add_extra_data(app_control_, "pkgId", package_name.c_str()); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to add extra data. (%s)", get_error_message(ret)); + return false; + } + return true; +} + +bool AppSettingsManager::SendLauchRequest() { + int ret = app_control_send_launch_request(app_control_, nullptr, nullptr); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to send a launch request. (%s)", get_error_message(ret)); + return false; + } + return true; +} + +void AppSettingsManager::DestroyAppControl() { + app_control_destroy(app_control_); +} \ No newline at end of file diff --git a/packages/geolocator/tizen/src/app_settings_manager.h b/packages/geolocator/tizen/src/app_settings_manager.h new file mode 100644 index 000000000..3da661ba0 --- /dev/null +++ b/packages/geolocator/tizen/src/app_settings_manager.h @@ -0,0 +1,31 @@ +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ +#define FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ + +#include + +#include + +class AppSettingsManager { + public: + AppSettingsManager() : app_control_(nullptr) {} + ~AppSettingsManager() {} + + bool OpenAppSettings(); + bool OpenLocationSetting(); + + private: + bool CreateAppControl(); + bool SetAppId(const char* app_id); + bool SetOperation(const char* operation); + bool AddExtraData(std::string package_name); + bool SendLauchRequest(); + void DestroyAppControl(); + + app_control_h app_control_; +}; + +#endif // FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ \ No newline at end of file diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 616392106..9fc94883a 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -14,7 +14,7 @@ #include "location_manager.h" #include "log.h" #include "permission_manager.h" -#include "setting.h" +#include "app_settings_manager.h" namespace { @@ -65,7 +65,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { private: void SetupGeolocatorServiceUpdatesChannel( flutter::BinaryMessenger *messenger) { - geolocator_service_updates_channel_ = std::make_unique( + service_updates_channel_ = std::make_unique( messenger, "flutter.baseflow.com/geolocator_service_updates", &flutter::StandardMethodCodec::GetInstance()); auto handler = std::make_unique>( @@ -80,11 +80,11 @@ class GeolocatorTizenPlugin : public flutter::Plugin { OnCancelGeolocatorServiceUpdates(); return nullptr; }); - geolocator_service_updates_channel_->SetStreamHandler(std::move(handler)); + service_updates_channel_->SetStreamHandler(std::move(handler)); } void SetupGeolocatorUpdatesChannel(flutter::BinaryMessenger *messenger) { - geolocator_updates_channel_ = std::make_unique( + updates_channel_ = std::make_unique( messenger, "flutter.baseflow.com/geolocator_updates", &flutter::StandardMethodCodec::GetInstance()); auto handler = std::make_unique>( @@ -99,7 +99,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { OnCancelGeolocatorUpdates(); return nullptr; }); - geolocator_updates_channel_->SetStreamHandler(std::move(handler)); + updates_channel_->SetStreamHandler(std::move(handler)); } void HandleMethodCall(const FlMethodCall &method_call, @@ -119,11 +119,11 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } else if (method_name == "getCurrentPosition") { OnGetCurrentPosition(); } else if (method_name == "openAppSettings") { - TizenResult ret = Setting::LaunchAppSetting(); - SendResult(flutter::EncodableValue(static_cast(ret))); + bool ret = app_settings_manager_->OpenAppSettings(); + result->Success(flutter::EncodableValue(ret)); } else if (method_name == "openLocationSettings") { - TizenResult ret = Setting::LaunchLocationSetting(); - SendResult(flutter::EncodableValue(static_cast(ret))); + bool ret = app_settings_manager_->OpenLocationSetting(); + result->Success(flutter::EncodableValue(ret)); } else { result->NotImplemented(); } @@ -229,39 +229,39 @@ class GeolocatorTizenPlugin : public flutter::Plugin { void OnListenGeolocatorServiceUpdates( std::unique_ptr &&event_sink) { - geolocator_service_updates_event_sink_ = std::move(event_sink); + service_updates_event_sink_ = std::move(event_sink); TizenResult tizen_result = location_manager_->SetOnServiceStateChanged( [this](ServiceState service_state) { flutter::EncodableValue value(static_cast(service_state)); - geolocator_service_updates_event_sink_->Success(value); + service_updates_event_sink_->Success(value); }); if (!tizen_result) { LOG_ERROR("Failed to set OnServiceStateChanged, %s.", tizen_result.message().c_str()); - geolocator_service_updates_event_sink_ = nullptr; + service_updates_event_sink_ = nullptr; } } void OnCancelGeolocatorServiceUpdates() { - geolocator_service_updates_event_sink_ = nullptr; + service_updates_event_sink_ = nullptr; location_manager_->UnsetOnServiceStateChanged(); } void OnListenGeolocatorUpdates(std::unique_ptr &&event_sink) { - geolocator_updates_event_sink_ = std::move(event_sink); + updates_event_sink_ = std::move(event_sink); TizenResult tizen_result = location_manager_->SetOnLocationUpdated([this](Location location) { - geolocator_updates_event_sink_->Success(location.ToEncodableValue()); + updates_event_sink_->Success(location.ToEncodableValue()); }); if (!tizen_result) { LOG_ERROR("Failed to set OnLocationUpdated, %s.", tizen_result.message().c_str()); - geolocator_updates_event_sink_ = nullptr; + updates_event_sink_ = nullptr; } } void OnCancelGeolocatorUpdates() { - geolocator_updates_event_sink_ = nullptr; + updates_event_sink_ = nullptr; location_manager_->UnsetOnLocationUpdated(); } @@ -285,10 +285,11 @@ class GeolocatorTizenPlugin : public flutter::Plugin { std::unique_ptr result_; std::unique_ptr permission_manager_; std::unique_ptr location_manager_; - std::unique_ptr geolocator_service_updates_channel_; - std::unique_ptr geolocator_service_updates_event_sink_; - std::unique_ptr geolocator_updates_channel_; - std::unique_ptr geolocator_updates_event_sink_; + std::unique_ptr app_settings_manager_; + std::unique_ptr service_updates_channel_; + std::unique_ptr service_updates_event_sink_; + std::unique_ptr updates_channel_; + std::unique_ptr updates_event_sink_; }; } // namespace diff --git a/packages/geolocator/tizen/src/setting.cc b/packages/geolocator/tizen/src/setting.cc deleted file mode 100644 index 71b405d7b..000000000 --- a/packages/geolocator/tizen/src/setting.cc +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "setting.h" - -#include -#include -#include - -namespace { -constexpr char kSettingAppId[] = "com.samsung.clocksetting.apps"; -constexpr char kLocationSettingAppId[] = "com.samsung.setting-location"; - -class AppControl { - public: - AppControl() { app_control_create(&app_control_); } - - ~AppControl() { app_control_destroy(app_control_); } - - TizenResult AddExtraData(const std::string& key, const std::string& value) { - return app_control_add_extra_data(app_control_, key.c_str(), value.c_str()); - } - - TizenResult SetAppId(const std::string& id) { - return app_control_set_app_id(app_control_, id.c_str()); - } - - TizenResult SetOperation(const std::string& operation) { - return app_control_set_operation(app_control_, operation.c_str()); - } - - TizenResult SendLauchRequest() { - return app_control_send_launch_request(app_control_, nullptr, nullptr); - } - - private: - app_control_h app_control_{nullptr}; -}; - -class PackageName { - public: - PackageName() { Init(); } - - ~PackageName() {} - - operator std::string() const { return package_name_; } - - private: - void Init() { - char* id = nullptr; - std::string app_id; - if (app_get_id(&id) != 0) { - return; - } - - app_id = id; - free(id); - - char* package_name = nullptr; - package_info_h package_info = nullptr; - - package_info_create(app_id.c_str(), &package_info); - - if (package_info_get_package(package_info, &package_name) != 0) { - return; - } - - package_info_destroy(package_info); - package_name_ = package_name; - - free(package_name); - } - - std::string package_name_; -}; - -} // namespace - -TizenResult Setting::LaunchAppSetting() { - PackageName name; - AppControl app_ctrl; - - app_ctrl.SetAppId(kSettingAppId); - app_ctrl.AddExtraData("pkgId", name); - - return app_ctrl.SendLauchRequest(); -} - -TizenResult Setting::LaunchLocationSetting() { - AppControl app_ctrl; - - app_ctrl.SetAppId(kLocationSettingAppId); - app_ctrl.SetOperation( - "http://tizen.org/appcontrol/operation/configure/location"); - - return app_ctrl.SendLauchRequest(); -} diff --git a/packages/geolocator/tizen/src/setting.h b/packages/geolocator/tizen/src/setting.h deleted file mode 100644 index eb72f2de5..000000000 --- a/packages/geolocator/tizen/src/setting.h +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_PLUGIN_SETTING_H_ -#define FLUTTER_PLUGIN_SETTING_H_ - -#include "tizen_result.h" - -namespace Setting { - -TizenResult LaunchAppSetting(); -TizenResult LaunchLocationSetting(); - -}; // namespace Setting - -#endif // FLUTTER_PLUGIN_SETTING_H_ From d20a31fd7af380e54e80a28ff307bb1b9998ebf7 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Wed, 18 May 2022 10:47:37 +0900 Subject: [PATCH 07/17] [geolocator] Enhance log message --- .../tizen/src/geolocator_tizen_plugin.cc | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 9fc94883a..c4cb0795d 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -11,10 +11,10 @@ #include #include +#include "app_settings_manager.h" #include "location_manager.h" #include "log.h" #include "permission_manager.h" -#include "app_settings_manager.h" namespace { @@ -164,7 +164,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { // TODO : add location service listener if (!ret) { - SendErrorResult(ret.message(), "Failed to check service enabled."); + SendErrorResult("Operation failed", ret.message()); } SendResult(flutter::EncodableValue(is_enabled)); } @@ -202,8 +202,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { TizenResult tizen_result = location_manager_->GetLastKnownLocation(&location); if (!tizen_result) { - SendErrorResult(tizen_result.message(), - "Failed to get last known position."); + SendErrorResult("Operation failed", tizen_result.message()); return; } SendResult(location.ToEncodableValue()); @@ -216,14 +215,11 @@ class GeolocatorTizenPlugin : public flutter::Plugin { result_ptr->Success(location.ToEncodableValue()); }, [result_ptr](TizenResult error) { - result_ptr->Error( - error.message(), - "An error occurred while requesting current location."); + result_ptr->Error("Operation failed", error.message()); }); if (!tizen_result) { - SendErrorResult(tizen_result.message(), - "Failed to call RequestCurrentLocationOnce."); + SendErrorResult("Operation failed", tizen_result.message()); } } From 822e7e627f5a2ae8253ba45ed5efd59e0e7c8711 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Thu, 19 May 2022 13:19:05 +0900 Subject: [PATCH 08/17] [geolocator] Change PermissionStatus enum --- .../tizen/src/geolocator_tizen_plugin.cc | 58 +++---------------- .../tizen/src/permission_manager.cc | 28 ++++----- .../geolocator/tizen/src/permission_manager.h | 17 ++++-- 3 files changed, 33 insertions(+), 70 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index c4cb0795d..0d3462a38 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -26,16 +26,6 @@ typedef flutter::MethodResult FlMethodResult; constexpr char kPrivilegeLocation[] = "http://tizen.org/privilege/location"; -// Keep in sync with the enum values implemented in: -// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/enums/location_permission.dart -// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_android/android/src/main/java/com/baseflow/geolocator/permission/LocationPermission.java -enum class LocationPermission { - kDenied = 0, - kDeniedForever = 1, - kWhileInUse = 2, - kAlways = 3, -}; - class GeolocatorTizenPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { @@ -132,30 +122,12 @@ class GeolocatorTizenPlugin : public flutter::Plugin { void OnCheckPermission() { PermissionStatus result = permission_manager_->CheckPermission(kPrivilegeLocation); - - if (result == PermissionStatus::kDeny) { - SendErrorResult("Permission denied", "Permission denied by user."); - return; - } else if (result == PermissionStatus::kError) { + if (result == PermissionStatus::kError) { SendErrorResult("Operation failed", "Failed to request permission."); return; } LOG_INFO("permission_status is %d", result); - LocationPermission location_permission = - ChangePermissionStatustoLocationPermission(result); - SendResult(flutter::EncodableValue(static_cast(location_permission))); - } - - LocationPermission ChangePermissionStatustoLocationPermission( - PermissionStatus permission) { - switch (permission) { - case PermissionStatus::kDeny: - case PermissionStatus::kAsk: - return LocationPermission::kDenied; - case PermissionStatus::kAllow: - default: - return LocationPermission::kAlways; - } + SendResult(flutter::EncodableValue(static_cast(result))); } void OnIsLocationServiceEnabled() { @@ -170,31 +142,17 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnRequestPermission() { - PermissionResult result = + PermissionStatus result = permission_manager_->RequestPermssion(kPrivilegeLocation); - if (result == PermissionResult::kDenyForever || - result == PermissionResult::kDenyOnce) { + if (result == PermissionStatus::kDeniedForever || + result == PermissionStatus::kDenied) { SendErrorResult("Permission denied", "Permission denied by user."); - } else if (result == PermissionResult::kError) { + return; + } else if (result == PermissionStatus::kError) { SendErrorResult("Operation failed", "Failed to request permission."); return; } - LocationPermission location_permission = - ChangePermissionResulttoLocationPermission(result); - SendResult(flutter::EncodableValue(static_cast(location_permission))); - } - - LocationPermission ChangePermissionResulttoLocationPermission( - PermissionResult permission) { - switch (permission) { - case PermissionResult::kDenyOnce: - return LocationPermission::kDenied; - case PermissionResult::kDenyForever: - return LocationPermission::kDeniedForever; - case PermissionResult::kAllowForever: - default: - return LocationPermission::kAlways; - } + SendResult(flutter::EncodableValue(static_cast(result))); } void OnGetLastKnownPosition() { diff --git a/packages/geolocator/tizen/src/permission_manager.cc b/packages/geolocator/tizen/src/permission_manager.cc index 435b362f9..599db9261 100644 --- a/packages/geolocator/tizen/src/permission_manager.cc +++ b/packages/geolocator/tizen/src/permission_manager.cc @@ -15,7 +15,7 @@ PermissionStatus PermissionManager::CheckPermission( const std::string &privilege) { #ifdef TV_PROFILE - return PermissionStatus::kAllow; + return PermissionStatus::kAlways; #else ppm_check_result_e result; int ret = ppm_check_permission(privilege.c_str(), &result); @@ -26,21 +26,20 @@ PermissionStatus PermissionManager::CheckPermission( } switch (result) { - case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ALLOW: - return PermissionStatus::kAllow; - case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ASK: - return PermissionStatus::kAsk; case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_DENY: + case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ASK: + return PermissionStatus::kDenied; + case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ALLOW: default: - return PermissionStatus::kDeny; + return PermissionStatus::kAlways; } #endif } -PermissionResult PermissionManager::RequestPermssion( +PermissionStatus PermissionManager::RequestPermssion( const std::string &privilege) { #ifdef TV_PROFILE - return PermissionResult::kAllowForever; + return PermissionStatus::kAlways; #else struct Response { bool received = false; @@ -61,7 +60,7 @@ PermissionResult PermissionManager::RequestPermssion( if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { LOG_ERROR("Permission request failed [%s]: %s", privilege.c_str(), get_error_message(ret)); - return PermissionResult::kError; + return PermissionStatus::kError; } // Wait until ppm_request_permission() completes with a response. @@ -71,17 +70,18 @@ PermissionResult PermissionManager::RequestPermssion( if (response.cause == PRIVACY_PRIVILEGE_MANAGER_CALL_CAUSE_ERROR) { LOG_ERROR("Received an error response [%s].", privilege.c_str()); - return PermissionResult::kError; + return PermissionStatus::kError; } switch (response.result) { - case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_ALLOW_FOREVER: - return PermissionResult::kAllowForever; case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_FOREVER: - return PermissionResult::kDenyForever; + return PermissionStatus::kDeniedForever; case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_ONCE: + return PermissionStatus::kDenied; + case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_ALLOW_FOREVER: + return PermissionStatus::kAlways; default: - return PermissionResult::kDenyOnce; + return PermissionStatus::kError; } #endif // TV_PROFILE } diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index b14322fa7..d31c51f24 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -7,11 +7,16 @@ #include -// The result of permission check. -enum class PermissionStatus { kAllow, kDeny, kAsk, kError }; - -// The result of permission request. -enum class PermissionResult { kAllowForever, kDenyForever, kDenyOnce, kError }; +// Keep in sync with the enum values implemented in: +// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/enums/location_permission.dart +// https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_android/android/src/main/java/com/baseflow/geolocator/permission/LocationPermission.java +enum class PermissionStatus { + kDenied = 0, + kDeniedForever = 1, + kWhileInUse = 2, + kAlways = 3, + kError = 4, // Only use in geolocator_tizen +}; class PermissionManager { public: @@ -20,7 +25,7 @@ class PermissionManager { PermissionStatus CheckPermission(const std::string &privilege); - PermissionResult RequestPermssion(const std::string &privilege); + PermissionStatus RequestPermssion(const std::string &privilege); }; #endif // FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ From 8de89fb9e5059921a3eb768543548abfc64dc8a5 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Thu, 19 May 2022 13:53:04 +0900 Subject: [PATCH 09/17] [geolocator] Refactoring AppSettingsManager --- .../tizen/src/app_settings_manager.cc | 91 ++++++------------- .../tizen/src/app_settings_manager.h | 16 +--- .../tizen/src/geolocator_tizen_plugin.cc | 2 +- 3 files changed, 33 insertions(+), 76 deletions(-) diff --git a/packages/geolocator/tizen/src/app_settings_manager.cc b/packages/geolocator/tizen/src/app_settings_manager.cc index a18d99399..c8efbe405 100644 --- a/packages/geolocator/tizen/src/app_settings_manager.cc +++ b/packages/geolocator/tizen/src/app_settings_manager.cc @@ -54,102 +54,69 @@ bool AppSettingsManager::OpenAppSettings() { return false; } - if (!CreateAppControl()) { - DestroyAppControl(); + app_control_h app_control; + int ret = app_control_create(&app_control); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to create an app control handle."); return false; } - if (!SetAppId(kSettingAppId)) { - DestroyAppControl(); + ret = app_control_set_app_id(app_control, kSettingAppId); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to set an app ID."); + app_control_destroy(app_control); return false; } - if (!AddExtraData(package_name)) { - DestroyAppControl(); + ret = app_control_add_extra_data(app_control, "pkgId", package_name.c_str()); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to add extra data."); + app_control_destroy(app_control); return false; } - if (!SendLauchRequest()) { - DestroyAppControl(); + ret = app_control_send_launch_request(app_control, nullptr, nullptr); + app_control_destroy(app_control); + if (ret != APP_CONTROL_ERROR_NONE) { + LOG_ERROR("Failed to send a launch request."); return false; } - DestroyAppControl(); return true; } -bool AppSettingsManager::OpenLocationSetting() { - if (!CreateAppControl()) { - DestroyAppControl(); - return false; - } - - if (!SetAppId(kLocationSettingAppId)) { - DestroyAppControl(); - return false; - } - - if (!SetOperation(kLocationSettingOperationId)) { - DestroyAppControl(); - return false; - } - - if (!SendLauchRequest()) { - DestroyAppControl(); +bool AppSettingsManager::OpenLocationSettings() { + std::string package_name = GetPackageName(); + if (package_name.empty()) { return false; } - DestroyAppControl(); - return true; -} - -bool AppSettingsManager::CreateAppControl() { - int ret = app_control_create(&app_control_); + app_control_h app_control; + int ret = app_control_create(&app_control); if (ret != APP_CONTROL_ERROR_NONE) { - LOG_ERROR("Failed to create an app control handle. (%s)", - get_error_message(ret)); + LOG_ERROR("Failed to create an app control handle."); return false; } - return true; -} -bool AppSettingsManager::SetAppId(const char* app_id) { - int ret = app_control_set_app_id(app_control_, app_id); + ret = app_control_set_app_id(app_control, kLocationSettingAppId); if (ret != APP_CONTROL_ERROR_NONE) { - LOG_ERROR("Failed to set an app ID. (%s)", get_error_message(ret)); + LOG_ERROR("Failed to set an app ID."); + app_control_destroy(app_control); return false; } - return true; -} -bool AppSettingsManager::SetOperation(const char* operation) { - int ret = app_control_set_operation(app_control_, operation); + ret = app_control_set_operation(app_control, kLocationSettingOperationId); if (ret != APP_CONTROL_ERROR_NONE) { LOG_ERROR("Failed to set operation. (%s)", get_error_message(ret)); return false; } - return true; -} -bool AppSettingsManager::AddExtraData(std::string package_name) { - int ret = - app_control_add_extra_data(app_control_, "pkgId", package_name.c_str()); + ret = app_control_send_launch_request(app_control, nullptr, nullptr); + app_control_destroy(app_control); if (ret != APP_CONTROL_ERROR_NONE) { - LOG_ERROR("Failed to add extra data. (%s)", get_error_message(ret)); + LOG_ERROR("Failed to send a launch request."); return false; } - return true; -} -bool AppSettingsManager::SendLauchRequest() { - int ret = app_control_send_launch_request(app_control_, nullptr, nullptr); - if (ret != APP_CONTROL_ERROR_NONE) { - LOG_ERROR("Failed to send a launch request. (%s)", get_error_message(ret)); - return false; - } return true; } - -void AppSettingsManager::DestroyAppControl() { - app_control_destroy(app_control_); -} \ No newline at end of file diff --git a/packages/geolocator/tizen/src/app_settings_manager.h b/packages/geolocator/tizen/src/app_settings_manager.h index 3da661ba0..9c29a3639 100644 --- a/packages/geolocator/tizen/src/app_settings_manager.h +++ b/packages/geolocator/tizen/src/app_settings_manager.h @@ -11,21 +11,11 @@ class AppSettingsManager { public: - AppSettingsManager() : app_control_(nullptr) {} + AppSettingsManager() {} ~AppSettingsManager() {} bool OpenAppSettings(); - bool OpenLocationSetting(); - - private: - bool CreateAppControl(); - bool SetAppId(const char* app_id); - bool SetOperation(const char* operation); - bool AddExtraData(std::string package_name); - bool SendLauchRequest(); - void DestroyAppControl(); - - app_control_h app_control_; + bool OpenLocationSettings(); }; -#endif // FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ \ No newline at end of file +#endif // FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 0d3462a38..bd64e1a7b 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -112,7 +112,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { bool ret = app_settings_manager_->OpenAppSettings(); result->Success(flutter::EncodableValue(ret)); } else if (method_name == "openLocationSettings") { - bool ret = app_settings_manager_->OpenLocationSetting(); + bool ret = app_settings_manager_->OpenLocationSettings(); result->Success(flutter::EncodableValue(ret)); } else { result->NotImplemented(); From 7996eaa859a45d389d7c026a2fd0d5d1761881d4 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Fri, 20 May 2022 11:23:17 +0900 Subject: [PATCH 10/17] [geolocator] Refactor LocationManager --- .../tizen/src/geolocator_tizen_plugin.cc | 216 +++++++++--------- .../geolocator/tizen/src/location_manager.cc | 204 +++++++++-------- .../geolocator/tizen/src/location_manager.h | 56 +++-- .../tizen/src/{location.h => position.h} | 8 +- packages/geolocator/tizen/src/tizen_result.h | 24 -- 5 files changed, 260 insertions(+), 248 deletions(-) rename packages/geolocator/tizen/src/{location.h => position.h} (92%) delete mode 100644 packages/geolocator/tizen/src/tizen_result.h diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index bd64e1a7b..c9178d802 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -23,9 +23,89 @@ typedef flutter::EventChannel FlEventChannel; typedef flutter::MethodCall FlMethodCall; typedef flutter::EventSink FlEventSink; typedef flutter::MethodResult FlMethodResult; +typedef flutter::StreamHandler FlStreamHandler; +typedef flutter::StreamHandlerError + FlStreamHandlerError; constexpr char kPrivilegeLocation[] = "http://tizen.org/privilege/location"; +class LocationUpdatesStreamHandler : public FlStreamHandler { + protected: + std::unique_ptr OnListenInternal( + const flutter::EncodableValue *arguments, + std::unique_ptr &&events) override { + events_ = std::move(events); + LocationCallback callback = [this](Position position) -> void { + events_->Success(position.ToEncodableValue()); + }; + if (!location_manager_.StartLocationUpdatesListen(callback)) { + return std::make_unique( + std::to_string(location_manager_.GetLastError()), + location_manager_.GetLastErrorString(), nullptr); + } + return nullptr; + } + + std::unique_ptr OnCancelInternal( + const flutter::EncodableValue *arguments) override { + if (!location_manager_.StopLocationUpdatesListen()) { + return std::make_unique( + std::to_string(location_manager_.GetLastError()), + location_manager_.GetLastErrorString(), nullptr); + } + events_.reset(); + return nullptr; + } + + private: + LocationManager location_manager_; + std::unique_ptr events_; +}; + +std::string ServiceStatusToString(ServiceStatus status) { + switch (status) { + case ServiceStatus::kEnabled: + return "enabled"; + case ServiceStatus::kDisabled: + return "disabled"; + default: + return "unknown"; + } +} + +class ServiceUpdatesStreamHandler : public FlStreamHandler { + protected: + std::unique_ptr OnListenInternal( + const flutter::EncodableValue *arguments, + std::unique_ptr &&events) override { + events_ = std::move(events); + ServiceStatusCallback callback = [this](ServiceStatus status) -> void { + events_->Success(flutter::EncodableValue(ServiceStatusToString(status))); + }; + if (!location_manager_.StartServiceUpdatedListen(callback)) { + return std::make_unique( + std::to_string(location_manager_.GetLastError()), + location_manager_.GetLastErrorString(), nullptr); + } + return nullptr; + } + + std::unique_ptr OnCancelInternal( + const flutter::EncodableValue *arguments) override { + if (!location_manager_.StopServiceUpdatedListen()) { + return std::make_unique( + std::to_string(location_manager_.GetLastError()), + location_manager_.GetLastErrorString(), nullptr); + } + events_.reset(); + return nullptr; + } + + private: + LocationManager location_manager_; + std::unique_ptr events_; +}; + class GeolocatorTizenPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { @@ -35,8 +115,18 @@ class GeolocatorTizenPlugin : public flutter::Plugin { auto plugin = std::make_unique(); - plugin->SetupGeolocatorServiceUpdatesChannel(registrar->messenger()); - plugin->SetupGeolocatorUpdatesChannel(registrar->messenger()); + plugin->service_updates_channel_ = std::make_unique( + registrar->messenger(), + "flutter.baseflow.com/geolocator_service_updates", + &flutter::StandardMethodCodec::GetInstance()); + plugin->service_updates_channel_->SetStreamHandler( + std::make_unique()); + + plugin->updates_channel_ = std::make_unique( + registrar->messenger(), "flutter.baseflow.com/geolocator_updates", + &flutter::StandardMethodCodec::GetInstance()); + plugin->updates_channel_->SetStreamHandler( + std::make_unique()); channel->SetMethodCallHandler( [plugin_pointer = plugin.get()](const auto &call, auto result) { @@ -53,45 +143,6 @@ class GeolocatorTizenPlugin : public flutter::Plugin { virtual ~GeolocatorTizenPlugin() {} private: - void SetupGeolocatorServiceUpdatesChannel( - flutter::BinaryMessenger *messenger) { - service_updates_channel_ = std::make_unique( - messenger, "flutter.baseflow.com/geolocator_service_updates", - &flutter::StandardMethodCodec::GetInstance()); - auto handler = std::make_unique>( - [this](const flutter::EncodableValue *arguments, - std::unique_ptr> &&events) - -> std::unique_ptr> { - OnListenGeolocatorServiceUpdates(std::move(events)); - return nullptr; - }, - [this](const flutter::EncodableValue *arguments) - -> std::unique_ptr> { - OnCancelGeolocatorServiceUpdates(); - return nullptr; - }); - service_updates_channel_->SetStreamHandler(std::move(handler)); - } - - void SetupGeolocatorUpdatesChannel(flutter::BinaryMessenger *messenger) { - updates_channel_ = std::make_unique( - messenger, "flutter.baseflow.com/geolocator_updates", - &flutter::StandardMethodCodec::GetInstance()); - auto handler = std::make_unique>( - [this](const flutter::EncodableValue *arguments, - std::unique_ptr> &&events) - -> std::unique_ptr> { - OnListenGeolocatorUpdates(std::move(events)); - return nullptr; - }, - [this](const flutter::EncodableValue *arguments) - -> std::unique_ptr> { - OnCancelGeolocatorUpdates(); - return nullptr; - }); - updates_channel_->SetStreamHandler(std::move(handler)); - } - void HandleMethodCall(const FlMethodCall &method_call, std::unique_ptr result) { std::string method_name = method_call.method_name(); @@ -131,14 +182,13 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnIsLocationServiceEnabled() { - bool is_enabled = false; - TizenResult ret = location_manager_->IsLocationServiceEnabled(&is_enabled); - - // TODO : add location service listener - if (!ret) { - SendErrorResult("Operation failed", ret.message()); + try { + bool is_enabled = location_manager_->IsLocationServiceEnabled(); + SendResult(flutter::EncodableValue(is_enabled)); + } catch (const LocationManagerError &error) { + // TODO : add location service listener + SendErrorResult("Operation failed", error.GetErrorString()); } - SendResult(flutter::EncodableValue(is_enabled)); } void OnRequestPermission() { @@ -156,69 +206,29 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnGetLastKnownPosition() { - Location location; - TizenResult tizen_result = - location_manager_->GetLastKnownLocation(&location); - if (!tizen_result) { - SendErrorResult("Operation failed", tizen_result.message()); - return; + try { + Position position = location_manager_->GetLastKnownPosition(); + SendResult(position.ToEncodableValue()); + } catch (const LocationManagerError &error) { + SendErrorResult("Operation failed", error.GetErrorString()); } - SendResult(location.ToEncodableValue()); } void OnGetCurrentPosition() { auto result_ptr = result_.get(); - TizenResult tizen_result = location_manager_->RequestCurrentLocationOnce( - [result_ptr](Location location) { - result_ptr->Success(location.ToEncodableValue()); - }, - [result_ptr](TizenResult error) { - result_ptr->Error("Operation failed", error.message()); - }); - - if (!tizen_result) { - SendErrorResult("Operation failed", tizen_result.message()); + try { + location_manager_->GetCurrentPosition( + [result_ptr](Position position) { + result_ptr->Success(position.ToEncodableValue()); + }, + [result_ptr](LocationManagerError error) { + result_ptr->Error("Operation failed", error.GetErrorString()); + }); + } catch (const LocationManagerError &error) { + SendErrorResult("Operation failed", error.GetErrorString()); } } - void OnListenGeolocatorServiceUpdates( - std::unique_ptr &&event_sink) { - service_updates_event_sink_ = std::move(event_sink); - TizenResult tizen_result = location_manager_->SetOnServiceStateChanged( - [this](ServiceState service_state) { - flutter::EncodableValue value(static_cast(service_state)); - service_updates_event_sink_->Success(value); - }); - if (!tizen_result) { - LOG_ERROR("Failed to set OnServiceStateChanged, %s.", - tizen_result.message().c_str()); - service_updates_event_sink_ = nullptr; - } - } - - void OnCancelGeolocatorServiceUpdates() { - service_updates_event_sink_ = nullptr; - location_manager_->UnsetOnServiceStateChanged(); - } - - void OnListenGeolocatorUpdates(std::unique_ptr &&event_sink) { - updates_event_sink_ = std::move(event_sink); - TizenResult tizen_result = - location_manager_->SetOnLocationUpdated([this](Location location) { - updates_event_sink_->Success(location.ToEncodableValue()); - }); - if (!tizen_result) { - LOG_ERROR("Failed to set OnLocationUpdated, %s.", - tizen_result.message().c_str()); - updates_event_sink_ = nullptr; - } - } - - void OnCancelGeolocatorUpdates() { - updates_event_sink_ = nullptr; - location_manager_->UnsetOnLocationUpdated(); - } - void SendResult(const flutter::EncodableValue &result) { if (!result_) { return; diff --git a/packages/geolocator/tizen/src/location_manager.cc b/packages/geolocator/tizen/src/location_manager.cc index 8b44609d4..2fc2a76bc 100644 --- a/packages/geolocator/tizen/src/location_manager.cc +++ b/packages/geolocator/tizen/src/location_manager.cc @@ -7,68 +7,34 @@ #include "log.h" LocationManager::LocationManager() { - CreateLocationManager(&manager_); - CreateLocationManager(&manager_for_current_location_); + location_manager_create(LOCATIONS_METHOD_HYBRID, &manager_); + location_manager_create(LOCATIONS_METHOD_HYBRID, + &manager_for_current_location_); } LocationManager::~LocationManager() { - DestroyLocationManager(manager_); - DestroyLocationManager(manager_for_current_location_); + location_manager_destroy(manager_); + location_manager_destroy(manager_for_current_location_); } -TizenResult LocationManager::IsLocationServiceEnabled(bool *is_enabled) { +bool LocationManager::IsLocationServiceEnabled() { bool gps_enabled = false; int ret_gps = location_manager_is_enabled_method(LOCATIONS_METHOD_GPS, &gps_enabled); - if (ret_gps != LOCATIONS_ERROR_NONE) { - return TizenResult(ret_gps); + if (LOCATIONS_ERROR_NONE != ret_gps) { + throw LocationManagerError(ret_gps); } bool wps_enabled = false; int ret_wps = location_manager_is_enabled_method(LOCATIONS_METHOD_WPS, &wps_enabled); - if (ret_wps != LOCATIONS_ERROR_NONE) { - return TizenResult(ret_wps); + if (LOCATIONS_ERROR_NONE != ret_wps) { + throw LocationManagerError(ret_wps); } - - *is_enabled = gps_enabled || wps_enabled; - - return TizenResult(); + return gps_enabled || wps_enabled; } -TizenResult LocationManager::RequestCurrentLocationOnce( - OnLocationUpdated on_success, OnError on_error) { - on_success_ = on_success; - on_error_ = on_error; - TizenResult ret = location_manager_request_single_location( - manager_for_current_location_, 5, - [](location_error_e error, double latitude, double longitude, - double altitude, time_t timestamp, double speed, double direction, - double climb, void *user_data) { - LocationManager *self = static_cast(user_data); - - if (error != LOCATIONS_ERROR_NONE && self->on_error_) { - self->on_error_(error); - } else if (self->on_success_) { - Location location; - location.latitude = latitude; - location.longitude = longitude; - location.altitude = altitude; - location.timestamp = timestamp; - location.speed = speed; - location.heading = direction; - - self->on_success_(location); - } - - self->on_error_ = nullptr; - self->on_success_ = nullptr; - }, - this); - return ret; -} - -TizenResult LocationManager::GetLastKnownLocation(Location *location) { +Position LocationManager::GetLastKnownPosition() { double altitude = 0.0; double latitude = 0.0; double longitude = 0.0; @@ -80,81 +46,133 @@ TizenResult LocationManager::GetLastKnownLocation(Location *location) { location_accuracy_level_e level = LOCATIONS_ACCURACY_NONE; time_t timestamp = 0; - TizenResult ret = location_manager_get_last_location( + int ret = location_manager_get_last_location( manager_, &altitude, &latitude, &longitude, &climb, &direction, &speed, &level, &horizontal, &vertical, ×tamp); - if (!ret) { - return ret; + if (LOCATIONS_ERROR_NONE != ret) { + throw LocationManagerError(ret); } + Position position; + position.longitude = longitude; + position.latitude = latitude; + position.timestamp = timestamp; + position.accuracy = horizontal; + position.altitude = altitude; + position.heading = direction; + position.speed = speed; + + return position; +} + +void LocationManager::GetCurrentPosition( + LocationCallback location_callback, + LocationErrorListener location_error_listener) { + location_callback_ = location_callback; + location_error_listener_ = location_error_listener; + int ret = location_manager_request_single_location( + manager_for_current_location_, 5, + [](location_error_e error, double latitude, double longitude, + double altitude, time_t timestamp, double speed, double direction, + double climb, void *user_data) { + LocationManager *self = static_cast(user_data); - location->longitude = longitude; - location->latitude = latitude; - location->timestamp = timestamp; - location->accuracy = horizontal; - location->altitude = altitude; - location->heading = direction; - location->speed = speed; + if (error != LOCATIONS_ERROR_NONE && self->location_error_listener_) { + self->location_error_listener_(LocationManagerError(error)); + } else if (self->location_callback_) { + Position position; + position.latitude = latitude; + position.longitude = longitude; + position.altitude = altitude; + position.timestamp = timestamp; + position.speed = speed; + position.heading = direction; + + self->location_callback_(position); + } - return ret; + self->location_callback_ = nullptr; + self->location_error_listener_ = nullptr; + }, + this); + if (LOCATIONS_ERROR_NONE != ret) { + throw LocationManagerError(ret); + } } -TizenResult LocationManager::SetOnServiceStateChanged( - OnServiceStateChanged on_service_state_changed) { - on_service_state_changed_ = on_service_state_changed; - return location_manager_set_service_state_changed_cb( +bool LocationManager::StartServiceUpdatedListen( + ServiceStatusCallback service_status_updated_callback) { + service_status_updated_callback_ = service_status_updated_callback; + int ret = location_manager_set_service_state_changed_cb( manager_, [](location_service_state_e state, void *user_data) { LocationManager *self = static_cast(user_data); - if (self->on_service_state_changed_) { - ServiceState service_state = ServiceState::kDisabled; + if (self->service_status_updated_callback_) { + ServiceStatus service_status = ServiceStatus::kDisabled; if (state == LOCATIONS_SERVICE_ENABLED) { - service_state = ServiceState::kEnabled; + service_status = ServiceStatus::kEnabled; } - self->on_service_state_changed_(service_state); + self->service_status_updated_callback_(service_status); } }, this); + if (LOCATIONS_ERROR_NONE != ret) { + last_error_ = ret; + return false; + } + return true; } -TizenResult LocationManager::UnsetOnServiceStateChanged() { - return location_manager_unset_service_state_changed_cb(manager_); +bool LocationManager::StopServiceUpdatedListen() { + int ret = location_manager_unset_service_state_changed_cb(manager_); + if (LOCATIONS_ERROR_NONE != ret) { + last_error_ = ret; + return false; + } + return true; } -TizenResult LocationManager::SetOnLocationUpdated( - OnLocationUpdated on_location_updated) { - on_location_updated_ = on_location_updated; - TizenResult result = location_manager_set_position_updated_cb( +bool LocationManager::StartLocationUpdatesListen( + LocationCallback location_updated_callback) { + location_updated_callback_ = location_updated_callback; + + int ret = location_manager_set_position_updated_cb( manager_, [](double latitude, double longitude, double altitude, time_t timestamp, void *user_data) { LocationManager *self = static_cast(user_data); - if (self->on_location_updated_) { - Location location; - location.longitude = longitude; - location.latitude = latitude; - location.timestamp = timestamp; - location.altitude = altitude; - self->on_location_updated_(location); + if (self->location_updated_callback_) { + Position position; + position.longitude = longitude; + position.latitude = latitude; + position.timestamp = timestamp; + position.altitude = altitude; + self->location_updated_callback_(position); } }, 2, this); - if (!result) { - return result; + if (LOCATIONS_ERROR_NONE != ret) { + last_error_ = ret; + return false; } - return location_manager_start(manager_); -} - -TizenResult LocationManager::UnsetOnLocationUpdated() { - location_manager_unset_position_updated_cb(manager_); - return location_manager_stop(manager_); -} -TizenResult LocationManager::CreateLocationManager( - location_manager_h *manager) { - return location_manager_create(LOCATIONS_METHOD_HYBRID, manager); + ret = location_manager_start(manager_); + if (LOCATIONS_ERROR_NONE != ret) { + last_error_ = ret; + return false; + } + return true; } -TizenResult LocationManager::DestroyLocationManager( - location_manager_h manager) { - return location_manager_destroy(manager); +bool LocationManager::StopLocationUpdatesListen() { + int ret = location_manager_unset_position_updated_cb(manager_); + if (LOCATIONS_ERROR_NONE != ret) { + last_error_ = ret; + return false; + } + ret = location_manager_stop(manager_); + if (LOCATIONS_ERROR_NONE != ret) { + last_error_ = ret; + return false; + } + return true; } diff --git a/packages/geolocator/tizen/src/location_manager.h b/packages/geolocator/tizen/src/location_manager.h index 63407a70b..bac9fb6a6 100644 --- a/packages/geolocator/tizen/src/location_manager.h +++ b/packages/geolocator/tizen/src/location_manager.h @@ -9,52 +9,60 @@ #include -#include "location.h" -#include "tizen_result.h" +#include "position.h" // Defined in: // https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/enums/location_service.dart -enum class ServiceState { kDisabled, kEnabled }; +enum class ServiceStatus { kDisabled, kEnabled }; -using OnLocationUpdated = std::function; -using OnError = std::function; -using OnServiceStateChanged = std::function; +class LocationManagerError { + public: + LocationManagerError(int error_code) : error_code_(error_code) {} + std::string GetErrorString() const { return get_error_message(error_code_); } + + private: + int error_code_; +}; + +typedef std::function LocationCallback; +typedef std::function ServiceStatusCallback; +typedef std::function LocationErrorListener; class LocationManager { public: LocationManager(); ~LocationManager(); - TizenResult IsLocationServiceEnabled(bool* is_enabled); + bool IsLocationServiceEnabled(); - TizenResult RequestCurrentLocationOnce(OnLocationUpdated on_success, - OnError on_error); + Position GetLastKnownPosition(); - TizenResult GetLastKnownLocation(Location* LOCATION); + void GetCurrentPosition(LocationCallback location_updated_callback, + LocationErrorListener location_error_listener); - TizenResult SetOnServiceStateChanged( - OnServiceStateChanged on_service_state_changed); + bool StartServiceUpdatedListen( + ServiceStatusCallback service_status_updated_callback); - TizenResult UnsetOnServiceStateChanged(); + bool StopServiceUpdatedListen(); - TizenResult SetOnLocationUpdated(OnLocationUpdated on_location_updated); + bool StartLocationUpdatesListen(LocationCallback location_updated_callback); - TizenResult UnsetOnLocationUpdated(); + bool StopLocationUpdatesListen(); - private: - TizenResult CreateLocationManager(location_manager_h* manager); - - TizenResult DestroyLocationManager(location_manager_h manager); + int GetLastError() { return last_error_; } - location_manager_h manager_ = nullptr; + std::string GetLastErrorString() { return get_error_message(last_error_); } + private: // According to the document, the handler to request current location once // must not be the same as a handler to listen position updated. location_manager_h manager_for_current_location_ = nullptr; + location_manager_h manager_ = nullptr; - OnLocationUpdated on_success_; - OnError on_error_; - OnServiceStateChanged on_service_state_changed_; - OnLocationUpdated on_location_updated_; + int last_error_ = TIZEN_ERROR_NONE; + ServiceStatusCallback service_status_updated_callback_; + LocationCallback location_updated_callback_; + LocationCallback location_callback_; + LocationErrorListener location_error_listener_; }; #endif // FLUTTER_PLUGIN_LOCATION_MANAGER_H_ diff --git a/packages/geolocator/tizen/src/location.h b/packages/geolocator/tizen/src/position.h similarity index 92% rename from packages/geolocator/tizen/src/location.h rename to packages/geolocator/tizen/src/position.h index e30a8c6c9..0952e4174 100644 --- a/packages/geolocator/tizen/src/location.h +++ b/packages/geolocator/tizen/src/position.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_PLUGIN_LOCATION_H_ -#define FLUTTER_PLUGIN_LOCATION_H_ +#ifndef FLUTTER_PLUGIN_POSITION_H_ +#define FLUTTER_PLUGIN_POSITION_H_ #include #include @@ -13,7 +13,7 @@ // Defined in: // https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/models/position.dart -struct Location { +struct Position { flutter::EncodableValue ToEncodableValue() { flutter::EncodableMap values = { {flutter::EncodableValue("longitude"), @@ -54,4 +54,4 @@ struct Location { std::optional speedAccuracy; }; -#endif // FLUTTER_PLUGIN_LOCATION_H_ +#endif // FLUTTER_PLUGIN_POSITION_H_ diff --git a/packages/geolocator/tizen/src/tizen_result.h b/packages/geolocator/tizen/src/tizen_result.h deleted file mode 100644 index d164cd202..000000000 --- a/packages/geolocator/tizen/src/tizen_result.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_PLUGIN_TIZEN_RESULT_H_ -#define FLUTTER_PLUGIN_TIZEN_RESULT_H_ - -#include - -#include - -struct TizenResult { - TizenResult() : error_code(TIZEN_ERROR_NONE){}; - TizenResult(int code) : error_code(code) {} - - // Returns false on error. - operator bool() const { return (error_code == TIZEN_ERROR_NONE); } - - std::string message() { return get_error_message(error_code); } - - int error_code = TIZEN_ERROR_NONE; -}; - -#endif // FLUTTER_PLUGIN_TIZEN_RESULT_H_ From 9e01be1909add7dcd02d8dabb990094a76fb233f Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Fri, 20 May 2022 11:27:13 +0900 Subject: [PATCH 11/17] [geolocator] Fix typo --- packages/geolocator/tizen/src/geolocator_tizen_plugin.cc | 2 +- packages/geolocator/tizen/src/permission_manager.cc | 2 +- packages/geolocator/tizen/src/permission_manager.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index c9178d802..9e0b09e2e 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -193,7 +193,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { void OnRequestPermission() { PermissionStatus result = - permission_manager_->RequestPermssion(kPrivilegeLocation); + permission_manager_->RequestPermission(kPrivilegeLocation); if (result == PermissionStatus::kDeniedForever || result == PermissionStatus::kDenied) { SendErrorResult("Permission denied", "Permission denied by user."); diff --git a/packages/geolocator/tizen/src/permission_manager.cc b/packages/geolocator/tizen/src/permission_manager.cc index 599db9261..94ca4999c 100644 --- a/packages/geolocator/tizen/src/permission_manager.cc +++ b/packages/geolocator/tizen/src/permission_manager.cc @@ -36,7 +36,7 @@ PermissionStatus PermissionManager::CheckPermission( #endif } -PermissionStatus PermissionManager::RequestPermssion( +PermissionStatus PermissionManager::RequestPermission( const std::string &privilege) { #ifdef TV_PROFILE return PermissionStatus::kAlways; diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index d31c51f24..7850c45c4 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -25,7 +25,7 @@ class PermissionManager { PermissionStatus CheckPermission(const std::string &privilege); - PermissionStatus RequestPermssion(const std::string &privilege); + PermissionStatus RequestPermission(const std::string &privilege); }; #endif // FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ From a7edb924b1eaaf8d82d594d824eef068b99fb68a Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Fri, 20 May 2022 14:37:42 +0900 Subject: [PATCH 12/17] [geolocator] Fix permission manager --- .../tizen/src/app_settings_manager.cc | 1 + .../tizen/src/geolocator_tizen_plugin.cc | 44 +++++++++---------- .../tizen/src/permission_manager.cc | 9 ++-- .../geolocator/tizen/src/permission_manager.h | 15 ++++++- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/packages/geolocator/tizen/src/app_settings_manager.cc b/packages/geolocator/tizen/src/app_settings_manager.cc index c8efbe405..e49143b43 100644 --- a/packages/geolocator/tizen/src/app_settings_manager.cc +++ b/packages/geolocator/tizen/src/app_settings_manager.cc @@ -108,6 +108,7 @@ bool AppSettingsManager::OpenLocationSettings() { ret = app_control_set_operation(app_control, kLocationSettingOperationId); if (ret != APP_CONTROL_ERROR_NONE) { LOG_ERROR("Failed to set operation. (%s)", get_error_message(ret)); + app_control_destroy(app_control); return false; } diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 9e0b09e2e..bdd92be63 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -171,14 +171,14 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnCheckPermission() { - PermissionStatus result = - permission_manager_->CheckPermission(kPrivilegeLocation); - if (result == PermissionStatus::kError) { - SendErrorResult("Operation failed", "Failed to request permission."); - return; + try { + PermissionStatus result = + permission_manager_->CheckPermission(kPrivilegeLocation); + LOG_INFO("permission_status is %d", result); + SendResult(flutter::EncodableValue(static_cast(result))); + } catch (const LocationManagerError &error) { + SendErrorResult("Operation failed", error.GetErrorString()); } - LOG_INFO("permission_status is %d", result); - SendResult(flutter::EncodableValue(static_cast(result))); } void OnIsLocationServiceEnabled() { @@ -186,23 +186,24 @@ class GeolocatorTizenPlugin : public flutter::Plugin { bool is_enabled = location_manager_->IsLocationServiceEnabled(); SendResult(flutter::EncodableValue(is_enabled)); } catch (const LocationManagerError &error) { - // TODO : add location service listener SendErrorResult("Operation failed", error.GetErrorString()); } } void OnRequestPermission() { - PermissionStatus result = - permission_manager_->RequestPermission(kPrivilegeLocation); - if (result == PermissionStatus::kDeniedForever || - result == PermissionStatus::kDenied) { - SendErrorResult("Permission denied", "Permission denied by user."); - return; - } else if (result == PermissionStatus::kError) { - SendErrorResult("Operation failed", "Failed to request permission."); + try { + PermissionStatus result = + permission_manager_->RequestPermission(kPrivilegeLocation); + if (result == PermissionStatus::kDeniedForever || + result == PermissionStatus::kDenied) { + SendErrorResult("Permission denied", "Permission denied by user."); + return; + } + SendResult(flutter::EncodableValue(static_cast(result))); + } catch (const PermissionManagerError &error) { + SendErrorResult("Operation failed", error.GetErrorString()); return; } - SendResult(flutter::EncodableValue(static_cast(result))); } void OnGetLastKnownPosition() { @@ -215,14 +216,13 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnGetCurrentPosition() { - auto result_ptr = result_.get(); try { location_manager_->GetCurrentPosition( - [result_ptr](Position position) { - result_ptr->Success(position.ToEncodableValue()); + [this](Position position) { + SendResult(position.ToEncodableValue()); }, - [result_ptr](LocationManagerError error) { - result_ptr->Error("Operation failed", error.GetErrorString()); + [this](LocationManagerError error) { + SendErrorResult("Operation failed", error.GetErrorString()); }); } catch (const LocationManagerError &error) { SendErrorResult("Operation failed", error.GetErrorString()); diff --git a/packages/geolocator/tizen/src/permission_manager.cc b/packages/geolocator/tizen/src/permission_manager.cc index 94ca4999c..42a837b68 100644 --- a/packages/geolocator/tizen/src/permission_manager.cc +++ b/packages/geolocator/tizen/src/permission_manager.cc @@ -22,7 +22,7 @@ PermissionStatus PermissionManager::CheckPermission( if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { LOG_ERROR("Permission check failed [%s]: %s", privilege.c_str(), get_error_message(ret)); - return PermissionStatus::kError; + throw PermissionManagerError(ret); } switch (result) { @@ -57,10 +57,11 @@ PermissionStatus PermissionManager::RequestPermission( response->result = result; }, &response); + if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { LOG_ERROR("Permission request failed [%s]: %s", privilege.c_str(), get_error_message(ret)); - return PermissionStatus::kError; + throw PermissionManagerError(ret); } // Wait until ppm_request_permission() completes with a response. @@ -70,7 +71,7 @@ PermissionStatus PermissionManager::RequestPermission( if (response.cause == PRIVACY_PRIVILEGE_MANAGER_CALL_CAUSE_ERROR) { LOG_ERROR("Received an error response [%s].", privilege.c_str()); - return PermissionStatus::kError; + throw PermissionManagerError("Received an error response"); } switch (response.result) { @@ -81,7 +82,7 @@ PermissionStatus PermissionManager::RequestPermission( case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_ALLOW_FOREVER: return PermissionStatus::kAlways; default: - return PermissionStatus::kError; + throw PermissionManagerError("Failed to request permission."); } #endif // TV_PROFILE } diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index 7850c45c4..54ab99a2e 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -5,6 +5,8 @@ #ifndef FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ #define FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ +#include + #include // Keep in sync with the enum values implemented in: @@ -15,7 +17,18 @@ enum class PermissionStatus { kDeniedForever = 1, kWhileInUse = 2, kAlways = 3, - kError = 4, // Only use in geolocator_tizen +}; + +class PermissionManagerError { + public: + PermissionManagerError(std::string error_message) + : error_message_(error_message) {} + PermissionManagerError(int error_code) + : error_message_(get_error_message(error_code)) {} + std::string GetErrorString() const { return error_message_; } + + private: + std::string error_message_; }; class PermissionManager { From 6813108bae50ca66c3fa44dcfb696add8140431b Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Mon, 23 May 2022 15:09:57 +0900 Subject: [PATCH 13/17] [geolocator] Fix MothodResult handling --- .../tizen/src/geolocator_tizen_plugin.cc | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index bdd92be63..e673b1c81 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -216,16 +216,26 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnGetCurrentPosition() { + auto result_ptr = result_.release(); try { location_manager_->GetCurrentPosition( - [this](Position position) { - SendResult(position.ToEncodableValue()); + [result_ptr](Position position) { + if (result_ptr) { + result_ptr->Success(position.ToEncodableValue()); + delete result_ptr; + } }, - [this](LocationManagerError error) { - SendErrorResult("Operation failed", error.GetErrorString()); + [result_ptr](LocationManagerError error) { + if (result_ptr) { + result_ptr->Error("Operation failed", error.GetErrorString()); + delete result_ptr; + } }); } catch (const LocationManagerError &error) { - SendErrorResult("Operation failed", error.GetErrorString()); + if (result_ptr) { + result_ptr->Error("Operation failed", error.GetErrorString()); + delete result_ptr; + } } } From ef7448eb14c624840e630059f48ee49ff29dc86e Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Thu, 26 May 2022 11:17:02 +0900 Subject: [PATCH 14/17] [geolocator] Fix code --- .../tizen/src/app_settings_manager.cc | 2 + .../tizen/src/app_settings_manager.h | 2 - .../tizen/src/geolocator_tizen_plugin.cc | 86 +++++++++++-------- .../geolocator/tizen/src/location_manager.cc | 80 +++++++---------- .../geolocator/tizen/src/location_manager.h | 36 ++++---- .../tizen/src/permission_manager.cc | 14 +-- .../geolocator/tizen/src/permission_manager.h | 17 +--- 7 files changed, 110 insertions(+), 127 deletions(-) diff --git a/packages/geolocator/tizen/src/app_settings_manager.cc b/packages/geolocator/tizen/src/app_settings_manager.cc index e49143b43..32e96cf11 100644 --- a/packages/geolocator/tizen/src/app_settings_manager.cc +++ b/packages/geolocator/tizen/src/app_settings_manager.cc @@ -7,6 +7,8 @@ #include #include +#include + #include "log.h" namespace { diff --git a/packages/geolocator/tizen/src/app_settings_manager.h b/packages/geolocator/tizen/src/app_settings_manager.h index 9c29a3639..02b7f058d 100644 --- a/packages/geolocator/tizen/src/app_settings_manager.h +++ b/packages/geolocator/tizen/src/app_settings_manager.h @@ -7,8 +7,6 @@ #include -#include - class AppSettingsManager { public: AppSettingsManager() {} diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index e673b1c81..4baec6555 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -11,6 +11,9 @@ #include #include +#include +#include + #include "app_settings_manager.h" #include "location_manager.h" #include "log.h" @@ -29,7 +32,7 @@ typedef flutter::StreamHandlerError constexpr char kPrivilegeLocation[] = "http://tizen.org/privilege/location"; -class LocationUpdatesStreamHandler : public FlStreamHandler { +class LocationStreamHandler : public FlStreamHandler { protected: std::unique_ptr OnListenInternal( const flutter::EncodableValue *arguments, @@ -38,20 +41,24 @@ class LocationUpdatesStreamHandler : public FlStreamHandler { LocationCallback callback = [this](Position position) -> void { events_->Success(position.ToEncodableValue()); }; - if (!location_manager_.StartLocationUpdatesListen(callback)) { + try { + location_manager_.StartListenLocationUpdate(callback); + } catch (const LocationManagerError &error) { return std::make_unique( - std::to_string(location_manager_.GetLastError()), - location_manager_.GetLastErrorString(), nullptr); + std::to_string(error.GetErrorCode()), error.GetErrorString(), + nullptr); } return nullptr; } std::unique_ptr OnCancelInternal( const flutter::EncodableValue *arguments) override { - if (!location_manager_.StopLocationUpdatesListen()) { + try { + location_manager_.StopListenLocationUpdate(); + } catch (const LocationManagerError &error) { return std::make_unique( - std::to_string(location_manager_.GetLastError()), - location_manager_.GetLastErrorString(), nullptr); + std::to_string(error.GetErrorCode()), error.GetErrorString(), + nullptr); } events_.reset(); return nullptr; @@ -73,7 +80,7 @@ std::string ServiceStatusToString(ServiceStatus status) { } } -class ServiceUpdatesStreamHandler : public FlStreamHandler { +class ServiceStatusStreamHandler : public FlStreamHandler { protected: std::unique_ptr OnListenInternal( const flutter::EncodableValue *arguments, @@ -82,20 +89,24 @@ class ServiceUpdatesStreamHandler : public FlStreamHandler { ServiceStatusCallback callback = [this](ServiceStatus status) -> void { events_->Success(flutter::EncodableValue(ServiceStatusToString(status))); }; - if (!location_manager_.StartServiceUpdatedListen(callback)) { + try { + location_manager_.StartListenServiceStatusUpdate(callback); + } catch (const LocationManagerError &error) { return std::make_unique( - std::to_string(location_manager_.GetLastError()), - location_manager_.GetLastErrorString(), nullptr); + std::to_string(error.GetErrorCode()), error.GetErrorString(), + nullptr); } return nullptr; } std::unique_ptr OnCancelInternal( const flutter::EncodableValue *arguments) override { - if (!location_manager_.StopServiceUpdatedListen()) { + try { + location_manager_.StopListenServiceStatusUpdate(); + } catch (const LocationManagerError &error) { return std::make_unique( - std::to_string(location_manager_.GetLastError()), - location_manager_.GetLastErrorString(), nullptr); + std::to_string(error.GetErrorCode()), error.GetErrorString(), + nullptr); } events_.reset(); return nullptr; @@ -120,13 +131,13 @@ class GeolocatorTizenPlugin : public flutter::Plugin { "flutter.baseflow.com/geolocator_service_updates", &flutter::StandardMethodCodec::GetInstance()); plugin->service_updates_channel_->SetStreamHandler( - std::make_unique()); + std::make_unique()); plugin->updates_channel_ = std::make_unique( registrar->messenger(), "flutter.baseflow.com/geolocator_updates", &flutter::StandardMethodCodec::GetInstance()); plugin->updates_channel_->SetStreamHandler( - std::make_unique()); + std::make_unique()); channel->SetMethodCallHandler( [plugin_pointer = plugin.get()](const auto &call, auto result) { @@ -160,25 +171,25 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } else if (method_name == "getCurrentPosition") { OnGetCurrentPosition(); } else if (method_name == "openAppSettings") { - bool ret = app_settings_manager_->OpenAppSettings(); - result->Success(flutter::EncodableValue(ret)); + bool opened = app_settings_manager_->OpenAppSettings(); + SendResult(flutter::EncodableValue(opened)); } else if (method_name == "openLocationSettings") { - bool ret = app_settings_manager_->OpenLocationSettings(); - result->Success(flutter::EncodableValue(ret)); + bool opened = app_settings_manager_->OpenLocationSettings(); + SendResult(flutter::EncodableValue(opened)); } else { result->NotImplemented(); } } void OnCheckPermission() { - try { - PermissionStatus result = - permission_manager_->CheckPermission(kPrivilegeLocation); - LOG_INFO("permission_status is %d", result); - SendResult(flutter::EncodableValue(static_cast(result))); - } catch (const LocationManagerError &error) { - SendErrorResult("Operation failed", error.GetErrorString()); + PermissionStatus result = + permission_manager_->CheckPermission(kPrivilegeLocation); + if (result == PermissionStatus::kError) { + SendErrorResult("Operation failed", "Permission request failed."); + return; } + LOG_INFO("permission_status is %d", result); + SendResult(flutter::EncodableValue(static_cast(result))); } void OnIsLocationServiceEnabled() { @@ -191,19 +202,18 @@ class GeolocatorTizenPlugin : public flutter::Plugin { } void OnRequestPermission() { - try { - PermissionStatus result = - permission_manager_->RequestPermission(kPrivilegeLocation); - if (result == PermissionStatus::kDeniedForever || - result == PermissionStatus::kDenied) { - SendErrorResult("Permission denied", "Permission denied by user."); - return; - } - SendResult(flutter::EncodableValue(static_cast(result))); - } catch (const PermissionManagerError &error) { - SendErrorResult("Operation failed", error.GetErrorString()); + PermissionStatus result = + permission_manager_->RequestPermission(kPrivilegeLocation); + + if (result == PermissionStatus::kError) { + SendErrorResult("Operation failed", "Permission request failed."); + return; + } else if (result == PermissionStatus::kDeniedForever || + result == PermissionStatus::kDenied) { + SendErrorResult("Permission denied", "Permission denied by user."); return; } + SendResult(flutter::EncodableValue(static_cast(result))); } void OnGetLastKnownPosition() { diff --git a/packages/geolocator/tizen/src/location_manager.cc b/packages/geolocator/tizen/src/location_manager.cc index 2fc2a76bc..e25ca086c 100644 --- a/packages/geolocator/tizen/src/location_manager.cc +++ b/packages/geolocator/tizen/src/location_manager.cc @@ -19,17 +19,16 @@ LocationManager::~LocationManager() { bool LocationManager::IsLocationServiceEnabled() { bool gps_enabled = false; - int ret_gps = + int ret = location_manager_is_enabled_method(LOCATIONS_METHOD_GPS, &gps_enabled); - if (LOCATIONS_ERROR_NONE != ret_gps) { - throw LocationManagerError(ret_gps); + if (LOCATIONS_ERROR_NONE != ret) { + throw LocationManagerError(ret); } bool wps_enabled = false; - int ret_wps = - location_manager_is_enabled_method(LOCATIONS_METHOD_WPS, &wps_enabled); - if (LOCATIONS_ERROR_NONE != ret_wps) { - throw LocationManagerError(ret_wps); + ret = location_manager_is_enabled_method(LOCATIONS_METHOD_WPS, &wps_enabled); + if (LOCATIONS_ERROR_NONE != ret) { + throw LocationManagerError(ret); } return gps_enabled || wps_enabled; } @@ -65,10 +64,10 @@ Position LocationManager::GetLastKnownPosition() { } void LocationManager::GetCurrentPosition( - LocationCallback location_callback, - LocationErrorListener location_error_listener) { - location_callback_ = location_callback; - location_error_listener_ = location_error_listener; + LocationCallback on_location_callback, + LocationErrorCallback on_error_callback) { + on_location_callback_ = on_location_callback; + on_error_callback_ = on_error_callback; int ret = location_manager_request_single_location( manager_for_current_location_, 5, [](location_error_e error, double latitude, double longitude, @@ -76,9 +75,9 @@ void LocationManager::GetCurrentPosition( double climb, void *user_data) { LocationManager *self = static_cast(user_data); - if (error != LOCATIONS_ERROR_NONE && self->location_error_listener_) { - self->location_error_listener_(LocationManagerError(error)); - } else if (self->location_callback_) { + if (error != LOCATIONS_ERROR_NONE && self->on_error_callback_) { + self->on_error_callback_(LocationManagerError(error)); + } else if (self->on_location_callback_) { Position position; position.latitude = latitude; position.longitude = longitude; @@ -87,11 +86,10 @@ void LocationManager::GetCurrentPosition( position.speed = speed; position.heading = direction; - self->location_callback_(position); + self->on_location_callback_(position); } - - self->location_callback_ = nullptr; - self->location_error_listener_ = nullptr; + self->on_location_callback_ = nullptr; + self->on_error_callback_ = nullptr; }, this); if (LOCATIONS_ERROR_NONE != ret) { @@ -99,80 +97,68 @@ void LocationManager::GetCurrentPosition( } } -bool LocationManager::StartServiceUpdatedListen( - ServiceStatusCallback service_status_updated_callback) { - service_status_updated_callback_ = service_status_updated_callback; +void LocationManager::StartListenServiceStatusUpdate( + ServiceStatusCallback callback) { + on_service_status_updated_callback_ = callback; int ret = location_manager_set_service_state_changed_cb( manager_, [](location_service_state_e state, void *user_data) { LocationManager *self = static_cast(user_data); - if (self->service_status_updated_callback_) { + if (self->on_service_status_updated_callback_) { ServiceStatus service_status = ServiceStatus::kDisabled; if (state == LOCATIONS_SERVICE_ENABLED) { service_status = ServiceStatus::kEnabled; } - self->service_status_updated_callback_(service_status); + self->on_service_status_updated_callback_(service_status); } }, this); if (LOCATIONS_ERROR_NONE != ret) { - last_error_ = ret; - return false; + throw LocationManagerError(ret); } - return true; } -bool LocationManager::StopServiceUpdatedListen() { +void LocationManager::StopListenServiceStatusUpdate() { int ret = location_manager_unset_service_state_changed_cb(manager_); if (LOCATIONS_ERROR_NONE != ret) { - last_error_ = ret; - return false; + throw LocationManagerError(ret); } - return true; } -bool LocationManager::StartLocationUpdatesListen( - LocationCallback location_updated_callback) { - location_updated_callback_ = location_updated_callback; +void LocationManager::StartListenLocationUpdate(LocationCallback callback) { + on_location_updated_callback_ = callback; int ret = location_manager_set_position_updated_cb( manager_, [](double latitude, double longitude, double altitude, time_t timestamp, void *user_data) { LocationManager *self = static_cast(user_data); - if (self->location_updated_callback_) { + if (self->on_location_updated_callback_) { Position position; position.longitude = longitude; position.latitude = latitude; position.timestamp = timestamp; position.altitude = altitude; - self->location_updated_callback_(position); + self->on_location_updated_callback_(position); } }, 2, this); if (LOCATIONS_ERROR_NONE != ret) { - last_error_ = ret; - return false; + throw LocationManagerError(ret); } - ret = location_manager_start(manager_); if (LOCATIONS_ERROR_NONE != ret) { - last_error_ = ret; - return false; + throw LocationManagerError(ret); } - return true; } -bool LocationManager::StopLocationUpdatesListen() { +void LocationManager::StopListenLocationUpdate() { int ret = location_manager_unset_position_updated_cb(manager_); if (LOCATIONS_ERROR_NONE != ret) { - last_error_ = ret; - return false; + throw LocationManagerError(ret); } ret = location_manager_stop(manager_); if (LOCATIONS_ERROR_NONE != ret) { - last_error_ = ret; - return false; + throw LocationManagerError(ret); } - return true; } diff --git a/packages/geolocator/tizen/src/location_manager.h b/packages/geolocator/tizen/src/location_manager.h index bac9fb6a6..26acbfdac 100644 --- a/packages/geolocator/tizen/src/location_manager.h +++ b/packages/geolocator/tizen/src/location_manager.h @@ -6,8 +6,10 @@ #define FLUTTER_PLUGIN_LOCATION_MANAGER_H_ #include +#include #include +#include #include "position.h" @@ -18,6 +20,9 @@ enum class ServiceStatus { kDisabled, kEnabled }; class LocationManagerError { public: LocationManagerError(int error_code) : error_code_(error_code) {} + + int GetErrorCode() const { return error_code_; } + std::string GetErrorString() const { return get_error_message(error_code_); } private: @@ -26,7 +31,7 @@ class LocationManagerError { typedef std::function LocationCallback; typedef std::function ServiceStatusCallback; -typedef std::function LocationErrorListener; +typedef std::function LocationErrorCallback; class LocationManager { public: @@ -37,32 +42,27 @@ class LocationManager { Position GetLastKnownPosition(); - void GetCurrentPosition(LocationCallback location_updated_callback, - LocationErrorListener location_error_listener); - - bool StartServiceUpdatedListen( - ServiceStatusCallback service_status_updated_callback); + void GetCurrentPosition(LocationCallback on_location_callback, + LocationErrorCallback on_error_callback); - bool StopServiceUpdatedListen(); + void StartListenServiceStatusUpdate(ServiceStatusCallback callback); - bool StartLocationUpdatesListen(LocationCallback location_updated_callback); + void StopListenServiceStatusUpdate(); - bool StopLocationUpdatesListen(); + void StartListenLocationUpdate(LocationCallback callback); - int GetLastError() { return last_error_; } - - std::string GetLastErrorString() { return get_error_message(last_error_); } + void StopListenLocationUpdate(); private: // According to the document, the handler to request current location once - // must not be the same as a handler to listen position updated. + // must not be the same as a handler to listen position updates. location_manager_h manager_for_current_location_ = nullptr; location_manager_h manager_ = nullptr; - int last_error_ = TIZEN_ERROR_NONE; - ServiceStatusCallback service_status_updated_callback_; - LocationCallback location_updated_callback_; - LocationCallback location_callback_; - LocationErrorListener location_error_listener_; + ServiceStatusCallback on_service_status_updated_callback_; + LocationCallback on_location_updated_callback_; + LocationCallback on_location_callback_; + LocationErrorCallback on_error_callback_; }; + #endif // FLUTTER_PLUGIN_LOCATION_MANAGER_H_ diff --git a/packages/geolocator/tizen/src/permission_manager.cc b/packages/geolocator/tizen/src/permission_manager.cc index 42a837b68..6291fc6ef 100644 --- a/packages/geolocator/tizen/src/permission_manager.cc +++ b/packages/geolocator/tizen/src/permission_manager.cc @@ -9,6 +9,9 @@ #include #include #endif +#include + +#include #include "log.h" @@ -22,7 +25,7 @@ PermissionStatus PermissionManager::CheckPermission( if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { LOG_ERROR("Permission check failed [%s]: %s", privilege.c_str(), get_error_message(ret)); - throw PermissionManagerError(ret); + return PermissionStatus::kError; } switch (result) { @@ -59,9 +62,8 @@ PermissionStatus PermissionManager::RequestPermission( &response); if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { - LOG_ERROR("Permission request failed [%s]: %s", privilege.c_str(), - get_error_message(ret)); - throw PermissionManagerError(ret); + LOG_ERROR("c[%s]: %s", privilege.c_str(), get_error_message(ret)); + return PermissionStatus::kError; } // Wait until ppm_request_permission() completes with a response. @@ -71,7 +73,7 @@ PermissionStatus PermissionManager::RequestPermission( if (response.cause == PRIVACY_PRIVILEGE_MANAGER_CALL_CAUSE_ERROR) { LOG_ERROR("Received an error response [%s].", privilege.c_str()); - throw PermissionManagerError("Received an error response"); + return PermissionStatus::kError; } switch (response.result) { @@ -82,7 +84,7 @@ PermissionStatus PermissionManager::RequestPermission( case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_ALLOW_FOREVER: return PermissionStatus::kAlways; default: - throw PermissionManagerError("Failed to request permission."); + return PermissionStatus::kError; } #endif // TV_PROFILE } diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index 54ab99a2e..5a678320c 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -5,10 +5,6 @@ #ifndef FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ #define FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ -#include - -#include - // Keep in sync with the enum values implemented in: // https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/enums/location_permission.dart // https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_android/android/src/main/java/com/baseflow/geolocator/permission/LocationPermission.java @@ -17,18 +13,7 @@ enum class PermissionStatus { kDeniedForever = 1, kWhileInUse = 2, kAlways = 3, -}; - -class PermissionManagerError { - public: - PermissionManagerError(std::string error_message) - : error_message_(error_message) {} - PermissionManagerError(int error_code) - : error_message_(get_error_message(error_code)) {} - std::string GetErrorString() const { return error_message_; } - - private: - std::string error_message_; + kError = 4 // tizen only. }; class PermissionManager { From dc67c403e7932f3f369da3b46a66bbcd80c2b164 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Thu, 26 May 2022 11:34:35 +0900 Subject: [PATCH 15/17] [geolocator] Fix build --- .../geolocator/tizen/src/geolocator_tizen_plugin.cc | 13 +------------ packages/geolocator/tizen/src/permission_manager.h | 2 ++ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 4baec6555..c8530c971 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -69,17 +69,6 @@ class LocationStreamHandler : public FlStreamHandler { std::unique_ptr events_; }; -std::string ServiceStatusToString(ServiceStatus status) { - switch (status) { - case ServiceStatus::kEnabled: - return "enabled"; - case ServiceStatus::kDisabled: - return "disabled"; - default: - return "unknown"; - } -} - class ServiceStatusStreamHandler : public FlStreamHandler { protected: std::unique_ptr OnListenInternal( @@ -87,7 +76,7 @@ class ServiceStatusStreamHandler : public FlStreamHandler { std::unique_ptr &&events) override { events_ = std::move(events); ServiceStatusCallback callback = [this](ServiceStatus status) -> void { - events_->Success(flutter::EncodableValue(ServiceStatusToString(status))); + events_->Success(flutter::EncodableValue(static_cast(status))); }; try { location_manager_.StartListenServiceStatusUpdate(callback); diff --git a/packages/geolocator/tizen/src/permission_manager.h b/packages/geolocator/tizen/src/permission_manager.h index 5a678320c..43bde52f3 100644 --- a/packages/geolocator/tizen/src/permission_manager.h +++ b/packages/geolocator/tizen/src/permission_manager.h @@ -5,6 +5,8 @@ #ifndef FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ #define FLUTTER_PLUGIN_PERMISSION_MANAGER_H_ +#include + // Keep in sync with the enum values implemented in: // https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_platform_interface/lib/src/enums/location_permission.dart // https://github.com/Baseflow/flutter-geolocator/blob/master/geolocator_android/android/src/main/java/com/baseflow/geolocator/permission/LocationPermission.java From b8444e3ef8a63e8ce146cbba272dd6bfb3895e56 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Fri, 27 May 2022 14:32:28 +0900 Subject: [PATCH 16/17] [geolocator] Fix more code --- .../tizen/src/app_settings_manager.cc | 1 + .../tizen/src/app_settings_manager.h | 2 - .../tizen/src/geolocator_tizen_plugin.cc | 36 +++++++--------- .../geolocator/tizen/src/location_manager.cc | 41 ++++++++++--------- .../geolocator/tizen/src/location_manager.h | 13 +++--- .../tizen/src/permission_manager.cc | 21 ++++------ 6 files changed, 52 insertions(+), 62 deletions(-) diff --git a/packages/geolocator/tizen/src/app_settings_manager.cc b/packages/geolocator/tizen/src/app_settings_manager.cc index 32e96cf11..902d93db5 100644 --- a/packages/geolocator/tizen/src/app_settings_manager.cc +++ b/packages/geolocator/tizen/src/app_settings_manager.cc @@ -5,6 +5,7 @@ #include "app_settings_manager.h" #include +#include #include #include diff --git a/packages/geolocator/tizen/src/app_settings_manager.h b/packages/geolocator/tizen/src/app_settings_manager.h index 02b7f058d..0f424eaa2 100644 --- a/packages/geolocator/tizen/src/app_settings_manager.h +++ b/packages/geolocator/tizen/src/app_settings_manager.h @@ -5,8 +5,6 @@ #ifndef FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ #define FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ -#include - class AppSettingsManager { public: AppSettingsManager() {} diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index c8530c971..7be77074b 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -174,10 +174,9 @@ class GeolocatorTizenPlugin : public flutter::Plugin { PermissionStatus result = permission_manager_->CheckPermission(kPrivilegeLocation); if (result == PermissionStatus::kError) { - SendErrorResult("Operation failed", "Permission request failed."); + SendErrorResult("Operation failed", "Permission check failed."); return; } - LOG_INFO("permission_status is %d", result); SendResult(flutter::EncodableValue(static_cast(result))); } @@ -216,26 +215,19 @@ class GeolocatorTizenPlugin : public flutter::Plugin { void OnGetCurrentPosition() { auto result_ptr = result_.release(); - try { - location_manager_->GetCurrentPosition( - [result_ptr](Position position) { - if (result_ptr) { - result_ptr->Success(position.ToEncodableValue()); - delete result_ptr; - } - }, - [result_ptr](LocationManagerError error) { - if (result_ptr) { - result_ptr->Error("Operation failed", error.GetErrorString()); - delete result_ptr; - } - }); - } catch (const LocationManagerError &error) { - if (result_ptr) { - result_ptr->Error("Operation failed", error.GetErrorString()); - delete result_ptr; - } - } + location_manager_->GetCurrentPosition( + [result_ptr](Position position) { + if (result_ptr) { + result_ptr->Success(position.ToEncodableValue()); + delete result_ptr; + } + }, + [result_ptr](LocationManagerError error) { + if (result_ptr) { + result_ptr->Error("Operation failed", error.GetErrorString()); + delete result_ptr; + } + }); } void SendResult(const flutter::EncodableValue &result) { diff --git a/packages/geolocator/tizen/src/location_manager.cc b/packages/geolocator/tizen/src/location_manager.cc index e25ca086c..5039d1eaa 100644 --- a/packages/geolocator/tizen/src/location_manager.cc +++ b/packages/geolocator/tizen/src/location_manager.cc @@ -63,11 +63,10 @@ Position LocationManager::GetLastKnownPosition() { return position; } -void LocationManager::GetCurrentPosition( - LocationCallback on_location_callback, - LocationErrorCallback on_error_callback) { - on_location_callback_ = on_location_callback; - on_error_callback_ = on_error_callback; +void LocationManager::GetCurrentPosition(LocationCallback on_location, + LocationErrorCallback on_error) { + location_callback_ = on_location; + error_callback_ = on_error; int ret = location_manager_request_single_location( manager_for_current_location_, 5, [](location_error_e error, double latitude, double longitude, @@ -75,9 +74,9 @@ void LocationManager::GetCurrentPosition( double climb, void *user_data) { LocationManager *self = static_cast(user_data); - if (error != LOCATIONS_ERROR_NONE && self->on_error_callback_) { - self->on_error_callback_(LocationManagerError(error)); - } else if (self->on_location_callback_) { + if (error != LOCATIONS_ERROR_NONE && self->error_callback_) { + self->error_callback_(LocationManagerError(error)); + } else if (self->location_callback_) { Position position; position.latitude = latitude; position.longitude = longitude; @@ -86,30 +85,31 @@ void LocationManager::GetCurrentPosition( position.speed = speed; position.heading = direction; - self->on_location_callback_(position); + self->location_callback_(position); } - self->on_location_callback_ = nullptr; - self->on_error_callback_ = nullptr; + self->location_callback_ = nullptr; + self->error_callback_ = nullptr; }, this); if (LOCATIONS_ERROR_NONE != ret) { - throw LocationManagerError(ret); + error_callback_(LocationManagerError(ret)); + error_callback_ = nullptr; } } void LocationManager::StartListenServiceStatusUpdate( - ServiceStatusCallback callback) { - on_service_status_updated_callback_ = callback; + ServiceStatusCallback on_service_status_update) { + service_status_update_callback_ = on_service_status_update; int ret = location_manager_set_service_state_changed_cb( manager_, [](location_service_state_e state, void *user_data) { LocationManager *self = static_cast(user_data); - if (self->on_service_status_updated_callback_) { + if (self->service_status_update_callback_) { ServiceStatus service_status = ServiceStatus::kDisabled; if (state == LOCATIONS_SERVICE_ENABLED) { service_status = ServiceStatus::kEnabled; } - self->on_service_status_updated_callback_(service_status); + self->service_status_update_callback_(service_status); } }, this); @@ -125,21 +125,22 @@ void LocationManager::StopListenServiceStatusUpdate() { } } -void LocationManager::StartListenLocationUpdate(LocationCallback callback) { - on_location_updated_callback_ = callback; +void LocationManager::StartListenLocationUpdate( + LocationCallback on_location_update) { + location_update_callback_ = on_location_update; int ret = location_manager_set_position_updated_cb( manager_, [](double latitude, double longitude, double altitude, time_t timestamp, void *user_data) { LocationManager *self = static_cast(user_data); - if (self->on_location_updated_callback_) { + if (self->location_update_callback_) { Position position; position.longitude = longitude; position.latitude = latitude; position.timestamp = timestamp; position.altitude = altitude; - self->on_location_updated_callback_(position); + self->location_update_callback_(position); } }, 2, this); diff --git a/packages/geolocator/tizen/src/location_manager.h b/packages/geolocator/tizen/src/location_manager.h index 26acbfdac..344db1aa7 100644 --- a/packages/geolocator/tizen/src/location_manager.h +++ b/packages/geolocator/tizen/src/location_manager.h @@ -45,11 +45,12 @@ class LocationManager { void GetCurrentPosition(LocationCallback on_location_callback, LocationErrorCallback on_error_callback); - void StartListenServiceStatusUpdate(ServiceStatusCallback callback); + void StartListenServiceStatusUpdate( + ServiceStatusCallback on_service_status_update); void StopListenServiceStatusUpdate(); - void StartListenLocationUpdate(LocationCallback callback); + void StartListenLocationUpdate(LocationCallback on_location_update); void StopListenLocationUpdate(); @@ -59,10 +60,10 @@ class LocationManager { location_manager_h manager_for_current_location_ = nullptr; location_manager_h manager_ = nullptr; - ServiceStatusCallback on_service_status_updated_callback_; - LocationCallback on_location_updated_callback_; - LocationCallback on_location_callback_; - LocationErrorCallback on_error_callback_; + ServiceStatusCallback service_status_update_callback_; + LocationCallback location_update_callback_; + LocationCallback location_callback_; + LocationErrorCallback error_callback_; }; #endif // FLUTTER_PLUGIN_LOCATION_MANAGER_H_ diff --git a/packages/geolocator/tizen/src/permission_manager.cc b/packages/geolocator/tizen/src/permission_manager.cc index 6291fc6ef..981bfa1ff 100644 --- a/packages/geolocator/tizen/src/permission_manager.cc +++ b/packages/geolocator/tizen/src/permission_manager.cc @@ -9,9 +9,6 @@ #include #include #endif -#include - -#include #include "log.h" @@ -29,12 +26,12 @@ PermissionStatus PermissionManager::CheckPermission( } switch (result) { - case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_DENY: - case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ASK: - return PermissionStatus::kDenied; case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ALLOW: - default: return PermissionStatus::kAlways; + case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_ASK: + case PRIVACY_PRIVILEGE_MANAGER_CHECK_RESULT_DENY: + default: + return PermissionStatus::kDenied; } #endif } @@ -62,7 +59,8 @@ PermissionStatus PermissionManager::RequestPermission( &response); if (ret != PRIVACY_PRIVILEGE_MANAGER_ERROR_NONE) { - LOG_ERROR("c[%s]: %s", privilege.c_str(), get_error_message(ret)); + LOG_ERROR("Permission request failed [%s]: %s", privilege.c_str(), + get_error_message(ret)); return PermissionStatus::kError; } @@ -77,14 +75,13 @@ PermissionStatus PermissionManager::RequestPermission( } switch (response.result) { + case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_ALLOW_FOREVER: + return PermissionStatus::kAlways; case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_FOREVER: return PermissionStatus::kDeniedForever; case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_ONCE: - return PermissionStatus::kDenied; - case PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_ALLOW_FOREVER: - return PermissionStatus::kAlways; default: - return PermissionStatus::kError; + return PermissionStatus::kDenied; } #endif // TV_PROFILE } From 50086e595cd1839ae000f05c46868a4eacc3faba Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Mon, 30 May 2022 11:57:53 +0900 Subject: [PATCH 17/17] [geolocator] Fix stream error handling --- .../tizen/src/geolocator_tizen_plugin.cc | 39 ++++++++++++------- .../geolocator/tizen/src/location_manager.cc | 2 - .../geolocator/tizen/src/location_manager.h | 4 +- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc index 7be77074b..0a8ce728e 100644 --- a/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc +++ b/packages/geolocator/tizen/src/geolocator_tizen_plugin.cc @@ -16,7 +16,6 @@ #include "app_settings_manager.h" #include "location_manager.h" -#include "log.h" #include "permission_manager.h" namespace { @@ -44,9 +43,11 @@ class LocationStreamHandler : public FlStreamHandler { try { location_manager_.StartListenLocationUpdate(callback); } catch (const LocationManagerError &error) { - return std::make_unique( - std::to_string(error.GetErrorCode()), error.GetErrorString(), - nullptr); + // Issue: https://github.com/flutter/flutter/issues/101682 + error_code_ = std::to_string(error.GetErrorCode()); + error_message_ = error.GetErrorString(); + return std::make_unique(error_code_, error_message_, + nullptr); } return nullptr; } @@ -56,9 +57,10 @@ class LocationStreamHandler : public FlStreamHandler { try { location_manager_.StopListenLocationUpdate(); } catch (const LocationManagerError &error) { - return std::make_unique( - std::to_string(error.GetErrorCode()), error.GetErrorString(), - nullptr); + error_code_ = std::to_string(error.GetErrorCode()); + error_message_ = error.GetErrorString(); + return std::make_unique(error_code_, error_message_, + nullptr); } events_.reset(); return nullptr; @@ -67,6 +69,9 @@ class LocationStreamHandler : public FlStreamHandler { private: LocationManager location_manager_; std::unique_ptr events_; + + std::string error_code_; + std::string error_message_; }; class ServiceStatusStreamHandler : public FlStreamHandler { @@ -81,9 +86,11 @@ class ServiceStatusStreamHandler : public FlStreamHandler { try { location_manager_.StartListenServiceStatusUpdate(callback); } catch (const LocationManagerError &error) { - return std::make_unique( - std::to_string(error.GetErrorCode()), error.GetErrorString(), - nullptr); + // Issue: https://github.com/flutter/flutter/issues/101682 + error_code_ = std::to_string(error.GetErrorCode()); + error_message_ = error.GetErrorString(); + return std::make_unique(error_code_, error_message_, + nullptr); } return nullptr; } @@ -93,9 +100,10 @@ class ServiceStatusStreamHandler : public FlStreamHandler { try { location_manager_.StopListenServiceStatusUpdate(); } catch (const LocationManagerError &error) { - return std::make_unique( - std::to_string(error.GetErrorCode()), error.GetErrorString(), - nullptr); + error_code_ = std::to_string(error.GetErrorCode()); + error_message_ = error.GetErrorString(); + return std::make_unique(error_code_, error_message_, + nullptr); } events_.reset(); return nullptr; @@ -104,6 +112,9 @@ class ServiceStatusStreamHandler : public FlStreamHandler { private: LocationManager location_manager_; std::unique_ptr events_; + + std::string error_code_; + std::string error_message_; }; class GeolocatorTizenPlugin : public flutter::Plugin { @@ -252,9 +263,7 @@ class GeolocatorTizenPlugin : public flutter::Plugin { std::unique_ptr location_manager_; std::unique_ptr app_settings_manager_; std::unique_ptr service_updates_channel_; - std::unique_ptr service_updates_event_sink_; std::unique_ptr updates_channel_; - std::unique_ptr updates_event_sink_; }; } // namespace diff --git a/packages/geolocator/tizen/src/location_manager.cc b/packages/geolocator/tizen/src/location_manager.cc index 5039d1eaa..2992cfd95 100644 --- a/packages/geolocator/tizen/src/location_manager.cc +++ b/packages/geolocator/tizen/src/location_manager.cc @@ -4,8 +4,6 @@ #include "location_manager.h" -#include "log.h" - LocationManager::LocationManager() { location_manager_create(LOCATIONS_METHOD_HYBRID, &manager_); location_manager_create(LOCATIONS_METHOD_HYBRID, diff --git a/packages/geolocator/tizen/src/location_manager.h b/packages/geolocator/tizen/src/location_manager.h index 344db1aa7..6bd3f9de5 100644 --- a/packages/geolocator/tizen/src/location_manager.h +++ b/packages/geolocator/tizen/src/location_manager.h @@ -42,8 +42,8 @@ class LocationManager { Position GetLastKnownPosition(); - void GetCurrentPosition(LocationCallback on_location_callback, - LocationErrorCallback on_error_callback); + void GetCurrentPosition(LocationCallback on_location, + LocationErrorCallback on_error); void StartListenServiceStatusUpdate( ServiceStatusCallback on_service_status_update);