1 /*
2  * Copyright (C) 2017 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.app.AlertDialog;
20 import android.app.Dialog;
21 import android.os.Bundle;
22 import android.support.v4.app.DialogFragment;
23 import android.telecom.TelecomManager;
24 import android.widget.EditText;
25 
26 /** Holds dialog logic for creating different types of voice calls. */
27 public final class SimulatorDialogFragment extends DialogFragment {
28 
29   private final String[] callerIdPresentationItems = {
30     "ALLOWED", "PAYPHONE", "RESTRICTED", "UNKNOWN"
31   };
32   private int callerIdPresentationChoice = 1;
33 
34   private DialogCallback dialogCallback;
35 
newInstance(DialogCallback dialogCallback)36   static SimulatorDialogFragment newInstance(DialogCallback dialogCallback) {
37     SimulatorDialogFragment fragment = new SimulatorDialogFragment();
38     fragment.setCallBack(dialogCallback);
39     return fragment;
40   }
41 
setCallBack(DialogCallback dialogCallback)42   public void setCallBack(DialogCallback dialogCallback) {
43     this.dialogCallback = dialogCallback;
44   }
45 
46   @Override
onCreateDialog(Bundle bundle)47   public Dialog onCreateDialog(Bundle bundle) {
48     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
49     final EditText input = new EditText(getActivity());
50     input.setHint("Please input phone number");
51     builder
52         .setTitle("Phone Number:")
53         .setView(input)
54         .setSingleChoiceItems(
55             callerIdPresentationItems,
56             0,
57             (dialog, id) -> {
58               switch (id) {
59                 case 0:
60                   callerIdPresentationChoice = TelecomManager.PRESENTATION_ALLOWED;
61                   break;
62                 case 1:
63                   callerIdPresentationChoice = TelecomManager.PRESENTATION_PAYPHONE;
64                   break;
65                 case 2:
66                   callerIdPresentationChoice = TelecomManager.PRESENTATION_RESTRICTED;
67                   break;
68                 case 3:
69                   callerIdPresentationChoice = TelecomManager.PRESENTATION_UNKNOWN;
70                   break;
71                 default:
72                   throw new IllegalStateException("Unknown presentation choice selected!");
73               }
74             })
75         .setPositiveButton(
76             R.string.call,
77             (dialog, id) -> {
78               dialogCallback.createCustomizedCall(
79                   input.getText().toString(), callerIdPresentationChoice);
80               dialog.cancel();
81               SimulatorDialogFragment.this.dismiss();
82             });
83     AlertDialog dialog = builder.create();
84     dialog.show();
85     return dialog;
86   }
87 
88   /** Callback for after clicking enter button on dialog. */
89   public interface DialogCallback {
createCustomizedCall(String callerId, int callerIdPresentation)90     void createCustomizedCall(String callerId, int callerIdPresentation);
91   }
92 }
93