private Task <FileData> TakeMediaAsync(string type, string action) { var id = GetRequestId(); var ntcs = new TaskCompletionSource <FileData>(id); if (Interlocked.CompareExchange(ref _completionSource, ntcs, null) != null) { throw new InvalidOperationException("Only one operation can be active at a time"); } var pickerIntent = new Intent(this._context, typeof(FilePickerActivity)); pickerIntent.SetFlags(ActivityFlags.NewTask); this._context.StartActivity(pickerIntent); EventHandler <FilePickerEventArgs> handler = null; EventHandler <EventArgs> cancelledHandler = null; handler = async(s, e) => { var tcs = Interlocked.Exchange(ref _completionSource, null); FilePickerActivity.FilePicked -= handler; Stream fileStream = null; if (!string.IsNullOrWhiteSpace(e.FilePath)) { using (AndroidRuntime.InputStreamInvoker inputStreamInvoker = (AndroidRuntime.InputStreamInvoker)_context.ContentResolver.OpenInputStream(AndroidNet.Uri.Parse(e.FilePath))) { byte[] array = await ReadBytes(inputStreamInvoker.BaseInputStream); fileStream = new MemoryStream(array); } } tcs?.SetResult(new FileData(e.FilePath, e.FileName, () => fileStream)); }; cancelledHandler = (s, e) => { var tcs = Interlocked.Exchange(ref _completionSource, null); FilePickerActivity.FilePickCancelled -= cancelledHandler; tcs?.SetResult(null); }; FilePickerActivity.FilePickCancelled += cancelledHandler; FilePickerActivity.FilePicked += handler; return(_completionSource.Task); }
private void SendStoredMessages() { if (HasInternetConnection) { try { using (File dir = Context.GetDir("RaygunIO", FileCreationMode.Private)) { File[] files = dir.ListFiles(); foreach (File file in files) { if (file.Name.StartsWith("RaygunErrorMessage")) { using (FileInputStream stream = new FileInputStream(file)) { using (InputStreamInvoker isi = new InputStreamInvoker(stream)) { using (InputStreamReader streamReader = new Java.IO.InputStreamReader(isi)) { using (BufferedReader bufferedReader = new BufferedReader(streamReader)) { StringBuilder stringBuilder = new StringBuilder(); string line; while ((line = bufferedReader.ReadLine()) != null) { stringBuilder.Append(line); } bool success = SendMessage(stringBuilder.ToString()); // If just one message fails to send, then don't delete the message, and don't attempt sending anymore until later. if (!success) { return; } System.Diagnostics.Debug.WriteLine("Sent " + file.Name); } } } } file.Delete(); } } if (dir.List().Length == 0) { if (files.Length > 0) { System.Diagnostics.Debug.WriteLine("Successfully sent all pending messages"); } dir.Delete(); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("Error sending stored messages to Raygun.io {0}", ex.Message)); } } }