Example #1
0
        bool OnCommitContentInternal(InputContentInfoCompat inputContentInfo, int flags)
        {
            if ((flags & InputConnectionCompat.InputContentGrantReadUriPermission) != 0)
            {
                try
                {
                    inputContentInfo.RequestPermission();
                }
                catch (Java.Lang.Exception e)
                {
                    Log.Error(Tag, "InputContentInfoCompat#requestPermission() failed.", e);
                    return(false);
                }
            }

            mMimeTypes.Text  = string.Join(".", inputContentInfo.Description.FilterMimeTypes("*/*"));
            mContentUri.Text = inputContentInfo.ContentUri.ToString();
            mLabel.Text      = inputContentInfo.Description.Label;
            Android.Net.Uri linkUri = inputContentInfo.LinkUri;
            mLinkUri.Text = linkUri != null?linkUri.ToString() : "null";

            mFlags.Text = FlagsToString(flags);
            mWebView.LoadUrl(inputContentInfo.ContentUri.ToString());
            mWebView.SetBackgroundColor(Color.Transparent);

            // Due to the asynchronous nature of WebView, it is a bit too early to call
            // inputContentInfo.releasePermission() here. Hence we call IC#releasePermission() when this
            // method is called next time.  Note that calling IC#releasePermission() is just to be a
            // good citizen. Even if we failed to call that method, the system would eventually revoke
            // the permission sometime after inputContentInfo object gets garbage-collected.
            mCurrentInputContentInfo = inputContentInfo;
            mCurrentFlags            = flags;

            return(true);
        }
        void DoCommitContent(string description, string mimeType, File file)
        {
            var editorInfo = CurrentInputEditorInfo;

            // Validate packageName again just in case.
            if (!ValidatePackageName(editorInfo))
            {
                return;
            }

            var contentUri = FileProvider.GetUriForFile(this, Authority, file);

            // As you as an IME author are most likely to have to implement your own content provider
            // to support CommitContent API, it is important to have a clear spec about what
            // applications are going to be allowed to access the content that your are going to share.
            int flag;

            if ((int)Build.VERSION.SdkInt >= 25)
            {
                // On API 25 and later devices, as an analogy of Intent.FLAG_GRANT_READ_URI_PERMISSION,
                // you can specify InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION to give
                // a temporary read access to the recipient application without exporting your content
                // provider.
                flag = InputConnectionCompat.InputContentGrantReadUriPermission;
            }
            else
            {
                // On API 24 and prior devices, we cannot rely on
                // InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION. You as an IME author
                // need to decide what access control is needed (or not needed) for content URIs that
                // you are going to expose. This sample uses Context.grantUriPermission(), but you can
                // implement your own mechanism that satisfies your own requirements.
                flag = 0;
                try
                {
                    // TODO: Use revokeUriPermission to revoke as needed.
                    GrantUriPermission(
                        editorInfo.PackageName, contentUri, ActivityFlags.GrantReadUriPermission);
                }
                catch (Exception e)
                {
                    Log.Error(Tag, "grantUriPermission failed packageName=" + editorInfo.PackageName
                              + " contentUri=" + contentUri, e);
                }
            }

            var inputContentInfoCompat = new InputContentInfoCompat(
                contentUri,
                new ClipDescription(description, new [] { mimeType }),
                null /* linkUrl */);

            InputConnectionCompat.CommitContent(
                CurrentInputConnection, CurrentInputEditorInfo, inputContentInfoCompat,
                flag, null);
        }
Example #3
0
 protected override void OnSaveInstanceState(Bundle outState)
 {
     if (mCurrentInputContentInfo != null)
     {
         outState.PutParcelable(InputContentInfoKey, (IParcelable)mCurrentInputContentInfo.Unwrap());
         outState.PutInt(CommitContentFlagsKey, mCurrentFlags);
     }
     mCurrentInputContentInfo = null;
     mCurrentFlags            = 0;
     base.OnSaveInstanceState(outState);
 }
Example #4
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.commit_content);

            var layout = FindViewById <LinearLayout>(Resource.Id.commit_content_sample_edit_boxes);

            // This declares that the IME cannot commit any content with
            // InputConnectionCompat#commitContent().
            layout.AddView(CreateEditTextWithContentMimeTypes(null));

            // This declares that the IME can commit contents with
            // InputConnectionCompat#commitContent() if they match "image/gif".
            layout.AddView(CreateEditTextWithContentMimeTypes(new [] { "image/gif" }));

            // This declares that the IME can commit contents with
            // InputConnectionCompat#commitContent() if they match "image/png".
            layout.AddView(CreateEditTextWithContentMimeTypes(new [] { "image/png" }));

            // This declares that the IME can commit contents with
            // InputConnectionCompat#commitContent() if they match "image/jpeg".
            layout.AddView(CreateEditTextWithContentMimeTypes(new [] { "image/jpeg" }));

            // This declares that the IME can commit contents with
            // InputConnectionCompat#commitContent() if they match "image/webp".
            layout.AddView(CreateEditTextWithContentMimeTypes(new [] { "image/webp" }));

            // This declares that the IME can commit contents with
            // InputConnectionCompat#commitContent() if they match "image/png", "image/gif",
            // "image/jpeg", or "image/webp".
            layout.AddView(CreateEditTextWithContentMimeTypes(new [] { "image/png", "image/gif", "image/jpeg", "image/webp" }));

            mWebView    = FindViewById <WebView>(Resource.Id.commit_content_webview);
            mMimeTypes  = FindViewById <TextView>(Resource.Id.text_commit_content_mime_types);
            mLabel      = FindViewById <TextView>(Resource.Id.text_commit_content_label);
            mContentUri = FindViewById <TextView>(Resource.Id.text_commit_content_content_uri);
            mLinkUri    = FindViewById <TextView>(Resource.Id.text_commit_content_link_uri);
            mFlags      = FindViewById <TextView>(Resource.Id.text_commit_content_link_flags);

            if (bundle == null)
            {
                return;
            }
            var previousInputContentInfo = InputContentInfoCompat.Wrap(
                bundle.GetParcelable(InputContentInfoKey));
            var previousFlags = bundle.GetInt(CommitContentFlagsKey);

            if (previousInputContentInfo != null)
            {
                OnCommitContentInternal(previousInputContentInfo, previousFlags);
            }
        }
Example #5
0
        public bool OnCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts)
        {
            bool permission_requested = false;
            bool processed            = false;

            // read and display inputContentInfo asynchronously
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.NMr1 &&
                (flags & InputConnectionCompat.InputContentGrantReadUriPermission) != 0)
            {
                try
                {
                    inputContentInfo.RequestPermission();
                    permission_requested = true;
                }
                catch (Exception)
                {
                    return(processed);
                }
            }

            if (inputContentInfo.LinkUri != null)
            {
                string url = inputContentInfo.LinkUri.ToString();

                Page p = App.Current.MainPage.Navigation.NavigationStack.Last();
                if (p != null && p.GetType() == typeof(SingleChatPage))
                {
                    string rx_pattern = @"^https://[A-Za-z0-9]+\.(tenor|giphy)\.com/[A-Za-z0-9_/=%\?\-\.\&]+$";

                    if (Regex.IsMatch(url, rx_pattern))
                    {
                        ((SingleChatPage)p).onSend(url);
                        processed = true;
                    }
                }
            }
            else
            {
                Logging.error("Error adding keyboard content, LinkUri is null");
            }

            if (permission_requested)
            {
                inputContentInfo.ReleasePermission();
            }

            return(processed);
        }
Example #6
0
        bool OnCommitContent(InputContentInfoCompat inputContentInfo, int flags,
                             Bundle opts, string[] contentMimeTypes)
        {
            // Clear the temporary permission (if any).  See below about why we do this here.
            try
            {
                if (mCurrentInputContentInfo != null)
                {
                    mCurrentInputContentInfo.ReleasePermission();
                }
            }
            catch (Java.Lang.Exception e)
            {
                Log.Error(Tag, "InputContentInfoCompat#releasePermission() failed.", e);
            }
            finally
            {
                mCurrentInputContentInfo = null;
            }

            mWebView.LoadUrl("about:blank");
            mMimeTypes.Text  = "";
            mContentUri.Text = "";
            mLabel.Text      = "";
            mLinkUri.Text    = "";
            mFlags.Text      = "";

            var supported = false;

            foreach (var contentMimeType in contentMimeTypes)
            {
                if (inputContentInfo.Description.HasMimeType(contentMimeType))
                {
                    supported = true;
                    break;
                }
            }
            if (!supported)
            {
                return(false);
            }

            return(OnCommitContentInternal(inputContentInfo, flags));
        }
Example #7
0
 public bool OnCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts)
 {
     return(Owner.OnCommitContent(inputContentInfo, flags, opts, MimeTypes));
 }