mirror of
https://github.com/flutter/samples.git
synced 2026-04-02 09:43:05 +00:00
Update federated_plugin runners (#972)
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
set(PROJECT_NAME "federated_plugin_windows")
|
||||
project(${PROJECT_NAME} LANGUAGES CXX)
|
||||
|
||||
set(PLUGIN_NAME "${PROJECT_NAME}_plugin")
|
||||
# This value is used when generating builds using this plugin, so it must
|
||||
# not be changed
|
||||
set(PLUGIN_NAME "federated_plugin_windows_plugin")
|
||||
|
||||
add_library(${PLUGIN_NAME} SHARED
|
||||
"${PLUGIN_NAME}.cpp"
|
||||
"federated_plugin_windows_plugin.cpp"
|
||||
)
|
||||
apply_standard_settings(${PLUGIN_NAME})
|
||||
set_target_properties(${PLUGIN_NAME} PROPERTIES
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "include/federated_plugin_windows/federated_plugin_windows_plugin.h"
|
||||
|
||||
// This must be included before many other Windows headers.
|
||||
#include <windows.h>
|
||||
|
||||
#include <flutter/method_channel.h>
|
||||
@@ -15,73 +16,74 @@
|
||||
|
||||
namespace {
|
||||
|
||||
class FederatedPluginWindowsPlugin : public flutter::Plugin {
|
||||
public:
|
||||
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows* registrar);
|
||||
class FederatedPluginWindowsPlugin : public flutter::Plugin {
|
||||
public:
|
||||
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
|
||||
|
||||
FederatedPluginWindowsPlugin();
|
||||
FederatedPluginWindowsPlugin();
|
||||
|
||||
virtual ~FederatedPluginWindowsPlugin();
|
||||
virtual ~FederatedPluginWindowsPlugin();
|
||||
|
||||
private:
|
||||
// Called when a method is called on this plugin's channel from Dart.
|
||||
void HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue>& method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
};
|
||||
private:
|
||||
// Called when a method is called on this plugin's channel from Dart.
|
||||
void HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
};
|
||||
|
||||
void FederatedPluginWindowsPlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarWindows* registrar) {
|
||||
auto channel =
|
||||
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
|
||||
registrar->messenger(), "battery",
|
||||
&flutter::StandardMethodCodec::GetInstance());
|
||||
// static
|
||||
void FederatedPluginWindowsPlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarWindows *registrar) {
|
||||
auto channel =
|
||||
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
|
||||
registrar->messenger(), "battery",
|
||||
&flutter::StandardMethodCodec::GetInstance());
|
||||
|
||||
auto plugin = std::make_unique<FederatedPluginWindowsPlugin>();
|
||||
auto plugin = std::make_unique<FederatedPluginWindowsPlugin>();
|
||||
|
||||
channel->SetMethodCallHandler(
|
||||
[plugin_pointer = plugin.get()](const auto& call, auto result) {
|
||||
plugin_pointer->HandleMethodCall(call, std::move(result));
|
||||
});
|
||||
|
||||
registrar->AddPlugin(std::move(plugin));
|
||||
}
|
||||
|
||||
FederatedPluginWindowsPlugin::FederatedPluginWindowsPlugin() {}
|
||||
|
||||
FederatedPluginWindowsPlugin::~FederatedPluginWindowsPlugin() {}
|
||||
|
||||
void FederatedPluginWindowsPlugin::HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue>& method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (method_call.method_name().compare("getBatteryLevel") == 0) {
|
||||
SYSTEM_POWER_STATUS systemPower;
|
||||
// GetSystemPowerStatus will retrieve the power status of the system.
|
||||
if (GetSystemPowerStatus(&systemPower)) {
|
||||
int batteryLevel = systemPower.BatteryLifePercent;
|
||||
// The batteryLevel value in the range 0 to 100, or 255 if status is unknown.
|
||||
if (batteryLevel != 255) {
|
||||
flutter::EncodableValue response(batteryLevel);
|
||||
result->Success(&response);
|
||||
}
|
||||
else {
|
||||
result->Error("STATUS_UNAVAILABLE", "Not able to determine battery level.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
result->Error("STATUS_UNAVAILABLE", "Not able to determine battery level.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
result->NotImplemented();
|
||||
}
|
||||
}
|
||||
channel->SetMethodCallHandler(
|
||||
[plugin_pointer = plugin.get()](const auto &call, auto result) {
|
||||
plugin_pointer->HandleMethodCall(call, std::move(result));
|
||||
});
|
||||
|
||||
registrar->AddPlugin(std::move(plugin));
|
||||
}
|
||||
|
||||
FederatedPluginWindowsPlugin::FederatedPluginWindowsPlugin() {}
|
||||
|
||||
FederatedPluginWindowsPlugin::~FederatedPluginWindowsPlugin() {}
|
||||
|
||||
void FederatedPluginWindowsPlugin::HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (method_call.method_name().compare("getBatteryLevel") == 0) {
|
||||
SYSTEM_POWER_STATUS systemPower;
|
||||
// GetSystemPowerStatus will retrieve the power status of the system.
|
||||
if (GetSystemPowerStatus(&systemPower)) {
|
||||
int batteryLevel = systemPower.BatteryLifePercent;
|
||||
// The batteryLevel value in the range 0 to 100, or 255 if status is unknown.
|
||||
if (batteryLevel != 255) {
|
||||
flutter::EncodableValue response(batteryLevel);
|
||||
result->Success(&response);
|
||||
}
|
||||
else {
|
||||
result->Error("STATUS_UNAVAILABLE", "Not able to determine battery level.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
result->Error("STATUS_UNAVAILABLE", "Not able to determine battery level.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
result->NotImplemented();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void FederatedPluginWindowsPluginRegisterWithRegistrar(
|
||||
FlutterDesktopPluginRegistrarRef registrar) {
|
||||
FederatedPluginWindowsPlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarManager::GetInstance()
|
||||
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
|
||||
FlutterDesktopPluginRegistrarRef registrar) {
|
||||
FederatedPluginWindowsPlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarManager::GetInstance()
|
||||
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user