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.dialer.app.calllog;
18
19import android.app.FragmentManager;
20import android.content.ContentValues;
21import android.content.Context;
22import android.net.Uri;
23import android.support.annotation.NonNull;
24import android.support.v7.widget.RecyclerView;
25import com.android.dialer.blocking.BlockReportSpamDialogs;
26import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
27import com.android.dialer.common.LogUtil;
28import com.android.dialer.logging.ContactSource;
29import com.android.dialer.logging.DialerImpression;
30import com.android.dialer.logging.Logger;
31import com.android.dialer.logging.ReportingLocation;
32import com.android.dialer.spam.Spam;
33
34/** Listener to show dialogs for block and report spam actions. */
35public class BlockReportSpamListener implements CallLogListItemViewHolder.OnClickListener {
36
37  private final Context mContext;
38  private final FragmentManager mFragmentManager;
39  private final RecyclerView.Adapter mAdapter;
40  private final FilteredNumberAsyncQueryHandler mFilteredNumberAsyncQueryHandler;
41
42  public BlockReportSpamListener(
43      Context context,
44      FragmentManager fragmentManager,
45      RecyclerView.Adapter adapter,
46      FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler) {
47    mContext = context;
48    mFragmentManager = fragmentManager;
49    mAdapter = adapter;
50    mFilteredNumberAsyncQueryHandler = filteredNumberAsyncQueryHandler;
51  }
52
53  @Override
54  public void onBlockReportSpam(
55      String displayNumber,
56      final String number,
57      final String countryIso,
58      final int callType,
59      @NonNull final ContactSource.Type contactSourceType) {
60    BlockReportSpamDialogs.BlockReportSpamDialogFragment.newInstance(
61            displayNumber,
62            Spam.get(mContext).isDialogReportSpamCheckedByDefault(),
63            new BlockReportSpamDialogs.OnSpamDialogClickListener() {
64              @Override
65              public void onClick(boolean isSpamChecked) {
66                LogUtil.i("BlockReportSpamListener.onBlockReportSpam", "onClick");
67                if (isSpamChecked && Spam.get(mContext).isSpamEnabled()) {
68                  Logger.get(mContext)
69                      .logImpression(
70                          DialerImpression.Type
71                              .REPORT_CALL_AS_SPAM_VIA_CALL_LOG_BLOCK_REPORT_SPAM_SENT_VIA_BLOCK_NUMBER_DIALOG);
72                  Spam.get(mContext)
73                      .reportSpamFromCallHistory(
74                          number,
75                          countryIso,
76                          callType,
77                          ReportingLocation.Type.CALL_LOG_HISTORY,
78                          contactSourceType);
79                }
80                mFilteredNumberAsyncQueryHandler.blockNumber(
81                    new FilteredNumberAsyncQueryHandler.OnBlockNumberListener() {
82                      @Override
83                      public void onBlockComplete(Uri uri) {
84                        Logger.get(mContext)
85                            .logImpression(DialerImpression.Type.USER_ACTION_BLOCKED_NUMBER);
86                        mAdapter.notifyDataSetChanged();
87                      }
88                    },
89                    number,
90                    countryIso);
91              }
92            },
93            null)
94        .show(mFragmentManager, BlockReportSpamDialogs.BLOCK_REPORT_SPAM_DIALOG_TAG);
95  }
96
97  @Override
98  public void onBlock(
99      String displayNumber,
100      final String number,
101      final String countryIso,
102      final int callType,
103      @NonNull final ContactSource.Type contactSourceType) {
104    BlockReportSpamDialogs.BlockDialogFragment.newInstance(
105            displayNumber,
106            Spam.get(mContext).isSpamEnabled(),
107            new BlockReportSpamDialogs.OnConfirmListener() {
108              @Override
109              public void onClick() {
110                LogUtil.i("BlockReportSpamListener.onBlock", "onClick");
111                if (Spam.get(mContext).isSpamEnabled()) {
112                  Logger.get(mContext)
113                      .logImpression(
114                          DialerImpression.Type
115                              .DIALOG_ACTION_CONFIRM_NUMBER_SPAM_INDIRECTLY_VIA_BLOCK_NUMBER);
116                  Spam.get(mContext)
117                      .reportSpamFromCallHistory(
118                          number,
119                          countryIso,
120                          callType,
121                          ReportingLocation.Type.CALL_LOG_HISTORY,
122                          contactSourceType);
123                }
124                mFilteredNumberAsyncQueryHandler.blockNumber(
125                    new FilteredNumberAsyncQueryHandler.OnBlockNumberListener() {
126                      @Override
127                      public void onBlockComplete(Uri uri) {
128                        Logger.get(mContext)
129                            .logImpression(DialerImpression.Type.USER_ACTION_BLOCKED_NUMBER);
130                        mAdapter.notifyDataSetChanged();
131                      }
132                    },
133                    number,
134                    countryIso);
135              }
136            },
137            null)
138        .show(mFragmentManager, BlockReportSpamDialogs.BLOCK_DIALOG_TAG);
139  }
140
141  @Override
142  public void onUnblock(
143      String displayNumber,
144      final String number,
145      final String countryIso,
146      final int callType,
147      final ContactSource.Type contactSourceType,
148      final boolean isSpam,
149      final Integer blockId) {
150    BlockReportSpamDialogs.UnblockDialogFragment.newInstance(
151            displayNumber,
152            isSpam,
153            new BlockReportSpamDialogs.OnConfirmListener() {
154              @Override
155              public void onClick() {
156                LogUtil.i("BlockReportSpamListener.onUnblock", "onClick");
157                if (isSpam && Spam.get(mContext).isSpamEnabled()) {
158                  Logger.get(mContext)
159                      .logImpression(DialerImpression.Type.REPORT_AS_NOT_SPAM_VIA_UNBLOCK_NUMBER);
160                  Spam.get(mContext)
161                      .reportNotSpamFromCallHistory(
162                          number,
163                          countryIso,
164                          callType,
165                          ReportingLocation.Type.CALL_LOG_HISTORY,
166                          contactSourceType);
167                }
168                mFilteredNumberAsyncQueryHandler.unblock(
169                    new FilteredNumberAsyncQueryHandler.OnUnblockNumberListener() {
170                      @Override
171                      public void onUnblockComplete(int rows, ContentValues values) {
172                        Logger.get(mContext)
173                            .logImpression(DialerImpression.Type.USER_ACTION_UNBLOCKED_NUMBER);
174                        mAdapter.notifyDataSetChanged();
175                      }
176                    },
177                    blockId);
178              }
179            },
180            null)
181        .show(mFragmentManager, BlockReportSpamDialogs.UNBLOCK_DIALOG_TAG);
182  }
183
184  @Override
185  public void onReportNotSpam(
186      String displayNumber,
187      final String number,
188      final String countryIso,
189      final int callType,
190      final ContactSource.Type contactSourceType) {
191    BlockReportSpamDialogs.ReportNotSpamDialogFragment.newInstance(
192            displayNumber,
193            new BlockReportSpamDialogs.OnConfirmListener() {
194              @Override
195              public void onClick() {
196                LogUtil.i("BlockReportSpamListener.onReportNotSpam", "onClick");
197                if (Spam.get(mContext).isSpamEnabled()) {
198                  Logger.get(mContext)
199                      .logImpression(DialerImpression.Type.DIALOG_ACTION_CONFIRM_NUMBER_NOT_SPAM);
200                  Spam.get(mContext)
201                      .reportNotSpamFromCallHistory(
202                          number,
203                          countryIso,
204                          callType,
205                          ReportingLocation.Type.CALL_LOG_HISTORY,
206                          contactSourceType);
207                }
208                mAdapter.notifyDataSetChanged();
209              }
210            },
211            null)
212        .show(mFragmentManager, BlockReportSpamDialogs.NOT_SPAM_DIALOG_TAG);
213  }
214}
215