android.support.v13.view.inputmethod.InputContentInfoCompat Java Examples

The following examples show how to use android.support.v13.view.inputmethod.InputContentInfoCompat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComposeText.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts) {
  if (BuildCompat.isAtLeastNMR1() && (flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
    try {
      inputContentInfo.requestPermission();
    } catch (Exception e) {
      Log.w(TAG, e);
      return false;
    }
  }

  if (inputContentInfo.getDescription().getMimeTypeCount() > 0) {
    mediaListener.onMediaSelected(inputContentInfo.getContentUri(),
                                  inputContentInfo.getDescription().getMimeType(0));

    return true;
  }

  return false;
}
 
Example #2
Source File: ConversationFragment.java    From Conversations with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts, String[] contentMimeTypes) {
    // try to get permission to read the image, if applicable
    if ((flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
        try {
            inputContentInfo.requestPermission();
        } catch (Exception e) {
            Log.e(Config.LOGTAG, "InputContentInfoCompat#requestPermission() failed.", e);
            Toast.makeText(getActivity(), activity.getString(R.string.no_permission_to_access_x, inputContentInfo.getDescription()), Toast.LENGTH_LONG
            ).show();
            return false;
        }
    }
    if (hasPermissions(REQUEST_ADD_EDITOR_CONTENT, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        attachEditorContentToConversation(inputContentInfo.getContentUri());
    } else {
        mPendingEditorContent = inputContentInfo.getContentUri();
    }
    return true;
}
 
Example #3
Source File: MainActivity.java    From input-samples with Apache License 2.0 5 votes vote down vote up
private boolean 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 (Exception e) {
        Log.e(TAG, "InputContentInfoCompat#releasePermission() failed.", e);
    } finally {
        mCurrentInputContentInfo = null;
    }

    mWebView.loadUrl("about:blank");
    mMimeTypes.setText("");
    mContentUri.setText("");
    mLabel.setText("");
    mLinkUri.setText("");
    mFlags.setText("");

    boolean supported = false;
    for (final String mimeType : contentMimeTypes) {
        if (inputContentInfo.getDescription().hasMimeType(mimeType)) {
            supported = true;
            break;
        }
    }
    if (!supported) {
        return false;
    }

    return onCommitContentInternal(inputContentInfo, flags);
}
 
Example #4
Source File: MainActivity.java    From input-samples with Apache License 2.0 5 votes vote down vote up
private boolean onCommitContentInternal(InputContentInfoCompat inputContentInfo, int flags) {
    if ((flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
        try {
            inputContentInfo.requestPermission();
        } catch (Exception e) {
            Log.e(TAG, "InputContentInfoCompat#requestPermission() failed.", e);
            return false;
        }
    }

    mMimeTypes.setText(
            Arrays.toString(inputContentInfo.getDescription().filterMimeTypes("*/*")));
    mContentUri.setText(inputContentInfo.getContentUri().toString());
    mLabel.setText(inputContentInfo.getDescription().getLabel());
    Uri linkUri = inputContentInfo.getLinkUri();
    mLinkUri.setText(linkUri != null ? linkUri.toString() : "null");
    mFlags.setText(flagsToString(flags));
    mWebView.loadUrl(inputContentInfo.getContentUri().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;
}
 
Example #5
Source File: MainActivity.java    From input-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of {@link EditText} that is configured to specify the given content
 * MIME types to EditorInfo#contentMimeTypes so that developers can locally test how the current
 * input method behaves for such content MIME types.
 *
 * @param contentMimeTypes A {@link String} array that indicates the supported content MIME
 *                         types
 * @return a new instance of {@link EditText}, which specifies EditorInfo#contentMimeTypes with
 * the given content MIME types
 */
private EditText createEditTextWithContentMimeTypes(String[] contentMimeTypes) {
    final CharSequence hintText;
    final String[] mimeTypes;  // our own copy of contentMimeTypes.
    if (contentMimeTypes == null || contentMimeTypes.length == 0) {
        hintText = "MIME: []";
        mimeTypes = new String[0];
    } else {
        hintText = "MIME: " + Arrays.toString(contentMimeTypes);
        mimeTypes = Arrays.copyOf(contentMimeTypes, contentMimeTypes.length);
    }
    EditText exitText = new EditText(this) {
        @Override
        public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
            final InputConnection ic = super.onCreateInputConnection(editorInfo);
            EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes);
            final InputConnectionCompat.OnCommitContentListener callback =
                    new InputConnectionCompat.OnCommitContentListener() {
                        @Override
                        public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
                                int flags, Bundle opts) {
                            return MainActivity.this.onCommitContent(
                                    inputContentInfo, flags, opts, mimeTypes);
                        }
                    };
            return InputConnectionCompat.createWrapper(ic, editorInfo, callback);
        }
    };
    exitText.setHint(hintText);
    exitText.setTextColor(Color.WHITE);
    exitText.setHintTextColor(Color.WHITE);
    return exitText;
}
 
Example #6
Source File: MainActivity.java    From android-CommitContentSampleApp with Apache License 2.0 5 votes vote down vote up
private boolean 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 (Exception e) {
        Log.e(TAG, "InputContentInfoCompat#releasePermission() failed.", e);
    } finally {
        mCurrentInputContentInfo = null;
    }

    mWebView.loadUrl("about:blank");
    mMimeTypes.setText("");
    mContentUri.setText("");
    mLabel.setText("");
    mLinkUri.setText("");
    mFlags.setText("");

    boolean supported = false;
    for (final String mimeType : contentMimeTypes) {
        if (inputContentInfo.getDescription().hasMimeType(mimeType)) {
            supported = true;
            break;
        }
    }
    if (!supported) {
        return false;
    }

    return onCommitContentInternal(inputContentInfo, flags);
}
 
Example #7
Source File: MainActivity.java    From android-CommitContentSampleApp with Apache License 2.0 5 votes vote down vote up
private boolean onCommitContentInternal(InputContentInfoCompat inputContentInfo, int flags) {
    if ((flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
        try {
            inputContentInfo.requestPermission();
        } catch (Exception e) {
            Log.e(TAG, "InputContentInfoCompat#requestPermission() failed.", e);
            return false;
        }
    }

    mMimeTypes.setText(
            Arrays.toString(inputContentInfo.getDescription().filterMimeTypes("*/*")));
    mContentUri.setText(inputContentInfo.getContentUri().toString());
    mLabel.setText(inputContentInfo.getDescription().getLabel());
    Uri linkUri = inputContentInfo.getLinkUri();
    mLinkUri.setText(linkUri != null ? linkUri.toString() : "null");
    mFlags.setText(flagsToString(flags));
    mWebView.loadUrl(inputContentInfo.getContentUri().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;
}
 
Example #8
Source File: MainActivity.java    From android-CommitContentSampleApp with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of {@link EditText} that is configured to specify the given content
 * MIME types to EditorInfo#contentMimeTypes so that developers can locally test how the current
 * input method behaves for such content MIME types.
 *
 * @param contentMimeTypes A {@link String} array that indicates the supported content MIME
 *                         types
 * @return a new instance of {@link EditText}, which specifies EditorInfo#contentMimeTypes with
 * the given content MIME types
 */
private EditText createEditTextWithContentMimeTypes(String[] contentMimeTypes) {
    final CharSequence hintText;
    final String[] mimeTypes;  // our own copy of contentMimeTypes.
    if (contentMimeTypes == null || contentMimeTypes.length == 0) {
        hintText = "MIME: []";
        mimeTypes = new String[0];
    } else {
        hintText = "MIME: " + Arrays.toString(contentMimeTypes);
        mimeTypes = Arrays.copyOf(contentMimeTypes, contentMimeTypes.length);
    }
    EditText exitText = new EditText(this) {
        @Override
        public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
            final InputConnection ic = super.onCreateInputConnection(editorInfo);
            EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes);
            final InputConnectionCompat.OnCommitContentListener callback =
                    new InputConnectionCompat.OnCommitContentListener() {
                        @Override
                        public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
                                int flags, Bundle opts) {
                            return MainActivity.this.onCommitContent(
                                    inputContentInfo, flags, opts, mimeTypes);
                        }
                    };
            return InputConnectionCompat.createWrapper(ic, editorInfo, callback);
        }
    };
    exitText.setHint(hintText);
    exitText.setTextColor(Color.WHITE);
    exitText.setHintTextColor(Color.WHITE);
    return exitText;
}
 
Example #9
Source File: ImageInsertEditText.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public InputConnection onCreateInputConnection(EditorInfo attrs) {
    InputConnection con = super.onCreateInputConnection(attrs);
    EditorInfoCompat.setContentMimeTypes(attrs, new String[] { "image/gif", "image/png" });

    return InputConnectionCompat.createWrapper(con, attrs, new InputConnectionCompat.OnCommitContentListener() {
        @Override
        public boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts) {
            if (callback != null) {
                if (BuildCompat.isAtLeastNMR1() &&
                        (flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                    try {
                        inputContentInfo.requestPermission();
                    } catch (Exception e) {
                        return false;
                    }
                }

                callback.onImageSelected(
                        inputContentInfo.getContentUri(),
                        inputContentInfo.getDescription().getMimeType(0)
                );

                return true;
            } else {
                return false;
            }
        }
    });
}
 
Example #10
Source File: ImageKeyboard.java    From input-samples with Apache License 2.0 4 votes vote down vote up
private void doCommitContent(@NonNull String description, @NonNull String mimeType,
        @NonNull File file) {
    final EditorInfo editorInfo = getCurrentInputEditorInfo();

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

    final Uri 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.
    final int flag;
    if (Build.VERSION.SDK_INT >= 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.INPUT_CONTENT_GRANT_READ_URI_PERMISSION;
    } 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, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } catch (Exception e){
            Log.e(TAG, "grantUriPermission failed packageName=" + editorInfo.packageName
                    + " contentUri=" + contentUri, e);
        }
    }

    final InputContentInfoCompat inputContentInfoCompat = new InputContentInfoCompat(
            contentUri,
            new ClipDescription(description, new String[]{mimeType}),
            null /* linkUrl */);
    InputConnectionCompat.commitContent(
            getCurrentInputConnection(), getCurrentInputEditorInfo(), inputContentInfoCompat,
            flag, null);
}
 
Example #11
Source File: ImageKeyboard.java    From android-CommitContentSampleIME with Apache License 2.0 4 votes vote down vote up
private void doCommitContent(@NonNull String description, @NonNull String mimeType,
        @NonNull File file) {
    final EditorInfo editorInfo = getCurrentInputEditorInfo();

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

    final Uri 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.
    final int flag;
    if (Build.VERSION.SDK_INT >= 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.INPUT_CONTENT_GRANT_READ_URI_PERMISSION;
    } 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, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } catch (Exception e){
            Log.e(TAG, "grantUriPermission failed packageName=" + editorInfo.packageName
                    + " contentUri=" + contentUri, e);
        }
    }

    final InputContentInfoCompat inputContentInfoCompat = new InputContentInfoCompat(
            contentUri,
            new ClipDescription(description, new String[]{mimeType}),
            null /* linkUrl */);
    InputConnectionCompat.commitContent(
            getCurrentInputConnection(), getCurrentInputEditorInfo(), inputContentInfoCompat,
            flag, null);
}
 
Example #12
Source File: EditMessage.java    From Conversations with GNU General Public License v3.0 votes vote down vote up
boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts, String[] mimeTypes);