From 66dfeb5373add98155de712f4304b058cf90ff88 Mon Sep 17 00:00:00 2001
From: guoqing <guoqingkuku@163.com>
Date: Wed, 14 Dec 2022 18:14:25 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A8=E9=80=81=E6=8F=92?=
 =?UTF-8?q?=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore                                    |  3 +-
 android/.gitignore                            |  2 +
 .../com/jarvanmo/rammus/RammusPlugin.kt       | 59 ++++++++++++-------
 example/test/widget_test.dart                 | 27 ---------
 test/rammus_test.dart                         | 21 -------
 5 files changed, 41 insertions(+), 71 deletions(-)
 delete mode 100644 example/test/widget_test.dart
 delete mode 100644 test/rammus_test.dart

diff --git a/.gitignore b/.gitignore
index c3bd594..12d67f8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,4 +10,5 @@ build/
 
 *.iml
 
-*.lock
\ No newline at end of file
+*.lock
+/android/gradlew.bat
diff --git a/android/.gitignore b/android/.gitignore
index c6cbe56..9fe586e 100644
--- a/android/.gitignore
+++ b/android/.gitignore
@@ -6,3 +6,5 @@
 .DS_Store
 /build
 /captures
+/gradle/
+/gradlew
diff --git a/android/src/main/kotlin/com/jarvanmo/rammus/RammusPlugin.kt b/android/src/main/kotlin/com/jarvanmo/rammus/RammusPlugin.kt
index 65cd2e7..ba9da68 100644
--- a/android/src/main/kotlin/com/jarvanmo/rammus/RammusPlugin.kt
+++ b/android/src/main/kotlin/com/jarvanmo/rammus/RammusPlugin.kt
@@ -10,29 +10,28 @@ import android.content.Context
 import android.content.pm.PackageManager
 import android.os.Build
 import android.os.Handler
+import android.os.Looper
 import android.util.Log
 import com.alibaba.sdk.android.push.CommonCallback
+import com.alibaba.sdk.android.push.HonorRegister
 import com.alibaba.sdk.android.push.huawei.HuaWeiRegister
 import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory
 import com.alibaba.sdk.android.push.register.*
+import io.flutter.embedding.engine.plugins.FlutterPlugin
 import io.flutter.plugin.common.MethodCall
 import io.flutter.plugin.common.MethodChannel
 import io.flutter.plugin.common.MethodChannel.MethodCallHandler
 import io.flutter.plugin.common.MethodChannel.Result
-import io.flutter.plugin.common.PluginRegistry.Registrar
 
 
-class RammusPlugin(private val registrar: Registrar, private val methodChannel: MethodChannel) : MethodCallHandler {
+
+class RammusPlugin :
+    FlutterPlugin, MethodCallHandler {
     companion object {
         private const val TAG = "RammusPlugin"
-        private val inHandler = Handler()
+        private val inHandler = Handler(Looper.getMainLooper())
         private var gottenApplication : Application? = null
-        @JvmStatic
-        fun registerWith(registrar: Registrar) {
-            val channel = MethodChannel(registrar.messenger(), "com.jarvanmo/rammus")
-            RammusPushHandler.methodChannel = channel
-            channel.setMethodCallHandler(RammusPlugin(registrar, channel))
-        }
+
         @JvmStatic
         fun initPushService(application: Application){
             gottenApplication = application
@@ -41,6 +40,7 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
             pushService.setPushIntentService(RammusPushIntentService::class.java)
         }
     }
+    private lateinit var channel : MethodChannel
 
     override fun onMethodCall(call: MethodCall, result: Result) {
         when (call.method) {
@@ -69,10 +69,10 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
     private fun register() {
         if (gottenApplication == null) {
             Log.w(TAG, "娉ㄥ唽鎺ㄩ€佹湇鍔″け璐ワ紝璇锋鏌ユ槸鍚﹀湪杩愯鏈鍙ュ墠鎵ц浜哷RammusPlugin.initPushService`.")
-            return;
+            return
         }
         val pushService = PushServiceFactory.getCloudPushService()
-        pushService.register(gottenApplication!!.applicationContext, object : CommonCallback {
+        pushService.register(gottenApplication, object : CommonCallback {
             override fun onSuccess(response: String?) {
                 inHandler.postDelayed({
                     RammusPushHandler.methodChannel?.invokeMethod("initCloudChannelResult", mapOf(
@@ -99,26 +99,31 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
         if ((xiaomiAppId != null && xiaomiAppId.isNotBlank())
             && (xiaomiAppKey != null && xiaomiAppKey.isNotBlank())){
             Log.d(TAG, "姝e湪娉ㄥ唽灏忕背鎺ㄩ€佹湇鍔�...")
-            MiPushRegister.register(gottenApplication!!.applicationContext, xiaomiAppId, xiaomiAppKey)
+            MiPushRegister.register(gottenApplication, xiaomiAppId, xiaomiAppKey)
         }
         val huaweiAppId = appInfo.metaData.getString("com.huawei.hms.client.appid")
         if (huaweiAppId != null && huaweiAppId.toString().isNotBlank()){
             Log.d(TAG, "姝e湪娉ㄥ唽鍗庝负鎺ㄩ€佹湇鍔�...")
-            HuaWeiRegister.register(gottenApplication!!)
+            HuaWeiRegister.register(gottenApplication)
         }
         val oppoAppKey = appInfo.metaData.getString("com.oppo.push.client.app_key")
         val oppoAppSecret = appInfo.metaData.getString("com.oppo.push.client.app_secret")
         if ((oppoAppKey != null && oppoAppKey.isNotBlank())
             && (oppoAppSecret != null && oppoAppSecret.isNotBlank())){
             Log.d(TAG, "姝e湪娉ㄥ唽Oppo鎺ㄩ€佹湇鍔�...")
-            OppoRegister.register(gottenApplication!!.applicationContext, oppoAppKey, oppoAppSecret)
+            OppoRegister.register(gottenApplication, oppoAppKey, oppoAppSecret)
         }
         val vivoAppId = appInfo.metaData.getString("com.vivo.push.app_id")
         val vivoApiKey = appInfo.metaData.getString("com.vivo.push.api_key")
         if ((vivoAppId != null && vivoAppId.isNotBlank())
             && (vivoApiKey != null && vivoApiKey.isNotBlank())){
             Log.d(TAG, "姝e湪娉ㄥ唽Vivo鎺ㄩ€佹湇鍔�...")
-            VivoRegister.register(gottenApplication!!.applicationContext)
+            VivoRegister.register(gottenApplication)
+        }
+        val hihonorId = appInfo.metaData.getString("com.hihonor.push.app_id")
+        if (hihonorId != null && hihonorId.toString().isNotBlank()){
+            Log.d(TAG, "姝e湪娉ㄥ唽鑽h€€鎺ㄩ€佹湇鍔�...")
+            HonorRegister.register(gottenApplication)
         }
     }
 
@@ -431,15 +436,15 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
     private fun setupNotificationManager(call: MethodCall, result: Result) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             val channels = call.arguments as List<Map<String, Any?>>
-            val mNotificationManager = registrar.context().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+            val mNotificationManager = gottenApplication?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
             val notificationChannels = mutableListOf<NotificationChannel>()
             for (channel in channels){
                 // 閫氱煡娓犻亾鐨刬d
-                val id = channel["id"] ?: registrar.context().packageName
+                val id = channel["id"] ?: gottenApplication?.packageName
                 // 鐢ㄦ埛鍙互鐪嬪埌鐨勯€氱煡娓犻亾鐨勫悕瀛�.
-                val name = channel["name"] ?: registrar.context().packageName
+                val name = channel["name"] ?: gottenApplication?.packageName
                 // 鐢ㄦ埛鍙互鐪嬪埌鐨勯€氱煡娓犻亾鐨勬弿杩�
-                val description = channel["description"] ?: registrar.context().packageName
+                val description = channel["description"] ?:gottenApplication?.packageName
                 val importance = channel["importance"] ?: NotificationManager.IMPORTANCE_DEFAULT
                 val mChannel = NotificationChannel(id as String, name as String, importance as Int)
                 // 閰嶇疆閫氱煡娓犻亾鐨勫睘鎬�
@@ -460,7 +465,7 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
         val appInfo = gottenApplication!!.packageManager
             .getApplicationInfo(gottenApplication!!.packageName, PackageManager.GET_META_DATA)
 
-        var num = call.argument("num") as Int? ?: 0
+        val num = call.argument("num") as Int? ?: 0
         setHuaWeiApplicationBadgeNumber(num,appInfo)
 
     }
@@ -471,8 +476,8 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
         if (huaweiAppId != null && huaweiAppId.toString().isNotBlank()){
             try {
                 val bundle = Bundle()
-                bundle.putString("package", registrar.context().packageName) // com.test.badge is your package name
-                bundle.putString("class", registrar.context().packageName+".MainActivity") // com.test. badge.MainActivity is your apk main activity
+                bundle.putString("package", gottenApplication?.packageName) // com.test.badge is your package name
+                bundle.putString("class", gottenApplication?.packageName+".MainActivity") // com.test. badge.MainActivity is your apk main activity
                 bundle.putInt("badgenumber", num)
                 gottenApplication!!.contentResolver.call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle)
 
@@ -483,4 +488,14 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
 
     }
 
+    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
+        channel = MethodChannel(flutterPluginBinding.binaryMessenger,  "com.jarvanmo/rammus")
+        channel.setMethodCallHandler(this)
+
+    }
+
+    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
+        channel.setMethodCallHandler(null)
+    }
+
 }
diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart
deleted file mode 100644
index 0e8b1bf..0000000
--- a/example/test/widget_test.dart
+++ /dev/null
@@ -1,27 +0,0 @@
-// This is a basic Flutter widget test.
-//
-// To perform an interaction with a widget in your test, use the WidgetTester
-// utility that Flutter provides. For example, you can send tap and scroll
-// gestures. You can also use WidgetTester to find child widgets in the widget
-// tree, read text, and verify that the values of widget properties are correct.
-
-import 'package:flutter/material.dart';
-import 'package:flutter_test/flutter_test.dart';
-
-import 'package:rammus_example/main.dart';
-
-void main() {
-  testWidgets('Verify Platform version', (WidgetTester tester) async {
-    // Build our app and trigger a frame.
-    await tester.pumpWidget(MyApp());
-
-    // Verify that platform version is retrieved.
-    expect(
-      find.byWidgetPredicate(
-        (Widget widget) => widget is Text &&
-                           widget.data!.startsWith('Running on:'),
-      ),
-      findsOneWidget,
-    );
-  });
-}
diff --git a/test/rammus_test.dart b/test/rammus_test.dart
deleted file mode 100644
index 55d2bf9..0000000
--- a/test/rammus_test.dart
+++ /dev/null
@@ -1,21 +0,0 @@
-import 'package:flutter/services.dart';
-import 'package:flutter_test/flutter_test.dart';
-import 'package:rammus/rammus.dart';
-
-void main() {
-  const MethodChannel channel = MethodChannel('rammus');
-
-  setUp(() {
-    channel.setMockMethodCallHandler((MethodCall methodCall) async {
-      return '42';
-    });
-  });
-
-  tearDown(() {
-    channel.setMockMethodCallHandler(null);
-  });
-
-  test('getPlatformVersion', () async {
-//    expect(await Rammus.platformVersion, '42');
-  });
-}
-- 
2.17.1