1 /* 2 * Copyright (C) 2023 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 17 package com.android.devicelockcontroller.activities; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.text.Html; 22 import android.text.Spannable; 23 import android.text.SpannableString; 24 import android.text.TextUtils; 25 import android.text.method.LinkMovementMethod; 26 import android.text.style.ClickableSpan; 27 import android.text.style.URLSpan; 28 import android.view.View; 29 import android.widget.TextView; 30 31 import androidx.annotation.NonNull; 32 import androidx.annotation.Nullable; 33 import androidx.recyclerview.widget.RecyclerView; 34 35 import com.android.devicelockcontroller.R; 36 import com.android.devicelockcontroller.util.LogUtil; 37 38 /** 39 * A {@link androidx.recyclerview.widget.RecyclerView.ViewHolder} class which describes the item 40 * views used in the {@link RecyclerView} 41 */ 42 public final class ProvisionInfoViewHolder extends RecyclerView.ViewHolder { 43 44 private static final String TAG = "ProvisionInfoViewHolder"; 45 46 private final TextView mTextView; 47 ProvisionInfoViewHolder(@onNull View itemView)48 public ProvisionInfoViewHolder(@NonNull View itemView) { 49 super(itemView); 50 mTextView = itemView.findViewById(R.id.text_view_item_provision_info); 51 } 52 bind(ProvisionInfo provisionInfo, String providerName, @Nullable String termsAndConditionsUrl)53 void bind(ProvisionInfo provisionInfo, String providerName, 54 @Nullable String termsAndConditionsUrl) { 55 Context context = itemView.getContext(); 56 if (TextUtils.isEmpty(providerName)) { 57 LogUtil.e(TAG, "Device provider name is empty, should not reach here."); 58 return; 59 } 60 61 // The Terms and Conditions URL is known at runtime and required for the string used for the 62 // Device Subsidy program. 63 if (provisionInfo.getTextId() == R.string.restrict_device_if_dont_make_payment) { 64 if (TextUtils.isEmpty(termsAndConditionsUrl)) { 65 LogUtil.e(TAG, "Terms and Conditions URL is empty, should not reach here."); 66 return; 67 } 68 69 SpannableString spannedTextView = new SpannableString(Html.fromHtml( 70 String.format( 71 context.getString(R.string.restrict_device_if_dont_make_payment), 72 providerName, termsAndConditionsUrl), 73 Html.FROM_HTML_MODE_LEGACY)); 74 URLSpan[] spans = spannedTextView.getSpans(0, spannedTextView.length(), 75 URLSpan.class); 76 77 for (URLSpan span : spans) { 78 int start = spannedTextView.getSpanStart(span); 79 int end = spannedTextView.getSpanEnd(span); 80 81 ClickableSpan clickableSpan = new CustomClickableSpan(span.getURL()); 82 spannedTextView.removeSpan(span); 83 spannedTextView.setSpan(clickableSpan, start, end, 84 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 85 } 86 87 mTextView.setText(spannedTextView); 88 mTextView.setMovementMethod(LinkMovementMethod.getInstance()); 89 } else { 90 mTextView.setText(context.getString(provisionInfo.getTextId(), providerName)); 91 } 92 93 mTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(provisionInfo.getDrawableId(), 94 /* top=*/ 0, 95 /* end=*/ 0, 96 /* bottom=*/ 0); 97 } 98 99 private static final class CustomClickableSpan extends ClickableSpan { 100 101 final String mUrl; 102 CustomClickableSpan(String url)103 CustomClickableSpan(String url) { 104 mUrl = url; 105 } 106 107 @Override onClick(@onNull View view)108 public void onClick(@NonNull View view) { 109 Intent webIntent = new Intent(view.getContext(), HelpActivity.class); 110 webIntent.putExtra(HelpActivity.EXTRA_URL_PARAM, mUrl); 111 view.getContext().startActivity(webIntent); 112 } 113 } 114 } 115