1/*
2 * Copyright (C) 2016 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
17package com.android.settings.support;
18
19import android.accounts.Account;
20import android.app.Activity;
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
24import android.content.DialogInterface;
25import android.os.Bundle;
26import android.text.Spannable;
27import android.text.TextPaint;
28import android.text.style.URLSpan;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.widget.CheckBox;
32import android.widget.TextView;
33
34import com.android.internal.logging.MetricsLogger;
35import com.android.internal.logging.MetricsProto;
36import com.android.settings.R;
37import com.android.settings.overlay.FeatureFactory;
38import com.android.settings.overlay.SupportFeatureProvider;
39
40/**
41 * {@link DialogFragment} for support disclaimer.
42 */
43public final class SupportDisclaimerDialogFragment extends DialogFragment implements
44        DialogInterface.OnClickListener {
45
46    public static final String TAG = "SupportDisclaimerDialog";
47    private static final String EXTRA_TYPE = "extra_type";
48    private static final String EXTRA_ACCOUNT = "extra_account";
49
50    public static SupportDisclaimerDialogFragment newInstance(Account account,
51            @SupportFeatureProvider.SupportType int type) {
52        final SupportDisclaimerDialogFragment fragment = new SupportDisclaimerDialogFragment();
53        final Bundle bundle = new Bundle(2);
54        bundle.putParcelable(SupportDisclaimerDialogFragment.EXTRA_ACCOUNT, account);
55        bundle.putInt(SupportDisclaimerDialogFragment.EXTRA_TYPE, type);
56        fragment.setArguments(bundle);
57        return fragment;
58    }
59
60    @Override
61    public Dialog onCreateDialog(Bundle savedInstanceState) {
62        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
63                .setTitle(R.string.support_disclaimer_title)
64                .setPositiveButton(android.R.string.ok, this)
65                .setNegativeButton(android.R.string.cancel, this);
66        final View content = LayoutInflater.from(builder.getContext())
67                .inflate(R.layout.support_disclaimer_content, null);
68        final TextView disclaimer = (TextView) content.findViewById(R.id.support_disclaimer_text);
69        final Activity activity = getActivity();
70        final SupportFeatureProvider supportFeatureProvider =
71                FeatureFactory.getFactory(activity).getSupportFeatureProvider(activity);
72        disclaimer.setText(supportFeatureProvider.getDisclaimerStringResId());
73        stripUnderlines((Spannable) disclaimer.getText());
74        return builder
75                .setView(content)
76                .create();
77    }
78
79    @Override
80    public void onClick(DialogInterface dialog, int which) {
81        if (which == Dialog.BUTTON_NEGATIVE) {
82            MetricsLogger.action(getContext(),
83                    MetricsProto.MetricsEvent.ACTION_SUPPORT_DISCLAIMER_CANCEL);
84            return;
85        }
86        final Activity activity = getActivity();
87        final CheckBox doNotShow =
88                (CheckBox) getDialog().findViewById(R.id.support_disclaimer_do_not_show_again);
89        final SupportFeatureProvider supportFeatureProvider =
90                FeatureFactory.getFactory(activity).getSupportFeatureProvider(activity);
91        supportFeatureProvider.setShouldShowDisclaimerDialog(getContext(), !doNotShow.isChecked());
92        final Bundle bundle = getArguments();
93        MetricsLogger.action(activity, MetricsProto.MetricsEvent.ACTION_SUPPORT_DISCLAIMER_OK);
94        supportFeatureProvider.startSupport(getActivity(),
95                (Account) bundle.getParcelable(EXTRA_ACCOUNT), bundle.getInt(EXTRA_TYPE));
96    }
97
98    @Override
99    public void onCancel(DialogInterface dialog) {
100        super.onCancel(dialog);
101        MetricsLogger.action(getContext(),
102                MetricsProto.MetricsEvent.ACTION_SUPPORT_DISCLAIMER_CANCEL);
103    }
104
105    /**
106     * Removes the underlines of {@link android.text.style.URLSpan}s.
107     */
108    private static void stripUnderlines(Spannable input) {
109        final URLSpan[] urls = input.getSpans(0, input.length(), URLSpan.class);
110
111        for (URLSpan span : urls) {
112            final int start = input.getSpanStart(span);
113            final int end = input.getSpanEnd(span);
114            input.removeSpan(span);
115            input.setSpan(new NoUnderlineUrlSpan(span.getURL()), start, end,
116                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
117        }
118    }
119
120    /**
121     * A {@link URLSpan} that doesn't decorate the link with underline.
122     */
123    public static class NoUnderlineUrlSpan extends URLSpan {
124
125        public NoUnderlineUrlSpan(String url) {
126            super(url);
127        }
128
129        @Override
130        public void updateDrawState(TextPaint ds) {
131            super.updateDrawState(ds);
132            ds.setUnderlineText(false);
133        }
134    }
135}
136