|
| 1 | +package com.firebase.ui.auth.ui.email; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.net.Uri; |
| 5 | +import android.support.annotation.ColorInt; |
| 6 | +import android.support.annotation.StringRes; |
| 7 | +import android.support.customtabs.CustomTabsIntent; |
| 8 | +import android.support.v4.content.ContextCompat; |
| 9 | +import android.text.SpannableStringBuilder; |
| 10 | +import android.text.TextUtils; |
| 11 | +import android.text.method.LinkMovementMethod; |
| 12 | +import android.text.style.ClickableSpan; |
| 13 | +import android.text.style.ForegroundColorSpan; |
| 14 | +import android.util.TypedValue; |
| 15 | +import android.view.View; |
| 16 | +import android.widget.TextView; |
| 17 | + |
| 18 | +import com.firebase.ui.auth.R; |
| 19 | +import com.firebase.ui.auth.ui.FlowParameters; |
| 20 | + |
| 21 | +public class PreambleHandler { |
| 22 | + private static final String TOS_TARGET = "%TOS%"; |
| 23 | + private static final String PP_TARGET = "%PP%"; |
| 24 | + |
| 25 | + private final Context mContext; |
| 26 | + private final FlowParameters mFlowParameters; |
| 27 | + private final ForegroundColorSpan mLinkSpan; |
| 28 | + |
| 29 | + private SpannableStringBuilder mBuilder; |
| 30 | + |
| 31 | + public PreambleHandler(Context context, FlowParameters parameters) { |
| 32 | + mContext = context; |
| 33 | + mFlowParameters = parameters; |
| 34 | + mLinkSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.linkColor)); |
| 35 | + |
| 36 | + setupCreateAccountPreamble(); |
| 37 | + } |
| 38 | + |
| 39 | + public void setPreamble(TextView textView) { |
| 40 | + textView.setMovementMethod(LinkMovementMethod.getInstance()); |
| 41 | + textView.setText(mBuilder); |
| 42 | + } |
| 43 | + |
| 44 | + private void setupCreateAccountPreamble() { |
| 45 | + int preambleType = getPreambleType(); |
| 46 | + if (preambleType == -1) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + String[] preambles = |
| 51 | + mContext.getResources().getStringArray(R.array.create_account_preamble); |
| 52 | + mBuilder = new SpannableStringBuilder(preambles[preambleType]); |
| 53 | + |
| 54 | + replaceTarget(TOS_TARGET, R.string.terms_of_service, mFlowParameters.termsOfServiceUrl); |
| 55 | + replaceTarget(PP_TARGET, R.string.privacy_policy, mFlowParameters.privacyPolicyUrl); |
| 56 | + } |
| 57 | + |
| 58 | + private void replaceTarget(String target, @StringRes int replacementRes, String url) { |
| 59 | + char[] currentPreambleChars = new char[mBuilder.length()]; |
| 60 | + mBuilder.getChars(0, mBuilder.length(), currentPreambleChars, 0); |
| 61 | + String currentPreamble = String.valueOf(currentPreambleChars); |
| 62 | + |
| 63 | + int targetIndex = currentPreamble.indexOf(target); |
| 64 | + if (targetIndex != -1) { |
| 65 | + String replacement = mContext.getString(replacementRes); |
| 66 | + mBuilder.replace(targetIndex, targetIndex + target.length(), replacement); |
| 67 | + |
| 68 | + int end = targetIndex + replacement.length(); |
| 69 | + mBuilder.setSpan(mLinkSpan, targetIndex, end, 0); |
| 70 | + mBuilder.setSpan(new CustomTabsSpan(url), targetIndex, end, 0); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 0 means we have both a TOS and a PP |
| 76 | + * <p>1 means we only have a TOS |
| 77 | + * <p>2 means we only have a PP |
| 78 | + * <p>-1 means we have neither |
| 79 | + */ |
| 80 | + private int getPreambleType() { |
| 81 | + int preambleType; |
| 82 | + |
| 83 | + boolean hasTos = !TextUtils.isEmpty(mFlowParameters.termsOfServiceUrl); |
| 84 | + boolean hasPp = !TextUtils.isEmpty(mFlowParameters.privacyPolicyUrl); |
| 85 | + |
| 86 | + if (hasTos && hasPp) { |
| 87 | + preambleType = 0; |
| 88 | + } else if (hasTos) { |
| 89 | + preambleType = 1; |
| 90 | + } else if (hasPp) { |
| 91 | + preambleType = 2; |
| 92 | + } else { |
| 93 | + preambleType = -1; |
| 94 | + } |
| 95 | + |
| 96 | + return preambleType; |
| 97 | + } |
| 98 | + |
| 99 | + private class CustomTabsSpan extends ClickableSpan { |
| 100 | + private final String mUrl; |
| 101 | + private final CustomTabsIntent mCustomTabsIntent; |
| 102 | + |
| 103 | + public CustomTabsSpan(String url) { |
| 104 | + mUrl = url; |
| 105 | + |
| 106 | + // Getting default color |
| 107 | + TypedValue typedValue = new TypedValue(); |
| 108 | + mContext.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true); |
| 109 | + @ColorInt int color = typedValue.data; |
| 110 | + |
| 111 | + mCustomTabsIntent = new CustomTabsIntent.Builder() |
| 112 | + .setToolbarColor(color) |
| 113 | + .setShowTitle(true) |
| 114 | + .build(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onClick(View widget) { |
| 119 | + mCustomTabsIntent.launchUrl(mContext, Uri.parse(mUrl)); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments