summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/simulator/impl/SimulatorMainPortal.java
blob: 2612757562426c7b5ef375f6efd267da13804fc2 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (C) 2018 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.Context;
import android.support.v7.app.AppCompatActivity;
import android.telecom.TelecomManager;
import android.telecom.VideoProfile;
import android.view.ActionProvider;
import com.android.dialer.common.concurrent.DialerExecutorComponent;
import com.android.dialer.enrichedcall.simulator.EnrichedCallSimulatorActivity;
import com.android.dialer.simulator.Simulator;
import com.android.dialer.simulator.SimulatorComponent;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ListenableFuture;

/** Implements the top level simulator menu. */
public class SimulatorMainPortal {

  private final Context context;
  private final AppCompatActivity activity;
  private SimulatorPortalEntryGroup simulatorPortalEntryGroup;
  private String callerId = "";
  private int presentation = TelecomManager.PRESENTATION_ALLOWED;
  private int missedCallNum = 1;

  public SimulatorMainPortal(AppCompatActivity activity) {
    this.activity = activity;
    this.context = activity.getApplicationContext();
    buildMainPortal();
  }

  public SimulatorMainPortal(Context context) {
    this.activity = null;
    this.context = context;
    buildMainPortal();
  }

  public void setCallerId(String callerId) {
    this.callerId = callerId;
  }

  public void setPresentation(int presentation) {
    this.presentation = presentation;
  }

  public void setMissedCallNum(int missedCallNum) {
    this.missedCallNum = missedCallNum;
  }

  /**
   * Executes commands sent to this portal.
   *
   * @param commands a string array that stores commands that trigger runnable methods stored in the
   *     portal. For example: ["VoiceCall", "Incoming Call"] triggers "() -> new
   *     SimulatorVoiceCall(context).addNewIncomingCall()" runnable method.
   */
  public void execute(String[] commands) {
    @SuppressWarnings("unused")
    ListenableFuture<?> executeCommand =
        DialerExecutorComponent.get(context)
            .backgroundExecutor()
            .submit(() -> execute(simulatorPortalEntryGroup, commands, 0));
  }

  private void execute(
      SimulatorPortalEntryGroup simulatorPortalEntryGroup, String[] commands, int index) {
    if (simulatorPortalEntryGroup.methods().containsKey(commands[index])) {
      simulatorPortalEntryGroup.methods().get(commands[index]).run();
    } else if (simulatorPortalEntryGroup.subPortals().containsKey(commands[index])) {
      execute(simulatorPortalEntryGroup.subPortals().get(commands[index]), commands, index + 1);
    }
  }

  private void buildMainPortal() {
    simulatorPortalEntryGroup =
        SimulatorPortalEntryGroup.builder()
            .setMethods(
                ImmutableMap.<String, Runnable>builder()
                    .put("Populate database", () -> SimulatorUtils.populateDatabase(context))
                    .put("Populate voicemail", () -> SimulatorUtils.populateVoicemail(context))
                    .put(
                        "Fast Populate database",
                        () -> SimulatorUtils.fastPopulateDatabase(context))
                    .put(
                        "Fast populate voicemail database",
                        () -> SimulatorUtils.populateVoicemailFast(context))
                    .put("Clean database", () -> SimulatorUtils.cleanDatabase(context))
                    .put("clear preferred SIM", () -> SimulatorUtils.clearPreferredSim(context))
                    .put("Sync voicemail", () -> SimulatorUtils.syncVoicemail(context))
                    .put("Share persistent log", () -> SimulatorUtils.sharePersistentLog(context))
                    .put(
                        "Enriched call simulator",
                        () ->
                            context.startActivity(EnrichedCallSimulatorActivity.newIntent(context)))
                    .put(
                        "Enable simulator mode",
                        () -> {
                          SimulatorComponent.get(context).getSimulator().enableSimulatorMode();
                          SimulatorSimCallManager.register(context);
                        })
                    .put(
                        "Disable simulator mode",
                        () -> {
                          SimulatorComponent.get(context).getSimulator().disableSimulatorMode();
                          SimulatorSimCallManager.unregister(context);
                        })
                    .build())
            .setSubPortals(
                ImmutableMap.of(
                    "VoiceCall",
                    buildSimulatorVoiceCallPortal(),
                    "VideoCall",
                    buildSimulatorVideoCallPortal(),
                    "RttCall",
                    buildSimulatorRttCallPortal(),
                    "Notifications",
                    buildSimulatorNotificationsPortal()))
            .build();
  }

  public SimulatorPortalEntryGroup buildSimulatorVoiceCallPortal() {
    return SimulatorPortalEntryGroup.builder()
        .setMethods(
            ImmutableMap.<String, Runnable>builder()
                .put("Incoming call", () -> new SimulatorVoiceCall(context).addNewIncomingCall())
                .put("Outgoing call", () -> new SimulatorVoiceCall(context).addNewOutgoingCall())
                .put(
                    "Customized incoming call (Dialog)",
                    () ->
                        new SimulatorVoiceCall(context)
                            .addCustomizedIncomingCallWithDialog(activity))
                .put(
                    "Customized outgoing call (Dialog)",
                    () ->
                        new SimulatorVoiceCall(context)
                            .addCustomizedOutgoingCallWithDialog(activity))
                .put(
                    "Customized incoming call",
                    () ->
                        new SimulatorVoiceCall(context)
                            .addCustomizedIncomingCall(this.callerId, this.presentation))
                .put(
                    "Customized outgoing call",
                    () ->
                        new SimulatorVoiceCall(context)
                            .addCustomizedOutgoingCall(this.callerId, this.presentation))
                .put(
                    "Incoming enriched call",
                    () -> new SimulatorVoiceCall(context).incomingEnrichedCall())
                .put(
                    "Outgoing enriched call",
                    () -> new SimulatorVoiceCall(context).outgoingEnrichedCall())
                .put(
                    "Spam incoming call",
                    () -> new SimulatorVoiceCall(context).addSpamIncomingCall())
                .put(
                    "Emergency call back",
                    () -> new SimulatorVoiceCall(context).addNewEmergencyCallBack())
                .put(
                    "GSM conference",
                    () ->
                        new SimulatorConferenceCreator(context, Simulator.CONFERENCE_TYPE_GSM)
                            .start(5))
                .put(
                    "VoLTE conference",
                    () ->
                        new SimulatorConferenceCreator(context, Simulator.CONFERENCE_TYPE_VOLTE)
                            .start(5))
                .build())
        .build();
  }

  private SimulatorPortalEntryGroup buildSimulatorVideoCallPortal() {
    return SimulatorPortalEntryGroup.builder()
        .setMethods(
            ImmutableMap.<String, Runnable>builder()
                .put(
                    "Incoming one way",
                    () ->
                        new SimulatorVideoCall(context, VideoProfile.STATE_RX_ENABLED)
                            .addNewIncomingCall())
                .put(
                    "Incoming two way",
                    () ->
                        new SimulatorVideoCall(context, VideoProfile.STATE_BIDIRECTIONAL)
                            .addNewIncomingCall())
                .put(
                    "Outgoing one way",
                    () ->
                        new SimulatorVideoCall(context, VideoProfile.STATE_TX_ENABLED)
                            .addNewOutgoingCall())
                .put(
                    "Outgoing two way",
                    () ->
                        new SimulatorVideoCall(context, VideoProfile.STATE_BIDIRECTIONAL)
                            .addNewOutgoingCall())
                .build())
        .build();
  }

  private SimulatorPortalEntryGroup buildSimulatorRttCallPortal() {
    return SimulatorPortalEntryGroup.builder()
        .setMethods(
            ImmutableMap.<String, Runnable>builder()
                .put("Incoming call", () -> new SimulatorRttCall(context).addNewIncomingCall(false))
                .put("Outgoing call", () -> new SimulatorRttCall(context).addNewOutgoingCall())
                .put("Emergency call", () -> new SimulatorRttCall(context).addNewEmergencyCall())
                .build())
        .build();
  }

  private SimulatorPortalEntryGroup buildSimulatorNotificationsPortal() {
    return SimulatorPortalEntryGroup.builder()
        .setMethods(
            ImmutableMap.<String, Runnable>builder()
                .put(
                    "Missed calls",
                    () ->
                        new SimulatorMissedCallCreator(context)
                            .start(SimulatorUtils.NOTIFICATION_COUNT))
                .put(
                    "Missed calls (few)",
                    () -> new SimulatorMissedCallCreator(context).start(missedCallNum))
                .put(
                    "Voicemails",
                    () ->
                        SimulatorUtils.addVoicemailNotifications(
                            context, SimulatorUtils.NOTIFICATION_COUNT))
                .build())
        .build();
  }

  public ActionProvider getActionProvider() {
    return new SimulatorMenu(context, simulatorPortalEntryGroup);
  }
}