Skip to content

Commit e910b1d

Browse files
authored
Pasted text should have fonts unified (for Caption/Description) (commons-app#4667)
* Changed hardcoded "More" to getStrings(R.string.more) for Unlogged user - MainActivity toolbar showed "More" when clicked on More options in other language than English * Changed hardcoded "More" to getStrings(R.string.more) for Logged user - MainActivity toolbar showed "More" when clicked on More options in other language than English * Added test for: MainActivity.setUpPager * Pasted text is now unformatted for caption and description * Removed other branch contribution * Added test * Rename .java to .kt * Test from Java to Kotlin; +small fix * PasteSensitiveTextInputEditTextTest - updated
1 parent ad0aa7d commit e910b1d

File tree

5 files changed

+159
-9
lines changed

5 files changed

+159
-9
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package fr.free.nrw.commons.ui
2+
3+
import android.R
4+
import android.content.Context
5+
import android.os.Build
6+
import android.util.AttributeSet
7+
import androidx.test.core.app.ApplicationProvider
8+
import androidx.test.runner.AndroidJUnit4
9+
import fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText
10+
import org.junit.Assert
11+
import org.junit.Before
12+
import org.junit.Test
13+
import org.junit.runner.RunWith
14+
import java.lang.Exception
15+
import kotlin.Throws
16+
17+
@RunWith(AndroidJUnit4::class)
18+
class PasteSensitiveTextInputEditTextTest {
19+
20+
private var context: Context? = null
21+
private var textView: PasteSensitiveTextInputEditText? = null
22+
23+
@Before
24+
fun setup() {
25+
context = ApplicationProvider.getApplicationContext()
26+
textView = PasteSensitiveTextInputEditText(context)
27+
}
28+
29+
@Test
30+
fun onTextContextMenuItemPasteFormattingDisabled() {
31+
textView!!.setFormattingAllowed(false);
32+
textView!!.setText("Text")
33+
textView!!.onTextContextMenuItem(R.id.paste)
34+
Assert.assertEquals("Text", textView!!.text.toString())
35+
}
36+
37+
@Test
38+
fun onTextContextMenuItemPasteFormattingAllowed() {
39+
textView!!.setFormattingAllowed(true);
40+
textView!!.setText("Text")
41+
textView!!.onTextContextMenuItem(R.id.paste)
42+
Assert.assertEquals("Text", textView!!.text.toString())
43+
}
44+
45+
@Test
46+
fun onTextContextMenuItemPaste() {
47+
textView!!.setText("Text")
48+
textView!!.onTextContextMenuItem(R.id.paste)
49+
Assert.assertEquals("Text", textView!!.text.toString())
50+
}
51+
52+
53+
@Test
54+
fun onTextContextMenuItemNotPaste() {
55+
textView!!.setText("Text")
56+
textView!!.onTextContextMenuItem(R.id.copy)
57+
Assert.assertEquals("Text", textView!!.text.toString())
58+
}
59+
60+
// this test has no real value, just % for test code coverage
61+
@Test
62+
fun extractFormattingAttributeSet(){
63+
val methodExtractFormattingAttribute = textView!!.javaClass.getDeclaredMethod(
64+
"extractFormattingAttribute", Context::class.java, AttributeSet::class.java)
65+
methodExtractFormattingAttribute.isAccessible = true
66+
methodExtractFormattingAttribute.invoke(textView, context, null)
67+
}
68+
69+
@Test
70+
@Throws(Exception::class)
71+
fun setFormattingAllowed() {
72+
val fieldFormattingAllowed = textView!!.javaClass.getDeclaredField("formattingAllowed")
73+
fieldFormattingAllowed.isAccessible = true
74+
textView!!.setFormattingAllowed(true)
75+
Assert.assertTrue(fieldFormattingAllowed.getBoolean(textView))
76+
textView!!.setFormattingAllowed(false)
77+
Assert.assertFalse(fieldFormattingAllowed.getBoolean(textView))
78+
}
79+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package fr.free.nrw.commons.ui;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.os.Build;
6+
import android.os.Build.VERSION;
7+
import android.util.AttributeSet;
8+
import android.util.Log;
9+
import com.google.android.material.textfield.TextInputEditText;
10+
import fr.free.nrw.commons.R;
11+
12+
public class PasteSensitiveTextInputEditText extends TextInputEditText {
13+
14+
private boolean formattingAllowed = true;
15+
16+
public PasteSensitiveTextInputEditText(final Context context) {
17+
super(context);
18+
}
19+
20+
public PasteSensitiveTextInputEditText(final Context context, final AttributeSet attrs) {
21+
super(context, attrs);
22+
formattingAllowed = extractFormattingAttribute(context, attrs);
23+
}
24+
25+
@Override
26+
public boolean onTextContextMenuItem(int id) {
27+
28+
// if not paste command, or formatting is allowed, return default
29+
if(id != android.R.id.paste || formattingAllowed){
30+
return super.onTextContextMenuItem(id);
31+
}
32+
33+
// if its paste and formatting not allowed
34+
boolean proceeded;
35+
if(VERSION.SDK_INT >= 23) {
36+
proceeded = super.onTextContextMenuItem(android.R.id.pasteAsPlainText);
37+
}else {
38+
proceeded = super.onTextContextMenuItem(id);
39+
if (proceeded && getText() != null) {
40+
// rewrite with plain text so formatting is lost
41+
setText(getText().toString());
42+
setSelection(getText().length());
43+
}
44+
}
45+
return proceeded;
46+
}
47+
48+
private boolean extractFormattingAttribute(Context context, AttributeSet attrs){
49+
50+
boolean formatAllowed = true;
51+
52+
TypedArray a = context.getTheme().obtainStyledAttributes(
53+
attrs, R.styleable.PasteSensitiveTextInputEditText, 0, 0);
54+
55+
try {
56+
formatAllowed = a.getBoolean(
57+
R.styleable.PasteSensitiveTextInputEditText_allowFormatting, true);
58+
} finally {
59+
a.recycle();
60+
}
61+
return formatAllowed;
62+
}
63+
64+
public void setFormattingAllowed(boolean formattingAllowed){
65+
this.formattingAllowed = formattingAllowed;
66+
}
67+
}

app/src/main/java/fr/free/nrw/commons/upload/UploadMediaDetailAdapter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
import android.widget.Spinner;
1111
import androidx.annotation.NonNull;
1212
import androidx.annotation.Nullable;
13-
import androidx.appcompat.widget.AppCompatEditText;
14-
import androidx.appcompat.widget.AppCompatSpinner;
1513
import androidx.recyclerview.widget.RecyclerView;
1614
import butterknife.BindView;
1715
import butterknife.ButterKnife;
18-
import com.google.android.material.textfield.TextInputEditText;
1916
import com.google.android.material.textfield.TextInputLayout;
2017
import fr.free.nrw.commons.R;
18+
import fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText;
2119
import fr.free.nrw.commons.utils.AbstractTextWatcher;
2220
import java.util.ArrayList;
2321
import java.util.HashMap;
@@ -93,13 +91,13 @@ public class ViewHolder extends RecyclerView.ViewHolder {
9391
Spinner spinnerDescriptionLanguages;
9492

9593
@BindView(R.id.description_item_edit_text)
96-
TextInputEditText descItemEditText;
94+
PasteSensitiveTextInputEditText descItemEditText;
9795

9896
@BindView(R.id.description_item_edit_text_input_layout)
9997
TextInputLayout descInputLayout;
10098

10199
@BindView(R.id.caption_item_edit_text)
102-
TextInputEditText captionItemEditText;
100+
PasteSensitiveTextInputEditText captionItemEditText;
103101

104102
@BindView(R.id.caption_item_edit_text_input_layout)
105103
TextInputLayout captionInputLayout;

app/src/main/res/layout/row_item_description.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
app:layout_constraintEnd_toEndOf="parent"
4040
app:layout_constraintStart_toStartOf="parent">
4141

42-
<com.google.android.material.textfield.TextInputEditText
42+
<fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText
4343
android:id="@+id/caption_item_edit_text"
4444
android:layout_width="match_parent"
4545
android:layout_height="match_parent"
4646
android:hint="@string/share_caption_hint"
4747
android:imeOptions="actionNext|flagNoExtractUi"
48-
android:inputType="textMultiLine" />
48+
android:inputType="textMultiLine"
49+
app:allowFormatting="false" />
4950
</com.google.android.material.textfield.TextInputLayout>
5051

5152
<com.google.android.material.textfield.TextInputLayout
@@ -56,13 +57,14 @@
5657
app:layout_constraintEnd_toEndOf="parent"
5758
app:layout_constraintStart_toStartOf="parent">
5859

59-
<com.google.android.material.textfield.TextInputEditText
60+
<fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText
6061
android:id="@+id/description_item_edit_text"
6162
android:layout_width="match_parent"
6263
android:layout_height="match_parent"
6364
android:hint="@string/share_description_hint"
6465
android:imeOptions="actionNext|flagNoExtractUi"
65-
android:inputType="textMultiLine" />
66+
android:inputType="textMultiLine"
67+
app:allowFormatting="false" />
6668
</com.google.android.material.textfield.TextInputLayout>
6769

6870
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,8 @@
5757
<attr name="camera" format="color"/>
5858
<attr name="centerRegion" format="color"/>
5959
</declare-styleable>
60+
61+
<declare-styleable name="PasteSensitiveTextInputEditText">
62+
<attr name="allowFormatting" format="boolean"/>
63+
</declare-styleable>
6064
</resources>

0 commit comments

Comments
 (0)