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 static java.util.Objects.requireNonNull;
19 
20 import android.content.Intent;
21 import android.text.TextPaint;
22 import android.text.style.ClickableSpan;
23 import android.view.SoundEffectConstants;
24 import android.view.View;
25 
26 import androidx.annotation.NonNull;
27 
28 import java.util.function.Consumer;
29 
30 /** Used to standardize the way we set up clickable spanned elements */
31 public class ClickableSpanFactory {
32     private final int mLinkColor;
33     private final Consumer<Intent> mClickHandler;
34 
35     /**
36      * @param linkColor color value (i.e. not resource id)
37      * @param clickHandler callback invoked when a link is tapped
38      */
ClickableSpanFactory(int linkColor, Consumer<Intent> clickHandler)39     public ClickableSpanFactory(int linkColor, Consumer<Intent> clickHandler) {
40         mLinkColor = linkColor;
41         mClickHandler = requireNonNull(clickHandler);
42     }
43 
44     /**
45      * @param intent to start on click
46      */
create(@onNull Intent intent)47     public @NonNull ClickableSpan create(@NonNull Intent intent) {
48         return new ClickableSpan() {
49             @Override
50             public void onClick(View widget) {
51                 widget.playSoundEffect(SoundEffectConstants.CLICK);
52                 mClickHandler.accept(intent);
53             }
54 
55             @Override
56             public void updateDrawState(TextPaint ds) {
57                 super.updateDrawState(ds);
58                 ds.setUnderlineText(false);
59                 ds.setColor(mLinkColor);
60             }
61         };
62     }
63 }