diff --git a/README.md b/README.md index eeb0c9d01..3cb21b9a1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ The _"non-endorsed"_ status means that the plugin is not endorsed by the origina | [**share_tizen**](packages/share) | [share](https://github.com/flutter/plugins/tree/master/packages/share) (1st-party) | [![pub package](https://img.shields.io/pub/v/share_tizen.svg)](https://pub.dev/packages/share_tizen) | No | | [**shared_preferences_tizen**](packages/path_provider) | [shared_preferences](https://github.com/flutter/plugins/tree/master/packages/shared_preferences) (1st-party) | [![pub package](https://img.shields.io/pub/v/shared_preferences_tizen.svg)](https://pub.dev/packages/shared_preferences_tizen) | No | | [**url_launcher_tizen**](packages/url_launcher) | [url_launcher](https://github.com/flutter/plugins/tree/master/packages/url_launcher) (1st-party) | [![pub package](https://img.shields.io/pub/v/url_launcher_tizen.svg)](https://pub.dev/packages/url_launcher_tizen) | No | +| [**wifi_info_flutter_tizen**](packages/wifi_info_flutter) | [wifi_info_flutter](https://github.com/flutter/plugins/tree/master/packages/wifi_info_flutter) (1st-party) | [![pub package](https://img.shields.io/pub/v/wifi_info_flutter_tizen.svg)](https://pub.dev/packages/wifi_info_flutter_tizen) | No | ## Device limitations @@ -39,3 +40,4 @@ The _"non-endorsed"_ status means that the plugin is not endorsed by the origina | [**share_tizen**](packages/share) | ⚠️ | ⚠️ | ❌ | ❌ | No SMS or e-mail app | | [**shared_preferences_tizen**](packages/path_provider) | ✔️ | ✔️ | ✔️ | ✔️ | | [**url_launcher_tizen**](packages/url_launcher) | ✔️ | ❌ | ✔️ | ❌ | No browser app | +| [**wifi_info_flutter_tizen**](packages/wifi_info_flutter) | ✔️ | ❌ | ✔️ | ❌ | diff --git a/packages/wifi_info_flutter/.gitignore b/packages/wifi_info_flutter/.gitignore new file mode 100644 index 000000000..e9dc58d3d --- /dev/null +++ b/packages/wifi_info_flutter/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +.dart_tool/ + +.packages +.pub/ + +build/ diff --git a/packages/wifi_info_flutter/CHANGELOG.md b/packages/wifi_info_flutter/CHANGELOG.md new file mode 100644 index 000000000..32f490b5f --- /dev/null +++ b/packages/wifi_info_flutter/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +* Initial release diff --git a/packages/wifi_info_flutter/LICENSE b/packages/wifi_info_flutter/LICENSE new file mode 100644 index 000000000..6ae330dee --- /dev/null +++ b/packages/wifi_info_flutter/LICENSE @@ -0,0 +1,26 @@ +Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. +Copyright (c) 2020 The Chromium Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the names of the copyright holders nor the names of the + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/wifi_info_flutter/README.md b/packages/wifi_info_flutter/README.md new file mode 100644 index 000000000..62d3ea144 --- /dev/null +++ b/packages/wifi_info_flutter/README.md @@ -0,0 +1,33 @@ +# wifi_info_flutter_tizen + +The Tizen implementation of [`wifi_info_flutter`](https://github.com/flutter/plugins/tree/master/packages/wifi_info_flutter). + +## Usage + +This package is not an _endorsed_ implementation of `wifi_info_flutter`. Therefore, you have to include `wifi_info_flutter_tizen` alongside `wifi_info_flutter` as dependencies in your `pubspec.yaml` file. + +```yaml +dependencies: + wifi_info_flutter: ^1.0.1 + wifi_info_flutter_tizen: ^1.0.0 +``` + +Then you can import `wifi_info_flutter` in your Dart code: + +```dart +import 'package:wifi_info_flutter/wifi_info_flutter.dart'; +``` + +For detailed usage, see https://github.com/flutter/plugins/tree/master/packages/wifi_info_flutter/wifi_info_flutter#usage. + +## Required privileges + +To get network information using this plugin, add below lines under the `` section in your `tizen-manifest.xml` file, + +```xml + + http://tizen.org/privilege/network.get + +``` + +For details, see [Security and API Privileges](https://docs.tizen.org/application/dotnet/tutorials/sec-privileges). diff --git a/packages/wifi_info_flutter/example/.gitignore b/packages/wifi_info_flutter/example/.gitignore new file mode 100644 index 000000000..9d532b18a --- /dev/null +++ b/packages/wifi_info_flutter/example/.gitignore @@ -0,0 +1,41 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +**/ios/Flutter/.last_build_id +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.packages +.pub-cache/ +.pub/ +/build/ + +# Web related +lib/generated_plugin_registrant.dart + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json diff --git a/packages/wifi_info_flutter/example/README.md b/packages/wifi_info_flutter/example/README.md new file mode 100644 index 000000000..5465eabbb --- /dev/null +++ b/packages/wifi_info_flutter/example/README.md @@ -0,0 +1,7 @@ +# wifi_info_flutter_example + +Demonstrates how to use the wifi_info_flutter_tizen plugin. + +## Getting Started + +To run this app on your Tizen device, use [flutter-tizen](https://github.com/flutter-tizen/flutter-tizen). diff --git a/packages/wifi_info_flutter/example/integration_test/wifi_info_test.dart b/packages/wifi_info_flutter/example/integration_test/wifi_info_test.dart new file mode 100644 index 000000000..103dc54a1 --- /dev/null +++ b/packages/wifi_info_flutter/example/integration_test/wifi_info_test.dart @@ -0,0 +1,27 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:io'; +import 'package:integration_test/integration_test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wifi_info_flutter/wifi_info_flutter.dart'; + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + group('$WifiInfo test driver', () { + WifiInfo _wifiInfo; + + setUpAll(() async { + _wifiInfo = WifiInfo(); + }); + + testWidgets('test location methods, iOS only', (WidgetTester tester) async { + expect( + (await _wifiInfo.getLocationServiceAuthorization()), + LocationAuthorizationStatus.notDetermined, + ); + }, skip: !Platform.isIOS); + }); +} diff --git a/packages/wifi_info_flutter/example/lib/main.dart b/packages/wifi_info_flutter/example/lib/main.dart new file mode 100644 index 000000000..8c64c5d9a --- /dev/null +++ b/packages/wifi_info_flutter/example/lib/main.dart @@ -0,0 +1,173 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ignore_for_file: public_member_api_docs + +import 'dart:async'; +import 'dart:io'; + +import 'package:connectivity/connectivity.dart' + show Connectivity, ConnectivityResult; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:wifi_info_flutter/wifi_info_flutter.dart'; + +// Sets a platform override for desktop to avoid exceptions. See +// https://flutter.dev/desktop#target-platform-override for more info. +void _enablePlatformOverrideForDesktop() { + if (!kIsWeb && (Platform.isWindows || Platform.isLinux)) { + debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; + } +} + +void main() { + _enablePlatformOverrideForDesktop(); + runApp(MyApp()); +} + +class MyApp extends StatelessWidget { + // This widget is the root of your application. + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Flutter Demo', + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: MyHomePage(title: 'Flutter Demo Home Page'), + ); + } +} + +class MyHomePage extends StatefulWidget { + MyHomePage({Key key, this.title}) : super(key: key); + + final String title; + + @override + _MyHomePageState createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + String _connectionStatus = 'Unknown'; + final Connectivity _connectivity = Connectivity(); + final WifiInfo _wifiInfo = WifiInfo(); + StreamSubscription _connectivitySubscription; + + @override + void initState() { + super.initState(); + initConnectivity(); + _connectivitySubscription = + _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); + } + + @override + void dispose() { + _connectivitySubscription.cancel(); + super.dispose(); + } + + // Platform messages are asynchronous, so we initialize in an async method. + Future initConnectivity() async { + ConnectivityResult result; + // Platform messages may fail, so we use a try/catch PlatformException. + try { + result = await _connectivity.checkConnectivity(); + } on PlatformException catch (e) { + print(e.toString()); + } + + // If the widget was removed from the tree while the asynchronous platform + // message was in flight, we want to discard the reply rather than calling + // setState to update our non-existent appearance. + if (!mounted) { + return Future.value(null); + } + + return _updateConnectionStatus(result); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Connectivity example app'), + ), + body: Center(child: Text('Connection Status: $_connectionStatus')), + ); + } + + Future _updateConnectionStatus(ConnectivityResult result) async { + switch (result) { + case ConnectivityResult.wifi: + String wifiName, wifiBSSID, wifiIP; + + try { + if (!kIsWeb && Platform.isIOS) { + LocationAuthorizationStatus status = + await _wifiInfo.getLocationServiceAuthorization(); + if (status == LocationAuthorizationStatus.notDetermined) { + status = await _wifiInfo.requestLocationServiceAuthorization(); + } + if (status == LocationAuthorizationStatus.authorizedAlways || + status == LocationAuthorizationStatus.authorizedWhenInUse) { + wifiName = await _wifiInfo.getWifiName(); + } else { + wifiName = await _wifiInfo.getWifiName(); + } + } else { + wifiName = await _wifiInfo.getWifiName(); + } + } on PlatformException catch (e) { + print(e.toString()); + wifiName = "Failed to get Wifi Name"; + } + + try { + if (!kIsWeb && Platform.isIOS) { + LocationAuthorizationStatus status = + await _wifiInfo.getLocationServiceAuthorization(); + if (status == LocationAuthorizationStatus.notDetermined) { + status = await _wifiInfo.requestLocationServiceAuthorization(); + } + if (status == LocationAuthorizationStatus.authorizedAlways || + status == LocationAuthorizationStatus.authorizedWhenInUse) { + wifiBSSID = await _wifiInfo.getWifiBSSID(); + } else { + wifiBSSID = await _wifiInfo.getWifiBSSID(); + } + } else { + wifiBSSID = await _wifiInfo.getWifiBSSID(); + } + } on PlatformException catch (e) { + print(e.toString()); + wifiBSSID = "Failed to get Wifi BSSID"; + } + + try { + wifiIP = await _wifiInfo.getWifiIP(); + } on PlatformException catch (e) { + print(e.toString()); + wifiIP = "Failed to get Wifi IP"; + } + + setState(() { + _connectionStatus = '$result\n' + 'Wifi Name: $wifiName\n' + 'Wifi BSSID: $wifiBSSID\n' + 'Wifi IP: $wifiIP\n'; + }); + break; + case ConnectivityResult.mobile: + case ConnectivityResult.none: + setState(() => _connectionStatus = result.toString()); + break; + default: + setState(() => _connectionStatus = 'Failed to get connectivity.'); + break; + } + } +} diff --git a/packages/wifi_info_flutter/example/pubspec.yaml b/packages/wifi_info_flutter/example/pubspec.yaml new file mode 100644 index 000000000..f688c8b4f --- /dev/null +++ b/packages/wifi_info_flutter/example/pubspec.yaml @@ -0,0 +1,28 @@ +name: wifi_info_flutter_tizen_example +description: Demonstrates how to use the wifi_info_flutter_tizen plugin. +publish_to: 'none' + +dependencies: + connectivity: ^2.0.2 + connectivity_tizen: + path: ../../connectivity/ + cupertino_icons: ^1.0.0 + flutter: + sdk: flutter + integration_test: ^1.0.1 + integration_test_tizen: + path: ../../integration_test/ + wifi_info_flutter: ^1.0.1 + wifi_info_flutter_tizen: + path: ../ + +dev_dependencies: + flutter_test: + sdk: flutter + +flutter: + uses-material-design: true + +environment: + sdk: ">=2.7.0 <3.0.0" + flutter: ">=1.20.0 <2.0.0" diff --git a/packages/wifi_info_flutter/example/test_driver/integration_test.dart b/packages/wifi_info_flutter/example/test_driver/integration_test.dart new file mode 100644 index 000000000..b38629cca --- /dev/null +++ b/packages/wifi_info_flutter/example/test_driver/integration_test.dart @@ -0,0 +1,3 @@ +import 'package:integration_test/integration_test_driver.dart'; + +Future main() => integrationDriver(); diff --git a/packages/wifi_info_flutter/example/tizen/.gitignore b/packages/wifi_info_flutter/example/tizen/.gitignore new file mode 100644 index 000000000..750f3af1b --- /dev/null +++ b/packages/wifi_info_flutter/example/tizen/.gitignore @@ -0,0 +1,5 @@ +flutter/ +.vs/ +*.user +bin/ +obj/ diff --git a/packages/wifi_info_flutter/example/tizen/App.cs b/packages/wifi_info_flutter/example/tizen/App.cs new file mode 100644 index 000000000..6dd4a6356 --- /dev/null +++ b/packages/wifi_info_flutter/example/tizen/App.cs @@ -0,0 +1,20 @@ +using Tizen.Flutter.Embedding; + +namespace Runner +{ + public class App : FlutterApplication + { + protected override void OnCreate() + { + base.OnCreate(); + + GeneratedPluginRegistrant.RegisterPlugins(this); + } + + static void Main(string[] args) + { + var app = new App(); + app.Run(args); + } + } +} diff --git a/packages/wifi_info_flutter/example/tizen/NuGet.Config b/packages/wifi_info_flutter/example/tizen/NuGet.Config new file mode 100644 index 000000000..c4ea70c17 --- /dev/null +++ b/packages/wifi_info_flutter/example/tizen/NuGet.Config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/wifi_info_flutter/example/tizen/Runner.csproj b/packages/wifi_info_flutter/example/tizen/Runner.csproj new file mode 100644 index 000000000..497f928b8 --- /dev/null +++ b/packages/wifi_info_flutter/example/tizen/Runner.csproj @@ -0,0 +1,26 @@ + + + + Exe + tizen40 + + + + portable + + + none + + + + + + + + + + %(RecursiveDir) + + + + diff --git a/packages/wifi_info_flutter/example/tizen/shared/res/ic_launcher.png b/packages/wifi_info_flutter/example/tizen/shared/res/ic_launcher.png new file mode 100644 index 000000000..4d6372eeb Binary files /dev/null and b/packages/wifi_info_flutter/example/tizen/shared/res/ic_launcher.png differ diff --git a/packages/wifi_info_flutter/example/tizen/tizen-manifest.xml b/packages/wifi_info_flutter/example/tizen/tizen-manifest.xml new file mode 100644 index 000000000..a50f45002 --- /dev/null +++ b/packages/wifi_info_flutter/example/tizen/tizen-manifest.xml @@ -0,0 +1,14 @@ + + + + + + ic_launcher.png + + + + + + http://tizen.org/privilege/network.get + + diff --git a/packages/wifi_info_flutter/pubspec.yaml b/packages/wifi_info_flutter/pubspec.yaml new file mode 100644 index 000000000..63b32a072 --- /dev/null +++ b/packages/wifi_info_flutter/pubspec.yaml @@ -0,0 +1,25 @@ +name: wifi_info_flutter_tizen +description: Tizen implementation of the wifi_info_flutter plugin +version: 1.0.0 +homepage: https://github.com/flutter-tizen/plugins + +flutter: + plugin: + platforms: + tizen: + pluginClass: WifiInfoFlutterTizenPlugin + fileName: wifi_info_flutter_tizen_plugin.h + +dependencies: + flutter: + sdk: flutter + wifi_info_flutter_platform_interface: ^1.0.0 + +dev_dependencies: + flutter_test: + sdk: flutter + plugin_platform_interface: ^1.0.0 + +environment: + sdk: ">=2.7.0 <3.0.0" + flutter: ">=1.20.0 <2.0.0" diff --git a/packages/wifi_info_flutter/tizen/.gitignore b/packages/wifi_info_flutter/tizen/.gitignore new file mode 100644 index 000000000..a2a7d62b1 --- /dev/null +++ b/packages/wifi_info_flutter/tizen/.gitignore @@ -0,0 +1,5 @@ +.cproject +.sign +crash-info/ +Debug/ +Release/ diff --git a/packages/wifi_info_flutter/tizen/inc/wifi_info_flutter_tizen_plugin.h b/packages/wifi_info_flutter/tizen/inc/wifi_info_flutter_tizen_plugin.h new file mode 100644 index 000000000..f6620c665 --- /dev/null +++ b/packages/wifi_info_flutter/tizen/inc/wifi_info_flutter_tizen_plugin.h @@ -0,0 +1,23 @@ +#ifndef FLUTTER_PLUGIN_WIFI_INFO_FLUTTER_TIZEN_PLUGIN_H_ +#define FLUTTER_PLUGIN_WIFI_INFO_FLUTTER_TIZEN_PLUGIN_H_ + +#include + +#ifdef FLUTTER_PLUGIN_IMPL +#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default"))) +#else +#define FLUTTER_PLUGIN_EXPORT +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + +FLUTTER_PLUGIN_EXPORT void WifiInfoFlutterTizenPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // FLUTTER_PLUGIN_WIFI_INFO_FLUTTER_TIZEN_PLUGIN_H_ diff --git a/packages/wifi_info_flutter/tizen/project_def.prop b/packages/wifi_info_flutter/tizen/project_def.prop new file mode 100644 index 000000000..16eca4583 --- /dev/null +++ b/packages/wifi_info_flutter/tizen/project_def.prop @@ -0,0 +1,30 @@ +# See https://docs.tizen.org/application/tizen-studio/native-tools/project-conversion +# for details. + +APPNAME = wifi_info_flutter_tizen_plugin +type = sharedLib +profile = common-4.0 + +# Source files +USER_SRCS += src/wifi_info_flutter_tizen_plugin.cc + +# User defines +USER_DEFS = +USER_UNDEFS = +USER_CPP_DEFS = TIZEN_DEPRECATION DEPRECATION_WARNING FLUTTER_PLUGIN_IMPL +USER_CPP_UNDEFS = + +# Compiler/linker flags +USER_CFLAGS_MISC = +USER_CPPFLAGS_MISC = -c -fmessage-length=0 +USER_LFLAGS = + +# Libraries and objects +USER_LIB_DIRS = lib +USER_LIBS = +USER_OBJS = + +# User includes +USER_INC_DIRS = inc src +USER_INC_FILES = +USER_CPP_INC_FILES = diff --git a/packages/wifi_info_flutter/tizen/src/log.h b/packages/wifi_info_flutter/tizen/src/log.h new file mode 100644 index 000000000..5d178ba6b --- /dev/null +++ b/packages/wifi_info_flutter/tizen/src/log.h @@ -0,0 +1,20 @@ +#ifndef __LOG_H__ +#define __LOG_H__ + +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "WifiInfoFlutterTizenPlugin" + +#define LOG(prio, fmt, arg...) \ + dlog_print(prio, LOG_TAG, "%s: %s(%d) > " fmt, __FILE__, __func__, __LINE__, \ + ##arg) + +#define LOG_DEBUG(fmt, args...) LOG(DLOG_DEBUG, fmt, ##args) +#define LOG_INFO(fmt, args...) LOG(DLOG_INFO, fmt, ##args) +#define LOG_WARN(fmt, args...) LOG(DLOG_WARN, fmt, ##args) +#define LOG_ERROR(fmt, args...) LOG(DLOG_ERROR, fmt, ##args) + +#endif // __LOG_H__ diff --git a/packages/wifi_info_flutter/tizen/src/wifi_info_flutter_tizen_plugin.cc b/packages/wifi_info_flutter/tizen/src/wifi_info_flutter_tizen_plugin.cc new file mode 100644 index 000000000..70d8a15cb --- /dev/null +++ b/packages/wifi_info_flutter/tizen/src/wifi_info_flutter_tizen_plugin.cc @@ -0,0 +1,134 @@ +#include "wifi_info_flutter_tizen_plugin.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "log.h" + +class WifiInfoFlutterTizenPlugin : public flutter::Plugin { + public: + enum WifiInfoType { ESSID, BSSID }; + + static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { + auto channel = + std::make_unique>( + registrar->messenger(), "plugins.flutter.io/wifi_info_flutter", + &flutter::StandardMethodCodec::GetInstance()); + + auto plugin = std::make_unique(); + + channel->SetMethodCallHandler( + [plugin_pointer = plugin.get()](const auto &call, auto result) { + plugin_pointer->HandleMethodCall(call, std::move(result)); + }); + + registrar->AddPlugin(std::move(plugin)); + } + + WifiInfoFlutterTizenPlugin() + : m_connection(nullptr), m_wifi_manager(nullptr) { + EnsureConnectionHandle(); + } + + virtual ~WifiInfoFlutterTizenPlugin() { + if (m_connection != nullptr) { + connection_destroy(m_connection); + m_connection = nullptr; + } + if (m_wifi_manager != nullptr) { + wifi_manager_deinitialize(m_wifi_manager); + m_wifi_manager = nullptr; + } + } + + private: + std::string GetWifiInfo(WifiInfoType type) { + std::string result; + wifi_manager_ap_h current_ap = nullptr; + char *name = NULL; + int errorcode; + + errorcode = wifi_manager_get_connected_ap(m_wifi_manager, ¤t_ap); + if (errorcode == WIFI_MANAGER_ERROR_NONE && current_ap != nullptr) { + if (type == WifiInfoType::ESSID) { + errorcode = wifi_manager_ap_get_essid(current_ap, &name); + } else { + errorcode = wifi_manager_ap_get_bssid(current_ap, &name); + } + if (errorcode == WIFI_MANAGER_ERROR_NONE) { + result = name; + free(name); + } + wifi_manager_ap_destroy(current_ap); + } + return result; + } + + void HandleMethodCall( + const flutter::MethodCall &method_call, + std::unique_ptr> result) { + if (!EnsureConnectionHandle()) { + result->Error("-1", "Initialization failed"); + return; + } + std::string replay = ""; + if (method_call.method_name().compare("wifiName") == 0) { + replay = GetWifiInfo(WifiInfoType::ESSID); + } else if (method_call.method_name().compare("wifiBSSID") == 0) { + replay = GetWifiInfo(WifiInfoType::BSSID); + } else if (method_call.method_name().compare("wifiIPAddress") == 0) { + char *ip_addr = NULL; + if (connection_get_ip_address(m_connection, + CONNECTION_ADDRESS_FAMILY_IPV4, + &ip_addr) != CONNECTION_ERROR_NONE) { + result->Error("-1", "Couldn't obtain current ip address"); + return; + } + replay = ip_addr; + free(ip_addr); + } else { + result->NotImplemented(); + return; + } + if (replay.length() == 0) { + result->Error("-1", "Not valid result"); + return; + } + flutter::EncodableValue msg(replay); + result->Success(msg); + } + + bool EnsureConnectionHandle() { + if (m_connection == nullptr) { + if (connection_create(&m_connection) != CONNECTION_ERROR_NONE) { + m_connection = nullptr; + return false; + } + } + if (m_wifi_manager == nullptr) { + if (wifi_manager_initialize(&m_wifi_manager) != WIFI_MANAGER_ERROR_NONE) { + m_wifi_manager = nullptr; + return false; + } + } + return true; + } + + connection_h m_connection; + wifi_manager_h m_wifi_manager; +}; + +void WifiInfoFlutterTizenPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar) { + WifiInfoFlutterTizenPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarManager::GetInstance() + ->GetRegistrar(registrar)); +}