Example usage for java.lang CharSequence subSequence

List of usage examples for java.lang CharSequence subSequence

Introduction

In this page you can find the example usage for java.lang CharSequence subSequence.

Prototype

CharSequence subSequence(int start, int end);

Source Link

Document

Returns a CharSequence that is a subsequence of this sequence.

Usage

From source file:net.bluehack.ui.ChatActivity.java

private void searchLinks(final CharSequence charSequence, final boolean force) {
    if (currentEncryptedChat != null && (MessagesController.getInstance().secretWebpagePreview == 0
            || AndroidUtilities.getPeerLayerVersion(currentEncryptedChat.layer) < 46)) {
        return;/*  ww w. j a v a  2 s.  c o  m*/
    }
    if (force && foundWebPage != null) {
        if (foundWebPage.url != null) {
            int index = TextUtils.indexOf(charSequence, foundWebPage.url);
            char lastChar = 0;
            boolean lenEqual = false;
            if (index == -1) {
                if (foundWebPage.display_url != null) {
                    index = TextUtils.indexOf(charSequence, foundWebPage.display_url);
                    lenEqual = index != -1
                            && index + foundWebPage.display_url.length() == charSequence.length();
                    lastChar = index != -1 && !lenEqual
                            ? charSequence.charAt(index + foundWebPage.display_url.length())
                            : 0;
                }
            } else {
                lenEqual = index + foundWebPage.url.length() == charSequence.length();
                lastChar = !lenEqual ? charSequence.charAt(index + foundWebPage.url.length()) : 0;
            }
            if (index != -1 && (lenEqual || lastChar == ' ' || lastChar == ',' || lastChar == '.'
                    || lastChar == '!' || lastChar == '/')) {
                return;
            }
        }
        pendingLinkSearchString = null;
        showReplyPanel(false, null, null, foundWebPage, false, true);
    }
    Utilities.searchQueue.postRunnable(new Runnable() {
        @Override
        public void run() {
            if (linkSearchRequestId != 0) {
                ConnectionsManager.getInstance().cancelRequest(linkSearchRequestId, true);
                linkSearchRequestId = 0;
            }
            ArrayList<CharSequence> urls = null;
            CharSequence textToCheck;
            try {
                Matcher m = AndroidUtilities.WEB_URL.matcher(charSequence);
                while (m.find()) {
                    if (m.start() > 0) {
                        if (charSequence.charAt(m.start() - 1) == '@') {
                            continue;
                        }
                    }
                    if (urls == null) {
                        urls = new ArrayList<>();
                    }
                    urls.add(charSequence.subSequence(m.start(), m.end()));
                }
                if (urls != null && foundUrls != null && urls.size() == foundUrls.size()) {
                    boolean clear = true;
                    for (int a = 0; a < urls.size(); a++) {
                        if (!TextUtils.equals(urls.get(a), foundUrls.get(a))) {
                            clear = false;
                        }
                    }
                    if (clear) {
                        return;
                    }
                }
                foundUrls = urls;
                if (urls == null) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            if (foundWebPage != null) {
                                showReplyPanel(false, null, null, foundWebPage, false, true);
                                foundWebPage = null;
                            }
                        }
                    });
                    return;
                }
                textToCheck = TextUtils.join(" ", urls);
            } catch (Exception e) {
                FileLog.e("tmessages", e);
                String text = charSequence.toString().toLowerCase();
                if (charSequence.length() < 13 || !text.contains("http://") && !text.contains("https://")) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            if (foundWebPage != null) {
                                showReplyPanel(false, null, null, foundWebPage, false, true);
                                foundWebPage = null;
                            }
                        }
                    });
                    return;
                }
                textToCheck = charSequence;
            }

            if (currentEncryptedChat != null && MessagesController.getInstance().secretWebpagePreview == 2) {
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                        builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                        builder.setPositiveButton(LocaleController.getString("OK", R.string.OK),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        MessagesController.getInstance().secretWebpagePreview = 1;
                                        ApplicationLoader.applicationContext
                                                .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE)
                                                .edit()
                                                .putInt("secretWebpage2",
                                                        MessagesController.getInstance().secretWebpagePreview)
                                                .commit();
                                        foundUrls = null;
                                        searchLinks(charSequence, force);
                                    }
                                });
                        builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                        builder.setMessage(LocaleController.getString("SecretLinkPreviewAlert",
                                R.string.SecretLinkPreviewAlert));
                        showDialog(builder.create());

                        MessagesController.getInstance().secretWebpagePreview = 0;
                        ApplicationLoader.applicationContext
                                .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE).edit()
                                .putInt("secretWebpage2", MessagesController.getInstance().secretWebpagePreview)
                                .commit();
                    }
                });
                return;
            }

            final TLRPC.TL_messages_getWebPagePreview req = new TLRPC.TL_messages_getWebPagePreview();
            if (textToCheck instanceof String) {
                req.message = (String) textToCheck;
            } else {
                req.message = textToCheck.toString();
            }
            linkSearchRequestId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                @Override
                public void run(final TLObject response, final TLRPC.TL_error error) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            linkSearchRequestId = 0;
                            if (error == null) {
                                if (response instanceof TLRPC.TL_messageMediaWebPage) {
                                    foundWebPage = ((TLRPC.TL_messageMediaWebPage) response).webpage;
                                    if (foundWebPage instanceof TLRPC.TL_webPage
                                            || foundWebPage instanceof TLRPC.TL_webPagePending) {
                                        if (foundWebPage instanceof TLRPC.TL_webPagePending) {
                                            pendingLinkSearchString = req.message;
                                        }
                                        if (currentEncryptedChat != null
                                                && foundWebPage instanceof TLRPC.TL_webPagePending) {
                                            foundWebPage.url = req.message;
                                        }
                                        showReplyPanel(true, null, null, foundWebPage, false, true);
                                    } else {
                                        if (foundWebPage != null) {
                                            showReplyPanel(false, null, null, foundWebPage, false, true);
                                            foundWebPage = null;
                                        }
                                    }
                                } else {
                                    if (foundWebPage != null) {
                                        showReplyPanel(false, null, null, foundWebPage, false, true);
                                        foundWebPage = null;
                                    }
                                }
                            }
                        }
                    });
                }
            });
            ConnectionsManager.getInstance().bindRequestToGuid(linkSearchRequestId, classGuid);
        }
    });
}

From source file:kr.wdream.ui.ChatActivity.java

private void searchLinks(final CharSequence charSequence, final boolean force) {
    if (currentEncryptedChat != null && (MessagesController.getInstance().secretWebpagePreview == 0
            || AndroidUtilities.getPeerLayerVersion(currentEncryptedChat.layer) < 46)) {
        return;/* ww  w .jav a  2s  . c  o m*/
    }
    if (force && foundWebPage != null) {
        if (foundWebPage.url != null) {
            int index = TextUtils.indexOf(charSequence, foundWebPage.url);
            char lastChar = 0;
            boolean lenEqual = false;
            if (index == -1) {
                if (foundWebPage.display_url != null) {
                    index = TextUtils.indexOf(charSequence, foundWebPage.display_url);
                    lenEqual = index != -1
                            && index + foundWebPage.display_url.length() == charSequence.length();
                    lastChar = index != -1 && !lenEqual
                            ? charSequence.charAt(index + foundWebPage.display_url.length())
                            : 0;
                }
            } else {
                lenEqual = index + foundWebPage.url.length() == charSequence.length();
                lastChar = !lenEqual ? charSequence.charAt(index + foundWebPage.url.length()) : 0;
            }
            if (index != -1 && (lenEqual || lastChar == ' ' || lastChar == ',' || lastChar == '.'
                    || lastChar == '!' || lastChar == '/')) {
                return;
            }
        }
        pendingLinkSearchString = null;
        showReplyPanel(false, null, null, foundWebPage, false, true);
    }
    Utilities.searchQueue.postRunnable(new Runnable() {
        @Override
        public void run() {
            if (linkSearchRequestId != 0) {
                ConnectionsManager.getInstance().cancelRequest(linkSearchRequestId, true);
                linkSearchRequestId = 0;
            }
            ArrayList<CharSequence> urls = null;
            CharSequence textToCheck;
            try {
                Matcher m = AndroidUtilities.WEB_URL.matcher(charSequence);
                while (m.find()) {
                    if (m.start() > 0) {
                        if (charSequence.charAt(m.start() - 1) == '@') {
                            continue;
                        }
                    }
                    if (urls == null) {
                        urls = new ArrayList<>();
                    }
                    urls.add(charSequence.subSequence(m.start(), m.end()));
                }
                if (urls != null && foundUrls != null && urls.size() == foundUrls.size()) {
                    boolean clear = true;
                    for (int a = 0; a < urls.size(); a++) {
                        if (!TextUtils.equals(urls.get(a), foundUrls.get(a))) {
                            clear = false;
                        }
                    }
                    if (clear) {
                        return;
                    }
                }
                foundUrls = urls;
                if (urls == null) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            if (foundWebPage != null) {
                                showReplyPanel(false, null, null, foundWebPage, false, true);
                                foundWebPage = null;
                            }
                        }
                    });
                    return;
                }
                textToCheck = TextUtils.join(" ", urls);
            } catch (Exception e) {
                FileLog.e("tmessages", e);
                String text = charSequence.toString().toLowerCase();
                if (charSequence.length() < 13 || !text.contains("http://") && !text.contains("https://")) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            if (foundWebPage != null) {
                                showReplyPanel(false, null, null, foundWebPage, false, true);
                                foundWebPage = null;
                            }
                        }
                    });
                    return;
                }
                textToCheck = charSequence;
            }

            if (currentEncryptedChat != null && MessagesController.getInstance().secretWebpagePreview == 2) {
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                        builder.setTitle(
                                LocaleController.getString("AppName", kr.wdream.storyshop.R.string.AppName));
                        builder.setPositiveButton(
                                LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        MessagesController.getInstance().secretWebpagePreview = 1;
                                        ApplicationLoader.applicationContext
                                                .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE)
                                                .edit()
                                                .putInt("secretWebpage2",
                                                        MessagesController.getInstance().secretWebpagePreview)
                                                .commit();
                                        foundUrls = null;
                                        searchLinks(charSequence, force);
                                    }
                                });
                        builder.setNegativeButton(
                                LocaleController.getString("Cancel", kr.wdream.storyshop.R.string.Cancel),
                                null);
                        builder.setMessage(LocaleController.getString("SecretLinkPreviewAlert",
                                kr.wdream.storyshop.R.string.SecretLinkPreviewAlert));
                        showDialog(builder.create());

                        MessagesController.getInstance().secretWebpagePreview = 0;
                        ApplicationLoader.applicationContext
                                .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE).edit()
                                .putInt("secretWebpage2", MessagesController.getInstance().secretWebpagePreview)
                                .commit();
                    }
                });
                return;
            }

            final TLRPC.TL_messages_getWebPagePreview req = new TLRPC.TL_messages_getWebPagePreview();
            if (textToCheck instanceof String) {
                req.message = (String) textToCheck;
            } else {
                req.message = textToCheck.toString();
            }
            linkSearchRequestId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                @Override
                public void run(final TLObject response, final TLRPC.TL_error error) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            linkSearchRequestId = 0;
                            if (error == null) {
                                if (response instanceof TLRPC.TL_messageMediaWebPage) {
                                    foundWebPage = ((TLRPC.TL_messageMediaWebPage) response).webpage;
                                    if (foundWebPage instanceof TLRPC.TL_webPage
                                            || foundWebPage instanceof TLRPC.TL_webPagePending) {
                                        if (foundWebPage instanceof TLRPC.TL_webPagePending) {
                                            pendingLinkSearchString = req.message;
                                        }
                                        if (currentEncryptedChat != null
                                                && foundWebPage instanceof TLRPC.TL_webPagePending) {
                                            foundWebPage.url = req.message;
                                        }
                                        showReplyPanel(true, null, null, foundWebPage, false, true);
                                    } else {
                                        if (foundWebPage != null) {
                                            showReplyPanel(false, null, null, foundWebPage, false, true);
                                            foundWebPage = null;
                                        }
                                    }
                                } else {
                                    if (foundWebPage != null) {
                                        showReplyPanel(false, null, null, foundWebPage, false, true);
                                        foundWebPage = null;
                                    }
                                }
                            }
                        }
                    });
                }
            });
            ConnectionsManager.getInstance().bindRequestToGuid(linkSearchRequestId, classGuid);
        }
    });
}