List of usage examples for android.text Spanned length
int length();
From source file:com.eleybourn.bookcatalogue.utils.Utils.java
/** * Linkify partial HTML. Linkify methods remove all spans before building links, this * method preserves them./*from ww w . jav a2 s . c o m*/ * * See: http://stackoverflow.com/questions/14538113/using-linkify-addlinks-combine-with-html-fromhtml * * @param html Partial HTML * @param linkifyMask Linkify mask to use in Linkify.addLinks * * @return Spannable with all links */ public static Spannable linkifyHtml(String html, int linkifyMask) { // Get the spannable HTML Spanned text = Html.fromHtml(html); // Save the span details for later restoration URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class); // Build an empty spannable then add the links SpannableString buffer = new SpannableString(text); Linkify.addLinks(buffer, linkifyMask); // Add back the HTML spannables for (URLSpan span : currentSpans) { int end = text.getSpanEnd(span); int start = text.getSpanStart(span); buffer.setSpan(span, start, end, 0); } return buffer; }
From source file:com.hippo.nimingban.ui.ListActivity.java
private Spanned fixURLSpan(Spanned spanned) { Spannable spannable;/*from ww w. j a v a 2 s . co m*/ if (spanned instanceof Spannable) { spannable = (Spannable) spanned; } else { spannable = new SpannableString(spanned); } URLSpan[] urlSpans = spannable.getSpans(0, spanned.length(), URLSpan.class); if (urlSpans == null) { return spanned; } for (URLSpan urlSpan : urlSpans) { String url = urlSpan.getURL(); if (TextUtils.isEmpty(url)) { spannable.removeSpan(urlSpan); } try { new URL(url); } catch (MalformedURLException e) { URL absoluteUrl; // It might be relative path try { // Use absolute url absoluteUrl = new URL(new URL(ACUrl.HOST), url); int start = spannable.getSpanStart(urlSpan); int end = spannable.getSpanEnd(urlSpan); spannable.removeSpan(urlSpan); spannable.setSpan(new URLSpan(absoluteUrl.toString()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (MalformedURLException e1) { // Can't get url spannable.removeSpan(urlSpan); } } } return spannable; }
From source file:com.nttec.everychan.ui.presentation.BoardFragment.java
private void openReferencesList(final String from) { final List<Integer> positions = new ArrayList<>(); int position = -1; for (int i = 0; i < presentationModel.presentationList.size(); ++i) { if (presentationModel.presentationList.get(i).sourceModel.number.equals(from)) { position = i;// w w w. j a v a2s . co m break; } } if (position != -1) { Spanned referencesString = presentationModel.presentationList.get(position).referencesString; if (referencesString == null) { Logger.e(TAG, "null referencesString"); return; } ClickableURLSpan[] spans = referencesString.getSpans(0, referencesString.length(), ClickableURLSpan.class); for (ClickableURLSpan span : spans) { String url = span.getURL(); try { //url , .. ??? PresentationItemModel ( ) UrlPageModel model = UrlHandler.getPageModel(url); for (; position < presentationModel.presentationList.size(); ++position) { if (presentationModel.presentationList.get(position).sourceModel.number .equals(model.postNumber)) { break; } } if (position < presentationModel.presentationList.size()) positions.add(position); } catch (Exception e) { Logger.e(TAG, e); } } } if (positions.size() == 0) { Logger.e(TAG, "no references"); return; } final int bgShadowResource = ThemeUtils.getThemeResId(activity.getTheme(), R.attr.dialogBackgroundShadow); final int bgColor = ThemeUtils.getThemeColor(activity.getTheme(), R.attr.activityRootBackground, Color.BLACK); final View tmpV = new View(activity); final Dialog tmpDlg = new Dialog(activity); tmpDlg.getWindow().setBackgroundDrawableResource(bgShadowResource); tmpDlg.requestWindowFeature(Window.FEATURE_NO_TITLE); tmpDlg.setCanceledOnTouchOutside(true); tmpDlg.setContentView(tmpV); tmpDlg.show(); Runnable next = new Runnable() { @Override public void run() { final int dlgWidth = tmpV.getWidth(); tmpDlg.hide(); tmpDlg.cancel(); ListView dlgList = new ListView(activity); dlgList.setAdapter(new ArrayAdapter<Integer>(activity, 0, positions) { @Override public View getView(int position, View convertView, ViewGroup parent) { try { int adapterPositon = getItem(position); View view = adapter.getView(adapterPositon, convertView, parent, dlgWidth, adapter.getItem(adapterPositon), from); view.setBackgroundColor(bgColor); return view; } catch (Exception e) { Logger.e(TAG, e); Toast.makeText(activity, R.string.error_unknown, Toast.LENGTH_LONG).show(); return new View(activity); } } }); Dialog dialog = new Dialog(activity); dialog.getWindow().setBackgroundDrawableResource(bgShadowResource); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCanceledOnTouchOutside(true); dialog.setContentView(dlgList); dialog.show(); dialogs.add(dialog); } }; if (tmpV.getWidth() != 0) { next.run(); } else { AppearanceUtils.callWhenLoaded(tmpDlg.getWindow().getDecorView(), next); } }
From source file:com.irccloud.android.activity.MainActivity.java
@SuppressLint("NewApi") @SuppressWarnings("deprecation") private void showUserPopup(UsersDataSource.User user, Spanned message) { ArrayList<String> itemList = new ArrayList<String>(); final String[] items; final Spanned text_to_copy = message; selected_user = user;/*from w ww .jav a 2 s .c o m*/ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setInverseBackgroundForced(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB); if (message != null) { if (message.getSpans(0, message.length(), URLSpan.class).length > 0) itemList.add("Copy URL"); itemList.add("Copy Message"); } if (selected_user != null) { itemList.add("Whois"); itemList.add("Send a message"); itemList.add("Mention"); itemList.add("Invite to a channel"); itemList.add("Ignore"); if (buffer.type.equalsIgnoreCase("channel")) { UsersDataSource.User self_user = UsersDataSource.getInstance().getUser(buffer.bid, server.nick); if (self_user != null && self_user.mode != null) { if (self_user.mode.contains(server != null ? server.MODE_OPER : "Y") || self_user.mode.contains(server != null ? server.MODE_OWNER : "q") || self_user.mode.contains(server != null ? server.MODE_ADMIN : "a") || self_user.mode.contains(server != null ? server.MODE_OP : "o")) { if (selected_user.mode.contains(server != null ? server.MODE_OP : "o")) itemList.add("Deop"); else itemList.add("Op"); } if (self_user.mode.contains(server != null ? server.MODE_OPER : "Y") || self_user.mode.contains(server != null ? server.MODE_OWNER : "q") || self_user.mode.contains(server != null ? server.MODE_ADMIN : "a") || self_user.mode.contains(server != null ? server.MODE_OP : "o") || self_user.mode.contains(server != null ? server.MODE_HALFOP : "h")) { itemList.add("Kick"); itemList.add("Ban"); } } } itemList.add("Copy Hostmask"); } items = itemList.toArray(new String[itemList.size()]); if (selected_user != null) if (selected_user.hostmask != null && selected_user.hostmask.length() > 0) builder.setTitle(selected_user.nick + "\n(" + selected_user.hostmask + ")"); else builder.setTitle(selected_user.nick); else builder.setTitle("Message"); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int item) { if (conn == null || buffer == null) return; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setInverseBackgroundForced(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB); View view; final TextView prompt; final EditText input; AlertDialog dialog; if (items[item].equals("Copy Message")) { if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); clipboard.setText(text_to_copy); } else { @SuppressLint("ServiceCast") android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); if (clipboard != null) { android.content.ClipData clip = android.content.ClipData .newPlainText("IRCCloud Message", text_to_copy); clipboard.setPrimaryClip(clip); } else { Toast.makeText(MainActivity.this, "Unable to copy message. Please try again.", Toast.LENGTH_SHORT).show(); return; } } Toast.makeText(MainActivity.this, "Message copied to clipboard", Toast.LENGTH_SHORT).show(); } else if (items[item].equals("Copy Hostmask")) { if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); clipboard.setText(selected_user.nick + "!" + selected_user.hostmask); } else { @SuppressLint("ServiceCast") android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); android.content.ClipData clip = android.content.ClipData.newPlainText("Hostmask", selected_user.nick + "!" + selected_user.hostmask); clipboard.setPrimaryClip(clip); } Toast.makeText(MainActivity.this, "Hostmask copied to clipboard", Toast.LENGTH_SHORT).show(); } else if (items[item].equals("Copy URL") && text_to_copy != null) { final ArrayList<String> urlListItems = new ArrayList<String>(); for (URLSpan o : text_to_copy.getSpans(0, text_to_copy.length(), URLSpan.class)) { String url = o.getURL(); url = url.replace(getResources().getString(R.string.IMAGE_SCHEME) + "://", "http://"); url = url.replace(getResources().getString(R.string.IMAGE_SCHEME_SECURE) + "://", "https://"); if (server != null) { url = url.replace( getResources().getString(R.string.IRCCLOUD_SCHEME) + "://cid/" + server.cid + "/", ((server.ssl > 0) ? "ircs://" : "irc://") + server.hostname + ":" + server.port + "/"); } urlListItems.add(url); } if (urlListItems.size() == 1) { if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); clipboard.setText(urlListItems.get(0)); } else { @SuppressLint("ServiceCast") android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); android.content.ClipData clip = android.content.ClipData .newPlainText(urlListItems.get(0), urlListItems.get(0)); clipboard.setPrimaryClip(clip); } Toast.makeText(MainActivity.this, "URL copied to clipboard", Toast.LENGTH_SHORT).show(); } else { builder = new AlertDialog.Builder(MainActivity.this); builder.setInverseBackgroundForced(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB); builder.setTitle("Choose a URL"); builder.setItems(urlListItems.toArray(new String[urlListItems.size()]), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); clipboard.setText(urlListItems.get(i)); } else { @SuppressLint("ServiceCast") android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService( CLIPBOARD_SERVICE); android.content.ClipData clip = android.content.ClipData .newPlainText(urlListItems.get(i), urlListItems.get(i)); clipboard.setPrimaryClip(clip); } Toast.makeText(MainActivity.this, "URL copied to clipboard", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.setOwnerActivity(MainActivity.this); dialog.getWindow() .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); } } else if (items[item].equals("Whois")) { conn.whois(buffer.cid, selected_user.nick, null); } else if (items[item].equals("Send a message")) { conn.say(buffer.cid, null, "/query " + selected_user.nick); } else if (items[item].equals("Mention")) { if (!getSharedPreferences("prefs", 0).getBoolean("mentionTip", false)) { Toast.makeText(MainActivity.this, "Double-tap a message to quickly reply to the sender", Toast.LENGTH_LONG).show(); SharedPreferences.Editor editor = getSharedPreferences("prefs", 0).edit(); editor.putBoolean("mentionTip", true); editor.commit(); } onUserDoubleClicked(selected_user.nick); } else if (items[item].equals("Invite to a channel")) { view = getDialogTextPrompt(); prompt = (TextView) view.findViewById(R.id.prompt); input = (EditText) view.findViewById(R.id.textInput); input.setText(""); prompt.setText("Invite " + selected_user.nick + " to a channel"); builder.setTitle(server.name + " (" + server.hostname + ":" + (server.port) + ")"); builder.setView(view); builder.setPositiveButton("Invite", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { conn.invite(buffer.cid, input.getText().toString(), selected_user.nick); dialog.dismiss(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.setOwnerActivity(MainActivity.this); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); } else if (items[item].equals("Ignore")) { view = getDialogTextPrompt(); prompt = (TextView) view.findViewById(R.id.prompt); input = (EditText) view.findViewById(R.id.textInput); input.setText("*!" + selected_user.hostmask); prompt.setText("Ignore messages for " + selected_user.nick + " at this hostmask"); builder.setTitle(server.name + " (" + server.hostname + ":" + (server.port) + ")"); builder.setView(view); builder.setPositiveButton("Ignore", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { conn.ignore(buffer.cid, input.getText().toString()); dialog.dismiss(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.setOwnerActivity(MainActivity.this); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); } else if (items[item].equals("Op")) { conn.mode(buffer.cid, buffer.name, "+" + (server != null ? server.MODE_OP : "o") + " " + selected_user.nick); } else if (items[item].equals("Deop")) { conn.mode(buffer.cid, buffer.name, "-" + (server != null ? server.MODE_OP : "o") + " " + selected_user.nick); } else if (items[item].equals("Kick")) { view = getDialogTextPrompt(); prompt = (TextView) view.findViewById(R.id.prompt); input = (EditText) view.findViewById(R.id.textInput); input.setText(""); prompt.setText("Give a reason for kicking"); builder.setTitle(server.name + " (" + server.hostname + ":" + (server.port) + ")"); builder.setView(view); builder.setPositiveButton("Kick", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { conn.kick(buffer.cid, buffer.name, selected_user.nick, input.getText().toString()); dialog.dismiss(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.setOwnerActivity(MainActivity.this); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); } else if (items[item].equals("Ban")) { view = getDialogTextPrompt(); prompt = (TextView) view.findViewById(R.id.prompt); input = (EditText) view.findViewById(R.id.textInput); input.setText("*!" + selected_user.hostmask); prompt.setText("Add a banmask for " + selected_user.nick); builder.setTitle(server.name + " (" + server.hostname + ":" + (server.port) + ")"); builder.setView(view); builder.setPositiveButton("Ban", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { conn.mode(buffer.cid, buffer.name, "+b " + input.getText().toString()); dialog.dismiss(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.setOwnerActivity(MainActivity.this); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); } dialogInterface.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.setOwnerActivity(this); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialogInterface) { MessageViewFragment mvf = (MessageViewFragment) getSupportFragmentManager() .findFragmentById(R.id.messageViewFragment); if (mvf != null) mvf.longPressOverride = false; } }); dialog.show(); }