|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.firebase.ui.auth.ui; |
| 16 | + |
| 17 | +import android.content.Context; |
| 18 | +import android.net.Uri; |
| 19 | +import android.support.annotation.ColorInt; |
| 20 | +import android.support.annotation.StringRes; |
| 21 | +import android.support.customtabs.CustomTabsIntent; |
| 22 | +import android.support.v4.content.ContextCompat; |
| 23 | +import android.text.SpannableStringBuilder; |
| 24 | +import android.text.style.ForegroundColorSpan; |
| 25 | +import android.util.AttributeSet; |
| 26 | +import android.util.TypedValue; |
| 27 | +import android.view.View; |
| 28 | + |
| 29 | +import com.firebase.ui.auth.R; |
| 30 | + |
| 31 | +/** |
| 32 | + * Text view to display terms of service before completing signup. |
| 33 | + * The view helps display TOS linking to the provided custom URI. |
| 34 | + * It handles the styling of the link and opens the uri in a CustomTabs on click. |
| 35 | + */ |
| 36 | +public class TermsTextView extends android.support.v7.widget.AppCompatTextView { |
| 37 | + public TermsTextView(Context context) { |
| 38 | + super(context); |
| 39 | + } |
| 40 | + |
| 41 | + public TermsTextView(Context context, AttributeSet attrs) { |
| 42 | + super(context, attrs); |
| 43 | + } |
| 44 | + |
| 45 | + public TermsTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 46 | + super(context, attrs, defStyleAttr); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param uri uri to link when the user clicks on Terms of Service |
| 51 | + * @param buttonText for the button that represents the "action" described in the terms. |
| 52 | + */ |
| 53 | + public void showTermsForUri(final Uri uri, @StringRes int buttonText) { |
| 54 | + // Format the terms interpolating the action string |
| 55 | + String buttonTextString = getContext().getString(buttonText); |
| 56 | + String preamble = getContext().getString(R.string.create_account_preamble, |
| 57 | + buttonTextString); |
| 58 | + //Apply the link color on the part of the string that links to the TOS upon clicking |
| 59 | + String link = getContext().getString(R.string.terms_of_service); |
| 60 | + SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(preamble + link); |
| 61 | + int start = preamble.length(); |
| 62 | + ForegroundColorSpan foregroundColorSpan = |
| 63 | + new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.linkColor)); |
| 64 | + spannableStringBuilder.setSpan(foregroundColorSpan, start, start + link.length(), 0); |
| 65 | + setText(spannableStringBuilder); |
| 66 | + |
| 67 | + // Open in custom tabs on click |
| 68 | + setOnClickListener(new View.OnClickListener() { |
| 69 | + @Override |
| 70 | + public void onClick(View view) { |
| 71 | + // Getting default color |
| 72 | + TypedValue typedValue = new TypedValue(); |
| 73 | + getContext().getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true); |
| 74 | + @ColorInt int color = typedValue.data; |
| 75 | + |
| 76 | + new CustomTabsIntent.Builder() |
| 77 | + .setToolbarColor(color) |
| 78 | + .build() |
| 79 | + .launchUrl(getContext(), uri); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | +} |
0 commit comments