public static string LowestCommonMimeType(this IMimeItemCollection mimeItemCollection) { if (mimeItemCollection.Items.Count > 0) { var parts = mimeItemCollection.Items[0].MimeType.Split('/'); var prefix = parts[0]; var suffix = parts[1]; foreach (var item in mimeItemCollection.Items) { parts = item.MimeType.Split('/'); if (parts[1] != suffix) { suffix = "*"; } if (parts[0] != prefix) { suffix = "*"; prefix = "*"; break; } } return(prefix + "/" + suffix); } return("*/*"); }
/// <summary> /// Does the MimeItemCollection contain at least one item of a given mimeType? /// </summary> /// <param name="mimeItemCollection"></param> /// <param name="mimeType"></param> /// <returns></returns> public static bool ContainsMimeType(this IMimeItemCollection mimeItemCollection, string mimeType) { mimeType = mimeType.ToLower(); foreach (var item in mimeItemCollection.Items) { if (item.MimeType == mimeType) { return(true); } } return(false); }
public static List <Android.Net.Uri> AsContentUris(this IMimeItemCollection mimeItemCollection) { ClipboardContentProvider.Clear(); var result = new List <Android.Net.Uri>(); foreach (var item in mimeItemCollection.Items) { var uri = ClipboardContentProvider.AsAsAndroidUri(item); if (uri != null) { result.Add(uri); } } return(result); }
/// <summary> /// Gets the all MimeItems of a given MIME type /// </summary> /// <returns>The MimeItems.</returns> /// <param name="mimeItemCollection">MIME item collection.</param> /// <param name="mimeType">MIME type.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static List <MimeItem <T> > GetMimeItems <T>(this IMimeItemCollection mimeItemCollection, string mimeType) { mimeType = mimeType.ToLower(); var mimeItems = new List <MimeItem <T> >(); /* * var untypedMimeItems = clipboardEntry.Items.FindAll((mi) => mi.MimeType == mimeType); * if (untypedMimeItems != null && untypedMimeItems.Count > 0) * foreach (var untypedMimeItem in untypedMimeItems) * if (MimeItem<T>.Create(untypedMimeItem) is MimeItem<T> item) * mimeItems.Add(item); */ foreach (var item in mimeItemCollection.Items) { if (item.MimeType == mimeType) { mimeItems.Add(MimeItem <T> .Create(item)); } } return(mimeItems); }
/// <summary> /// Gets the first MimeItem of a given MIME type. /// </summary> /// <returns>The first MIME item.</returns> /// <param name="mimeItemCollection">MimeItemCollection.</param> /// <param name="mimeType">MIME type.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static MimeItem <T> GetFirstMimeItem <T>(this IMimeItemCollection mimeItemCollection, string mimeType) { //MimeItem<T> result = null; var stopwatch = new Stopwatch(); stopwatch.Start(); mimeType = mimeType.ToLower(); foreach (var item in mimeItemCollection.Items) { if (item.MimeType == mimeType) { stopwatch.Stop(); //System.Diagnostics.Debug.WriteLine("\t\t GetFirstMimeItem<T> elapsed: " + stopwatch.ElapsedMilliseconds); return(MimeItem <T> .Create(item)); } } //var untypedMimeItem = clipboardEntry.Items.FirstOrDefault((mi) => mi.MimeType == mimeType); return(null); }
/// <summary> /// MIME types in the collection /// </summary> /// <returns>The MIME types.</returns> /// <param name="mimeItemCollection">MIME item collection.</param> public static List <string> MimeTypes(this IMimeItemCollection mimeItemCollection) { return(mimeItemCollection.Items.Select((mimeItem) => mimeItem.MimeType).ToList()); }
public static List <ClipData.Item> AsClipDataItems(this IMimeItemCollection mimeItemCollection) { ClipboardContentProvider.Clear(); var result = new List <ClipData.Item>(); string text = null; string html = null; // this has to happen because GMAIL will only paste the first "text/html" AND it won't parse correctly unless it is supplied as ClipData.Item(string,string) foreach (var item in mimeItemCollection.Items) { if (text == null && item.MimeType == "text/plain") { text = item.Value as string; } else if (html == null && item.MimeType == "text/html") { html = item.Value as string; } } if (html != null) { result.Add(new ClipData.Item(text ?? html, html)); } else if (text != null) { result.Add(new ClipData.Item(text)); } foreach (var item in mimeItemCollection.Items) { ClipData.Item androidClipItem = null; // The following block was added to support copying images by intent. // However, I have yet to see where it actually works with 3rd party apps. // Maybe I'm not doing it right? // START OF BLOCK if (item.MimeType.StartsWith("image/", StringComparison.InvariantCultureIgnoreCase) || item.Value is FileInfo) { Java.IO.File file = null; if (item.Value is FileInfo fileInfo) { file = new Java.IO.File(fileInfo.FullName); } else if (item.Value is byte[] byteArray && MimeSharp.Current.Extension(item.MimeType) is List <string> extensions && extensions.Count > 0) { var ext = extensions[0]; var fileName = Guid.NewGuid() + "." + ext; var dir = P42.Utils.Environment.TemporaryStoragePath; var path = Path.Combine(dir, fileName); System.IO.File.WriteAllBytes(path, byteArray); file = new Java.IO.File(path); } if (file != null && file.Exists()) { Android.Net.Uri uri = Android.Net.Uri.FromFile(file); var intent = new Intent(Intent.ActionSend); intent.SetType(item.MimeType); intent.PutExtra(Intent.ExtraStream, uri); intent.SetFlags(ActivityFlags.GrantReadUriPermission); androidClipItem = new ClipData.Item(intent); } file?.Dispose(); } if (androidClipItem == null) { // END OF BLOCK androidClipItem = ClipboardContentProvider.AddAsClipDataItem(item); } result.Add(androidClipItem); } return(result); }
public static void Source(this Windows.ApplicationModel.DataTransfer.DataPackage dataPackage, IMimeItemCollection mimeItemCollection) { dataPackage.RequestedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy; var properties = dataPackage.Properties; if (mimeItemCollection.Description != null) { properties.Description = mimeItemCollection.Description ?? Forms9Patch.ApplicationInfoService.Name; } properties.ApplicationName = Forms9Patch.ApplicationInfoService.Name; properties.Title = mimeItemCollection.Description ?? "Share " + Forms9Patch.ApplicationInfoService.Name + " data ..."; var storageItems = new List <IStorageItem>(); var textSet = false; var htmlSet = false; var rtfSet = false; var uriSet = false; //var htmlItems = mimeItemCollection.GetMimeItems<string>("text/html"); foreach (var item in mimeItemCollection.Items) { var mimeType = item.MimeType.ToLower(); if (mimeType == "text/plain" && !textSet && item.AsString() is string text) { dataPackage.SetText(text); textSet = true; } // else if (mimeType == "text/html" && !htmlSet && item.AsString() is string html) else if (mimeType == "text/html" && !htmlSet && item.AsWindowsHtmlFragment() is string html) { dataPackage.SetHtmlFormat(html); htmlSet = true; } else if ((mimeType == "text/rtf" || mimeType == "text/richtext" || mimeType == "application/rtf" || mimeType == "application/x-rtf") && !rtfSet && item.AsString() is string rtf) { dataPackage.SetRtf(rtf); rtfSet = true; } else if (item.Value is Uri uri && !uriSet && uri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { dataPackage.SetWebLink(uri); uriSet = true; } if (item.ToStorageFile() is StorageFile storageFile) { storageItems.Add(storageFile); } else { properties.Add(GetFormatId(item.MimeType), item.Value); } } if (storageItems.Count > 0) { dataPackage.SetStorageItems(storageItems); } }