use of android.text.style.URLSpan in project XobotOS by xamarin.
the class TextView method onTextContextMenuItem.
/**
* Called when a context menu option for the text view is selected. Currently
* this will be {@link android.R.id#copyUrl}, {@link android.R.id#selectTextMode},
* {@link android.R.id#selectAll}, {@link android.R.id#paste}, {@link android.R.id#cut}
* or {@link android.R.id#copy}.
*
* @return true if the context menu item action was performed.
*/
public boolean onTextContextMenuItem(int id) {
int min = 0;
int max = mText.length();
if (isFocused()) {
final int selStart = getSelectionStart();
final int selEnd = getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
switch(id) {
case ID_COPY_URL:
URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class);
if (urls.length >= 1) {
ClipData clip = null;
for (int i = 0; i < urls.length; i++) {
Uri uri = Uri.parse(urls[0].getURL());
if (clip == null) {
clip = ClipData.newRawUri(null, uri);
} else {
clip.addItem(new ClipData.Item(uri));
}
}
if (clip != null) {
setPrimaryClip(clip);
}
}
stopSelectionActionMode();
return true;
case ID_SELECTION_MODE:
if (mSelectionActionMode != null) {
// Selection mode is already started, simply change selected part.
selectCurrentWord();
} else {
startSelectionActionMode();
}
return true;
case ID_SELECT_ALL:
// This does not enter text selection mode. Text is highlighted, so that it can be
// bulk edited, like selectAllOnFocus does. Returns true even if text is empty.
selectAll();
return true;
case ID_PASTE:
paste(min, max);
return true;
case ID_CUT:
setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
((Editable) mText).delete(min, max);
stopSelectionActionMode();
return true;
case ID_COPY:
setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
stopSelectionActionMode();
return true;
}
return false;
}
use of android.text.style.URLSpan in project WordPress-Android by wordpress-mobile.
the class LegacyEditorFragment method createLinkFromSelection.
private void createLinkFromSelection(String linkURL, String linkText) {
try {
if (linkURL != null && !linkURL.equals("http://") && !linkURL.equals("")) {
if (mSelectionStart > mSelectionEnd) {
int temp = mSelectionEnd;
mSelectionEnd = mSelectionStart;
mSelectionStart = temp;
}
Editable editable = mContentEditText.getText();
if (editable == null) {
return;
}
if (mIsLocalDraft) {
if (linkText == null) {
if (mSelectionStart < mSelectionEnd) {
editable.delete(mSelectionStart, mSelectionEnd);
}
editable.insert(mSelectionStart, linkURL);
editable.setSpan(new URLSpan(linkURL), mSelectionStart, mSelectionStart + linkURL.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mContentEditText.setSelection(mSelectionStart + linkURL.length());
} else {
if (mSelectionStart < mSelectionEnd) {
editable.delete(mSelectionStart, mSelectionEnd);
}
editable.insert(mSelectionStart, linkText);
editable.setSpan(new URLSpan(linkURL), mSelectionStart, mSelectionStart + linkText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mContentEditText.setSelection(mSelectionStart + linkText.length());
}
} else {
if (linkText == null) {
if (mSelectionStart < mSelectionEnd) {
editable.delete(mSelectionStart, mSelectionEnd);
}
String urlHTML = "<a href=\"" + linkURL + "\">" + linkURL + "</a>";
editable.insert(mSelectionStart, urlHTML);
mContentEditText.setSelection(mSelectionStart + urlHTML.length());
} else {
if (mSelectionStart < mSelectionEnd) {
editable.delete(mSelectionStart, mSelectionEnd);
}
String urlHTML = "<a href=\"" + linkURL + "\">" + linkText + "</a>";
editable.insert(mSelectionStart, urlHTML);
mContentEditText.setSelection(mSelectionStart + urlHTML.length());
}
}
}
} catch (RuntimeException e) {
AppLog.e(T.POSTS, e);
}
}
use of android.text.style.URLSpan in project WordPress-Android by wordpress-mobile.
the class HtmlToSpannedConverter method endA.
private static void endA(SpannableStringBuilder text) {
int len = text.length();
Object obj = getLast(text, Href.class);
int where = text.getSpanStart(obj);
text.removeSpan(obj);
if (where != len) {
Href h = (Href) obj;
if (h != null) {
if (h.mHref != null) {
text.setSpan(new URLSpan(h.mHref), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
use of android.text.style.URLSpan in project WordPress-Android by wordpress-mobile.
the class StatsWPLinkMovementMethod method onTouchEvent.
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
if (layout == null) {
return super.onTouchEvent(widget, buffer, event);
}
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length != 0) {
String url = link[0].getURL();
AppLog.d(AppLog.T.UTILS, "Tapped on the Link: " + url);
if (url.startsWith("https://wordpress.com/my-stats") || url.startsWith("http://wordpress.com/my-stats")) {
// make sure to load the no-chrome version of Stats over https
url = UrlUtils.makeHttps(url);
if (url.contains("?")) {
// add the no chrome parameters if not available
if (!url.contains("?no-chrome") && !url.contains("&no-chrome")) {
url += "&no-chrome";
}
} else {
url += "?no-chrome";
}
AppLog.d(AppLog.T.UTILS, "Opening the Authenticated in-app browser : " + url);
// Let's try the global wpcom credentials
WPWebViewActivity.openUrlByUsingGlobalWPCOMCredentials(widget.getContext(), url);
return true;
} else if (url.startsWith("https") || url.startsWith("http")) {
AppLog.d(AppLog.T.UTILS, "Opening the in-app browser: " + url);
WPWebViewActivity.openURL(widget.getContext(), url);
return true;
}
}
}
return super.onTouchEvent(widget, buffer, event);
}
use of android.text.style.URLSpan in project android_frameworks_base by AOSPA.
the class LinkSpec method applyLink.
private static final void applyLink(String url, int start, int end, Spannable text) {
URLSpan span = new URLSpan(url);
text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Aggregations