private void ToForeground() { Logger.info("ToForeground()"); if (IsInOperation) { Logger.logWithStackTrace("ToForeground() while the service is in operation", Logging.Level.Warning); return; } if (IsForegroundRunning) { Logger.logWithStackTrace("ToForeground() while the service is already running in foreground", Logging.Level.Warning); return; } SetRunningState(true, true); CreateNotifBuilder(); StartForeground(MainNotificationId, builder.Build()); Controller = new Controller(); Controller.Logger.ParentLogger = Logging.RootLogger; onScreen(powerManager.IsInteractive); var filter = new IntentFilter(Intent.ActionScreenOff); filter.AddAction(Intent.ActionScreenOn); RegisterReceiver(receiverScreenState = new Receiver((c, i) => { if (i.Action == Intent.ActionScreenOn) { onScreen(true); } else if (i.Action == Intent.ActionScreenOff) { onScreen(false); } }), filter); Task.Run(() => { Logger.info("load and start controller..."); try { Controller.ConfigTomlLoaded += (t) => { App.CrashLogFile = Controller.ProcessFilePath("UnhandledException.txt"); if (t.TryGetValue <Config>("android", out var config)) { currentConfig = config; if (config.socket_impl != null) { MyStream.SetSocketImpl(config.socket_impl); } else { MyStream.CurrentSocketImpl = MyStream.SocketImpl.YASocket; } MyStream.Copier.TryReadSync = config.copier_sync_r; MyStream.Copier.TryWriteSync = config.copier_sync_w; SocketStream.EnableUnderlyingCalls = config.socket_underlying; onScreen(isScreenOn); } }; var paths = AppConfig.GetNaiveSocksConfigPaths(this); putLine(GetString(R.String.controller_loading)); Controller.LoadConfigFileFromMultiPaths(paths, true); putLine(GetString(R.String.controller_starting)); Controller.Start(); Logger.info("controller started."); putLine(GetString(R.String.controller_started)); CheckVPN(); } catch (System.Exception e) { Logger.exception(e, Logging.Level.Error, "loading/starting controller"); string msg = GetString(R.String.starting_error) + e.Message; putLine(msg, 10000); ShowToast(msg); StopSelf(); } finally { SetRunningState(true, false); } }); updateNotif(true); }