From 6809503e0befa0a6dca1f7ad8d42a8310a1fefe7 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Fri, 1 Sep 2023 12:27:23 +0800 Subject: [PATCH] Refactor: extract native log package --- app/pubspec.lock | 7 +++ app/pubspec.yaml | 2 + np_platform_log/.gitignore | 30 +++++++++++ np_platform_log/.metadata | 30 +++++++++++ np_platform_log/analysis_options.yaml | 1 + np_platform_log/android/.gitignore | 9 ++++ np_platform_log/android/build.gradle | 52 +++++++++++++++++++ np_platform_log/android/gradle.properties | 3 ++ .../gradle/wrapper/gradle-wrapper.properties | 5 ++ np_platform_log/android/settings.gradle | 1 + .../android/src/main/AndroidManifest.xml | 3 ++ .../com/nkming/nc_photos/np_platform_log/K.kt | 7 +++ .../np_platform_log/LogcatChannelHandler.kt | 34 ++++++++++++ .../np_platform_log/NpPlatformLogPlugin.kt | 26 ++++++++++ np_platform_log/lib/np_platform_log.dart | 3 ++ np_platform_log/lib/src/k.dart | 1 + .../lib/src/platform_log.dart | 5 +- np_platform_log/pubspec.yaml | 24 +++++++++ .../nc_photos/plugin/LogcatChannelHandler.kt | 34 ------------ .../nkming/nc_photos/plugin/NcPhotosPlugin.kt | 9 ---- plugin/lib/nc_photos_plugin.dart | 1 - 21 files changed, 241 insertions(+), 46 deletions(-) create mode 100644 np_platform_log/.gitignore create mode 100644 np_platform_log/.metadata create mode 100644 np_platform_log/analysis_options.yaml create mode 100644 np_platform_log/android/.gitignore create mode 100644 np_platform_log/android/build.gradle create mode 100644 np_platform_log/android/gradle.properties create mode 100644 np_platform_log/android/gradle/wrapper/gradle-wrapper.properties create mode 100644 np_platform_log/android/settings.gradle create mode 100644 np_platform_log/android/src/main/AndroidManifest.xml create mode 100644 np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/K.kt create mode 100644 np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/LogcatChannelHandler.kt create mode 100644 np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/NpPlatformLogPlugin.kt create mode 100644 np_platform_log/lib/np_platform_log.dart create mode 100644 np_platform_log/lib/src/k.dart rename plugin/lib/src/logcat.dart => np_platform_log/lib/src/platform_log.dart (69%) create mode 100644 np_platform_log/pubspec.yaml delete mode 100644 plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/LogcatChannelHandler.kt diff --git a/app/pubspec.lock b/app/pubspec.lock index 5d0e9a45..11a24710 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -1025,6 +1025,13 @@ packages: relative: true source: path version: "0.0.1" + np_platform_log: + dependency: "direct main" + description: + path: "../np_platform_log" + relative: true + source: path + version: "0.0.1" np_platform_permission: dependency: "direct main" description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index dc05a1cf..56ef3ace 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -113,6 +113,8 @@ dependencies: path: ../np_platform_image_processor np_platform_lock: path: ../np_platform_lock + np_platform_log: + path: ../np_platform_log np_platform_permission: path: ../np_platform_permission np_platform_raw_image: diff --git a/np_platform_log/.gitignore b/np_platform_log/.gitignore new file mode 100644 index 00000000..96486fd9 --- /dev/null +++ b/np_platform_log/.gitignore @@ -0,0 +1,30 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +.packages +build/ diff --git a/np_platform_log/.metadata b/np_platform_log/.metadata new file mode 100644 index 00000000..b0ed2b59 --- /dev/null +++ b/np_platform_log/.metadata @@ -0,0 +1,30 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled. + +version: + revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + channel: stable + +project_type: plugin + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + - platform: android + create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - 'lib/main.dart' + - 'ios/Runner.xcodeproj/project.pbxproj' diff --git a/np_platform_log/analysis_options.yaml b/np_platform_log/analysis_options.yaml new file mode 100644 index 00000000..f92d2567 --- /dev/null +++ b/np_platform_log/analysis_options.yaml @@ -0,0 +1 @@ +include: package:np_lints/np.yaml diff --git a/np_platform_log/android/.gitignore b/np_platform_log/android/.gitignore new file mode 100644 index 00000000..161bdcda --- /dev/null +++ b/np_platform_log/android/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.cxx diff --git a/np_platform_log/android/build.gradle b/np_platform_log/android/build.gradle new file mode 100644 index 00000000..fd5e03a8 --- /dev/null +++ b/np_platform_log/android/build.gradle @@ -0,0 +1,52 @@ +group 'com.nkming.nc_photos.np_platform_log' +version '1.0-SNAPSHOT' + +buildscript { + ext.kotlin_version = '1.8.20' + repositories { + google() + mavenCentral() + gradlePluginPortal() + } + + dependencies { + classpath 'com.android.tools.build:gradle:7.4.2' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +allprojects { + repositories { + google() + mavenCentral() + } +} + +apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' + +android { + namespace 'com.nkming.nc_photos.np_platform_log' + compileSdk 31 + + defaultConfig { + minSdk 21 + } + + buildTypes { + release { + minifyEnabled false + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + implementation "androidx.annotation:annotation:1.6.0" +} diff --git a/np_platform_log/android/gradle.properties b/np_platform_log/android/gradle.properties new file mode 100644 index 00000000..94adc3a3 --- /dev/null +++ b/np_platform_log/android/gradle.properties @@ -0,0 +1,3 @@ +org.gradle.jvmargs=-Xmx1536M +android.useAndroidX=true +android.enableJetifier=true diff --git a/np_platform_log/android/gradle/wrapper/gradle-wrapper.properties b/np_platform_log/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..98debb84 --- /dev/null +++ b/np_platform_log/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/np_platform_log/android/settings.gradle b/np_platform_log/android/settings.gradle new file mode 100644 index 00000000..5a308afd --- /dev/null +++ b/np_platform_log/android/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'np_platform_log' diff --git a/np_platform_log/android/src/main/AndroidManifest.xml b/np_platform_log/android/src/main/AndroidManifest.xml new file mode 100644 index 00000000..9bd55c9e --- /dev/null +++ b/np_platform_log/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + diff --git a/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/K.kt b/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/K.kt new file mode 100644 index 00000000..2a77cf8c --- /dev/null +++ b/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/K.kt @@ -0,0 +1,7 @@ +package com.nkming.nc_photos.np_platform_log + +internal interface K { + companion object { + const val LIB_ID = "com.nkming.nc_photos.np_platform_log" + } +} diff --git a/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/LogcatChannelHandler.kt b/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/LogcatChannelHandler.kt new file mode 100644 index 00000000..656b30cf --- /dev/null +++ b/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/LogcatChannelHandler.kt @@ -0,0 +1,34 @@ +package com.nkming.nc_photos.np_platform_log + +import io.flutter.plugin.common.MethodCall +import io.flutter.plugin.common.MethodChannel + +internal class LogcatChannelHandler : MethodChannel.MethodCallHandler { + companion object { + const val METHOD_CHANNEL = "${K.LIB_ID}/logcat_method" + } + + override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { + when (call.method) { + "dump" -> { + try { + dump(result) + } catch (e: Throwable) { + result.error("systemException", e.toString(), null) + } + } + + else -> result.notImplemented() + } + } + + private fun dump(result: MethodChannel.Result) { + val logs = StringBuilder() + val process = Runtime.getRuntime().exec("logcat -d") + process.inputStream.bufferedReader().use { + while (it.readLine()?.also(logs::appendLine) != null) { + } + } + result.success(logs.toString()) + } +} diff --git a/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/NpPlatformLogPlugin.kt b/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/NpPlatformLogPlugin.kt new file mode 100644 index 00000000..bde6551e --- /dev/null +++ b/np_platform_log/android/src/main/kotlin/com/nkming/nc_photos/np_platform_log/NpPlatformLogPlugin.kt @@ -0,0 +1,26 @@ +package com.nkming.nc_photos.np_platform_log + +import androidx.annotation.NonNull +import io.flutter.embedding.engine.plugins.FlutterPlugin +import io.flutter.plugin.common.MethodChannel + +class NpPlatformLogPlugin : FlutterPlugin { + override fun onAttachedToEngine( + @NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding + ) { + val logcatChannelHandler = LogcatChannelHandler() + logcatMethodChannel = MethodChannel( + flutterPluginBinding.binaryMessenger, + LogcatChannelHandler.METHOD_CHANNEL + ) + logcatMethodChannel.setMethodCallHandler(logcatChannelHandler) + } + + override fun onDetachedFromEngine( + @NonNull binding: FlutterPlugin.FlutterPluginBinding + ) { + logcatMethodChannel.setMethodCallHandler(null) + } + + private lateinit var logcatMethodChannel: MethodChannel +} diff --git a/np_platform_log/lib/np_platform_log.dart b/np_platform_log/lib/np_platform_log.dart new file mode 100644 index 00000000..230e0253 --- /dev/null +++ b/np_platform_log/lib/np_platform_log.dart @@ -0,0 +1,3 @@ +library np_platform_log; + +export 'src/platform_log.dart'; diff --git a/np_platform_log/lib/src/k.dart b/np_platform_log/lib/src/k.dart new file mode 100644 index 00000000..f5fbe041 --- /dev/null +++ b/np_platform_log/lib/src/k.dart @@ -0,0 +1 @@ +const libId = "com.nkming.nc_photos.np_platform_log"; diff --git a/plugin/lib/src/logcat.dart b/np_platform_log/lib/src/platform_log.dart similarity index 69% rename from plugin/lib/src/logcat.dart rename to np_platform_log/lib/src/platform_log.dart index 2a7a57de..7e2ed76a 100644 --- a/plugin/lib/src/logcat.dart +++ b/np_platform_log/lib/src/platform_log.dart @@ -1,9 +1,10 @@ import 'dart:async'; import 'package:flutter/services.dart'; -import 'package:nc_photos_plugin/src/k.dart' as k; +import 'package:np_platform_log/src/k.dart' as k; -class Logcat { +class PlatformLog { + /// Get the current native logs static Future dump() async { return await _methodChannel.invokeMethod("dump"); } diff --git a/np_platform_log/pubspec.yaml b/np_platform_log/pubspec.yaml new file mode 100644 index 00000000..c026a268 --- /dev/null +++ b/np_platform_log/pubspec.yaml @@ -0,0 +1,24 @@ +name: np_platform_log +description: Retrieve logs from native platforms +version: 0.0.1 +homepage: +publish_to: none + +environment: + sdk: '>=2.19.6 <3.0.0' + flutter: ">=3.3.0" + +dependencies: + flutter: + sdk: flutter + +dev_dependencies: + np_lints: + path: ../np_lints + +flutter: + plugin: + platforms: + android: + package: com.nkming.nc_photos.np_platform_log + pluginClass: NpPlatformLogPlugin diff --git a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/LogcatChannelHandler.kt b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/LogcatChannelHandler.kt deleted file mode 100644 index f59788bc..00000000 --- a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/LogcatChannelHandler.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.nkming.nc_photos.plugin - -import io.flutter.plugin.common.MethodCall -import io.flutter.plugin.common.MethodChannel - -internal class LogcatChannelHandler : MethodChannel.MethodCallHandler { - companion object { - const val METHOD_CHANNEL = "${K.LIB_ID}/logcat_method" - } - - override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { - when (call.method) { - "dump" -> { - try { - dump(result) - } catch (e: Throwable) { - result.error("systemException", e.toString(), null) - } - } - - else -> result.notImplemented() - } - } - - private fun dump(result: MethodChannel.Result) { - val logs = StringBuilder() - val process = Runtime.getRuntime().exec("logcat -d") - process.inputStream.bufferedReader().use { - while (it.readLine()?.also(logs::appendLine) != null) { - } - } - result.success(logs.toString()) - } -} diff --git a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt index 2599f016..fac5edad 100644 --- a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt +++ b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt @@ -53,13 +53,6 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware, ContentUriChannelHandler(flutterPluginBinding.applicationContext) ) - val logcatChannelHandler = LogcatChannelHandler() - logcatMethodChannel = MethodChannel( - flutterPluginBinding.binaryMessenger, - LogcatChannelHandler.METHOD_CHANNEL - ) - logcatMethodChannel.setMethodCallHandler(logcatChannelHandler) - val preferenceChannelHandler = PreferenceChannelHandler(flutterPluginBinding.applicationContext) preferenceMethodChannel = MethodChannel( @@ -76,7 +69,6 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware, mediaStoreChannel.setStreamHandler(null) mediaStoreMethodChannel.setMethodCallHandler(null) contentUriMethodChannel.setMethodCallHandler(null) - logcatMethodChannel.setMethodCallHandler(null) preferenceMethodChannel.setMethodCallHandler(null) } @@ -129,7 +121,6 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware, private lateinit var mediaStoreChannel: EventChannel private lateinit var mediaStoreMethodChannel: MethodChannel private lateinit var contentUriMethodChannel: MethodChannel - private lateinit var logcatMethodChannel: MethodChannel private lateinit var preferenceMethodChannel: MethodChannel private lateinit var mediaStoreChannelHandler: MediaStoreChannelHandler diff --git a/plugin/lib/nc_photos_plugin.dart b/plugin/lib/nc_photos_plugin.dart index dd6e02eb..425aa4ec 100644 --- a/plugin/lib/nc_photos_plugin.dart +++ b/plugin/lib/nc_photos_plugin.dart @@ -2,7 +2,6 @@ library nc_photos_plugin; export 'src/content_uri.dart'; export 'src/exception.dart'; -export 'src/logcat.dart'; export 'src/media_store.dart'; export 'src/native_event.dart'; export 'src/notification.dart';