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:
dado1111 2021-12-06 19:59:44 +01:00 committed by GitHub
parent ad0aa7d4ea
commit e910b1d14f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 159 additions and 9 deletions

View file

@ -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;
}
}