2021-01-13 23:31:37 +00:00
|
|
|
package org.signal.core.util;
|
|
|
|
|
2021-09-14 12:01:42 +00:00
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.annotation.TargetApi;
|
2021-07-20 16:08:52 +00:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffColorFilter;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Build;
|
2021-01-13 23:31:37 +00:00
|
|
|
import android.text.InputFilter;
|
|
|
|
import android.widget.EditText;
|
2021-07-20 16:08:52 +00:00
|
|
|
import android.widget.TextView;
|
2021-01-13 23:31:37 +00:00
|
|
|
|
2021-07-20 16:08:52 +00:00
|
|
|
import androidx.annotation.ColorInt;
|
2021-07-22 20:14:58 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2021-09-14 12:01:42 +00:00
|
|
|
import androidx.annotation.RequiresApi;
|
2021-07-20 16:08:52 +00:00
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2021-01-13 23:31:37 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public final class EditTextUtil {
|
|
|
|
|
|
|
|
private EditTextUtil() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addGraphemeClusterLimitFilter(EditText text, int maximumGraphemes) {
|
|
|
|
List<InputFilter> filters = new ArrayList<>(Arrays.asList(text.getFilters()));
|
|
|
|
filters.add(new GraphemeClusterLimitFilter(maximumGraphemes));
|
|
|
|
text.setFilters(filters.toArray(new InputFilter[0]));
|
|
|
|
}
|
2021-07-20 16:08:52 +00:00
|
|
|
|
2021-07-22 20:14:58 +00:00
|
|
|
public static void setCursorColor(@NonNull EditText text, @ColorInt int colorInt) {
|
2021-07-20 16:08:52 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= 29) {
|
2021-07-22 20:14:58 +00:00
|
|
|
Drawable drawable = text.getTextCursorDrawable();
|
2021-07-20 16:08:52 +00:00
|
|
|
|
2021-07-22 20:14:58 +00:00
|
|
|
if (drawable == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Drawable cursorDrawable = drawable.mutate();
|
2021-07-20 16:08:52 +00:00
|
|
|
cursorDrawable.setColorFilter(new PorterDuffColorFilter(colorInt, PorterDuff.Mode.SRC_IN));
|
|
|
|
text.setTextCursorDrawable(cursorDrawable);
|
|
|
|
} else {
|
|
|
|
setCursorColorViaReflection(text, colorInt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:01:42 +00:00
|
|
|
/**
|
|
|
|
* Note: This is only ever called in API 28 and less.
|
|
|
|
*/
|
|
|
|
@SuppressLint("SoonBlockedPrivateApi")
|
2021-07-20 16:08:52 +00:00
|
|
|
private static void setCursorColorViaReflection(EditText editText, int color) {
|
|
|
|
try {
|
|
|
|
Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
|
|
|
|
fCursorDrawableRes.setAccessible(true);
|
|
|
|
|
|
|
|
int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
|
|
|
|
Field fEditor = TextView.class.getDeclaredField("mEditor");
|
|
|
|
fEditor.setAccessible(true);
|
|
|
|
|
|
|
|
Object editor = fEditor.get(editText);
|
|
|
|
Class<?> clazz = editor.getClass();
|
|
|
|
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
|
|
|
|
|
|
|
|
fCursorDrawable.setAccessible(true);
|
|
|
|
Drawable[] drawables = new Drawable[2];
|
|
|
|
|
|
|
|
drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
|
|
|
|
drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
|
|
|
|
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
|
|
|
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
|
|
|
|
|
|
|
fCursorDrawable.set(editor, drawables);
|
|
|
|
} catch (Throwable ignored) {
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 23:31:37 +00:00
|
|
|
}
|