示例#1
0
        private async void UdpClientAction()
        {
            try
            {
                udpClient = new UdpClient(4536);
                while (IsStarted)
                {
                    var udpReceiveResult = await udpClient.ReceiveAsync();

                    if (!udpReceiveResult.RemoteEndPoint.Address.ToString().Equals(Networker.GetLocalIp()))
                    {
                        var defconStatus = Encoding.ASCII.GetString(udpReceiveResult.Buffer);
                        if (int.TryParse(defconStatus, out int parsedDefconStatus))
                        {
                            if (parsedDefconStatus > 0 && parsedDefconStatus < 6)
                            {
                                new SettingsService().SaveSetting("DefconStatus", defconStatus.ToString());

                                Intent widgetIntent = new Intent(this, typeof(MyDefconWidget));
                                widgetIntent.SetAction("com.marcusrunge.MyDEFCON.DEFCON_UPDATE");
                                widgetIntent.PutExtra("DefconStatus", defconStatus.ToString());

                                Intent statusReceiverIntent = new Intent(this, typeof(ForegroundDefconStatusReceiver));
                                statusReceiverIntent.SetAction("com.marcusrunge.MyDEFCON.STATUS_RECEIVER_ACTION");
                                statusReceiverIntent.PutExtra("DefconStatus", defconStatus.ToString());

                                SendBroadcast(widgetIntent);
                                SendBroadcast(statusReceiverIntent);

                                if (_settingsService.GetSetting <bool>("isStatusUpdateAlertEnabled"))
                                {
                                    Notifier.AlertWithVibration();
                                    Notifier.AlertWithAudioNotification(this, _settingsService.GetSetting <int>("StatusUpdateAlertSelection"));
                                }
                            }
                            else if (parsedDefconStatus == 0 && _settingsService.GetSetting <bool>("IsMulticastEnabled") && DateTimeOffset.Now > _lastConnect.AddSeconds(5))
                            {
                                Intent tcpActionIntent = new Intent(this, typeof(TcpActionReceiver));
                                tcpActionIntent.SetAction("com.marcusrunge.MyDEFCON.TCP_ACTION");
                                tcpActionIntent.PutExtra("RemoteEndPointAddress", udpReceiveResult.RemoteEndPoint.Address.ToString());
                                SendBroadcast(tcpActionIntent);
                                _lastConnect = DateTimeOffset.Now;
                            }
                        }
                    }
                }
                if (udpClient != null)
                {
                    udpClient.Close();
                    udpClient.Dispose();
                }
            }
            catch { }
        }
示例#2
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            _isOnCreateView = true;
            var view = inflater.Inflate(Resource.Layout.settings_fragment, null);
            var isBroadcastEnabledSwitch         = view.FindViewById <AndroidX.AppCompat.Widget.SwitchCompat>(Resource.Id.isBroadcastEnabledSwitch);
            var isMulticastEnabledSwitch         = view.FindViewById <AndroidX.AppCompat.Widget.SwitchCompat>(Resource.Id.isMulticastEnabledSwitch);
            var isForegroundServiceEnabledSwitch = view.FindViewById <AndroidX.AppCompat.Widget.SwitchCompat>(Resource.Id.isForegroundServiceEnabledSwitch);
            var isStatusUpdateAlertEnabledSwitch = view.FindViewById <AndroidX.AppCompat.Widget.SwitchCompat>(Resource.Id.isStatusUpdateAlertEnabledSwitch);
            var statusUpdateAlertSelectSpinner   = view.FindViewById <Spinner>(Resource.Id.statusUpdateAlertSelectSpinner);

            isBroadcastEnabledSwitch.Checked        = _settingsService.GetSetting <bool>("IsBroadcastEnabled");
            isBroadcastEnabledSwitch.CheckedChange += (s, e) =>
            {
                _settingsService.SaveSetting("IsBroadcastEnabled", e.IsChecked);
                if (e.IsChecked)
                {
                    Context.StartService(new Intent(Context, typeof(UdpClientService)));
                    if (isMulticastEnabledSwitch.Checked)
                    {
                        var tcpClientServiceIntent = new Intent(Context, typeof(TcpClientService));
                        Context.StopService(tcpClientServiceIntent);
                        Context.StartService(tcpClientServiceIntent);
                    }
                }
                else
                {
                    Context.StopService(new Intent(Context, typeof(UdpClientService)));
                    if (isMulticastEnabledSwitch.Checked)
                    {
                        isMulticastEnabledSwitch.Checked         = false;
                        isForegroundServiceEnabledSwitch.Checked = false;
                        var tcpClientServiceIntent = new Intent(Context, typeof(TcpClientService));
                        Context.StopService(tcpClientServiceIntent);
                    }
                }
            };

            isMulticastEnabledSwitch.Checked        = _settingsService.GetSetting <bool>("IsMulticastEnabled");
            isMulticastEnabledSwitch.CheckedChange += (s, e) =>
            {
                _settingsService.SaveSetting("IsMulticastEnabled", e.IsChecked);
                if (e.IsChecked && !isBroadcastEnabledSwitch.Checked)
                {
                    isBroadcastEnabledSwitch.Checked = true;
                }
                else
                {
                    var tcpClientServiceIntent = new Intent(Context, typeof(TcpClientService));
                    Context.StopService(tcpClientServiceIntent);
                    Context.StartService(tcpClientServiceIntent);
                }
            };
            isForegroundServiceEnabledSwitch.Checked        = _settingsService.GetSetting <bool>("IsForegroundServiceEnabled");
            isForegroundServiceEnabledSwitch.CheckedChange += (s, e) =>
            {
                if (!e.IsChecked)
                {
                    Intent stopServiceIntent = new Intent(Context, typeof(ForegroundService));
                    stopServiceIntent.SetAction(Constants.ACTION_STOP_SERVICE);
                    Context.StopService(stopServiceIntent);
                    if (_settingsService.GetSetting <bool>("IsBroadcastEnabled"))
                    {
                        Context.StartService(new Intent(Context, typeof(UdpClientService)));
                    }
                    _workerService.CancelWorker <KeepForegroundServiceRunningWorker>();
                }
                else
                {
                    if (!_settingsService.GetSetting <bool>("IsBroadcastEnabled"))
                    {
                        isForegroundServiceEnabledSwitch.Checked = false;
                        return;
                    }
                    Context.StopService(new Intent(Context, typeof(UdpClientService)));
                    Intent startServiceIntent = new Intent(Context, typeof(ForegroundService));
                    startServiceIntent.SetAction(Constants.ACTION_START_SERVICE);
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                    {
                        Context.StartForegroundService(startServiceIntent);
                    }
                    else
                    {
                        Context.StartService(startServiceIntent);
                    }
                    _workerService.CreateUniquePeriodicWorker <KeepForegroundServiceRunningWorker>(TimeSpan.FromMinutes(15));
                }
                _settingsService.SaveSetting("IsForegroundServiceEnabled", e.IsChecked);
            };

            isStatusUpdateAlertEnabledSwitch.Checked        = _settingsService.GetSetting <bool>("isStatusUpdateAlertEnabled");
            isStatusUpdateAlertEnabledSwitch.CheckedChange += (s, e) =>
            {
                _settingsService.SaveSetting("isStatusUpdateAlertEnabled", e.IsChecked);
                statusUpdateAlertSelectSpinner.Visibility = e.IsChecked ? ViewStates.Visible : ViewStates.Gone;
            };

            var ringtoneManager = new RingtoneManager(Context);

            ringtoneManager.SetType(RingtoneType.All);
            var           ringtoneCursor = ringtoneManager.Cursor;
            List <string> ringtoneTitles = new List <string>();

            ringtoneCursor.MoveToFirst();
            while (ringtoneCursor.MoveToNext())
            {
                ringtoneTitles.Add(ringtoneCursor.GetString(1));
            }
            var arrayAdapter = new ArrayAdapter <string>(Context, Resource.Layout.CustomSpinnerItem, ringtoneTitles.ToArray());

            arrayAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            _statusUpdateAlertSelectSpinnerViewState  = isStatusUpdateAlertEnabledSwitch.Checked ? ViewStates.Visible : ViewStates.Gone;
            statusUpdateAlertSelectSpinner.Visibility = _statusUpdateAlertSelectSpinnerViewState;
            statusUpdateAlertSelectSpinner.Adapter    = arrayAdapter;
            int statusUpdateAlertSelection = _settingsService.GetSetting <int>("StatusUpdateAlertSelection");

            statusUpdateAlertSelectSpinner.SetSelection(statusUpdateAlertSelection > -1 ? statusUpdateAlertSelection : 0);
            statusUpdateAlertSelectSpinner.ItemSelected += (s, e) =>
            {
                if ((s as Spinner).Visibility == _statusUpdateAlertSelectSpinnerViewState && !_isOnCreateView)
                {
                    _settingsService.SaveSetting("StatusUpdateAlertSelection", e.Position);
                    Notifier.AlertWithAudioNotification(Context, e.Position);
                }
                else
                {
                    _statusUpdateAlertSelectSpinnerViewState = (s as Spinner).Visibility;
                    _isOnCreateView = false;
                }
            };

            return(view);
        }
示例#3
0
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            _isServiceRunning = true;

            _isConnectionBlocked = false;
            _lastConnect         = DateTimeOffset.MinValue;
            try
            {
                _settingsService = MainActivity.Container.Resolve <ISettingsService>();
                _eventService    = MainActivity.Container.Resolve <IEventService>();
            }
            catch (Exception)
            {
                return(StartCommandResult.RedeliverIntent);
            }

            _eventService.BlockConnectionEvent += (s, e) => _isConnectionBlocked = (e as BlockConnectionEventArgs).Blocked;
            _udpClient = new UdpClient(4536);
            Task.Run(async() =>
            {
                try
                {
                    while (_isServiceRunning)
                    {
                        var udpReceiveResult = await _udpClient.ReceiveAsync();
                        if (!udpReceiveResult.RemoteEndPoint.Address.ToString().Equals(Networker.GetLocalIp()))
                        {
                            var defconStatus = Encoding.ASCII.GetString(udpReceiveResult.Buffer);
                            if (int.TryParse(defconStatus, out int parsedDefconStatus) && !_isConnectionBlocked)
                            {
                                if (parsedDefconStatus > 0 && parsedDefconStatus < 6)
                                {
                                    new SettingsService().SaveSetting("DefconStatus", defconStatus.ToString());

                                    Intent widgetIntent = new Intent(this, typeof(MyDefconWidget));
                                    widgetIntent.SetAction("com.marcusrunge.MyDEFCON.DEFCON_UPDATE");
                                    widgetIntent.PutExtra("DefconStatus", defconStatus.ToString());

                                    Intent statusReceiverIntent = new Intent(this, typeof(DefconStatusReceiver));
                                    statusReceiverIntent.SetAction("com.marcusrunge.MyDEFCON.STATUS_RECEIVER_ACTION");
                                    statusReceiverIntent.PutExtra("DefconStatus", defconStatus.ToString());

                                    SendBroadcast(widgetIntent);
                                    SendBroadcast(statusReceiverIntent);

                                    if (!_settingsService.GetSetting <bool>("IsForegroundServiceEnabled") && _settingsService.GetSetting <bool>("isStatusUpdateAlertEnabled"))
                                    {
                                        Notifier.AlertWithVibration();
                                        Notifier.AlertWithAudioNotification(this, _settingsService.GetSetting <int>("StatusUpdateAlertSelection"));
                                    }
                                }
                                else if (parsedDefconStatus == 0 && _settingsService.GetSetting <bool>("IsMulticastEnabled") && DateTimeOffset.Now > _lastConnect.AddSeconds(5))
                                {
                                    Intent tcpActionIntent = new Intent(this, typeof(TcpActionReceiver));
                                    tcpActionIntent.SetAction("com.marcusrunge.MyDEFCON.TCP_ACTION");
                                    tcpActionIntent.PutExtra("RemoteEndPointAddress", udpReceiveResult.RemoteEndPoint.Address.ToString());
                                    SendBroadcast(tcpActionIntent);
                                    _lastConnect = DateTimeOffset.Now;
                                }
                            }
                            _isConnectionBlocked = false;
                        }
                    }
                }
                catch { }
            }, _cancellationToken);

            //return base.OnStartCommand(intent, flags, startId);
            return(StartCommandResult.Sticky);
        }