Example usage for android.text Spanned getSpans

List of usage examples for android.text Spanned getSpans

Introduction

In this page you can find the example usage for android.text Spanned getSpans.

Prototype

public <T> T[] getSpans(int start, int end, Class<T> type);

Source Link

Document

Return an array of the markup objects attached to the specified slice of this CharSequence and whose type is the specified type or a subclass of it.

Usage

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private int findMatchingPanPosition(Content oldContent, Content newContent,
        TranslationResult oldTranslationResult, TranslationResult newTranslationResult,
        int oldDisplayPosition) {
    Spanned oldSpanned = oldContent.getSpanned();
    Spanned newSpanned = newContent.getSpanned();
    if (oldSpanned == null || newSpanned == null) {
        return -1;
    }// w  ww . j  av  a  2  s. co  m
    // Map the current display start and past-the-end positions
    // to the corresponding input positions.
    int oldTextStart = displayToTextPosition(oldTranslationResult, -1 /*cursorPosition*/, oldDisplayPosition);
    int oldTextEnd = displayToTextPosition(oldTranslationResult, -1 /*cursorPosition*/,
            oldDisplayPosition + getNumTextCells());
    // Find the nodes that overlap with the display.
    AccessibilityNodeInfoCompat[] displayedNodes = oldSpanned.getSpans(oldTextStart, oldTextEnd,
            AccessibilityNodeInfoCompat.class);
    Arrays.sort(displayedNodes, new ByDistanceComparator(oldSpanned, oldTextStart));
    // Find corresponding node in new content.
    for (AccessibilityNodeInfoCompat oldNode : displayedNodes) {
        AccessibilityNodeInfoCompat newNode = (AccessibilityNodeInfoCompat) DisplaySpans
                .getEqualSpan(newSpanned, oldNode);
        if (newNode == null) {
            continue;
        }
        int oldDisplayStart = textToDisplayPosition(oldTranslationResult, -1 /*cursorPosition*/,
                oldSpanned.getSpanStart(oldNode));
        int newDisplayStart = textToDisplayPosition(newTranslationResult, -1 /*cursorPosition*/,
                newSpanned.getSpanStart(newNode));
        // Offset position according to diff in node position.
        int newDisplayPosition = oldDisplayPosition + (newDisplayStart - oldDisplayStart);
        return newDisplayPosition;
    }
    return -1;
}

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;//from  ww  w. j av  a2 s.  c o 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 w w.j  av a 2  s  .co 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();
}