Exemplo n.º 1
0
        public static async Task <AccessState> RequestBackgroundLocationAccess(this IAndroidContext context, LocationPermissionType locType)
        {
            if (!context.IsLocationEnabled(false, false))
            {
                return(AccessState.Disabled);
            }

            context.AssertBackgroundInManifest();

            AccessState status;

            if (context.IsMinApiLevel(30))
            {
                // Android 11+ need to request background separately
                // Android 12+ user can decline fine, but allow coarse
                status = await context.RequestLocationAccess(locType);

                if (status == AccessState.Available || status == AccessState.Restricted)
                {
                    var bg = await context.RequestAccess(P.AccessBackgroundLocation).ToTask();

                    status = bg == AccessState.Available ? status : AccessState.Restricted;
                }
            }
            else if (context.IsMinApiLevel(29))
            {
                // Android 10: Request BG permission with other permissions
                var perms = new List <string> {
                    P.AccessBackgroundLocation, P.AccessCoarseLocation
                };
                if (locType != LocationPermissionType.Coarse)
                {
                    perms.Add(P.AccessFineLocation);
                }

                var results = await context
                              .RequestPermissions(perms.ToArray())
                              .ToTask();

                status = FromResult(results, locType);
                if (status == AccessState.Available || status == AccessState.Available)
                {
                    status = results.IsGranted(P.AccessBackgroundLocation) ? status : AccessState.Restricted;
                }
            }
            else
            {
                status = await context.RequestLocationAccess(locType);
            }
            return(status);
        }
Exemplo n.º 2
0
        //static AccessState GetCurrentLocationAccess(this IAndroidContext context, bool background, bool fineAccess, bool gpsRequired, bool networkRequired)
        //{
        //    var status = context.GetLocationManagerStatus(gpsRequired, networkRequired);
        //    if (status != AccessState.Available)
        //        return status;

        //    if (context.IsMinApiLevel(29) && background)
        //    {
        //        status = context.GetCurrentAccessState(P.AccessBackgroundLocation);
        //        if (status != AccessState.Available)
        //            return status;
        //    }
        //    var next = fineAccess ? P.AccessFineLocation : P.AccessCoarseLocation;
        //    status = context.GetCurrentAccessState(next);

        //    return status;
        //}


        public static async Task <AccessState> RequestLocationAccess(this IAndroidContext context, bool background, bool fineAccess, bool gpsRequired, bool networkRequired)
        {
            if (!IsLocationEnabled(context, gpsRequired, networkRequired))
            {
                return(AccessState.Disabled);
            }

            var locationPerm = fineAccess ? P.AccessFineLocation : P.AccessCoarseLocation;

            if (!context.IsMinApiLevel(29) || !background)
            {
                return(await context.RequestAccess(locationPerm).ToTask());
            }

            var access = await context
                         .RequestPermissions
                         (
                P.AccessBackgroundLocation,
                P.ForegroundService,
                locationPerm
                         )
                         .ToTask();

            if (!access.IsGranted(locationPerm))
            {
                return(AccessState.Denied);
            }

            if (!access.IsGranted(P.AccessBackgroundLocation))
            {
                return(AccessState.Restricted);
            }

            return(AccessState.Available);
        }
Exemplo n.º 3
0
 static void AssertBackgroundInManifest(this IAndroidContext context)
 {
     if (context.IsMinApiLevel(29) && !context.IsInManifest(P.AccessBackgroundLocation))
     {
         throw new ArgumentException($"{P.AccessBackgroundLocation} is not in your manifest but is required");
     }
 }
Exemplo n.º 4
0
        static bool IsLocationEnabled(IAndroidContext context, bool gpsRequired, bool networkRequired)
        {
            var lm = context.GetSystemService <LocationManager>(Context.LocationService);

            if (context.IsMinApiLevel(28) && !lm.IsLocationEnabled)
            {
                return(false);
            }

            if (networkRequired && !lm.IsProviderEnabled(LocationManager.NetworkProvider))
            {
                return(false);
            }

            if (gpsRequired && !lm.IsProviderEnabled(LocationManager.GpsProvider))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 5
0
        public static AccessState GetLocationManagerStatus(this IAndroidContext context, bool gpsRequired, bool networkRequired)
        {
            var lm = context.GetSystemService <LocationManager>(Context.LocationService);

            if (context.IsMinApiLevel(28) && !lm.IsLocationEnabled)
            {
                return(AccessState.Disabled);
            }

            if (networkRequired && !lm.IsProviderEnabled(LocationManager.NetworkProvider))
            {
                return(AccessState.Disabled);
            }

            if (gpsRequired && !lm.IsProviderEnabled(LocationManager.GpsProvider))
            {
                return(AccessState.Disabled);
            }

            return(AccessState.Available);
        }
Exemplo n.º 6
0
        public static AccessState GetCurrentLocationAccess(this IAndroidContext context, bool background, bool fineAccess, bool gpsRequired, bool networkRequired)
        {
            var status = context.GetLocationManagerStatus(gpsRequired, networkRequired);

            if (status != AccessState.Available)
            {
                return(status);
            }

            if (context.IsMinApiLevel(29) && background)
            {
                status = context.GetCurrentAccessState(P.AccessBackgroundLocation);
                if (status != AccessState.Available)
                {
                    return(status);
                }
            }
            var next = fineAccess ? P.AccessFineLocation : P.AccessCoarseLocation;

            status = context.GetCurrentAccessState(next);

            return(status);
        }