Search in sources :

Example 26 with UnderlineSpan

use of android.text.style.UnderlineSpan in project Knife by mthli.

the class KnifeText method containUnderline.

protected boolean containUnderline(int start, int end) {
    if (start > end) {
        return false;
    }
    if (start == end) {
        if (start - 1 < 0 || start + 1 > getEditableText().length()) {
            return false;
        } else {
            UnderlineSpan[] before = getEditableText().getSpans(start - 1, start, UnderlineSpan.class);
            UnderlineSpan[] after = getEditableText().getSpans(start, start + 1, UnderlineSpan.class);
            return before.length > 0 && after.length > 0;
        }
    } else {
        StringBuilder builder = new StringBuilder();
        for (int i = start; i < end; i++) {
            if (getEditableText().getSpans(i, i + 1, UnderlineSpan.class).length > 0) {
                builder.append(getEditableText().subSequence(i, i + 1).toString());
            }
        }
        return getEditableText().subSequence(start, end).toString().equals(builder.toString());
    }
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) UnderlineSpan(android.text.style.UnderlineSpan)

Example 27 with UnderlineSpan

use of android.text.style.UnderlineSpan in project Knife by mthli.

the class KnifeText method underlineInvalid.

protected void underlineInvalid(int start, int end) {
    if (start >= end) {
        return;
    }
    UnderlineSpan[] spans = getEditableText().getSpans(start, end, UnderlineSpan.class);
    List<KnifePart> list = new ArrayList<>();
    for (UnderlineSpan span : spans) {
        list.add(new KnifePart(getEditableText().getSpanStart(span), getEditableText().getSpanEnd(span)));
        getEditableText().removeSpan(span);
    }
    for (KnifePart part : list) {
        if (part.isValid()) {
            if (part.getStart() < start) {
                underlineValid(part.getStart(), start);
            }
            if (part.getEnd() > end) {
                underlineValid(end, part.getEnd());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) UnderlineSpan(android.text.style.UnderlineSpan)

Example 28 with UnderlineSpan

use of android.text.style.UnderlineSpan in project android_frameworks_base by AOSPA.

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    Application application = ActivityThread.currentApplication();
                    sizeDip /= application.getResources().getDisplayMetrics().density;
                }
                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }
        withinStyle(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 29 with UnderlineSpan

use of android.text.style.UnderlineSpan in project NetGuard by M66B.

the class ActivityMain method initAds.

private void initAds() {
    // https://developers.google.com/android/reference/com/google/android/gms/ads/package-summary
    MobileAds.initialize(getApplicationContext(), getString(R.string.ad_app_id));
    final LinearLayout llAd = (LinearLayout) findViewById(R.id.llAd);
    TextView tvAd = (TextView) findViewById(R.id.tvAd);
    final AdView adView = (AdView) findViewById(R.id.adView);
    SpannableString content = new SpannableString(getString(R.string.title_pro_ads));
    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
    tvAd.setText(content);
    tvAd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            startActivity(new Intent(ActivityMain.this, ActivityPro.class));
        }
    });
    adView.setAdListener(new AdListener() {

        @Override
        public void onAdLoaded() {
            Log.i(TAG, "Ad loaded");
            llAd.setVisibility(View.GONE);
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            llAd.setVisibility(View.VISIBLE);
            switch(errorCode) {
                case AdRequest.ERROR_CODE_INTERNAL_ERROR:
                    Log.w(TAG, "Ad load error=INTERNAL_ERROR");
                    break;
                case AdRequest.ERROR_CODE_INVALID_REQUEST:
                    Log.w(TAG, "Ad load error=INVALID_REQUEST");
                    break;
                case AdRequest.ERROR_CODE_NETWORK_ERROR:
                    Log.w(TAG, "Ad load error=NETWORK_ERROR");
                    break;
                case AdRequest.ERROR_CODE_NO_FILL:
                    Log.w(TAG, "Ad load error=NO_FILL");
                    break;
                default:
                    Log.w(TAG, "Ad load error=" + errorCode);
            }
        }

        @Override
        public void onAdOpened() {
            Log.i(TAG, "Ad opened");
        }

        @Override
        public void onAdClosed() {
            Log.i(TAG, "Ad closed");
        }

        @Override
        public void onAdLeftApplication() {
            Log.i(TAG, "Ad left app");
        }
    });
}
Also used : SpannableString(android.text.SpannableString) TextView(android.widget.TextView) Intent(android.content.Intent) AdView(com.google.android.gms.ads.AdView) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) AdView(com.google.android.gms.ads.AdView) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayout(android.widget.LinearLayout) UnderlineSpan(android.text.style.UnderlineSpan) AdListener(com.google.android.gms.ads.AdListener)

Example 30 with UnderlineSpan

use of android.text.style.UnderlineSpan in project NetGuard by M66B.

the class AdapterAccess method bindView.

@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
    // Get values
    final long id = cursor.getLong(colID);
    final int version = cursor.getInt(colVersion);
    final int protocol = cursor.getInt(colProtocol);
    final String daddr = cursor.getString(colDaddr);
    final int dport = cursor.getInt(colDPort);
    long time = cursor.getLong(colTime);
    int allowed = cursor.getInt(colAllowed);
    int block = cursor.getInt(colBlock);
    int count = cursor.getInt(colCount);
    long sent = cursor.isNull(colSent) ? -1 : cursor.getLong(colSent);
    long received = cursor.isNull(colReceived) ? -1 : cursor.getLong(colReceived);
    int connections = cursor.isNull(colConnections) ? -1 : cursor.getInt(colConnections);
    // Get views
    TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
    ImageView ivBlock = (ImageView) view.findViewById(R.id.ivBlock);
    final TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
    LinearLayout llTraffic = (LinearLayout) view.findViewById(R.id.llTraffic);
    TextView tvConnections = (TextView) view.findViewById(R.id.tvConnections);
    TextView tvTraffic = (TextView) view.findViewById(R.id.tvTraffic);
    // Set values
    tvTime.setText(new SimpleDateFormat("dd HH:mm").format(time));
    if (block < 0)
        ivBlock.setImageDrawable(null);
    else {
        ivBlock.setImageResource(block > 0 ? R.drawable.host_blocked : R.drawable.host_allowed);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrap = DrawableCompat.wrap(ivBlock.getDrawable());
            DrawableCompat.setTint(wrap, block > 0 ? colorOff : colorOn);
        }
    }
    String dest = Util.getProtocolName(protocol, version, true) + " " + daddr + (dport > 0 ? "/" + dport : "") + (count > 1 ? " ?" + count : "");
    SpannableString span = new SpannableString(dest);
    span.setSpan(new UnderlineSpan(), 0, dest.length(), 0);
    tvDest.setText(span);
    if (Util.isNumericAddress(daddr))
        new AsyncTask<String, Object, String>() {

            @Override
            protected void onPreExecute() {
                ViewCompat.setHasTransientState(tvDest, true);
            }

            @Override
            protected String doInBackground(String... args) {
                try {
                    return InetAddress.getByName(args[0]).getHostName();
                } catch (UnknownHostException ignored) {
                    return args[0];
                }
            }

            @Override
            protected void onPostExecute(String addr) {
                tvDest.setText(Util.getProtocolName(protocol, version, true) + " >" + addr + (dport > 0 ? "/" + dport : ""));
                ViewCompat.setHasTransientState(tvDest, false);
            }
        }.execute(daddr);
    if (allowed < 0)
        tvDest.setTextColor(colorText);
    else if (allowed > 0)
        tvDest.setTextColor(colorOn);
    else
        tvDest.setTextColor(colorOff);
    llTraffic.setVisibility(connections > 0 || sent > 0 || received > 0 ? View.VISIBLE : View.GONE);
    if (connections > 0)
        tvConnections.setText(context.getString(R.string.msg_count, connections));
    if (sent > 1024 * 1204 * 1024L || received > 1024 * 1024 * 1024L)
        tvTraffic.setText(context.getString(R.string.msg_gb, (sent > 0 ? sent / (1024 * 1024 * 1024f) : 0), (received > 0 ? received / (1024 * 1024 * 1024f) : 0)));
    else if (sent > 1204 * 1024L || received > 1024 * 1024L)
        tvTraffic.setText(context.getString(R.string.msg_mb, (sent > 0 ? sent / (1024 * 1024f) : 0), (received > 0 ? received / (1024 * 1024f) : 0)));
    else
        tvTraffic.setText(context.getString(R.string.msg_kb, (sent > 0 ? sent / 1024f : 0), (received > 0 ? received / 1024f : 0)));
}
Also used : SpannableString(android.text.SpannableString) UnknownHostException(java.net.UnknownHostException) Drawable(android.graphics.drawable.Drawable) AsyncTask(android.os.AsyncTask) TextView(android.widget.TextView) SpannableString(android.text.SpannableString) ImageView(android.widget.ImageView) SimpleDateFormat(java.text.SimpleDateFormat) LinearLayout(android.widget.LinearLayout) UnderlineSpan(android.text.style.UnderlineSpan)

Aggregations

UnderlineSpan (android.text.style.UnderlineSpan)34 StyleSpan (android.text.style.StyleSpan)16 ForegroundColorSpan (android.text.style.ForegroundColorSpan)13 BackgroundColorSpan (android.text.style.BackgroundColorSpan)11 ImageSpan (android.text.style.ImageSpan)10 URLSpan (android.text.style.URLSpan)9 TextView (android.widget.TextView)9 SpannableStringBuilder (android.text.SpannableStringBuilder)8 StrikethroughSpan (android.text.style.StrikethroughSpan)8 SubscriptSpan (android.text.style.SubscriptSpan)8 SuperscriptSpan (android.text.style.SuperscriptSpan)8 TypefaceSpan (android.text.style.TypefaceSpan)8 View (android.view.View)8 SpannableString (android.text.SpannableString)7 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)7 RelativeSizeSpan (android.text.style.RelativeSizeSpan)7 LinearLayout (android.widget.LinearLayout)7 Application (android.app.Application)5 CharacterStyle (android.text.style.CharacterStyle)5 SuggestionSpan (android.text.style.SuggestionSpan)5