|
6 | 6 |
|
7 | 7 | #include <flutter/event_channel.h>
|
8 | 8 | #include <flutter/event_sink.h>
|
9 |
| -#include <flutter/event_stream_handler_functions.h> |
| 9 | +#include <flutter/event_stream_handler.h> |
10 | 10 | #include <flutter/method_channel.h>
|
11 | 11 | #include <flutter/plugin_registrar.h>
|
12 | 12 | #include <flutter/standard_method_codec.h>
|
13 |
| -#include <net_connection.h> |
14 |
| -#include <wifi-manager.h> |
15 | 13 |
|
16 | 14 | #include <memory>
|
17 | 15 | #include <string>
|
18 | 16 |
|
19 |
| -#include "log.h" |
20 |
| - |
21 |
| -class ConnectivityPlusTizenPlugin : public flutter::Plugin { |
22 |
| - public: |
23 |
| - static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { |
24 |
| - auto plugin = std::make_unique<ConnectivityPlusTizenPlugin>(); |
25 |
| - plugin->SetupChannels(registrar); |
26 |
| - registrar->AddPlugin(std::move(plugin)); |
27 |
| - } |
28 |
| - |
29 |
| - ConnectivityPlusTizenPlugin() : connection_(nullptr), events_(nullptr) { |
30 |
| - EnsureConnectionHandle(); |
| 17 | +#include "connection.h" |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +typedef flutter::EventChannel<flutter::EncodableValue> FlEventChannel; |
| 22 | +typedef flutter::EventSink<flutter::EncodableValue> FlEventSink; |
| 23 | +typedef flutter::MethodCall<flutter::EncodableValue> FlMethodCall; |
| 24 | +typedef flutter::MethodResult<flutter::EncodableValue> FlMethodResult; |
| 25 | +typedef flutter::MethodChannel<flutter::EncodableValue> FlMethodChannel; |
| 26 | +typedef flutter::StreamHandler<flutter::EncodableValue> FlStreamHandler; |
| 27 | +typedef flutter::StreamHandlerError<flutter::EncodableValue> |
| 28 | + FlStreamHandlerError; |
| 29 | + |
| 30 | +std::string ConnectionTypeToString(ConnectionType type) { |
| 31 | + switch (type) { |
| 32 | + case ConnectionType::kEthernet: |
| 33 | + return "ethernet"; |
| 34 | + case ConnectionType::kWiFi: |
| 35 | + return "wifi"; |
| 36 | + case ConnectionType::kMobile: |
| 37 | + return "mobile"; |
| 38 | + case ConnectionType::kNone: |
| 39 | + default: |
| 40 | + return "none"; |
31 | 41 | }
|
| 42 | +} |
32 | 43 |
|
33 |
| - virtual ~ConnectivityPlusTizenPlugin() { |
34 |
| - if (connection_ != nullptr) { |
35 |
| - connection_destroy(connection_); |
36 |
| - connection_ = nullptr; |
37 |
| - } |
38 |
| - } |
| 44 | +class ConnectivityStreamHandler : public FlStreamHandler { |
| 45 | + protected: |
| 46 | + std::unique_ptr<FlStreamHandlerError> OnListenInternal( |
| 47 | + const flutter::EncodableValue *arguments, |
| 48 | + std::unique_ptr<FlEventSink> &&events) override { |
| 49 | + events_ = std::move(events); |
39 | 50 |
|
40 |
| - void RegisterObserver( |
41 |
| - std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> &&events) { |
42 |
| - EnsureConnectionHandle(); |
43 |
| - if (connection_set_type_changed_cb( |
44 |
| - connection_, |
45 |
| - [](connection_type_e state, void *data) -> void { |
46 |
| - auto *self = static_cast<ConnectivityPlusTizenPlugin *>(data); |
47 |
| - self->SendConnectivityChangedEvent(state); |
48 |
| - }, |
49 |
| - this) != CONNECTION_ERROR_NONE) { |
50 |
| - return; |
| 51 | + ConnectionTypeCallback callback = [this](ConnectionType type) -> void { |
| 52 | + if (type != ConnectionType::kError) { |
| 53 | + events_->Success(flutter::EncodableValue(ConnectionTypeToString(type))); |
| 54 | + } else { |
| 55 | + events_->Error(std::to_string(connection_.GetLastError()), |
| 56 | + connection_.GetLastErrorString()); |
| 57 | + } |
| 58 | + }; |
| 59 | + if (!connection_.StartListen(callback)) { |
| 60 | + return std::make_unique<FlStreamHandlerError>( |
| 61 | + std::to_string(connection_.GetLastError()), |
| 62 | + connection_.GetLastErrorString(), nullptr); |
51 | 63 | }
|
52 |
| - events_ = std::move(events); |
53 |
| - } |
54 | 64 |
|
55 |
| - void ClearObserver() { |
56 |
| - if (connection_ == nullptr || events_ == nullptr) return; |
| 65 | + // Send an initial event once the stream has been set up. |
| 66 | + callback(connection_.GetType()); |
57 | 67 |
|
58 |
| - connection_unset_type_changed_cb(connection_); |
59 |
| - events_ = nullptr; |
| 68 | + return nullptr; |
60 | 69 | }
|
61 | 70 |
|
62 |
| - void SendConnectivityChangedEvent(connection_type_e state) { |
63 |
| - if (events_ == nullptr) { |
64 |
| - return; |
65 |
| - } |
66 |
| - std::string replay = ConvertConnectionTypeToString(state); |
67 |
| - flutter::EncodableValue msg(replay); |
68 |
| - events_->Success(msg); |
| 71 | + std::unique_ptr<FlStreamHandlerError> OnCancelInternal( |
| 72 | + const flutter::EncodableValue *arguments) override { |
| 73 | + connection_.StopListen(); |
| 74 | + events_.reset(); |
| 75 | + return nullptr; |
69 | 76 | }
|
70 | 77 |
|
71 | 78 | private:
|
72 |
| - void EnsureConnectionHandle() { |
73 |
| - if (connection_ == nullptr) { |
74 |
| - if (connection_create(&connection_) != CONNECTION_ERROR_NONE) { |
75 |
| - connection_ = nullptr; |
76 |
| - } |
77 |
| - } |
78 |
| - } |
| 79 | + Connection connection_; |
| 80 | + std::unique_ptr<FlEventSink> events_; |
| 81 | +}; |
79 | 82 |
|
80 |
| - std::string ConvertConnectionTypeToString(connection_type_e net_state) { |
81 |
| - std::string result; |
82 |
| - switch (net_state) { |
83 |
| - case CONNECTION_TYPE_WIFI: |
84 |
| - result = "wifi"; |
85 |
| - break; |
86 |
| - case CONNECTION_TYPE_ETHERNET: |
87 |
| - result = "ethernet"; |
88 |
| - break; |
89 |
| - case CONNECTION_TYPE_CELLULAR: |
90 |
| - result = "mobile"; |
91 |
| - break; |
92 |
| - case CONNECTION_TYPE_DISCONNECTED: |
93 |
| - default: |
94 |
| - result = "none"; |
95 |
| - } |
96 |
| - return result; |
| 83 | +class ConnectivityPlusTizenPlugin : public flutter::Plugin { |
| 84 | + public: |
| 85 | + static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { |
| 86 | + auto plugin = std::make_unique<ConnectivityPlusTizenPlugin>(); |
| 87 | + |
| 88 | + auto method_channel = std::make_unique<FlMethodChannel>( |
| 89 | + registrar->messenger(), "dev.fluttercommunity.plus/connectivity", |
| 90 | + &flutter::StandardMethodCodec::GetInstance()); |
| 91 | + method_channel->SetMethodCallHandler( |
| 92 | + [plugin_pointer = plugin.get()](const auto &call, auto result) { |
| 93 | + plugin_pointer->HandleMethodCall(call, std::move(result)); |
| 94 | + }); |
| 95 | + |
| 96 | + auto event_channel = std::make_unique<FlEventChannel>( |
| 97 | + registrar->messenger(), "dev.fluttercommunity.plus/connectivity_status", |
| 98 | + &flutter::StandardMethodCodec::GetInstance()); |
| 99 | + event_channel->SetStreamHandler( |
| 100 | + std::make_unique<ConnectivityStreamHandler>()); |
| 101 | + |
| 102 | + registrar->AddPlugin(std::move(plugin)); |
97 | 103 | }
|
98 | 104 |
|
99 |
| - void HandleMethodCall( |
100 |
| - const flutter::MethodCall<flutter::EncodableValue> &method_call, |
101 |
| - std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) { |
102 |
| - EnsureConnectionHandle(); |
103 |
| - LOG_INFO("method : %s", method_call.method_name().data()); |
104 |
| - |
105 |
| - std::string replay = ""; |
106 |
| - if (method_call.method_name().compare("check") == 0) { |
107 |
| - connection_type_e net_state; |
108 |
| - if (connection_get_type(connection_, &net_state) != |
109 |
| - CONNECTION_ERROR_NONE) { |
110 |
| - result->Error("-1", "Couldn't know current connection type"); |
111 |
| - return; |
| 105 | + ConnectivityPlusTizenPlugin() {} |
| 106 | + |
| 107 | + virtual ~ConnectivityPlusTizenPlugin() {} |
| 108 | + |
| 109 | + private: |
| 110 | + void HandleMethodCall(const FlMethodCall &method_call, |
| 111 | + std::unique_ptr<FlMethodResult> result) { |
| 112 | + const auto &method_name = method_call.method_name(); |
| 113 | + |
| 114 | + if (method_name == "check") { |
| 115 | + Connection connection; |
| 116 | + ConnectionType type = connection.GetType(); |
| 117 | + if (type != ConnectionType::kError) { |
| 118 | + result->Success(flutter::EncodableValue(ConnectionTypeToString(type))); |
| 119 | + } else { |
| 120 | + result->Error(std::to_string(connection.GetLastError()), |
| 121 | + connection.GetLastErrorString()); |
112 | 122 | }
|
113 |
| - replay = ConvertConnectionTypeToString(net_state); |
114 | 123 | } else {
|
115 |
| - result->Error("-1", "Not supported method"); |
116 |
| - return; |
| 124 | + result->NotImplemented(); |
117 | 125 | }
|
118 |
| - if (replay.length() == 0) { |
119 |
| - result->Error("-1", "Not valid result"); |
120 |
| - return; |
121 |
| - } |
122 |
| - |
123 |
| - flutter::EncodableValue msg(replay); |
124 |
| - result->Success(msg); |
125 | 126 | }
|
126 |
| - |
127 |
| - void SetupChannels(flutter::PluginRegistrar *registrar) { |
128 |
| - auto method_channel = |
129 |
| - std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>( |
130 |
| - registrar->messenger(), "dev.fluttercommunity.plus/connectivity", |
131 |
| - &flutter::StandardMethodCodec::GetInstance()); |
132 |
| - event_channel_ = |
133 |
| - std::make_unique<flutter::EventChannel<flutter::EncodableValue>>( |
134 |
| - registrar->messenger(), |
135 |
| - "dev.fluttercommunity.plus/connectivity_status", |
136 |
| - &flutter::StandardMethodCodec::GetInstance()); |
137 |
| - method_channel->SetMethodCallHandler([this](const auto &call, auto result) { |
138 |
| - HandleMethodCall(call, std::move(result)); |
139 |
| - }); |
140 |
| - |
141 |
| - auto event_channel_handler = |
142 |
| - std::make_unique<flutter::StreamHandlerFunctions<>>( |
143 |
| - [this](const flutter::EncodableValue *arguments, |
144 |
| - std::unique_ptr<flutter::EventSink<>> &&events) |
145 |
| - -> std::unique_ptr<flutter::StreamHandlerError<>> { |
146 |
| - LOG_INFO("OnListen"); |
147 |
| - RegisterObserver(std::move(events)); |
148 |
| - return nullptr; |
149 |
| - }, |
150 |
| - [this](const flutter::EncodableValue *arguments) |
151 |
| - -> std::unique_ptr<flutter::StreamHandlerError<>> { |
152 |
| - LOG_INFO("OnCancel"); |
153 |
| - ClearObserver(); |
154 |
| - return nullptr; |
155 |
| - }); |
156 |
| - event_channel_->SetStreamHandler(std::move(event_channel_handler)); |
157 |
| - } |
158 |
| - |
159 |
| - std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> |
160 |
| - event_channel_; |
161 |
| - connection_h connection_; |
162 |
| - std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> events_; |
163 | 127 | };
|
164 | 128 |
|
| 129 | +} // namespace |
| 130 | + |
165 | 131 | void ConnectivityPlusTizenPluginRegisterWithRegistrar(
|
166 | 132 | FlutterDesktopPluginRegistrarRef registrar) {
|
167 | 133 | ConnectivityPlusTizenPlugin::RegisterWithRegistrar(
|
|
0 commit comments