Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2014 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mail.utils;
     19 
     20 import android.text.Spannable;
     21 import android.text.Spanned;
     22 import android.text.style.URLSpan;
     23 import android.view.View;
     24 import android.widget.TextView;
     25 
     26 import com.android.mail.text.LinkStyleSpan;
     27 
     28 /**
     29  * Utility class for styling UI.
     30  */
     31 public class StyleUtils {
     32     /**
     33      * Removes any {@link android.text.style.URLSpan}s from the text view and replaces them with a
     34      * non-underline version {@link LinkStyleSpan} which calls the supplied listener when clicked.
     35      */
     36     public static void stripUnderlinesAndLinkUrls(TextView textView,
     37             View.OnClickListener onClickListener) {
     38         final Spannable spannable = (Spannable) textView.getText();
     39         stripUnderlinesAndLinkUrls(spannable, onClickListener);
     40     }
     41 
     42     /**
     43      * Removes any {@link android.text.style.URLSpan}s from the Spannable and replaces them with a
     44      * non-underline version {@link LinkStyleSpan} which calls the supplied listener when clicked.
     45      */
     46     public static void stripUnderlinesAndLinkUrls(Spannable input,
     47             View.OnClickListener onClickListener) {
     48         final URLSpan[] urls = input.getSpans(0, input.length(), URLSpan.class);
     49 
     50         for (URLSpan span : urls) {
     51             final int start = input.getSpanStart(span);
     52             final int end = input.getSpanEnd(span);
     53             input.removeSpan(span);
     54             input.setSpan(new LinkStyleSpan(onClickListener), start, end,
     55                     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
     56         }
     57     }
     58 
     59     /**
     60      * Removes any {@link android.text.style.URLSpan}s from the text view and replaces them with a
     61      * non-underline version {@link LinkStyleSpan} that does nothing when clicked.
     62      */
     63     public static void stripUnderlinesAndUrl(TextView textView) {
     64         stripUnderlinesAndLinkUrls(textView, null /* onClickListener */);
     65     }
     66 }
     67