summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/activity/fragment/BlockContactDialogFragment.java
blob: b07d914ddb5c52a11d1bcc400c3b1bebeb1deba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.android.contacts.common.activity.fragment;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import com.android.contacts.common.R;

/**
 * Interacts with the framework implementation of Blacklist and any phonenumber Lookup Providers
 * interested in spam collection
 *
 * NOTE: ensure you have Blacklist permissions before using this class
 */
public class BlockContactDialogFragment extends DialogFragment
        implements DialogInterface.OnClickListener {

    public static final int BLOCK_MODE = 0;
    public static final int UNBLOCK_MODE = 1;
    public static final String KEY_CURRENT_LOOKUP_PROVIDER_NAME = "CURRENT_LOOKUP_PROVIDER_NAME";
    public static final String KEY_LAUNCH_MODE = "LAUNCH_MODE";

    private int mLaunchMode;
    private CheckBox mNotifyProviderCheckBox;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String lookupProviderName = null;
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            mLaunchMode = bundle.getInt(KEY_LAUNCH_MODE);
            lookupProviderName = bundle.getString(KEY_CURRENT_LOOKUP_PROVIDER_NAME);
        }

        Activity hostActivity = getActivity();
        boolean blockMode = mLaunchMode == BLOCK_MODE;
        String dialogTitle;
        String positiveButtonText;

        AlertDialog.Builder builder = new AlertDialog.Builder(hostActivity);
        LayoutInflater inflater = (LayoutInflater) hostActivity.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.block_contact_dialog_view, null);
        mNotifyProviderCheckBox = (CheckBox) v.findViewById(R.id.spamCheckbox);

        if (blockMode) {
            dialogTitle = hostActivity.getString(R.string.block_dialog_title);
            positiveButtonText = hostActivity.getString(R.string.block_dialog_positive);
        } else {
            // unblock mode
            dialogTitle = hostActivity.getString(R.string.unblock_dialog_title);
            positiveButtonText = hostActivity.getString(R.string.unblock_dialog_positive);
            v.findViewById(R.id.description).setVisibility(View.GONE);
        }

        String checkboxDescription = TextUtils.isEmpty(lookupProviderName) ?
                null :
                hostActivity.getResources().getString(
                        blockMode ? R.string.block_dialog_report_spam :
                                R.string.block_dialog_report_nonspam,
                        lookupProviderName);

        if (TextUtils.isEmpty(checkboxDescription)) {
            mNotifyProviderCheckBox.setChecked(false);
            mNotifyProviderCheckBox.setVisibility(View.GONE);
        } else {
            mNotifyProviderCheckBox.setText(checkboxDescription);
            mNotifyProviderCheckBox.setVisibility(View.VISIBLE);
        }

        builder.setTitle(dialogTitle)
                .setView(v)
                .setPositiveButton(positiveButtonText, this)
                .setNegativeButton(R.string.block_dialog_negative, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        return builder.create();
    }

    @Override
    public void onCancel(DialogInterface dialog) {}

    @Override
    public void onClick(DialogInterface dialog, int which) {
        boolean mCheckboxStatus = mNotifyProviderCheckBox.isChecked();
        Activity parentActivity = getActivity();
        if (parentActivity instanceof BlockContactCallbacks) {
            if (mLaunchMode == BLOCK_MODE) {
                ((BlockContactCallbacks) parentActivity).onBlockContact(mCheckboxStatus);
            } else {
                ((BlockContactCallbacks) parentActivity).onUnblockContact(mCheckboxStatus);
            }
        }
    }

    public interface BlockContactCallbacks {
        /**
         * Callback noting that the user opted to block the contact
         *
         * @param notifyLookupProvider indicates whether the user opted to report the contact
         *                             to the current LookupProvider
         */
        void onBlockContact(boolean notifyLookupProvider);

        /**
         * Callback noting that the user opted to unblock the contact
         *
         * @param notifyLookupProvider indicates whether the user opted to notify the current
         *                             LookupProvider of the unblock
         */
        void onUnblockContact(boolean notifyLookupProvider);
    }
}