OSDN Git Service

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