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.simulator.impl;
18 
19 import android.content.Context;
20 import android.support.v7.app.AppCompatActivity;
21 import android.telecom.TelecomManager;
22 import android.telecom.VideoProfile;
23 import android.view.ActionProvider;
24 import com.android.dialer.common.concurrent.DialerExecutorComponent;
25 import com.android.dialer.enrichedcall.simulator.EnrichedCallSimulatorActivity;
26 import com.android.dialer.simulator.Simulator;
27 import com.android.dialer.simulator.SimulatorComponent;
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.util.concurrent.ListenableFuture;
30 
31 /** Implements the top level simulator menu. */
32 public class SimulatorMainPortal {
33 
34   private final Context context;
35   private final AppCompatActivity activity;
36   private SimulatorPortalEntryGroup simulatorPortalEntryGroup;
37   private String callerId = "";
38   private int presentation = TelecomManager.PRESENTATION_ALLOWED;
39   private int missedCallNum = 1;
40 
SimulatorMainPortal(AppCompatActivity activity)41   public SimulatorMainPortal(AppCompatActivity activity) {
42     this.activity = activity;
43     this.context = activity.getApplicationContext();
44     buildMainPortal();
45   }
46 
SimulatorMainPortal(Context context)47   public SimulatorMainPortal(Context context) {
48     this.activity = null;
49     this.context = context;
50     buildMainPortal();
51   }
52 
setCallerId(String callerId)53   public void setCallerId(String callerId) {
54     this.callerId = callerId;
55   }
56 
setPresentation(int presentation)57   public void setPresentation(int presentation) {
58     this.presentation = presentation;
59   }
60 
setMissedCallNum(int missedCallNum)61   public void setMissedCallNum(int missedCallNum) {
62     this.missedCallNum = missedCallNum;
63   }
64 
65   /**
66    * Executes commands sent to this portal.
67    *
68    * @param commands a string array that stores commands that trigger runnable methods stored in the
69    *     portal. For example: ["VoiceCall", "Incoming Call"] triggers "() -> new
70    *     SimulatorVoiceCall(context).addNewIncomingCall()" runnable method.
71    */
execute(String[] commands)72   public void execute(String[] commands) {
73     @SuppressWarnings("unused")
74     ListenableFuture<?> executeCommand =
75         DialerExecutorComponent.get(context)
76             .backgroundExecutor()
77             .submit(() -> execute(simulatorPortalEntryGroup, commands, 0));
78   }
79 
execute( SimulatorPortalEntryGroup simulatorPortalEntryGroup, String[] commands, int index)80   private void execute(
81       SimulatorPortalEntryGroup simulatorPortalEntryGroup, String[] commands, int index) {
82     if (simulatorPortalEntryGroup.methods().containsKey(commands[index])) {
83       simulatorPortalEntryGroup.methods().get(commands[index]).run();
84     } else if (simulatorPortalEntryGroup.subPortals().containsKey(commands[index])) {
85       execute(simulatorPortalEntryGroup.subPortals().get(commands[index]), commands, index + 1);
86     }
87   }
88 
buildMainPortal()89   private void buildMainPortal() {
90     simulatorPortalEntryGroup =
91         SimulatorPortalEntryGroup.builder()
92             .setMethods(
93                 ImmutableMap.<String, Runnable>builder()
94                     .put("Populate database", () -> SimulatorUtils.populateDatabase(context))
95                     .put("Populate voicemail", () -> SimulatorUtils.populateVoicemail(context))
96                     .put(
97                         "Fast Populate database",
98                         () -> SimulatorUtils.fastPopulateDatabase(context))
99                     .put(
100                         "Fast populate voicemail database",
101                         () -> SimulatorUtils.populateVoicemailFast(context))
102                     .put("Clean database", () -> SimulatorUtils.cleanDatabase(context))
103                     .put("clear preferred SIM", () -> SimulatorUtils.clearPreferredSim(context))
104                     .put("Sync voicemail", () -> SimulatorUtils.syncVoicemail(context))
105                     .put("Share persistent log", () -> SimulatorUtils.sharePersistentLog(context))
106                     .put(
107                         "Enriched call simulator",
108                         () ->
109                             context.startActivity(EnrichedCallSimulatorActivity.newIntent(context)))
110                     .put(
111                         "Enable simulator mode",
112                         () -> {
113                           SimulatorComponent.get(context).getSimulator().enableSimulatorMode();
114                           SimulatorSimCallManager.register(context);
115                         })
116                     .put(
117                         "Disable simulator mode",
118                         () -> {
119                           SimulatorComponent.get(context).getSimulator().disableSimulatorMode();
120                           SimulatorSimCallManager.unregister(context);
121                         })
122                     .build())
123             .setSubPortals(
124                 ImmutableMap.of(
125                     "VoiceCall",
126                     buildSimulatorVoiceCallPortal(),
127                     "VideoCall",
128                     buildSimulatorVideoCallPortal(),
129                     "RttCall",
130                     buildSimulatorRttCallPortal(),
131                     "Notifications",
132                     buildSimulatorNotificationsPortal()))
133             .build();
134   }
135 
buildSimulatorVoiceCallPortal()136   public SimulatorPortalEntryGroup buildSimulatorVoiceCallPortal() {
137     return SimulatorPortalEntryGroup.builder()
138         .setMethods(
139             ImmutableMap.<String, Runnable>builder()
140                 .put("Incoming call", () -> new SimulatorVoiceCall(context).addNewIncomingCall())
141                 .put("Outgoing call", () -> new SimulatorVoiceCall(context).addNewOutgoingCall())
142                 .put(
143                     "Customized incoming call (Dialog)",
144                     () ->
145                         new SimulatorVoiceCall(context)
146                             .addCustomizedIncomingCallWithDialog(activity))
147                 .put(
148                     "Customized outgoing call (Dialog)",
149                     () ->
150                         new SimulatorVoiceCall(context)
151                             .addCustomizedOutgoingCallWithDialog(activity))
152                 .put(
153                     "Customized incoming call",
154                     () ->
155                         new SimulatorVoiceCall(context)
156                             .addCustomizedIncomingCall(this.callerId, this.presentation))
157                 .put(
158                     "Customized outgoing call",
159                     () ->
160                         new SimulatorVoiceCall(context)
161                             .addCustomizedOutgoingCall(this.callerId, this.presentation))
162                 .put(
163                     "Incoming enriched call",
164                     () -> new SimulatorVoiceCall(context).incomingEnrichedCall())
165                 .put(
166                     "Outgoing enriched call",
167                     () -> new SimulatorVoiceCall(context).outgoingEnrichedCall())
168                 .put(
169                     "Spam incoming call",
170                     () -> new SimulatorVoiceCall(context).addSpamIncomingCall())
171                 .put(
172                     "Emergency call back",
173                     () -> new SimulatorVoiceCall(context).addNewEmergencyCallBack())
174                 .put(
175                     "GSM conference",
176                     () ->
177                         new SimulatorConferenceCreator(context, Simulator.CONFERENCE_TYPE_GSM)
178                             .start(5))
179                 .put(
180                     "VoLTE conference",
181                     () ->
182                         new SimulatorConferenceCreator(context, Simulator.CONFERENCE_TYPE_VOLTE)
183                             .start(5))
184                 .build())
185         .build();
186   }
187 
buildSimulatorVideoCallPortal()188   private SimulatorPortalEntryGroup buildSimulatorVideoCallPortal() {
189     return SimulatorPortalEntryGroup.builder()
190         .setMethods(
191             ImmutableMap.<String, Runnable>builder()
192                 .put(
193                     "Incoming one way",
194                     () ->
195                         new SimulatorVideoCall(context, VideoProfile.STATE_RX_ENABLED)
196                             .addNewIncomingCall())
197                 .put(
198                     "Incoming two way",
199                     () ->
200                         new SimulatorVideoCall(context, VideoProfile.STATE_BIDIRECTIONAL)
201                             .addNewIncomingCall())
202                 .put(
203                     "Outgoing one way",
204                     () ->
205                         new SimulatorVideoCall(context, VideoProfile.STATE_TX_ENABLED)
206                             .addNewOutgoingCall())
207                 .put(
208                     "Outgoing two way",
209                     () ->
210                         new SimulatorVideoCall(context, VideoProfile.STATE_BIDIRECTIONAL)
211                             .addNewOutgoingCall())
212                 .build())
213         .build();
214   }
215 
buildSimulatorRttCallPortal()216   private SimulatorPortalEntryGroup buildSimulatorRttCallPortal() {
217     return SimulatorPortalEntryGroup.builder()
218         .setMethods(
219             ImmutableMap.<String, Runnable>builder()
220                 .put("Incoming call", () -> new SimulatorRttCall(context).addNewIncomingCall(false))
221                 .put("Outgoing call", () -> new SimulatorRttCall(context).addNewOutgoingCall())
222                 .put("Emergency call", () -> new SimulatorRttCall(context).addNewEmergencyCall())
223                 .build())
224         .build();
225   }
226 
buildSimulatorNotificationsPortal()227   private SimulatorPortalEntryGroup buildSimulatorNotificationsPortal() {
228     return SimulatorPortalEntryGroup.builder()
229         .setMethods(
230             ImmutableMap.<String, Runnable>builder()
231                 .put(
232                     "Missed calls",
233                     () ->
234                         new SimulatorMissedCallCreator(context)
235                             .start(SimulatorUtils.NOTIFICATION_COUNT))
236                 .put(
237                     "Missed calls (few)",
238                     () -> new SimulatorMissedCallCreator(context).start(missedCallNum))
239                 .put(
240                     "Voicemails",
241                     () ->
242                         SimulatorUtils.addVoicemailNotifications(
243                             context, SimulatorUtils.NOTIFICATION_COUNT))
244                 .build())
245         .build();
246   }
247 
getActionProvider()248   public ActionProvider getActionProvider() {
249     return new SimulatorMenu(context, simulatorPortalEntryGroup);
250   }
251 }
252