1 /*
2  * Copyright (C) 2018 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.dialer.spam.promo;
18 
19 import android.app.Dialog;
20 import android.content.DialogInterface;
21 import android.os.Bundle;
22 import android.support.annotation.Nullable;
23 import android.support.v4.app.DialogFragment;
24 import android.support.v7.app.AlertDialog;
25 
26 /** Dialog for spam blocking on-boarding promotion. */
27 public class SpamBlockingPromoDialogFragment extends DialogFragment {
28 
29   public static final String SPAM_BLOCKING_PROMO_DIALOG_TAG = "SpamBlockingPromoDialog";
30 
31   /** Called when dialog positive button is pressed. */
32   protected OnEnableListener positiveListener;
33 
34   /** Called when the dialog is dismissed. */
35   @Nullable protected DialogInterface.OnDismissListener dismissListener;
36 
newInstance( OnEnableListener positiveListener, @Nullable DialogInterface.OnDismissListener dismissListener)37   public static DialogFragment newInstance(
38       OnEnableListener positiveListener,
39       @Nullable DialogInterface.OnDismissListener dismissListener) {
40     SpamBlockingPromoDialogFragment fragment = new SpamBlockingPromoDialogFragment();
41     fragment.positiveListener = positiveListener;
42     fragment.dismissListener = dismissListener;
43     return fragment;
44   }
45 
46   @Override
onDismiss(DialogInterface dialog)47   public void onDismiss(DialogInterface dialog) {
48     if (dismissListener != null) {
49       dismissListener.onDismiss(dialog);
50     }
51     super.onDismiss(dialog);
52   }
53 
54   @Override
onPause()55   public void onPause() {
56     // The dialog is dismissed onPause, i.e. rotation.
57     dismiss();
58     dismissListener = null;
59     positiveListener = null;
60     super.onPause();
61   }
62 
63   @Override
onCreateDialog(Bundle savedInstanceState)64   public Dialog onCreateDialog(Bundle savedInstanceState) {
65     super.onCreateDialog(savedInstanceState);
66     // Return the newly created dialog
67     return new AlertDialog.Builder(getActivity())
68         .setCancelable(true)
69         .setTitle(R.string.spam_blocking_promo_title)
70         .setMessage(R.string.spam_blocking_promo_text)
71         .setNegativeButton(
72             R.string.spam_blocking_promo_action_dismiss, (dialog, which) -> dismiss())
73         .setPositiveButton(
74             R.string.spam_blocking_promo_action_filter_spam,
75             (dialog, which) -> {
76               dismiss();
77               positiveListener.onClick();
78             })
79         .create();
80   }
81 
82   /** Positive listener for spam blocking promotion dialog. */
83   public interface OnEnableListener {
84     /** Called when user clicks on positive button in the spam blocking promo dialog. */
onClick()85     void onClick();
86   }
87 }
88