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

[WEEX-219][Android] support copy action for text component #1037

Merged
merged 2 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public interface Name {
String STRATEGY = "strategy";

String ALLOW_COPY_PASTE = "allowCopyPaste";
String ENABLE_COPY = "enableCopy";



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,19 @@
*/
package com.taobao.weex.ui.component;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Layout;

import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.WXSDKManager;
import com.taobao.weex.annotation.Component;
import com.taobao.weex.common.Constants;
import com.taobao.weex.dom.DOMActionContext;
import com.taobao.weex.dom.WXDomHandler;
import com.taobao.weex.dom.WXDomObject;
import com.taobao.weex.ui.ComponentCreator;
import com.taobao.weex.ui.flat.FlatComponent;
import com.taobao.weex.ui.flat.widget.TextWidget;
import com.taobao.weex.ui.view.WXTextView;
import com.taobao.weex.utils.TypefaceUtil;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXUtils;

import java.lang.reflect.InvocationTargetException;

Expand Down Expand Up @@ -144,6 +135,12 @@ protected boolean setProperty(String key, Object param) {
return true;
case Constants.Name.FONT_FAMILY:
return true;
case Constants.Name.ENABLE_COPY:
boolean enabled = WXUtils.getBoolean(param, false);
if (getHostView() != null) {
getHostView().enableCopy(enabled);
}
return true;
default:
return super.setProperty(key, param);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
*/
package com.taobao.weex.ui.view;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.support.v7.widget.PopupMenu;
import android.text.Layout;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

Expand Down Expand Up @@ -118,4 +122,42 @@ public void holdComponent(WXText component) {
public WXText getComponent() {
return null != mWeakReference ? mWeakReference.get() : null;
}

public void enableCopy(boolean enable) {
if (enable) {
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
PopupMenu popupMenu = new PopupMenu(getContext(), WXTextView.this);
String s = "Copy";
try {
s = getContext().getResources().getString(android.R.string.copy);
} catch (Throwable t) {
//ignore
}
final String title = s;
popupMenu.getMenu().add(title);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (title.equals(item.getTitle())) {
String data = getText().toString();
ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboardManager != null) {
ClipData clipData = ClipData.newPlainText(data, data);
clipboardManager.setPrimaryClip(clipData);
}
return true;
}
return false;
}
});
popupMenu.show();
return true;
}
});
} else {
setOnLongClickListener(null);
}
}
}