diff --git a/CHANGELOG.md b/CHANGELOG.md index 3adf146..5f38219 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +## ## 0.1.1 * Fix plugin crashes in background on Android. ## 0.1.0 diff --git a/ios/.gitignore b/darwin/.gitignore similarity index 100% rename from ios/.gitignore rename to darwin/.gitignore diff --git a/ios/Assets/.gitkeep b/darwin/Assets/.gitkeep similarity index 100% rename from ios/Assets/.gitkeep rename to darwin/Assets/.gitkeep diff --git a/ios/Classes/ManagedConfigurationsPlugin.h b/darwin/Classes/ManagedConfigurationsPlugin.h similarity index 100% rename from ios/Classes/ManagedConfigurationsPlugin.h rename to darwin/Classes/ManagedConfigurationsPlugin.h diff --git a/ios/Classes/ManagedConfigurationsPlugin.m b/darwin/Classes/ManagedConfigurationsPlugin.m similarity index 100% rename from ios/Classes/ManagedConfigurationsPlugin.m rename to darwin/Classes/ManagedConfigurationsPlugin.m diff --git a/ios/Classes/SwiftManagedConfigurationsPlugin.swift b/darwin/Classes/SwiftManagedConfigurationsPlugin.swift similarity index 100% rename from ios/Classes/SwiftManagedConfigurationsPlugin.swift rename to darwin/Classes/SwiftManagedConfigurationsPlugin.swift diff --git a/ios/managed_configurations.podspec b/darwin/managed_configurations.podspec similarity index 100% rename from ios/managed_configurations.podspec rename to darwin/managed_configurations.podspec diff --git a/example/pubspec.lock b/example/pubspec.lock index 39ed3b4..beb4ef4 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -81,7 +81,7 @@ packages: path: ".." relative: true source: path - version: "0.1.1" + version: "0.1.2" matcher: dependency: transitive description: @@ -114,6 +114,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" + url: "https://pub.dev" + source: hosted + version: "2.1.8" sky_engine: dependency: transitive description: flutter @@ -176,5 +184,5 @@ packages: source: hosted version: "2.1.4" sdks: - dart: ">=3.0.0-0 <4.0.0" - flutter: ">=1.20.0" + dart: ">=3.0.0 <4.0.0" + flutter: ">=3.10.6" diff --git a/lib/managed_configurations.dart b/lib/managed_configurations.dart index 0a516a2..98c2802 100644 --- a/lib/managed_configurations.dart +++ b/lib/managed_configurations.dart @@ -1,11 +1,4 @@ -import 'dart:async'; -import 'dart:convert'; -import 'dart:io'; - -import 'package:flutter/services.dart'; - -const String getManagedConfiguration = "getManagedConfigurations"; -const String reportKeyedAppState = "reportKeyedAppState"; +import 'managed_configurations_platform_interface.dart'; enum Severity { SEVERITY_INFO, SEVERITY_ERROR } @@ -21,48 +14,16 @@ extension SeverityExtensions on Severity { } class ManagedConfigurations { - static const MethodChannel _managedConfigurationMethodChannel = - const MethodChannel('managed_configurations_method'); - static const EventChannel _managedConfigurationEventChannel = - const EventChannel('managed_configurations_event'); - - static StreamController?> - _mangedConfigurationsController = - StreamController?>.broadcast(); - - static Stream?> _managedConfigurationsStream = - _mangedConfigurationsController.stream.asBroadcastStream(); + Future getPlatformVersion() { + return ManagedConfigurationsPlatform.instance.getPlatformVersion(); + } - /// Returns a broadcast stream which calls on managed app configuration changes - /// Json will be returned - /// Call [dispose] when stream is not more necessary static Stream?> get mangedConfigurationsStream { - if (_actionApplicationRestrictionsChangedSubscription == null) { - _actionApplicationRestrictionsChangedSubscription = - _managedConfigurationEventChannel - .receiveBroadcastStream() - .listen((newManagedConfigurations) { - if (newManagedConfigurations != null) { - _mangedConfigurationsController - .add(json.decode(newManagedConfigurations)); - } - }); - } - return _managedConfigurationsStream; + return ManagedConfigurationsPlatform.mangedConfigurationsStream; } - static StreamSubscription? - _actionApplicationRestrictionsChangedSubscription; - - /// Returns managed app configurations as Json static Future?> get getManagedConfigurations async { - final String? rawJson = await _managedConfigurationMethodChannel - .invokeMethod(getManagedConfiguration); - if (rawJson != null) { - return json.decode(rawJson); - } else { - return null; - } + return ManagedConfigurationsPlatform.getManagedConfigurations; } /// This method is only supported on Android Platform @@ -72,20 +33,11 @@ class ManagedConfigurations { String? message, String? data, ) async { - if (Platform.isAndroid) { - await _managedConfigurationMethodChannel.invokeMethod( - reportKeyedAppState, - { - 'key': key, - 'severity': severity.toInteger(), - 'message': message, - 'data': data, - }, - ); - } + return ManagedConfigurationsPlatform.reportKeyedAppStates( + key, severity, message, data); } - static dispose() { - _actionApplicationRestrictionsChangedSubscription?.cancel(); + static void dispose() { + return ManagedConfigurationsPlatform.dispose(); } } diff --git a/lib/managed_configurations_method_channel.dart b/lib/managed_configurations_method_channel.dart new file mode 100644 index 0000000..83c4dd3 --- /dev/null +++ b/lib/managed_configurations_method_channel.dart @@ -0,0 +1,86 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; +import 'package:managed_configurations/managed_configurations.dart'; +import 'package:managed_configurations/managed_configurations_platform_interface.dart'; + +const String getManagedConfiguration = "getManagedConfigurations"; +const String reportKeyedAppState = "reportKeyedAppState"; + +/// An implementation of [ManagedConfigurationsPlatform] that uses method channels. +class MethodChannelManagedConfigurations extends ManagedConfigurationsPlatform { + /// The method channel used to interact with the native platform. + @visibleForTesting + final methodChannel = const MethodChannel('managed_configurations'); + + static const MethodChannel _managedConfigurationMethodChannel = + const MethodChannel('managed_configurations_method'); + static const EventChannel _managedConfigurationEventChannel = + const EventChannel('managed_configurations_event'); + + static StreamController?> + _mangedConfigurationsController = + StreamController?>.broadcast(); + + static Stream?> _managedConfigurationsStream = + _mangedConfigurationsController.stream.asBroadcastStream(); + + /// Returns a broadcast stream which calls on managed app configuration changes + /// Json will be returned + /// Call [dispose] when stream is not more necessary + static Stream?> get mangedConfigurationsStream { + if (_actionApplicationRestrictionsChangedSubscription == null) { + _actionApplicationRestrictionsChangedSubscription = + _managedConfigurationEventChannel + .receiveBroadcastStream() + .listen((newManagedConfigurations) { + if (newManagedConfigurations != null) { + _mangedConfigurationsController + .add(json.decode(newManagedConfigurations)); + } + }); + } + return _managedConfigurationsStream; + } + + static StreamSubscription? + _actionApplicationRestrictionsChangedSubscription; + + /// Returns managed app configurations as Json + static Future?> get getManagedConfigurations async { + final String? rawJson = await _managedConfigurationMethodChannel + .invokeMethod(getManagedConfiguration); + if (rawJson != null) { + return json.decode(rawJson); + } else { + return null; + } + } + + /// This method is only supported on Android Platform + static Future reportKeyedAppStates( + String key, + Severity severity, + String? message, + String? data, + ) async { + if (Platform.isAndroid) { + await _managedConfigurationMethodChannel.invokeMethod( + reportKeyedAppState, + { + 'key': key, + 'severity': severity.toInteger(), + 'message': message, + 'data': data, + }, + ); + } + } + + static dispose() { + _actionApplicationRestrictionsChangedSubscription?.cancel(); + } +} diff --git a/lib/managed_configurations_platform_interface.dart b/lib/managed_configurations_platform_interface.dart new file mode 100644 index 0000000..a9788a6 --- /dev/null +++ b/lib/managed_configurations_platform_interface.dart @@ -0,0 +1,53 @@ +import 'package:managed_configurations/managed_configurations.dart'; +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; + +import 'managed_configurations_method_channel.dart'; + +abstract class ManagedConfigurationsPlatform extends PlatformInterface { + /// Constructs a ManagedConfigurationsPlatform. + ManagedConfigurationsPlatform() : super(token: _token); + + static final Object _token = Object(); + + static ManagedConfigurationsPlatform _instance = + MethodChannelManagedConfigurations(); + + /// The default instance of [ManagedConfigurationsPlatform] to use. + /// + /// Defaults to [MethodChannelManagedConfigurations]. + static ManagedConfigurationsPlatform get instance => _instance; + + /// Platform-specific implementations should set this with their own + /// platform-specific class that extends [ManagedConfigurationsPlatform] when + /// they register themselves. + static set instance(ManagedConfigurationsPlatform instance) { + PlatformInterface.verifyToken(instance, _token); + _instance = instance; + } + + Future getPlatformVersion() { + throw UnimplementedError('platformVersion() has not been implemented.'); + } + + static Stream?> get mangedConfigurationsStream { + throw UnimplementedError('platformVersion() has not been implemented.'); + } + + static Future?> get getManagedConfigurations async { + throw UnimplementedError('platformVersion() has not been implemented.'); + } + + /// This method is only supported on Android Platform + static Future reportKeyedAppStates( + String key, + Severity severity, + String? message, + String? data, + ) async { + throw UnimplementedError('platformVersion() has not been implemented.'); + } + + static void dispose() { + throw UnimplementedError('platformVersion() has not been implemented.'); + } +} diff --git a/pubspec.lock b/pubspec.lock index 440f635..94e2339 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -54,6 +54,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_lints: + dependency: "direct dev" + description: + name: flutter_lints + sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04 + url: "https://pub.dev" + source: hosted + version: "2.0.3" flutter_test: dependency: "direct dev" description: flutter @@ -67,6 +75,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.7" + lints: + dependency: transitive + description: + name: lints + sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" + url: "https://pub.dev" + source: hosted + version: "2.1.1" matcher: dependency: transitive description: @@ -99,6 +115,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + plugin_platform_interface: + dependency: "direct main" + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" + url: "https://pub.dev" + source: hosted + version: "2.1.8" sky_engine: dependency: transitive description: flutter @@ -161,5 +185,5 @@ packages: source: hosted version: "2.1.4" sdks: - dart: ">=3.0.0-0 <4.0.0" - flutter: ">=1.20.0" + dart: ">=3.0.0 <4.0.0" + flutter: ">=3.10.6" diff --git a/pubspec.yaml b/pubspec.yaml index 0feca9f..3dc0cb6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,28 +1,39 @@ name: managed_configurations description: Plugin to support managed app configuration provided by a Mobile device management (MDM). -version: 0.1.1 +version: 0.1.2 homepage: https://github.com/mwaylabs/flutter-managed-configuration + environment: - sdk: ">=2.12.0" - flutter: ">=1.20.0" + sdk: ^3.0.0 + # Flutter versions prior to 3.7 did not support the + # sharedDarwinSource option. + flutter: ">=3.10.6" dependencies: flutter: sdk: flutter + plugin_platform_interface: ^2.0.2 dev_dependencies: flutter_test: sdk: flutter + flutter_lints: ^2.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec -# The following section is specific to Flutter. +# The following section is specific to Flutter packages. flutter: # This section identifies this Flutter project as a plugin project. - # The 'pluginClass' and Android 'package' identifiers should not ordinarily - # be modified. They are used by the tooling to maintain consistency when + # The 'pluginClass' specifies the class (in Java, Kotlin, Swift, Objective-C, etc.) + # which should be registered in the plugin registry. This is required for + # using method channels. + # The Android 'package' specifies package in which the registered class is. + # This is required for using method channels on Android. + # The 'ffiPlugin' specifies that native code should be built and bundled. + # This is required for using `dart:ffi`. + # All these are used by the tooling to maintain consistency when # adding or updating assets for this project. plugin: platforms: @@ -31,4 +42,7 @@ flutter: pluginClass: ManagedConfigurationsPlugin ios: pluginClass: ManagedConfigurationsPlugin - + sharedDarwinSource: true + macos: + pluginClass: ManagedConfigurationsPlugin + sharedDarwinSource: true \ No newline at end of file