summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/simulator/impl/SimulatorSpamCallCreator.java
blob: ae97bc16282a3d8823c17f871aaa19f916c51936 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.dialer.simulator.impl;

import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.OperationApplicationException;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.CallLog;
import android.support.annotation.NonNull;
import android.telecom.Connection;
import android.telecom.DisconnectCause;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.ThreadUtil;
import com.android.dialer.spam.Spam;
import com.android.dialer.spam.SpamBindingsStub;
import java.util.ArrayList;

/**
 * Creates many spam call notifications by adding new incoming calls one at a time and disconnecting
 * them.
 */
final class SimulatorSpamCallCreator implements SimulatorConnectionService.Listener {
  private static final String EXTRA_CALL_COUNT = "call_count";
  private static final String EXTRA_IS_SPAM_CALL_CONNECTION = "is_spam_call_connection";
  private static final int DISCONNECT_DELAY_MILLIS = 1000;

  private final Context context;
  private final boolean isSpam;

  SimulatorSpamCallCreator(@NonNull Context context, boolean isSpam) {
    this.context = Assert.isNotNull(context);
    this.isSpam = isSpam;
  }

  public void start(int callCount) {
    Spam.setForTesting(new SimulatorSpamBindings(isSpam));
    SimulatorConnectionService.addListener(this);
    addNextIncomingCall(callCount);
  }

  @Override
  public void onNewOutgoingConnection(@NonNull SimulatorConnection connection) {}

  @Override
  public void onNewIncomingConnection(@NonNull SimulatorConnection connection) {
    if (!isSpamCallConnection(connection)) {
      return;
    }
    ThreadUtil.postDelayedOnUiThread(
        () -> {
          LogUtil.i("SimulatorSpamCallCreator.onNewIncomingConnection", "disconnecting");
          connection.setActive();
          connection.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
          ThreadUtil.postDelayedOnUiThread(
              () -> addNextIncomingCall(getCallCount(connection)), DISCONNECT_DELAY_MILLIS);
        },
        DISCONNECT_DELAY_MILLIS);
  }

  private void addNextIncomingCall(int callCount) {
    if (callCount <= 0) {
      LogUtil.i("SimulatorSpamCallCreator.addNextIncomingCall", "done adding calls");
      SimulatorConnectionService.removeListener(this);
      Spam.setForTesting(null);
      return;
    }

    // Caller ID must be e.164 formatted and unique.
    String callerId = String.format("+1-650-234%04d", callCount);
    Bundle extras = new Bundle();
    extras.putInt(EXTRA_CALL_COUNT, callCount - 1);
    extras.putBoolean(EXTRA_IS_SPAM_CALL_CONNECTION, true);

    // We need to clear the call log because spam notifications are only shown for new calls.
    clearCallLog(context);

    SimulatorConnectionService.addNewIncomingCall(context, extras, callerId);
  }

  private static boolean isSpamCallConnection(@NonNull Connection connection) {
    return connection.getExtras().getBoolean(EXTRA_IS_SPAM_CALL_CONNECTION);
  }

  private static int getCallCount(@NonNull Connection connection) {
    return connection.getExtras().getInt(EXTRA_CALL_COUNT);
  }

  private static void clearCallLog(@NonNull Context context) {
    try {
      ArrayList<ContentProviderOperation> operations = new ArrayList<>();
      operations.add(ContentProviderOperation.newDelete(CallLog.Calls.CONTENT_URI).build());
      context.getContentResolver().applyBatch(CallLog.AUTHORITY, operations);
    } catch (RemoteException | OperationApplicationException e) {
      Assert.fail("failed to clear call log: " + e);
    }
  }

  /**
   * Custom spam bindings that allow us to override which phone numbers are considered to be spam.
   * Also disables throttling of spam notifications.
   */
  private static class SimulatorSpamBindings extends SpamBindingsStub {
    private final boolean isSpam;

    SimulatorSpamBindings(boolean isSpam) {
      this.isSpam = isSpam;
    }

    @Override
    public boolean isSpamEnabled() {
      return true;
    }

    @Override
    public boolean isSpamNotificationEnabled() {
      return true;
    }

    @Override
    public int percentOfSpamNotificationsToShow() {
      return 100;
    }

    @Override
    public int percentOfNonSpamNotificationsToShow() {
      return 100;
    }

    @Override
    public void checkSpamStatus(String number, String countryIso, Listener listener) {
      listener.onComplete(isSpam);
    }
  }
}