public bool Stop()
        {
            StopScript();

            if (!ServiceStarted)
            {
                return(false);
            }

            _virtualDisplay?.Release();
            _virtualDisplay = null;

            _imgListener?.Dispose();
            _imgListener = null;

            _imageReader?.Close();
            _imageReader = null;

            _mediaProjection?.Stop();
            _mediaProjection = null;

            ScreenshotManager.ReleaseMemory();
            ImageLocator.ClearCache();

            _windowManager.RemoveView(_layout);
            ServiceStarted = false;

            HideForegroundNotification();

            return(true);
        }
        public bool Start(Intent MediaProjectionToken = null)
        {
            if (ServiceStarted)
            {
                return(false);
            }

            if (MediaProjectionToken != null)
            {
                _mediaProjection = MediaProjectionManager.GetMediaProjection((int)Result.Ok, MediaProjectionToken);
            }

            _imageReader = ImageReader.NewInstance(_screenWidth, _screenHeight, (ImageFormatType)1, 2);
            _imgListener = new ImgListener(_imageReader);
            _imageReader.SetOnImageAvailableListener(_imgListener, null);

            SetupVirtualDisplay();

            _windowManager.AddView(_layout, _layoutParams);
            ServiceStarted = true;

            ShowForegroundNotification();

            return(true);
        }