Created
July 28, 2017 12:16
-
-
Save oianmol/53b5db12d681878739660b7fbad74732 to your computer and use it in GitHub Desktop.
This EditText will support GBOARD for gifs and other attachments like "image/png", "image/gif", "image/jpeg","image/webp"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sports.spornado.util.view; | |
import android.content.Context; | |
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; | |
public class UriAwareEditText extends android.support.v7.widget.AppCompatEditText { | |
private String[] imgTypeString; | |
private KeyBoardInputCallbackListener keyBoardInputCallbackListener; | |
public UriAwareEditText(Context context) { | |
super(context); | |
initView(); | |
} | |
public UriAwareEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initView(); | |
} | |
private void initView() { | |
imgTypeString = new String[]{"image/png", | |
"image/gif", | |
"image/jpeg", | |
"image/webp"}; | |
} | |
@Override | |
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { | |
final InputConnection ic = super.onCreateInputConnection(outAttrs); | |
EditorInfoCompat.setContentMimeTypes(outAttrs, | |
imgTypeString); | |
return InputConnectionCompat.createWrapper(ic, outAttrs, callback); | |
} | |
final InputConnectionCompat.OnCommitContentListener callback = | |
new InputConnectionCompat.OnCommitContentListener() { | |
@Override | |
public boolean onCommitContent(InputContentInfoCompat inputContentInfo, | |
int flags, Bundle opts) { | |
// read and display inputContentInfo asynchronously | |
if (BuildCompat.isAtLeastNMR1() && (flags & | |
InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) { | |
try { | |
inputContentInfo.requestPermission(); | |
} catch (Exception e) { | |
return false; // return false if failed | |
} | |
} | |
boolean supported = false; | |
for (final String mimeType : imgTypeString) { | |
if (inputContentInfo.getDescription().hasMimeType(mimeType)) { | |
supported = true; | |
break; | |
} | |
} | |
if (!supported) { | |
return false; | |
} | |
if (keyBoardInputCallbackListener != null) { | |
keyBoardInputCallbackListener.onCommitContent(inputContentInfo, flags, opts); | |
} | |
return true; // return true if succeeded | |
} | |
}; | |
public interface KeyBoardInputCallbackListener { | |
void onCommitContent(InputContentInfoCompat inputContentInfo, | |
int flags, Bundle opts); | |
} | |
public void setKeyBoardInputCallbackListener(KeyBoardInputCallbackListener keyBoardInputCallbackListener) { | |
this.keyBoardInputCallbackListener = keyBoardInputCallbackListener; | |
} | |
public String[] getImgTypeString() { | |
return imgTypeString; | |
} | |
public void setImgTypeString(String[] imgTypeString) { | |
this.imgTypeString = imgTypeString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment