This repository has been archived by the owner on Jan 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageKeyboardEditText.java
77 lines (62 loc) · 2.63 KB
/
ImageKeyboardEditText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package xyz.klinker.nougatplayground;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v13.view.inputmethod.EditorInfoCompat;
import android.support.v13.view.inputmethod.InputConnectionCompat;
import android.support.v13.view.inputmethod.InputContentInfoCompat;
import android.support.v4.os.BuildCompat;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;
public class ImageKeyboardEditText extends EditText {
public interface ImageSelectedCallback {
void onImageSelected(Uri content, String mimeType);
}
// region view constructors
public ImageKeyboardEditText(Context context) {
super(context);
}
public ImageKeyboardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImageKeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ImageKeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
// endregion
private ImageSelectedCallback callback;
public void setImageSelectedCallback(ImageSelectedCallback callback) {
this.callback = callback;
}
@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;
}
}
});
}
}