Home | History | Annotate | Download | only in support
      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         stripUnderlines((Spannable) disclaimer.getText());
     72         return builder
     73                 .setView(content)
     74                 .create();
     75     }
     76 
     77     @Override
     78     public void onClick(DialogInterface dialog, int which) {
     79         if (which == Dialog.BUTTON_NEGATIVE) {
     80             MetricsLogger.action(getContext(),
     81                     MetricsProto.MetricsEvent.ACTION_SUPPORT_DISCLAIMER_CANCEL);
     82             return;
     83         }
     84         final Activity activity = getActivity();
     85         final CheckBox doNotShow =
     86                 (CheckBox) getDialog().findViewById(R.id.support_disclaimer_do_not_show_again);
     87         final SupportFeatureProvider supportFeatureProvider =
     88                 FeatureFactory.getFactory(activity).getSupportFeatureProvider(activity);
     89         supportFeatureProvider.setShouldShowDisclaimerDialog(getContext(), !doNotShow.isChecked());
     90         final Bundle bundle = getArguments();
     91         MetricsLogger.action(activity, MetricsProto.MetricsEvent.ACTION_SUPPORT_DISCLAIMER_OK);
     92         supportFeatureProvider.startSupport(getActivity(),
     93                 (Account) bundle.getParcelable(EXTRA_ACCOUNT), bundle.getInt(EXTRA_TYPE));
     94     }
     95 
     96     @Override
     97     public void onCancel(DialogInterface dialog) {
     98         super.onCancel(dialog);
     99         MetricsLogger.action(getContext(),
    100                 MetricsProto.MetricsEvent.ACTION_SUPPORT_DISCLAIMER_CANCEL);
    101     }
    102 
    103     /**
    104      * Removes the underlines of {@link android.text.style.URLSpan}s.
    105      */
    106     private static void stripUnderlines(Spannable input) {
    107         final URLSpan[] urls = input.getSpans(0, input.length(), URLSpan.class);
    108 
    109         for (URLSpan span : urls) {
    110             final int start = input.getSpanStart(span);
    111             final int end = input.getSpanEnd(span);
    112             input.removeSpan(span);
    113             input.setSpan(new NoUnderlineUrlSpan(span.getURL()), start, end,
    114                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    115         }
    116     }
    117 
    118     /**
    119      * A {@link URLSpan} that doesn't decorate the link with underline.
    120      */
    121     public static class NoUnderlineUrlSpan extends URLSpan {
    122 
    123         public NoUnderlineUrlSpan(String url) {
    124             super(url);
    125         }
    126 
    127         @Override
    128         public void updateDrawState(TextPaint ds) {
    129             super.updateDrawState(ds);
    130             ds.setUnderlineText(false);
    131         }
    132     }
    133 }
    134