Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright 2017, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.managedprovisioning.common;
     17 
     18 import android.content.Intent;
     19 import android.support.annotation.NonNull;
     20 import android.text.TextPaint;
     21 import android.text.style.ClickableSpan;
     22 import android.view.SoundEffectConstants;
     23 import android.view.View;
     24 
     25 /** Used to standardize the way we set up clickable spanned elements */
     26 public class ClickableSpanFactory {
     27     private final int linkColor;
     28 
     29     /** @param linkColor color value (i.e. not resource id) */
     30     public ClickableSpanFactory(int linkColor) {
     31         this.linkColor = linkColor;
     32     }
     33 
     34     /**
     35      * @param intent to start on click
     36      */
     37     public @NonNull ClickableSpan create(@NonNull Intent intent) {
     38         return new ClickableSpan() {
     39             @Override
     40             public void onClick(View widget) {
     41                 widget.playSoundEffect(SoundEffectConstants.CLICK);
     42                 widget.getContext().startActivity(intent);
     43             }
     44 
     45             @Override
     46             public void updateDrawState(TextPaint ds) {
     47                 super.updateDrawState(ds);
     48                 ds.setUnderlineText(false);
     49                 ds.setColor(linkColor);
     50             }
     51         };
     52     }
     53 }