bool CheckPermissions(string permission, bool throwOnError = true)
        {
            var result = true;
            var perf   = PerformanceCounter.Start();

            Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Checking " + permission + "...");

            if (!PlatformChecks.IsPermissionInManifest(this.Context, permission) ||
                !PlatformChecks.IsPermissionGranted(this.Context, permission))
            {
                result = false;

                if (throwOnError)
                {
                    var msg = "ZXing.Net.Mobile requires: " + permission + ", but was not found in your AndroidManifest.xml file.";
                    Android.Util.Log.Error("ZXing.Net.Mobile", msg);

                    throw new UnauthorizedAccessException(msg);
                }
            }

            PerformanceCounter.Stop(perf, "CheckPermissions took {0}ms");

            return(result);
        }
Beispiel #2
0
        public void Torch(bool on)
        {
            if (!this.Context.PackageManager.HasSystemFeature(PackageManager.FeatureCameraFlash))
            {
                Android.Util.Log.Info(MobileBarcodeScanner.TAG, "Flash not supported on this device");
                return;
            }

            if (!PlatformChecks.IsPermissionInManifest(this.Context, Android.Manifest.Permission.Flashlight) ||
                !PlatformChecks.IsPermissionGranted(this.Context, Android.Manifest.Permission.Flashlight))
            {
                var msg = "ZXing.Net.Mobile requires permission to use the Flash (" + Android.Manifest.Permission.Flashlight + "), but was not found in your AndroidManifest.xml file.";
                Android.Util.Log.Error(MobileBarcodeScanner.TAG, msg);

                throw new UnauthorizedAccessException(msg);
            }

            if (camera == null)
            {
                Android.Util.Log.Info(MobileBarcodeScanner.TAG, "NULL Camera");
                return;
            }

            var p = camera.GetParameters();
            var supportedFlashModes = p.SupportedFlashModes;

            if (supportedFlashModes == null)
            {
                supportedFlashModes = new List <string>();
            }

            var flashMode = string.Empty;

            if (on)
            {
                if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch))
                {
                    flashMode = Android.Hardware.Camera.Parameters.FlashModeTorch;
                }
                else if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeOn))
                {
                    flashMode = Android.Hardware.Camera.Parameters.FlashModeOn;
                }
                isTorchOn = true;
            }
            else
            {
                if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeOff))
                {
                    flashMode = Android.Hardware.Camera.Parameters.FlashModeOff;
                }
                isTorchOn = false;
            }

            if (!string.IsNullOrEmpty(flashMode))
            {
                p.FlashMode = flashMode;
                camera.SetParameters(p);
            }
        }
Beispiel #3
0
        protected override void OnResume()
        {
            base.OnResume();

            var permissionsToRequest = new List <string> ();

            // Check and request any permissions
            foreach (var permission in RequiredPermissions)
            {
                if (PlatformChecks.IsPermissionInManifest(this, permission))
                {
                    if (!PlatformChecks.IsPermissionGranted(this, permission))
                    {
                        permissionsToRequest.Add(permission);
                    }
                }
            }

            if (permissionsToRequest.Any())
            {
                PlatformChecks.RequestPermissions(this, permissionsToRequest.ToArray(), 101);
            }
            else
            {
                StartScanning();
            }
        }
Beispiel #4
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            this.RequestWindowFeature(WindowFeatures.NoTitle);

            this.Window.AddFlags(WindowManagerFlags.Fullscreen);   //to show
            this.Window.AddFlags(WindowManagerFlags.KeepScreenOn); //Don't go to sleep while scanning

            if (ScanningOptions.AutoRotate.HasValue && !ScanningOptions.AutoRotate.Value)
            {
                RequestedOrientation = ScreenOrientation.Nosensor;
            }

            SetContentView(Resource.Layout.zxingscanneractivitylayout);

            scannerFragment = new ZXingScannerFragment();
            scannerFragment.CustomOverlayView    = CustomOverlayView;
            scannerFragment.UseCustomOverlayView = UseCustomOverlayView;
            scannerFragment.TopText    = TopText;
            scannerFragment.BottomText = BottomText;

            SupportFragmentManager.BeginTransaction()
            .Replace(Resource.Id.contentFrame, scannerFragment, "ZXINGFRAGMENT")
            .Commit();

            CancelRequestedHandler    = CancelScan;
            AutoFocusRequestedHandler = AutoFocus;
            TorchRequestedHandler     = SetTorch;
            PauseAnalysisHandler      = scannerFragment.PauseAnalysis;
            ResumeAnalysisHandler     = scannerFragment.ResumeAnalysis;

            var permissionsToRequest = new List <string> ();

            // Check and request any permissions
            foreach (var permission in RequiredPermissions)
            {
                if (PlatformChecks.IsPermissionInManifest(this, permission))
                {
                    if (!PlatformChecks.IsPermissionGranted(this, permission))
                    {
                        permissionsToRequest.Add(permission);
                    }
                }
            }

            if (permissionsToRequest.Any())
            {
                waitingForPermission = PlatformChecks.RequestPermissions(this, permissionsToRequest.ToArray(), 101);
            }
        }
Beispiel #5
0
        void CheckPermissions()
        {
            var perf = PerformanceCounter.Start();

            Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Checking Camera Permissions...");

            if (!PlatformChecks.IsPermissionInManifest(this.Context, Android.Manifest.Permission.Camera) ||
                !PlatformChecks.IsPermissionGranted(this.Context, Android.Manifest.Permission.Camera))
            {
                var msg = "ZXing.Net.Mobile requires permission to use the Camera (" + Android.Manifest.Permission.Camera + "), but was not found in your AndroidManifest.xml file.";
                Android.Util.Log.Error("ZXing.Net.Mobile", msg);

                throw new UnauthorizedAccessException(msg);
            }

            PerformanceCounter.Stop(perf, "CheckPermissions took {0}ms");
        }