Skip to content

Commit

Permalink
fix: crash while opening terminal (#658)
Browse files Browse the repository at this point in the history
Fixes #639
  • Loading branch information
lollipopkit authored Dec 14, 2024
1 parent 831efa8 commit 2f6db29
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 85 deletions.
67 changes: 45 additions & 22 deletions android/app/src/main/kotlin/tech/lolli/toolbox/ForegroundService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,58 @@ import android.app.*
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log

class ForegroundService : Service() {
private val chanId = "ForegroundServiceChannel"

override fun onCreate() {
super.onCreate()
Log.d("ForegroundService", "Service onCreate")
createNotificationChannel()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
if (intent == null) {
Log.w("ForegroundService", "onStartCommand called with null intent")
stopForegroundService()
return START_NOT_STICKY
}

val action = intent.action
Log.d("ForegroundService", "onStartCommand action=$action")

return when (action) {
"ACTION_STOP_FOREGROUND" -> {
stopForegroundService()
return START_NOT_STICKY
START_NOT_STICKY
}
else -> {
val notification = createNotification()
startForeground(1, notification)
return START_STICKY
START_STICKY
}
}
}

override fun onBind(intent: Intent): IBinder? {
override fun onBind(intent: Intent?): IBinder? {
return null
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = getSystemService(NotificationManager::class.java)
if (manager == null) {
Log.e("ForegroundService", "Failed to get NotificationManager")
return
}
val serviceChannel = NotificationChannel(
chanId,
chanId,
"ForegroundServiceChannel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
).apply {
description = "For foreground service"
}
manager.createNotificationChannel(serviceChannel)
}
}
Expand All @@ -62,27 +79,33 @@ class ForegroundService : Service() {
PendingIntent.FLAG_IMMUTABLE
)

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(this, chanId)
.setContentTitle("Server Box")
.setContentText("Open the app")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build()
} else {
Notification.Builder(this)
.setContentTitle("Server Box")
.setContentText("Open the app")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build()
}

return builder
.setContentTitle("Server Box")
.setContentText("Running in background")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build()
}

fun stopForegroundService() {
stopForeground(true)
private fun stopForegroundService() {
try {
stopForeground(true)
} catch (e: Exception) {
Log.e("ForegroundService", "Error stopping foreground: ${e.message}")
}
stopSelf()
Log.d("ForegroundService", "ForegroundService stopped")
}

override fun onDestroy() {
super.onDestroy()
Log.d("ForegroundService", "Service onDestroy")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ class MainActivity: FlutterFragmentActivity() {
}
}
}

36 changes: 18 additions & 18 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = "Runner/Info-$(CONFIGURATION).plist";
Expand All @@ -682,7 +682,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -808,7 +808,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = "Runner/Info-$(CONFIGURATION).plist";
Expand All @@ -818,7 +818,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand All @@ -836,7 +836,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = "Runner/Info-$(CONFIGURATION).plist";
Expand All @@ -846,7 +846,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand All @@ -867,7 +867,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_TEAM = BA88US33G6;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -880,7 +880,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
Expand All @@ -906,7 +906,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_TEAM = BA88US33G6;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -919,7 +919,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -942,7 +942,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_TEAM = BA88US33G6;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -955,7 +955,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -978,7 +978,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_PREVIEWS = YES;
Expand All @@ -990,7 +990,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
Expand Down Expand Up @@ -1019,7 +1019,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_PREVIEWS = YES;
Expand All @@ -1031,7 +1031,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
PRODUCT_NAME = ServerBox;
Expand All @@ -1057,7 +1057,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1104;
CURRENT_PROJECT_VERSION = 1117;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_PREVIEWS = YES;
Expand All @@ -1069,7 +1069,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1110;
MARKETING_VERSION = 1.0.1117;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
PRODUCT_NAME = ServerBox;
Expand Down
5 changes: 3 additions & 2 deletions lib/core/channel/bg_run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ abstract final class BgRunMC {
_channel.invokeMethod('sendToBackground');
}

/// TODO: try fix the fn, then uncomment it and [stopService]
static void startService() {
_channel.invokeMethod('startService');
// _channel.invokeMethod('startService');
}

static void stopService() {
_channel.invokeMethod('stopService');
// _channel.invokeMethod('stopService');
}
}
2 changes: 1 addition & 1 deletion lib/data/res/build_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

abstract class BuildData {
static const String name = "ServerBox";
static const int build = 1110;
static const int build = 1117;
static const int script = 59;
}
42 changes: 20 additions & 22 deletions lib/view/page/server/top_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,26 @@ final class _TopBar extends StatelessWidget implements PreferredSizeWidget {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Center(
child: InkWell(
borderRadius: BorderRadius.circular(13),
onTap: () {
SettingsPage.route.go(context);
},
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 7, vertical: 3),
child: Row(
children: [
Text(
BuildData.name,
style: TextStyle(fontSize: 19),
),
SizedBox(width: 3),
Icon(
Icons.keyboard_arrow_right,
color: Colors.grey,
size: 17,
),
],
),
InkWell(
borderRadius: BorderRadius.circular(13),
onTap: () {
SettingsPage.route.go(context);
},
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 7, vertical: 3),
child: Row(
children: [
Text(
BuildData.name,
style: TextStyle(fontSize: 19),
),
SizedBox(width: 5),
Icon(
Icons.settings,
color: Colors.grey,
size: 17,
),
],
),
),
),
Expand Down
4 changes: 2 additions & 2 deletions lib/view/page/setting/entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class _SettingsPageState extends State<SettingsPage>
.toList(growable: false),
),
actions: [
Btn.icon(
icon: const Icon(Icons.developer_board),
Btn.text(
text: 'Logs',
onTap: () => DebugPage.route.go(
context,
args: const DebugPageArgs(title: 'Logs(${BuildData.build})'),
Expand Down
Loading

0 comments on commit 2f6db29

Please sign in to comment.