• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.blockreportspam;
18 
19 import android.app.FragmentManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.widget.Toast;
25 import com.android.dialer.blockreportspam.BlockReportSpamDialogs.OnConfirmListener;
26 import com.android.dialer.blockreportspam.BlockReportSpamDialogs.OnSpamDialogClickListener;
27 import com.android.dialer.common.Assert;
28 import com.android.dialer.common.LogUtil;
29 import com.android.dialer.logging.DialerImpression;
30 import com.android.dialer.logging.Logger;
31 import com.android.dialer.protos.ProtoParsers;
32 import com.android.dialer.spam.Spam;
33 import com.android.dialer.spam.SpamComponent;
34 import java.util.Locale;
35 
36 /**
37  * A {@link BroadcastReceiver} that shows an appropriate dialog upon receiving notifications from
38  * {@link ShowBlockReportSpamDialogNotifier}.
39  */
40 public final class ShowBlockReportSpamDialogReceiver extends BroadcastReceiver {
41 
42   static final String ACTION_SHOW_DIALOG_TO_BLOCK_NUMBER_AND_OPTIONALLY_REPORT_SPAM =
43       "show_dialog_to_block_number_and_optionally_report_spam";
44   static final String ACTION_SHOW_DIALOG_TO_REPORT_NOT_SPAM = "show_dialog_to_report_not_spam";
45   static final String EXTRA_DIALOG_INFO = "dialog_info";
46 
47   /** {@link FragmentManager} needed to show a {@link android.app.DialogFragment}. */
48   private final FragmentManager fragmentManager;
49 
50   /** Returns an {@link IntentFilter} containing all actions accepted by this broadcast receiver. */
getIntentFilter()51   public static IntentFilter getIntentFilter() {
52     IntentFilter intentFilter = new IntentFilter();
53     intentFilter.addAction(ACTION_SHOW_DIALOG_TO_BLOCK_NUMBER_AND_OPTIONALLY_REPORT_SPAM);
54     intentFilter.addAction(ACTION_SHOW_DIALOG_TO_REPORT_NOT_SPAM);
55     return intentFilter;
56   }
57 
ShowBlockReportSpamDialogReceiver(FragmentManager fragmentManager)58   public ShowBlockReportSpamDialogReceiver(FragmentManager fragmentManager) {
59     this.fragmentManager = fragmentManager;
60   }
61 
62   @Override
onReceive(Context context, Intent intent)63   public void onReceive(Context context, Intent intent) {
64     LogUtil.enterBlock("ShowBlockReportSpamDialogReceiver.onReceive");
65 
66     String action = intent.getAction();
67 
68     switch (Assert.isNotNull(action)) {
69       case ACTION_SHOW_DIALOG_TO_BLOCK_NUMBER_AND_OPTIONALLY_REPORT_SPAM:
70         showDialogToBlockNumberAndOptionallyReportSpam(context, intent);
71         break;
72       case ACTION_SHOW_DIALOG_TO_REPORT_NOT_SPAM:
73         showDialogToReportNotSpam(context, intent);
74         break;
75       default:
76         throw new IllegalStateException("Unsupported action: " + action);
77     }
78   }
79 
showDialogToBlockNumberAndOptionallyReportSpam(Context context, Intent intent)80   private void showDialogToBlockNumberAndOptionallyReportSpam(Context context, Intent intent) {
81     LogUtil.enterBlock(
82         "ShowBlockReportSpamDialogReceiver.showDialogToBlockNumberAndOptionallyReportSpam");
83 
84     Assert.checkArgument(intent.hasExtra(EXTRA_DIALOG_INFO));
85     BlockReportSpamDialogInfo dialogInfo =
86         ProtoParsers.getTrusted(
87             intent, EXTRA_DIALOG_INFO, BlockReportSpamDialogInfo.getDefaultInstance());
88 
89     Spam spam = SpamComponent.get(context).spam();
90 
91     // Set up the positive listener for the dialog.
92     OnSpamDialogClickListener onSpamDialogClickListener =
93         reportSpam -> {
94           LogUtil.i(
95               "ShowBlockReportSpamDialogReceiver.showDialogToBlockNumberAndOptionallyReportSpam",
96               "confirmed");
97 
98           if (reportSpam && spam.isSpamEnabled()) {
99             LogUtil.i(
100                 "ShowBlockReportSpamDialogReceiver.showDialogToBlockNumberAndOptionallyReportSpam",
101                 "report spam");
102             Logger.get(context)
103                 .logImpression(
104                     DialerImpression.Type
105                         .REPORT_CALL_AS_SPAM_VIA_CALL_LOG_BLOCK_REPORT_SPAM_SENT_VIA_BLOCK_NUMBER_DIALOG);
106             spam.reportSpamFromCallHistory(
107                 dialogInfo.getNormalizedNumber(),
108                 dialogInfo.getCountryIso(),
109                 dialogInfo.getCallType(),
110                 dialogInfo.getReportingLocation(),
111                 dialogInfo.getContactSource());
112           }
113 
114           // TODO(a bug): Block the number.
115           Toast.makeText(
116                   context,
117                   String.format(
118                       Locale.ENGLISH,
119                       "TODO: " + "Block number %s.",
120                       dialogInfo.getNormalizedNumber()),
121                   Toast.LENGTH_SHORT)
122               .show();
123         };
124 
125     // Create and show the dialog.
126     BlockReportSpamDialogs.BlockReportSpamDialogFragment.newInstance(
127             dialogInfo.getNormalizedNumber(),
128             spam.isDialogReportSpamCheckedByDefault(),
129             onSpamDialogClickListener,
130             /* dismissListener = */ null)
131         .show(fragmentManager, BlockReportSpamDialogs.BLOCK_REPORT_SPAM_DIALOG_TAG);
132   }
133 
showDialogToReportNotSpam(Context context, Intent intent)134   private void showDialogToReportNotSpam(Context context, Intent intent) {
135     LogUtil.enterBlock("ShowBlockReportSpamDialogReceiver.showDialogToReportNotSpam");
136 
137     Assert.checkArgument(intent.hasExtra(EXTRA_DIALOG_INFO));
138     BlockReportSpamDialogInfo dialogInfo =
139         ProtoParsers.getTrusted(
140             intent, EXTRA_DIALOG_INFO, BlockReportSpamDialogInfo.getDefaultInstance());
141 
142     // Set up the positive listener for the dialog.
143     OnConfirmListener onConfirmListener =
144         () -> {
145           LogUtil.i("ShowBlockReportSpamDialogReceiver.showDialogToReportNotSpam", "confirmed");
146 
147           Spam spam = SpamComponent.get(context).spam();
148           if (spam.isSpamEnabled()) {
149             Logger.get(context)
150                 .logImpression(DialerImpression.Type.DIALOG_ACTION_CONFIRM_NUMBER_NOT_SPAM);
151             spam.reportNotSpamFromCallHistory(
152                 dialogInfo.getNormalizedNumber(),
153                 dialogInfo.getCountryIso(),
154                 dialogInfo.getCallType(),
155                 dialogInfo.getReportingLocation(),
156                 dialogInfo.getContactSource());
157           }
158         };
159 
160     // Create & show the dialog.
161     BlockReportSpamDialogs.ReportNotSpamDialogFragment.newInstance(
162             dialogInfo.getNormalizedNumber(), onConfirmListener, /* dismissListener = */ null)
163         .show(fragmentManager, BlockReportSpamDialogs.NOT_SPAM_DIALOG_TAG);
164   }
165 }
166