From 0942f92fa9d4454e4ac46c703468cdba582414cf Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 11 Sep 2023 13:36:39 -0700 Subject: [PATCH 1/4] In debug mode, display debug channel information in Settings (initial implementation) --- .../kotlin/im/cwtch/flwtch/MainActivity.kt | 27 ++++++++++++++-- lib/cwtch/cwtch.dart | 4 +++ lib/cwtch/ffi.dart | 6 ++++ lib/cwtch/gomobile.dart | 18 +++++++++++ lib/views/globalsettingsview.dart | 31 +++++++++++++++++-- 5 files changed, 82 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt index 400ece7c..a13b4690 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt @@ -29,7 +29,7 @@ import org.json.JSONObject import java.nio.file.Files import java.nio.file.Paths import java.util.concurrent.TimeUnit - +import kotlinx.coroutines.* class MainActivity: FlutterActivity() { override fun provideSplashScreen(): SplashScreen? = SplashView() @@ -38,6 +38,7 @@ class MainActivity: FlutterActivity() { // Channel to get app info private val CHANNEL_APP_INFO = "test.flutter.dev/applicationInfo" private val CALL_APP_INFO = "getNativeLibDir" + private val CALL_SERVICE_INFO = "getForegroundServiceInfo" private val ANDROID_SETTINGS_CHANNEL_NAME = "androidSettings" private val ANDROID_SETTINGS_CHANGE_NAME= "androidSettingsChanged" private var andoidSettingsChangeChannel: MethodChannel? = null @@ -189,7 +190,9 @@ class MainActivity: FlutterActivity() { when (call.method) { CALL_APP_INFO -> result.success(getNativeLibDir()) ?: result.error("Unavailable", "nativeLibDir not available", null); - else -> result.notImplemented() + CALL_SERVICE_INFO -> result.success(getForegroundServiceInfo()) + ?: result.error("Unavailable", "nativeLibDir not available", null); + else -> result.notImplemented() } } @@ -224,6 +227,26 @@ class MainActivity: FlutterActivity() { return ainfo.nativeLibraryDir } + + fun getForegroundServiceInfo(): HashMap { + Log.d("MainActivity.kt", "getForgegroundServiceInfo called...") + var hashMap : HashMap + = HashMap (); + val workmanager = WorkManager.getInstance(this); + if (workmanager != null) { + runBlocking { + var lastCancelAllTime = workmanager.getLastCancelAllTimeMillis().await(); + hashMap.put("workmanager.lastCancelAllTime", lastCancelAllTime.toString()); + } + val works = workmanager.getWorkInfosByTag(WORKER_TAG).get() + for (workInfo in works) { + hashMap.put("workmanager.${workInfo.id}.state", workInfo.state.toString()); + } + } + Log.d("MainActivity.kt", "getForgegroundServiceInfo finished...") + return hashMap + } + // receives messages from the ForegroundService (which provides, ironically enough, the backend) private fun handleCwtch(@NonNull call: MethodCall, @NonNull result: Result) { var method = call.method diff --git a/lib/cwtch/cwtch.dart b/lib/cwtch/cwtch.dart index cc41f3b0..f930d260 100644 --- a/lib/cwtch/cwtch.dart +++ b/lib/cwtch/cwtch.dart @@ -1,10 +1,14 @@ // To handle profiles that are "unencrypted" (i.e don't require a password to open) we currently create a profile with a defacto, hardcoded password. // Details: https://docs.openprivacy.ca/cwtch-security-handbook/profile_encryption_and_storage.html +import 'dart:collection'; + const DefaultPassword = "be gay do crime"; const LastMessageSeenTimeKey = "profile.lastMessageSeenTime"; abstract class Cwtch { + Future> PlatformChannelInfo(); + // ignore: non_constant_identifier_names Future Start(); // ignore: non_constant_identifier_names diff --git a/lib/cwtch/ffi.dart b/lib/cwtch/ffi.dart index 665276e4..f178c209 100644 --- a/lib/cwtch/ffi.dart +++ b/lib/cwtch/ffi.dart @@ -1,3 +1,4 @@ +import 'dart:collection'; import 'dart:convert'; import 'dart:ffi'; import 'dart:io'; @@ -1055,4 +1056,9 @@ class CwtchFfi implements Cwtch { malloc.free(utf8pattern); return searchID; } + + @override + Future> PlatformChannelInfo() { + throw HashMap(); + } } diff --git a/lib/cwtch/gomobile.dart b/lib/cwtch/gomobile.dart index ada66b34..fdd22556 100644 --- a/lib/cwtch/gomobile.dart +++ b/lib/cwtch/gomobile.dart @@ -1,3 +1,4 @@ +import 'dart:collection'; import 'dart:convert'; import 'package:cwtch/config.dart'; @@ -32,6 +33,23 @@ class CwtchGomobile implements Cwtch { late CwtchNotifier cwtchNotifier; bool _isL10nInit = false; + @override + Future> PlatformChannelInfo() async { + HashMap info = HashMap(); + + info.addAll({"appInfoPlatform.name": appInfoPlatform.name}); + info.addAll({"appInfoPlatform.codec": appInfoPlatform.codec.toString()}); + EnvironmentConfig.debugLog("getting foreground service info..."); + appInfoPlatform.invokeMapMethod('getForegroundServiceInfo').then((foregroundServiceInfoMap) { + EnvironmentConfig.debugLog("parsing foreground service info..."); + foregroundServiceInfoMap?.entries.forEach((element) { + info.addAll({element.key.toString(): element.value.toString()}); + }); + }); + EnvironmentConfig.debugLog("returning foreground service info..."); + return info; + } + CwtchGomobile(CwtchNotifier _cwtchNotifier) { print("gomobile.dart: CwtchGomobile()"); cwtchNotifier = _cwtchNotifier; diff --git a/lib/views/globalsettingsview.dart b/lib/views/globalsettingsview.dart index 5a8bfdbe..750df424 100644 --- a/lib/views/globalsettingsview.dart +++ b/lib/views/globalsettingsview.dart @@ -1,6 +1,6 @@ +import 'dart:collection'; import 'dart:convert'; import 'dart:io'; -import 'dart:math'; import 'package:cwtch/cwtch_icons_icons.dart'; import 'package:cwtch/models/servers.dart'; import 'package:cwtch/themes/juniper.dart'; @@ -635,7 +635,18 @@ class _GlobalSettingsViewState extends State { } }, ), - ) + ), + Visibility( + visible: EnvironmentConfig.BUILD_VER == dev_version, + child: FutureBuilder( + future: Provider.of(context).cwtch.PlatformChannelInfo(), + builder: (context, snapshot) { + if (snapshot.hasData) { + HashMap data = snapshot.data as HashMap; + return getPlatformInfo(settings, data); + } + return Container(); + })) ])))); }); }); @@ -665,6 +676,22 @@ class _GlobalSettingsViewState extends State { }, ); } + + getPlatformInfo(settings, HashMap platformChannelInfo) { + var sortedKeys = platformChannelInfo.keys.toList(); + sortedKeys.sort(); + var widgets = List.empty(growable: true); + sortedKeys.forEach((element) { + widgets.add(ListTile( + leading: Icon(Icons.android, color: settings.current().mainTextColor), + title: Text(element), + subtitle: Text(platformChannelInfo[element]!), + )); + }); + return Column( + children: widgets, + ); + } } bool checkDownloadDirectory(context, settings) { From 7f8150c8b7002a1f3cafa9f21d676ef050a11b3c Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 11 Sep 2023 13:38:03 -0700 Subject: [PATCH 2/4] Fix History Setting Icon --- lib/views/globalsettingsview.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/views/globalsettingsview.dart b/lib/views/globalsettingsview.dart index 750df424..bc34aa1a 100644 --- a/lib/views/globalsettingsview.dart +++ b/lib/views/globalsettingsview.dart @@ -366,7 +366,7 @@ class _GlobalSettingsViewState extends State { }, activeTrackColor: settings.theme.defaultButtonColor, inactiveTrackColor: settings.theme.defaultButtonDisabledColor, - secondary: Icon(CwtchIcons.block_unknown, color: settings.current().mainTextColor), + secondary: Icon(CwtchIcons.peer_history, color: settings.current().mainTextColor), ), SizedBox( height: 40, From 11fb137f6c1e3087f29b8fc3671566799e8f6f64 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 11 Sep 2023 13:40:44 -0700 Subject: [PATCH 3/4] Return empty platform info for desktop builds --- lib/cwtch/ffi.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cwtch/ffi.dart b/lib/cwtch/ffi.dart index f178c209..057eb904 100644 --- a/lib/cwtch/ffi.dart +++ b/lib/cwtch/ffi.dart @@ -1059,6 +1059,6 @@ class CwtchFfi implements Cwtch { @override Future> PlatformChannelInfo() { - throw HashMap(); + return Future.value(HashMap()); } } From 864d862219a11a6a626643fb94ffbb19af80a9e0 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 11 Sep 2023 13:45:17 -0700 Subject: [PATCH 4/4] Fix up logs --- android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt index a13b4690..ac807b5d 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt @@ -191,7 +191,7 @@ class MainActivity: FlutterActivity() { CALL_APP_INFO -> result.success(getNativeLibDir()) ?: result.error("Unavailable", "nativeLibDir not available", null); CALL_SERVICE_INFO -> result.success(getForegroundServiceInfo()) - ?: result.error("Unavailable", "nativeLibDir not available", null); + ?: result.error("Unavailable", "getForegroundServiceInfo not available", null); else -> result.notImplemented() } } @@ -229,7 +229,6 @@ class MainActivity: FlutterActivity() { fun getForegroundServiceInfo(): HashMap { - Log.d("MainActivity.kt", "getForgegroundServiceInfo called...") var hashMap : HashMap = HashMap (); val workmanager = WorkManager.getInstance(this); @@ -243,7 +242,6 @@ class MainActivity: FlutterActivity() { hashMap.put("workmanager.${workInfo.id}.state", workInfo.state.toString()); } } - Log.d("MainActivity.kt", "getForgegroundServiceInfo finished...") return hashMap }