Exemplo n.º 1
0
        public static void SetContent(DataPackage content)
        {
            if (content is null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var items     = new List <ClipData.Item>();
            var mimeTypes = new List <string>();

            if (content.Text != null)
            {
                items.Add(new ClipData.Item(content.Text));
                mimeTypes.Add("text/plaintext");
            }

            if (content.Uri != null)
            {
                var androidUri = Android.Net.Uri.Parse(content.Uri.ToString());
                items.Add(new ClipData.Item(androidUri));
                mimeTypes.Add("text/uri-list");
            }

            if (content.Html != null)
            {
                // Matches all tags
                Regex regex = new Regex("(<.*?>\\s*)+", RegexOptions.Singleline);
                // Replace tags by spaces and trim
                var plainText = regex.Replace(content.Html, " ").Trim();

                items.Add(new ClipData.Item(plainText, content.Html));
                mimeTypes.Add("text/html");
            }

            if (items.Count > 0)
            {
                ClipData clipData = new ClipData(
                    new ClipDescription(ClipboardDataLabel, mimeTypes.ToArray()),
                    items[0]);
                for (int itemIndex = 1; itemIndex < items.Count; itemIndex++)
                {
                    clipData.AddItem(items[itemIndex]);
                }
                var manager = ContextHelper.Current.GetSystemService(Context.ClipboardService) as ClipboardManager;
                if (manager is null)
                {
                    return;
                }
                manager.PrimaryClip = clipData;
            }
            else
            {
                // Clear clipboard
                Clear();
            }
        }
        public void OnLongPress(MotionEvent e)
        {
            if (!HasAnyDragGestures())
            {
                return;
            }

            SendEventArgs <DragGestureRecognizer>(rec =>
            {
                if (!rec.CanDrag)
                {
                    return;
                }

                var element  = GetView();
                var renderer = AppCompat.Platform.GetRenderer(element);
                var v        = renderer.View;

                if (v.Handle == IntPtr.Zero)
                {
                    return;
                }

                var args = rec.SendDragStarting(element);

                if (args.Cancel)
                {
                    return;
                }

                CustomLocalStateData customLocalStateData = new CustomLocalStateData();
                customLocalStateData.DataPackage          = args.Data;

                //_dragSource[element] = args.Data;
                string clipDescription  = FastRenderers.AutomationPropertiesProvider.ConcatenateNameAndHelpText(element) ?? String.Empty;
                ClipData.Item item      = null;
                List <string> mimeTypes = new List <string>();

                if (!args.Handled)
                {
                    if (args.Data.Image != null)
                    {
                        mimeTypes.Add("image/jpeg");
                        item = ConvertToClipDataItem(args.Data.Image, mimeTypes);
                    }
                    else
                    {
                        string text = clipDescription ?? args.Data.Text;
                        if (Uri.TryCreate(text, UriKind.Absolute, out _))
                        {
                            item = new ClipData.Item(AUri.Parse(text));
                            mimeTypes.Add(ClipDescription.MimetypeTextUrilist);
                        }
                        else
                        {
                            item = new ClipData.Item(text);
                            mimeTypes.Add(ClipDescription.MimetypeTextPlain);
                        }
                    }
                }

                var dataPackage        = args.Data;
                ClipData.Item userItem = null;
                if (dataPackage.Image != null)
                {
                    userItem = ConvertToClipDataItem(dataPackage.Image, mimeTypes);
                }

                if (dataPackage.Text != null)
                {
                    userItem = new ClipData.Item(dataPackage.Text);
                }

                if (item == null)
                {
                    item     = userItem;
                    userItem = null;
                }

                ClipData data = new ClipData(clipDescription, mimeTypes.ToArray(), item);

                if (userItem != null)
                {
                    data.AddItem(userItem);
                }

                var dragShadowBuilder = new AView.DragShadowBuilder(v);

                customLocalStateData.SourceNativeView = v;
                customLocalStateData.SourceElement    = renderer?.Element;

                if (Forms.IsNougatOrNewer)
                {
                    v.StartDragAndDrop(data, dragShadowBuilder, customLocalStateData, (int)ADragFlags.Global | (int)ADragFlags.GlobalUriRead);
                }
                else
#pragma warning disable CS0618 // Type or member is obsolete
                {
                    v.StartDrag(data, dragShadowBuilder, customLocalStateData, (int)ADragFlags.Global | (int)ADragFlags.GlobalUriRead);
                }
#pragma warning restore CS0618 // Type or member is obsolete
            });
        }
Exemplo n.º 3
0
        internal static async Task SetContentAsync(DataPackage content)
        {
            var data = content?.GetView();

            var items     = new List <ClipData.Item>();
            var mimeTypes = new List <string>();

            if (data?.Contains(StandardDataFormats.Text) ?? false)
            {
                var text = await data.GetTextAsync();

                items.Add(new ClipData.Item(text));
                mimeTypes.Add("text/plaintext");
            }

            // UWP has the following standard data formats that correspond with an Android Uri:
            //
            //  1. Uri, now deprecated in favor of:
            //  2. ApplicationLink and
            //  3. WebLink
            //
            // For maximum compatibility with Android all are mapped to Uri. However, only
            // one may be used at a time in the above defined priority. WebLink is
            // considered more specific than ApplicationLink.
            if (data?.Contains(StandardDataFormats.Uri) ?? false)
            {
                var uri = await data.GetUriAsync();

                AddAndroidUri(uri.ToString());
            }
            else if (data?.Contains(StandardDataFormats.WebLink) ?? false)
            {
                var webLink = await data.GetWebLinkAsync();

                AddAndroidUri(webLink.ToString());
            }
            else if (data?.Contains(StandardDataFormats.ApplicationLink) ?? false)
            {
                var appLink = await data.GetApplicationLinkAsync();

                AddAndroidUri(appLink.ToString());
            }

            if (data?.Contains(StandardDataFormats.Html) ?? false)
            {
                var html = await data.GetHtmlFormatAsync();

                // Matches all tags
                Regex regex = new Regex("(<.*?>\\s*)+", RegexOptions.Singleline);
                // Replace tags by spaces and trim
                var plainText = regex.Replace(html, " ").Trim();

                items.Add(new ClipData.Item(plainText, html));
                mimeTypes.Add("text/html");
            }

            // Set all the data formats to the Android clipboard
            if (items.Count > 0)
            {
                ClipData clipData = new ClipData(
                    new ClipDescription(ClipboardDataLabel, mimeTypes.ToArray()),
                    items[0]);

                for (int itemIndex = 1; itemIndex < items.Count; itemIndex++)
                {
                    clipData.AddItem(items[itemIndex]);
                }

                var manager = ContextHelper.Current.GetSystemService(Context.ClipboardService) as ClipboardManager;
                if (manager is null)
                {
                    return;
                }

                manager.PrimaryClip = clipData;
            }
            else
            {
                // Clear clipboard
                Clear();
            }

            // Local function to convert a UWP Uri for Android and add it to the items list
            void AddAndroidUri(string uri)
            {
                var androidUri = Android.Net.Uri.Parse(uri);

                items.Add(new ClipData.Item(androidUri));
                mimeTypes.Add("text/uri-list");
            }
        }
Exemplo n.º 4
0
        internal static async Task SetContentAsync(DataPackage content)
        {
            var data = content?.GetView();

            var items     = new List <ClipData.Item>();
            var mimeTypes = new List <string>();

            if (data?.Contains(StandardDataFormats.Text) ?? false)
            {
                var text = await data.GetTextAsync();

                items.Add(new ClipData.Item(text));
                mimeTypes.Add("text/plaintext");
            }

            if (data != null)
            {
                var uri = DataPackage.CombineUri(
                    data.Contains(StandardDataFormats.WebLink) ? (await data.GetWebLinkAsync()).ToString() : null,
                    data.Contains(StandardDataFormats.ApplicationLink) ? (await data.GetApplicationLinkAsync()).ToString() : null,
                    data.Contains(StandardDataFormats.Uri) ? (await data.GetUriAsync()).ToString() : null);

                if (string.IsNullOrEmpty(uri) == false)
                {
                    var androidUri = Android.Net.Uri.Parse(uri);

                    items.Add(new ClipData.Item(androidUri));
                    mimeTypes.Add("text/uri-list");
                }
            }

            if (data?.Contains(StandardDataFormats.Html) ?? false)
            {
                var html = await data.GetHtmlFormatAsync();

                // Matches all tags
                Regex regex = new Regex("(<.*?>\\s*)+", RegexOptions.Singleline);
                // Replace tags by spaces and trim
                var plainText = regex.Replace(html, " ").Trim();

                items.Add(new ClipData.Item(plainText, html));
                mimeTypes.Add("text/html");
            }

            // Set all the data formats to the Android clipboard
            if (items.Count > 0)
            {
                ClipData clipData = new ClipData(
                    new ClipDescription(ClipboardDataLabel, mimeTypes.ToArray()),
                    items[0]);

                for (int itemIndex = 1; itemIndex < items.Count; itemIndex++)
                {
                    clipData.AddItem(items[itemIndex]);
                }

                var manager = ContextHelper.Current.GetSystemService(Context.ClipboardService) as ClipboardManager;
                if (manager is null)
                {
                    return;
                }

                manager.PrimaryClip = clipData;
            }
            else
            {
                // Clear clipboard
                Clear();
            }
        }