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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package ml.docilealligator.infinityforreddit.bottomsheetfragments;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import java.lang.reflect.Field;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.PostGalleryActivity;
import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment;
import ml.docilealligator.infinityforreddit.utils.Utils;
public class SetRedditGalleryItemCaptionAndUrlBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment {
public static final String EXTRA_POSITION = "EP";
public static final String EXTRA_CAPTION = "EC";
public static final String EXTRA_URL = "EU";
private PostGalleryActivity mActivity;
private TextInputLayout captionTextInputLayout;
private TextInputEditText captionTextInputEditText;
private TextInputLayout urlTextInputLayout;
private TextInputEditText urlTextInputEditText;
private MaterialButton okButton;
public SetRedditGalleryItemCaptionAndUrlBottomSheetFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_set_reddit_gallery_item_caption_and_url_bottom_sheet, container, false);
captionTextInputLayout = rootView.findViewById(R.id.caption_text_input_layout_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment);
captionTextInputEditText = rootView.findViewById(R.id.caption_text_input_edit_text_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment);
urlTextInputLayout = rootView.findViewById(R.id.url_text_input_layout_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment);
urlTextInputEditText = rootView.findViewById(R.id.url_text_input_edit_text_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment);
okButton = rootView.findViewById(R.id.ok_button_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment);
int primaryTextColor = mActivity.getResources().getColor(R.color.primaryTextColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
captionTextInputLayout.setCursorColor(ColorStateList.valueOf(primaryTextColor));
urlTextInputLayout.setCursorColor(ColorStateList.valueOf(primaryTextColor));
} else {
setCursorDrawableColor(captionTextInputEditText, primaryTextColor);
setCursorDrawableColor(urlTextInputEditText, primaryTextColor);
}
int position = getArguments().getInt(EXTRA_POSITION, -1);
String caption = getArguments().getString(EXTRA_CAPTION, "");
String url = getArguments().getString(EXTRA_URL, "");
captionTextInputEditText.setText(caption);
urlTextInputEditText.setText(url);
okButton.setOnClickListener(view -> {
mActivity.setCaptionAndUrl(position, captionTextInputEditText.getText().toString(), urlTextInputEditText.getText().toString());
dismiss();
});
if (mActivity.typeface != null) {
Utils.setFontToAllTextViews(rootView, mActivity.typeface);
}
return rootView;
}
private void setCursorDrawableColor(EditText editText, int color) {
try {
@SuppressLint("SoonBlockedPrivateApi") 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) { }
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mActivity = (PostGalleryActivity) context;
}
}
|