Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
* [android] add allow-copy-paste attribute to control whether allow c…
Browse files Browse the repository at this point in the history
…opy and paste text content
  • Loading branch information
misakuo committed Feb 2, 2018
1 parent e33761c commit 8eb4075
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public interface Name {

String STRATEGY = "strategy";

String ALLOW_COPY_PASTE = "allowCopyPaste";



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public abstract class AbstractEditComponent extends WXComponent<WXEditText> {
private TextFormatter mFormatter = null;
private List<TextWatcher> mTextChangedListeners;
private TextWatcher mTextChangedEventDispatcher;
private int mFormatRepeatCount = 0;
private static final int MAX_TEXT_FORMAT_REPEAT = 3;

public AbstractEditComponent(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, boolean isLazy) {
super(instance, dom, parent, isLazy);
Expand Down Expand Up @@ -176,13 +178,17 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mFormatter != null) {
String raw = mFormatter.recover(s.toString());
String result = mFormatter.format(raw);
if (!result.equals(s.toString())) {
// prevent infinite loop caused by bad format and recover regexp
if (!result.equals(s.toString()) && mFormatRepeatCount < MAX_TEXT_FORMAT_REPEAT) {
mFormatRepeatCount = mFormatRepeatCount + 1;
int index = editText.getSelectionStart();
int cursorIndex = mFormatter.format(mFormatter.recover(s.subSequence(0, index).toString())).length();
editText.setText(result);
editText.setSelection(cursorIndex);
return;
}

mFormatRepeatCount = 0;
}

if (mTextChangedListeners != null) {
Expand Down Expand Up @@ -409,6 +415,12 @@ protected boolean setProperty(String key, Object param) {
boolean keepIndex = WXUtils.getBoolean(param, false);
mKeepSelectionIndex = keepIndex;
return true;
case Constants.Name.ALLOW_COPY_PASTE:
boolean allowCopyPaste = WXUtils.getBoolean(param, true);
if (getHostView() != null) {
getHostView().setAllowCopyPaste(allowCopyPaste);
}
return true;
}
return super.setProperty(key, param);
}
Expand Down Expand Up @@ -909,23 +921,33 @@ private TextFormatter(PatternWrapper format, PatternWrapper recover) {
}

private String format(String src) {
if (format != null) {
if (format.global) {
return format.matcher.matcher(src).replaceAll(format.replace);
} else {
return format.matcher.matcher(src).replaceFirst(format.replace);
try {
if (format != null) {
if (format.global) {
return format.matcher.matcher(src).replaceAll(format.replace);
} else {
return format.matcher.matcher(src).replaceFirst(format.replace);
}
}
} catch (Throwable t) {
//maybe IndexOutOfBoundsException caused by illegal replace
WXLogUtils.w("WXInput", "[format] " + t.getMessage());
}
return src;
}

private String recover(String formatted) {
if (recover != null) {
if (recover.global) {
return recover.matcher.matcher(formatted).replaceAll(recover.replace);
} else {
return recover.matcher.matcher(formatted).replaceFirst(recover.replace);
try {
if (recover != null) {
if (recover.global) {
return recover.matcher.matcher(formatted).replaceAll(recover.replace);
} else {
return recover.matcher.matcher(formatted).replaceFirst(recover.replace);
}
}
} catch (Throwable t) {
//same cause as format
WXLogUtils.w("WXInput", "[formatted] " + t.getMessage());
}
return formatted;
}
Expand Down
49 changes: 49 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/ui/view/WXEditText.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import android.content.Context;
import android.os.Build;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.EditText;
Expand All @@ -35,6 +38,7 @@ public class WXEditText extends EditText implements WXGestureObservable {
private WXGesture wxGesture;
private int mLines = 1;
private boolean mAllowDisableMovement = true;
private boolean mAllowCopyPaste = true;

public WXEditText(Context context) {
super(context);
Expand Down Expand Up @@ -96,4 +100,49 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
public void setAllowDisableMovement(boolean allow) {
mAllowDisableMovement = allow;
}

public void setAllowCopyPaste(boolean allow) {
mAllowCopyPaste = allow;
if (allow) {
setLongClickable(true);
setTextIsSelectable(true);
setCustomSelectionActionModeCallback(null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setCustomInsertionActionModeCallback(null);
}
} else {
setLongClickable(false);
setTextIsSelectable(false);
ActionMode.Callback callback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setCustomInsertionActionModeCallback(callback);
}
setCustomSelectionActionModeCallback(callback);
}
}

@Override
public boolean onTextContextMenuItem(int id) {
return !mAllowCopyPaste || super.onTextContextMenuItem(id);
}
}

0 comments on commit 8eb4075

Please sign in to comment.