diff --git a/.gitignore b/.gitignore index 4946cd9..51fc06f 100644 --- a/.gitignore +++ b/.gitignore @@ -98,3 +98,4 @@ coverage/ !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages +example/.flutter-plugins-dependencies diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index a639f72..31ffdcb 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,3 +1,5 @@ + + diff --git a/android/src/main/java/flutter/moum/headset_event/HeadsetBroadcastReceiver.java b/android/src/main/java/flutter/moum/headset_event/HeadsetBroadcastReceiver.java index 9915ba7..98dd2f4 100644 --- a/android/src/main/java/flutter/moum/headset_event/HeadsetBroadcastReceiver.java +++ b/android/src/main/java/flutter/moum/headset_event/HeadsetBroadcastReceiver.java @@ -1,5 +1,6 @@ package flutter.moum.headset_event; +import android.bluetooth.BluetoothAdapter; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -7,9 +8,10 @@ import android.view.KeyEvent; public class HeadsetBroadcastReceiver extends BroadcastReceiver { - private static final String TAG = "log"; + private static final String TAG = "HeadsetBroadcastReceiver"; HeadsetEventListener headsetEventListener; + public HeadsetBroadcastReceiver(HeadsetEventListener listener) { this.headsetEventListener = listener; } @@ -29,6 +31,23 @@ public void onReceive(Context context, Intent intent) { default: Log.d(TAG, "I have no idea what the headset state is"); } + } else if (intent.getAction().equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { + int connectionState = intent.getExtras().getInt(BluetoothAdapter.EXTRA_CONNECTION_STATE); + switch (connectionState) { + case BluetoothAdapter.STATE_CONNECTED: + headsetEventListener.onHeadsetConnect(); + break; + case BluetoothAdapter.STATE_DISCONNECTED: + headsetEventListener.onHeadsetDisconnect(); + break; + default: + break; + } + } else if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { + int connectionState = intent.getExtras().getInt(BluetoothAdapter.EXTRA_STATE); + if (connectionState == BluetoothAdapter.STATE_OFF) { + headsetEventListener.onHeadsetDisconnect(); + } } else { abortBroadcast(); @@ -36,8 +55,8 @@ public void onReceive(Context context, Intent intent) { if (key.getAction() == KeyEvent.ACTION_UP) { int keycode = key.getKeyCode(); - Log.d(TAG, "onReceive: "+keycode); - switch(keycode) { + Log.d(TAG, "onReceive: " + keycode); + switch (keycode) { case KeyEvent.KEYCODE_MEDIA_NEXT: headsetEventListener.onNextButtonPress(); break; @@ -49,4 +68,4 @@ public void onReceive(Context context, Intent intent) { } } } -} +} \ No newline at end of file diff --git a/android/src/main/java/flutter/moum/headset_event/HeadsetEventPlugin.java b/android/src/main/java/flutter/moum/headset_event/HeadsetEventPlugin.java index 0bf90d7..0f74778 100644 --- a/android/src/main/java/flutter/moum/headset_event/HeadsetEventPlugin.java +++ b/android/src/main/java/flutter/moum/headset_event/HeadsetEventPlugin.java @@ -1,65 +1,86 @@ package flutter.moum.headset_event; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothProfile; import android.content.Intent; import android.content.IntentFilter; -import android.util.Log; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; -import io.flutter.plugin.common.MethodChannel.MethodCallHandler; +import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; -/** HeadsetEventPlugin */ -public class HeadsetEventPlugin implements MethodCallHandler{ +/** + * HeadsetEventPlugin + */ +public class HeadsetEventPlugin implements FlutterPlugin { - public static MethodChannel headsetEventChannel; - public static int currentState = -1; - private static HeadsetBroadcastReceiver hReceiver; - private static final String TAG = "HeadsetEventPlugin"; + public static MethodChannel headsetEventChannel; + public static int currentState = -1; + private static HeadsetBroadcastReceiver hReceiver; + private static final String TAG = "HeadsetEventPlugin"; - /** Plugin registration. */ - public static void registerWith(Registrar registrar) { - headsetEventChannel = new MethodChannel(registrar.messenger(), "flutter.moum/headset_event"); - headsetEventChannel.setMethodCallHandler(new HeadsetEventPlugin()); - hReceiver = new HeadsetBroadcastReceiver(headsetEventListener); - IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); - registrar.activeContext().registerReceiver(hReceiver, filter); + /** + * Plugin registration. + */ + public static void registerWith(Registrar registrar) { + headsetEventChannel = new MethodChannel(registrar.messenger(), "flutter.moum/headset_event"); + headsetEventChannel.setMethodCallHandler(new HeadsetEventPlugin()); + hReceiver = new HeadsetBroadcastReceiver(headsetEventListener); + String actionHeadsetPlug = Intent.ACTION_HEADSET_PLUG; + String actionBluetoothConnectionState = BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED; + String actionBluetoothState = BluetoothAdapter.ACTION_STATE_CHANGED; + IntentFilter filter = new IntentFilter(); + filter.addAction(actionHeadsetPlug); + filter.addAction(actionBluetoothConnectionState); + filter.addAction(actionBluetoothState); + registrar.activeContext().registerReceiver(hReceiver, filter); - } + } + + @Override + public void onMethodCall(MethodCall call, Result result) { + if (call.method.equals("getPlatformVersion")) { + result.success("Android " + android.os.Build.VERSION.RELEASE); + } else if (call.method.equals("register")) { - @Override - public void onMethodCall(MethodCall call, Result result) { - if (call.method.equals("getPlatformVersion")) { - result.success("Android " + android.os.Build.VERSION.RELEASE); - } else if(call.method.equals("register")) { + } else if (call.method.equals("getCurrentState")) { + updateHeadsetStatus(); + result.success(currentState); + } else { + result.notImplemented(); + } + } - } else if(call.method.equals("getCurrentState")) { - result.success(currentState); - } else { - result.notImplemented(); + private void updateHeadsetStatus() { + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + boolean status = (bluetoothAdapter != null && BluetoothProfile.STATE_CONNECTED == + bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)); + if (status) { + currentState = 1; + } } - } - static HeadsetEventListener headsetEventListener = new HeadsetEventListener() { - @Override - public void onHeadsetConnect() { - headsetEventChannel.invokeMethod("connect", "true"); - } + static HeadsetEventListener headsetEventListener = new HeadsetEventListener() { + @Override + public void onHeadsetConnect() { + headsetEventChannel.invokeMethod("connect", "true"); + } - @Override - public void onHeadsetDisconnect() { - headsetEventChannel.invokeMethod("disconnect", "true"); - } + @Override + public void onHeadsetDisconnect() { + headsetEventChannel.invokeMethod("disconnect", "true"); + } - @Override - public void onNextButtonPress() { - headsetEventChannel.invokeMethod("nextButton", "true"); - } + @Override + public void onNextButtonPress() { + headsetEventChannel.invokeMethod("nextButton", "true"); + } - @Override - public void onPrevButtonPress() { - headsetEventChannel.invokeMethod("prevButton", "true"); - } - }; -} + @Override + public void onPrevButtonPress() { + headsetEventChannel.invokeMethod("prevButton", "true"); + } + }; +} \ No newline at end of file diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 9ba38c5..ed34eda 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -7,7 +7,7 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> + + diff --git a/example/android/app/src/main/java/flutter/moum/headset_event_example/MainActivity.java b/example/android/app/src/main/java/flutter/moum/headset_event_example/MainActivity.java index 452068a..38dfb7f 100644 --- a/example/android/app/src/main/java/flutter/moum/headset_event_example/MainActivity.java +++ b/example/android/app/src/main/java/flutter/moum/headset_event_example/MainActivity.java @@ -1,13 +1,6 @@ package flutter.moum.headset_event_example; -import android.os.Bundle; -import io.flutter.app.FlutterActivity; -import io.flutter.plugins.GeneratedPluginRegistrant; +import io.flutter.embedding.android.FlutterActivity; public class MainActivity extends FlutterActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - GeneratedPluginRegistrant.registerWith(this); - } } diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 53ae0ae..b6e61b6 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,3 +1,4 @@ android.enableJetifier=true android.useAndroidX=true org.gradle.jvmargs=-Xmx1536M +android.enableR8=true diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 7d6090f..d8cd4e2 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -14,9 +14,9 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/headset_event/ios" SPEC CHECKSUMS: - Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a + Flutter: 0e3d915762c693b495b44d77113d4970485de6ec headset_event: 7e97247360e17f7a182fcd35a65ebfcbf9a8fac9 PODFILE CHECKSUM: ebd43b443038e611b86ede96e613bd6033c49497 -COCOAPODS: 1.7.4 +COCOAPODS: 1.8.4 diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index dab63ae..a6297a3 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -9,12 +9,8 @@ /* Begin PBXBuildFile section */ 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; - 3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; }; - 3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 7225D5D1F3F201D9E23C8B47 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 23F5D1C3FC6231DA3DC5E5BE /* Pods_Runner.framework */; }; 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; }; - 9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9740EEBA1CF902C7004384FC /* Flutter.framework */; }; - 9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9740EEBA1CF902C7004384FC /* Flutter.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 9740EEB41CF90195004384FC /* Debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Debug.xcconfig */; }; 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; @@ -28,8 +24,6 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - 3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */, - 9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */, ); name = "Embed Frameworks"; runOnlyForDeploymentPostprocessing = 0; @@ -42,14 +36,12 @@ 23F5D1C3FC6231DA3DC5E5BE /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 358F5E74F65F84C78718142D /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = ""; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; - 3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = ""; }; 602F24C3F95A7E584885DFE7 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = ""; }; 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; }; 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; 9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = ""; }; 9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = ""; }; - 9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = ""; }; 97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; }; 97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -63,8 +55,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */, - 3B80C3941E831B6300D905FE /* App.framework in Frameworks */, 7225D5D1F3F201D9E23C8B47 /* Pods_Runner.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -83,9 +73,7 @@ 9740EEB11CF90186004384FC /* Flutter */ = { isa = PBXGroup; children = ( - 3B80C3931E831B6300D905FE /* App.framework */, 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */, - 9740EEBA1CF902C7004384FC /* Flutter.framework */, 9740EEB21CF90195004384FC /* Debug.xcconfig */, 7AFA3C8E1D35360C0083082E /* Release.xcconfig */, 9740EEB31CF90195004384FC /* Generated.xcconfig */, @@ -232,7 +220,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" thin"; + shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin"; }; 488B5AFEBFD8DB9D63FD3459 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; diff --git a/example/pubspec.lock b/example/pubspec.lock index 77bd29d..d304f6c 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,34 +1,104 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + url: "https://pub.dartlang.org" + source: hosted + version: "47.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + url: "https://pub.dartlang.org" + source: hosted + version: "4.7.0" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.1" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.2.0" + version: "2.9.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" - charcode: + version: "2.1.0" + build: dependency: transitive description: - name: charcode + name: build url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "2.3.1" + built_collection: + dependency: transitive + description: + name: built_collection + url: "https://pub.dartlang.org" + source: hosted + version: "5.1.1" + built_value: + dependency: transitive + description: + name: built_value + url: "https://pub.dartlang.org" + source: hosted + version: "8.4.2" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.1" + code_builder: + dependency: transitive + description: + name: code_builder + url: "https://pub.dartlang.org" + source: hosted + version: "4.4.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.16.0" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.2" cupertino_icons: dependency: "direct main" description: @@ -36,6 +106,34 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + dart_style: + dependency: transitive + description: + name: dart_style + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.4" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.4" + fixnum: + dependency: transitive + description: + name: fixnum + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" flutter: dependency: "direct main" description: flutter @@ -46,108 +144,157 @@ packages: description: flutter source: sdk version: "0.0.0" + glob: + dependency: transitive + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" headset_event: dependency: "direct dev" description: path: ".." relative: true source: path - version: "0.0.2" + version: "0.0.5" + logging: + dependency: transitive + description: + name: logging + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.5" + version: "0.12.12" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.5" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" - path: + version: "1.8.0" + mockito: dependency: transitive description: - name: path + name: mockito url: "https://pub.dartlang.org" source: hosted - version: "1.6.2" - pedantic: + version: "5.3.2" + package_config: dependency: transitive description: - name: pedantic + name: package_config url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" - quiver: + version: "2.1.0" + path: dependency: transitive description: - name: quiver + name: path url: "https://pub.dartlang.org" source: hosted - version: "2.0.3" + version: "1.8.2" + pub_semver: + dependency: transitive + description: + name: pub_semver + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" sky_engine: dependency: transitive description: flutter source: sdk version: "0.0.99" + source_gen: + dependency: transitive + description: + name: source_gen + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.6" source_span: dependency: transitive description: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.5.5" + version: "1.9.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.5" + version: "0.4.12" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.1" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.2" + watcher: + dependency: transitive + description: + name: watcher + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + yaml: + dependency: transitive + description: + name: yaml + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.1" sdks: - dart: ">=2.2.2 <3.0.0" + dart: ">=2.18.0 <3.0.0" diff --git a/ios/Classes/SwiftHeadsetEventPlugin.swift b/ios/Classes/SwiftHeadsetEventPlugin.swift index 21a6365..4eee689 100644 --- a/ios/Classes/SwiftHeadsetEventPlugin.swift +++ b/ios/Classes/SwiftHeadsetEventPlugin.swift @@ -38,7 +38,7 @@ public class SwiftHeadsetEventPlugin: NSObject, FlutterPlugin { ///////////////////////////////////////////////////////////// func registerAudioRouteChangeBlock(){ - NotificationCenter.default.addObserver( forName:NSNotification.Name.AVAudioSessionRouteChange, object: AVAudioSession.sharedInstance(), queue: nil) { notification in + NotificationCenter.default.addObserver( forName:AVAudioSession.routeChangeNotification, object: AVAudioSession.sharedInstance(), queue: nil) { notification in guard let userInfo = notification.userInfo, let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else { @@ -49,6 +49,8 @@ public class SwiftHeadsetEventPlugin: NSObject, FlutterPlugin { self.channel!.invokeMethod("connect",arguments: "true") case .oldDeviceUnavailable: self.channel!.invokeMethod("disconnect",arguments: "true") + case .override: + self.channel!.invokeMethod("disconnect",arguments: "true") default: () } } @@ -57,9 +59,9 @@ public class SwiftHeadsetEventPlugin: NSObject, FlutterPlugin { func HeadsetIsConnect() -> Int { let currentRoute = AVAudioSession.sharedInstance().currentRoute for output in currentRoute.outputs { - if output.portType == AVAudioSessionPortHeadphones { + if output.portType == AVAudioSession.Port.headphones || output.portType == AVAudioSession.Port.bluetoothA2DP{ return 1 - }else { + } else { return 0 } } diff --git a/lib/headset_event.dart b/lib/headset_event.dart index eb7061a..69e6670 100644 --- a/lib/headset_event.dart +++ b/lib/headset_event.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'package:flutter/services.dart'; import 'package:meta/meta.dart' show visibleForTesting; - typedef DetectPluggedCallback = Function(HeadsetState payload); enum HeadsetState { @@ -13,20 +12,19 @@ enum HeadsetState { } class HeadsetEvent { - - static HeadsetEvent _instance; + static HeadsetEvent? _instance; final MethodChannel _channel; - DetectPluggedCallback detectPluggedCallback; + DetectPluggedCallback? detectPluggedCallback; - factory HeadsetEvent() { + factory HeadsetEvent() { if (_instance == null) { final MethodChannel methodChannel = const MethodChannel('flutter.moum/headset_event'); _instance = HeadsetEvent.private(methodChannel); } - return _instance; + return _instance!; } - + @visibleForTesting HeadsetEvent.private(this._channel); @@ -37,7 +35,7 @@ class HeadsetEvent { Future get getCurrentState async { final int state = await _channel.invokeMethod('getCurrentState'); - switch(state) { + switch (state) { case 0: return Future.value(HeadsetState.DISCONNECT); case 1: @@ -54,20 +52,18 @@ class HeadsetEvent { } Future _handleMethod(MethodCall call) async { - switch(call.method) { + switch (call.method) { case "connect": - return detectPluggedCallback(HeadsetState.CONNECT); + return detectPluggedCallback?.call(HeadsetState.CONNECT); case "disconnect": - return detectPluggedCallback(HeadsetState.DISCONNECT); + return detectPluggedCallback?.call(HeadsetState.DISCONNECT); case "nextButton": - return detectPluggedCallback(HeadsetState.NEXT); + return detectPluggedCallback?.call(HeadsetState.NEXT); case "prevButton": - return detectPluggedCallback(HeadsetState.PREV); + return detectPluggedCallback?.call(HeadsetState.PREV); default: print('No idea'); - return detectPluggedCallback(HeadsetState.DISCONNECT); + return detectPluggedCallback?.call(HeadsetState.DISCONNECT); } } - - } diff --git a/pubspec.lock b/pubspec.lock index fbcb51e..f5b4a2d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,34 +1,132 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + url: "https://pub.dartlang.org" + source: hosted + version: "47.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + url: "https://pub.dartlang.org" + source: hosted + version: "4.7.0" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.1" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.2.0" + version: "2.9.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" - charcode: + version: "2.1.0" + build: + dependency: transitive + description: + name: build + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.1" + built_collection: dependency: transitive description: - name: charcode + name: built_collection url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "5.1.1" + built_value: + dependency: transitive + description: + name: built_value + url: "https://pub.dartlang.org" + source: hosted + version: "8.4.2" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.1" + code_builder: + dependency: transitive + description: + name: code_builder + url: "https://pub.dartlang.org" + source: hosted + version: "4.4.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.16.0" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.2" + dart_style: + dependency: transitive + description: + name: dart_style + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.4" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.4" + fixnum: + dependency: transitive + description: + name: fixnum + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" flutter: dependency: "direct main" description: flutter @@ -39,108 +137,150 @@ packages: description: flutter source: sdk version: "0.0.0" + glob: + dependency: transitive + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + logging: + dependency: transitive + description: + name: logging + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.5" + version: "0.12.12" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.5" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.8.0" mockito: dependency: "direct main" description: name: mockito url: "https://pub.dartlang.org" source: hosted - version: "4.1.1" - path: + version: "5.3.2" + package_config: dependency: transitive description: - name: path + name: package_config url: "https://pub.dartlang.org" source: hosted - version: "1.6.2" - pedantic: + version: "2.1.0" + path: dependency: transitive description: - name: pedantic + name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" - quiver: + version: "1.8.2" + pub_semver: dependency: transitive description: - name: quiver + name: pub_semver url: "https://pub.dartlang.org" source: hosted - version: "2.0.3" + version: "2.1.3" sky_engine: dependency: transitive description: flutter source: sdk version: "0.0.99" + source_gen: + dependency: transitive + description: + name: source_gen + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.6" source_span: dependency: transitive description: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.5.5" + version: "1.9.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.5" + version: "0.4.12" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.1" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.2" + watcher: + dependency: transitive + description: + name: watcher + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + yaml: + dependency: transitive + description: + name: yaml + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.1" sdks: - dart: ">=2.2.2 <3.0.0" + dart: ">=2.18.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index fb812ed..b52547c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,16 +1,15 @@ name: headset_event description: Flutter Plugin for headset events. Detect headset is plugged and unplugged. Get current headset state. -version: 0.0.2 -author: songyiYu, yenoss, FlutterMoum Group +version: 0.0.5 homepage: https://github.com/flutter-moum/flutter_headset_event environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ">=2.16.0 <3.0.0" dependencies: flutter: sdk: flutter - mockito: 3.0.0 + mockito: 5.3.2 dev_dependencies: flutter_test: diff --git a/test/headset_event_test.dart b/test/headset_event_test.dart index 96640a8..883e17b 100644 --- a/test/headset_event_test.dart +++ b/test/headset_event_test.dart @@ -6,12 +6,12 @@ import 'package:mockito/mockito.dart'; typedef DetectPluggedCallback = Function(HeadsetState payload); void main() { - MockMethodChannel methodChannel; - HeadsetEvent he; + late MockMethodChannel methodChannel; + late HeadsetEvent he; setUp(() { methodChannel = MockMethodChannel(); - he = HeadsetEvent.private(methodChannel); + he = HeadsetEvent.private(methodChannel); }); test('getCurrentState', () async { @@ -26,7 +26,6 @@ void main() { when(methodChannel.invokeMethod('getCurrentState')) .thenAnswer((Invocation invoke) => Future.value(-1)); expect(await he.getCurrentState, HeadsetState.DISCONNECT); - }); }