From 0a6eae13775ade152170f74276e9ae9a9856fa03 Mon Sep 17 00:00:00 2001 From: Username * Date: Sat, 23 Mar 2024 00:50:26 +0100 Subject: [PATCH] feat: remove device_info_plus and implement getting the android version directly in the app --- .../tk/user5522/timetable/MainActivity.kt | 29 +++++++++++++++++++ lib/components/settings/general.dart | 14 ++++----- lib/helpers/get_os_version.dart | 17 +++++++++++ lib/screens/settings_screen.dart | 7 ----- pubspec.lock | 24 --------------- pubspec.yaml | 1 - 6 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 lib/helpers/get_os_version.dart diff --git a/android/app/src/main/kotlin/tk/user5522/timetable/MainActivity.kt b/android/app/src/main/kotlin/tk/user5522/timetable/MainActivity.kt index 369e437..14e96c1 100644 --- a/android/app/src/main/kotlin/tk/user5522/timetable/MainActivity.kt +++ b/android/app/src/main/kotlin/tk/user5522/timetable/MainActivity.kt @@ -1,6 +1,35 @@ package tk.user5522.timetable import io.flutter.embedding.android.FlutterActivity +import android.os.Build +import androidx.annotation.NonNull +import io.flutter.embedding.engine.FlutterEngine +import io.flutter.plugin.common.MethodChannel + class MainActivity: FlutterActivity() { + private val CHANNEL = "tk.user5522.timetable/androidVersion" + + fun getAndroidVersion(): Int { + return Build.VERSION.SDK_INT + } + + override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { + call, result -> + if (call.method == "getAndroidVersion") { + val androidVersion = getAndroidVersion() + + if (androidVersion != -1) { + result.success(androidVersion) + } else { + // result.error("UNAVAILABLE", 0, null) + } + } else { + result.notImplemented() + } + } + } + } diff --git a/lib/components/settings/general.dart b/lib/components/settings/general.dart index 5771767..84ad506 100644 --- a/lib/components/settings/general.dart +++ b/lib/components/settings/general.dart @@ -1,9 +1,9 @@ -import 'package:device_info_plus/device_info_plus.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:timetable/components/settings/language_options.dart'; import 'package:timetable/components/settings/theme_options.dart'; +import 'package:timetable/helpers/get_os_version.dart'; import 'package:timetable/provider/language.dart'; import 'package:timetable/provider/settings.dart'; import 'package:timetable/provider/themes.dart'; @@ -32,16 +32,16 @@ class GeneralOptions extends ConsumerWidget { ), onTap: () {}, ), - FutureBuilder( - future: DeviceInfoPlugin().androidInfo, + FutureBuilder( + future: getAndroidVersion(), builder: ( BuildContext context, - AsyncSnapshot snapshot, + AsyncSnapshot snapshot, ) { - final androidDeviceInfo = snapshot.data; + final version = snapshot.data; - if (androidDeviceInfo != null && - androidDeviceInfo.version.sdkInt >= 31) { + if (version != null && + version >= 31) { return SwitchListTile( title: const Text("monet_theming").tr(), subtitle: const Text("Android 12+"), diff --git a/lib/helpers/get_os_version.dart b/lib/helpers/get_os_version.dart new file mode 100644 index 0000000..daba05e --- /dev/null +++ b/lib/helpers/get_os_version.dart @@ -0,0 +1,17 @@ +import 'dart:async'; +import 'package:flutter/services.dart'; + +const platform = MethodChannel('tk.user5522.timetable/androidVersion'); + +Future getAndroidVersion() async { + int version; + + try { + final result = await platform.invokeMethod('getAndroidVersion'); + version = result!; + } on PlatformException { + version = 0; + } + + return version; +} diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 2ec0388..09f1730 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -1,4 +1,3 @@ -import 'package:device_info_plus/device_info_plus.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -17,12 +16,6 @@ class SettingsScreen extends ConsumerWidget { super.key, }); - Future getAndroidVersion() async { - DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); - AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo; - return androidInfo.version.sdkInt; - } - @override Widget build(BuildContext context, WidgetRef ref) { final navbarToggle = ref.watch(settingsProvider).navbarVisible; diff --git a/pubspec.lock b/pubspec.lock index 8b06b76..a476949 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -209,22 +209,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.6" - device_info_plus: - dependency: "direct main" - description: - name: device_info_plus - sha256: "77f757b789ff68e4eaf9c56d1752309bd9f7ad557cb105b938a7f8eb89e59110" - url: "https://pub.dev" - source: hosted - version: "9.1.2" - device_info_plus_platform_interface: - dependency: transitive - description: - name: device_info_plus_platform_interface - sha256: d3b01d5868b50ae571cd1dc6e502fc94d956b665756180f7b16ead09e836fd64 - url: "https://pub.dev" - source: hosted - version: "7.0.0" drift: dependency: "direct main" description: @@ -882,14 +866,6 @@ packages: url: "https://pub.dev" source: hosted version: "5.2.0" - win32_registry: - dependency: transitive - description: - name: win32_registry - sha256: "41fd8a189940d8696b1b810efb9abcf60827b6cbfab90b0c43e8439e3a39d85a" - url: "https://pub.dev" - source: hosted - version: "1.1.2" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index f70bf12..7642f8d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,7 +38,6 @@ dependencies: flutter_hooks: ^0.20.5 hooks_riverpod: ^2.5.1 non_uniform_border: ^1.0.0 - device_info_plus: ^9.0.3 drift: ^2.16.0 sqlite3_flutter_libs: ^0.5.20 path_provider: ^2.1.2