mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Pasted text should have fonts unified (for Caption/Description) (#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
This commit is contained in:
parent
ad0aa7d4ea
commit
e910b1d14f
5 changed files with 159 additions and 9 deletions
|
|
@ -0,0 +1,79 @@
|
|||
package fr.free.nrw.commons.ui
|
||||
|
||||
import android.R
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.util.AttributeSet
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.runner.AndroidJUnit4
|
||||
import fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.lang.Exception
|
||||
import kotlin.Throws
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class PasteSensitiveTextInputEditTextTest {
|
||||
|
||||
private var context: Context? = null
|
||||
private var textView: PasteSensitiveTextInputEditText? = null
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
context = ApplicationProvider.getApplicationContext()
|
||||
textView = PasteSensitiveTextInputEditText(context)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onTextContextMenuItemPasteFormattingDisabled() {
|
||||
textView!!.setFormattingAllowed(false);
|
||||
textView!!.setText("Text")
|
||||
textView!!.onTextContextMenuItem(R.id.paste)
|
||||
Assert.assertEquals("Text", textView!!.text.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onTextContextMenuItemPasteFormattingAllowed() {
|
||||
textView!!.setFormattingAllowed(true);
|
||||
textView!!.setText("Text")
|
||||
textView!!.onTextContextMenuItem(R.id.paste)
|
||||
Assert.assertEquals("Text", textView!!.text.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onTextContextMenuItemPaste() {
|
||||
textView!!.setText("Text")
|
||||
textView!!.onTextContextMenuItem(R.id.paste)
|
||||
Assert.assertEquals("Text", textView!!.text.toString())
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun onTextContextMenuItemNotPaste() {
|
||||
textView!!.setText("Text")
|
||||
textView!!.onTextContextMenuItem(R.id.copy)
|
||||
Assert.assertEquals("Text", textView!!.text.toString())
|
||||
}
|
||||
|
||||
// this test has no real value, just % for test code coverage
|
||||
@Test
|
||||
fun extractFormattingAttributeSet(){
|
||||
val methodExtractFormattingAttribute = textView!!.javaClass.getDeclaredMethod(
|
||||
"extractFormattingAttribute", Context::class.java, AttributeSet::class.java)
|
||||
methodExtractFormattingAttribute.isAccessible = true
|
||||
methodExtractFormattingAttribute.invoke(textView, context, null)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun setFormattingAllowed() {
|
||||
val fieldFormattingAllowed = textView!!.javaClass.getDeclaredField("formattingAllowed")
|
||||
fieldFormattingAllowed.isAccessible = true
|
||||
textView!!.setFormattingAllowed(true)
|
||||
Assert.assertTrue(fieldFormattingAllowed.getBoolean(textView))
|
||||
textView!!.setFormattingAllowed(false)
|
||||
Assert.assertFalse(fieldFormattingAllowed.getBoolean(textView))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package fr.free.nrw.commons.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Build;
|
||||
import android.os.Build.VERSION;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import fr.free.nrw.commons.R;
|
||||
|
||||
public class PasteSensitiveTextInputEditText extends TextInputEditText {
|
||||
|
||||
private boolean formattingAllowed = true;
|
||||
|
||||
public PasteSensitiveTextInputEditText(final Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public PasteSensitiveTextInputEditText(final Context context, final AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
formattingAllowed = extractFormattingAttribute(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTextContextMenuItem(int id) {
|
||||
|
||||
// if not paste command, or formatting is allowed, return default
|
||||
if(id != android.R.id.paste || formattingAllowed){
|
||||
return super.onTextContextMenuItem(id);
|
||||
}
|
||||
|
||||
// if its paste and formatting not allowed
|
||||
boolean proceeded;
|
||||
if(VERSION.SDK_INT >= 23) {
|
||||
proceeded = super.onTextContextMenuItem(android.R.id.pasteAsPlainText);
|
||||
}else {
|
||||
proceeded = super.onTextContextMenuItem(id);
|
||||
if (proceeded && getText() != null) {
|
||||
// rewrite with plain text so formatting is lost
|
||||
setText(getText().toString());
|
||||
setSelection(getText().length());
|
||||
}
|
||||
}
|
||||
return proceeded;
|
||||
}
|
||||
|
||||
private boolean extractFormattingAttribute(Context context, AttributeSet attrs){
|
||||
|
||||
boolean formatAllowed = true;
|
||||
|
||||
TypedArray a = context.getTheme().obtainStyledAttributes(
|
||||
attrs, R.styleable.PasteSensitiveTextInputEditText, 0, 0);
|
||||
|
||||
try {
|
||||
formatAllowed = a.getBoolean(
|
||||
R.styleable.PasteSensitiveTextInputEditText_allowFormatting, true);
|
||||
} finally {
|
||||
a.recycle();
|
||||
}
|
||||
return formatAllowed;
|
||||
}
|
||||
|
||||
public void setFormattingAllowed(boolean formattingAllowed){
|
||||
this.formattingAllowed = formattingAllowed;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,14 +10,12 @@ import android.widget.ImageView;
|
|||
import android.widget.Spinner;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.AppCompatEditText;
|
||||
import androidx.appcompat.widget.AppCompatSpinner;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText;
|
||||
import fr.free.nrw.commons.utils.AbstractTextWatcher;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -93,13 +91,13 @@ public class UploadMediaDetailAdapter extends RecyclerView.Adapter<UploadMediaDe
|
|||
Spinner spinnerDescriptionLanguages;
|
||||
|
||||
@BindView(R.id.description_item_edit_text)
|
||||
TextInputEditText descItemEditText;
|
||||
PasteSensitiveTextInputEditText descItemEditText;
|
||||
|
||||
@BindView(R.id.description_item_edit_text_input_layout)
|
||||
TextInputLayout descInputLayout;
|
||||
|
||||
@BindView(R.id.caption_item_edit_text)
|
||||
TextInputEditText captionItemEditText;
|
||||
PasteSensitiveTextInputEditText captionItemEditText;
|
||||
|
||||
@BindView(R.id.caption_item_edit_text_input_layout)
|
||||
TextInputLayout captionInputLayout;
|
||||
|
|
|
|||
|
|
@ -39,13 +39,14 @@
|
|||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
<fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText
|
||||
android:id="@+id/caption_item_edit_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:hint="@string/share_caption_hint"
|
||||
android:imeOptions="actionNext|flagNoExtractUi"
|
||||
android:inputType="textMultiLine" />
|
||||
android:inputType="textMultiLine"
|
||||
app:allowFormatting="false" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
|
|
@ -56,13 +57,14 @@
|
|||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
<fr.free.nrw.commons.ui.PasteSensitiveTextInputEditText
|
||||
android:id="@+id/description_item_edit_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:hint="@string/share_description_hint"
|
||||
android:imeOptions="actionNext|flagNoExtractUi"
|
||||
android:inputType="textMultiLine" />
|
||||
android:inputType="textMultiLine"
|
||||
app:allowFormatting="false" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
|
|||
|
|
@ -57,4 +57,8 @@
|
|||
<attr name="camera" format="color"/>
|
||||
<attr name="centerRegion" format="color"/>
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="PasteSensitiveTextInputEditText">
|
||||
<attr name="allowFormatting" format="boolean"/>
|
||||
</declare-styleable>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue