protected override async void OnAppearing() { base.OnAppearing(); // TODO: move to viewModel (UpdateAsync) & ObservableCollection<Venue> try { PermissionStatus locationStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location); if (!locationStatus.Equals(PermissionStatus.Granted)) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location)) { await DisplayAlert("Location Needed", "We need permission to access device location to show you nearby venues", "OK"); } Dictionary <Permission, PermissionStatus> permissionResults = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location); if (permissionResults.ContainsKey(Permission.Location)) { locationStatus = permissionResults[Permission.Location]; } } if (locationStatus.Equals(PermissionStatus.Granted)) { var geolocator = CrossGeolocator.Current; var position = await geolocator.GetPositionAsync(); List <Venue> venues = await Venue.GetVenuesAsync(position.Latitude, position.Longitude); venuesListView.ItemsSource = venues; } else { await DisplayAlert("Location Denied", "We cannot show you venues because you denied location access permissions", "OK"); } } catch (Exception ex) { await DisplayAlert("Error", $"An unexpected error ocurred: {ex.Message}", "OK"); } }
/// <summary> /// Return a bool representing wether we have camera permissions /// </summary> /// <returns></returns> private async Task <bool> RequestPermissions() { PermissionStatus status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera); if (status.Equals(PermissionStatus.Granted)) { return(true); } var stati = await CrossPermissions.Current.RequestPermissionsAsync(new Permission[] { Permission.Camera }); return(stati.Where(s => s.Key.Equals(Permission.Camera)).FirstOrDefault().Value.Equals(PermissionStatus.Granted)); }
private async void GetLocationPermissionsAsync() { try { PermissionStatus locationStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location); if (!locationStatus.Equals(PermissionStatus.Granted)) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location)) { await DisplayAlert("Location Needed", "We need permission to access device location to show you the map", "OK"); } Dictionary <Permission, PermissionStatus> permissionResults = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location); if (permissionResults.ContainsKey(Permission.Location)) { locationStatus = permissionResults[Permission.Location]; SetLocationAsync(); } } travelsMap.IsShowingUser = locationStatus.Equals(PermissionStatus.Granted); hasLocationPermission = locationStatus.Equals(PermissionStatus.Granted); if (!locationStatus.Equals(PermissionStatus.Granted)) { await DisplayAlert("Location Denied", "We cannot show you map because you denied location access permissions", "OK"); } } catch (Exception ex) { await DisplayAlert("Error", $"We ran into a issue showing you the map :( {ex.Message}", "OK"); } }
public async Task <bool> SaveZipToFolder() { try { var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "priceCalculator.sqlite"); if (File.Exists(path)) { PermissionStatus status = await Plugin.Permissions.CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.Storage); if (!status.Equals(PermissionStatus.Granted)) { await Plugin.Permissions.CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.Storage); } if (App.Connection != null) { await App.Connection.CloseAsync(); } FileStream fsOut = File.Create(Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).Path, "price_backup.zip")); ZipOutputStream zipStream = new ZipOutputStream(fsOut); //FastZip zip = new FastZip(); //zip.CreateZip( "price_backup.zip",Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).Path,true,null); //zip.CreateEmptyDirectories = true; zipStream.SetLevel(6); zipStream.Password = "******"; //FileInfo db = new FileInfo(path); ZipEntry dbEntry = new ZipEntry(ZipEntry.CleanName(path.Split("/").LastOrDefault())); //zip. zipStream.PutNextEntry(dbEntry); byte[] dbBuffer = new byte[4096]; using (FileStream streamReader = File.OpenRead(path)) { StreamUtils.Copy(streamReader, zipStream, dbBuffer); } zipStream.CloseEntry(); string imgDir = CrossCurrentActivity.Current.AppContext.GetExternalFilesDir(null).Path; if (Directory.Exists(imgDir)) { string[] files = Directory.GetDirectories(imgDir); foreach (var item in files) { if (!item.Contains("override")) { string folderName = ZipEntry.CleanName(item.Split("/").LastOrDefault()); string[] file = Directory.GetFiles(item); foreach (var fileName in file) { ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(folderName + "/" + fileName.Split("/").LastOrDefault())); byte[] buffer = new byte[4096]; zipStream.PutNextEntry(zipEntry); using (FileStream fileReader = File.OpenRead(fileName)) { StreamUtils.Copy(fileReader, zipStream, buffer); } zipStream.CloseEntry(); } } } } zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream zipStream.Close(); } return(true); } catch (Exception ex) { return(false); } }