summaryrefslogtreecommitdiffstats
path: root/src/com/android/car/dialer/ui/dialpad/DialpadFragment.java
blob: 4b4adcd2d76113e761261ca208d353a690035d76 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (C) 2015 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.car.dialer.ui.dialpad;

import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.Settings;
import android.telecom.Call;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.android.car.dialer.R;
import com.android.car.dialer.log.L;
import com.android.car.dialer.telecom.UiCallManager;
import com.android.car.dialer.ui.activecall.InCallViewModel;
import com.android.car.dialer.ui.common.DialerBaseFragment;
import com.android.car.telephony.common.Contact;
import com.android.car.telephony.common.InMemoryPhoneBook;
import com.android.car.telephony.common.TelecomUtils;

import com.google.common.annotations.VisibleForTesting;

/**
 * Fragment that controls the dialpad.
 */
public class DialpadFragment extends DialerBaseFragment implements
        KeypadFragment.KeypadCallback {
    private static final String TAG = "CD.DialpadFragment";
    private static final String DIAL_NUMBER_KEY = "DIAL_NUMBER_KEY";
    private static final String DIALPAD_MODE_KEY = "DIALPAD_MODE_KEY";
    @VisibleForTesting
    static final int MAX_DIAL_NUMBER = 20;

    private static final SparseIntArray sToneMap = new SparseIntArray();
    private static final SparseArray<Character> sDialValueMap = new SparseArray<>();

    private static final int TONE_LENGTH_INFINITE = -1;
    private static final int TONE_RELATIVE_VOLUME = 80;
    private static final int PLAY_DTMF_TONE = 1;

    static {
        sToneMap.put(KeyEvent.KEYCODE_1, ToneGenerator.TONE_DTMF_1);
        sToneMap.put(KeyEvent.KEYCODE_2, ToneGenerator.TONE_DTMF_2);
        sToneMap.put(KeyEvent.KEYCODE_3, ToneGenerator.TONE_DTMF_3);
        sToneMap.put(KeyEvent.KEYCODE_4, ToneGenerator.TONE_DTMF_4);
        sToneMap.put(KeyEvent.KEYCODE_5, ToneGenerator.TONE_DTMF_5);
        sToneMap.put(KeyEvent.KEYCODE_6, ToneGenerator.TONE_DTMF_6);
        sToneMap.put(KeyEvent.KEYCODE_7, ToneGenerator.TONE_DTMF_7);
        sToneMap.put(KeyEvent.KEYCODE_8, ToneGenerator.TONE_DTMF_8);
        sToneMap.put(KeyEvent.KEYCODE_9, ToneGenerator.TONE_DTMF_9);
        sToneMap.put(KeyEvent.KEYCODE_0, ToneGenerator.TONE_DTMF_0);
        sToneMap.put(KeyEvent.KEYCODE_STAR, ToneGenerator.TONE_DTMF_S);
        sToneMap.put(KeyEvent.KEYCODE_POUND, ToneGenerator.TONE_DTMF_P);

        sDialValueMap.put(KeyEvent.KEYCODE_1, '1');
        sDialValueMap.put(KeyEvent.KEYCODE_2, '2');
        sDialValueMap.put(KeyEvent.KEYCODE_3, '3');
        sDialValueMap.put(KeyEvent.KEYCODE_4, '4');
        sDialValueMap.put(KeyEvent.KEYCODE_5, '5');
        sDialValueMap.put(KeyEvent.KEYCODE_6, '6');
        sDialValueMap.put(KeyEvent.KEYCODE_7, '7');
        sDialValueMap.put(KeyEvent.KEYCODE_8, '8');
        sDialValueMap.put(KeyEvent.KEYCODE_9, '9');
        sDialValueMap.put(KeyEvent.KEYCODE_0, '0');
        sDialValueMap.put(KeyEvent.KEYCODE_STAR, '*');
        sDialValueMap.put(KeyEvent.KEYCODE_POUND, '#');
    }

    /**
     * Shows the dialpad for an active phone call.
     */
    private static final int MODE_IN_CALL = 1;

    /** Shows dialpad for dialing. */
    private static final int MODE_DIAL = 2;

    private static final int MODE_EMERGENCY = 3;

    private TextView mTitleView;
    private TextView mDisplayName;
    private ImageButton mDeleteButton;
    private int mMode;
    private StringBuffer mNumber = new StringBuffer(MAX_DIAL_NUMBER);
    private ToneGenerator mToneGenerator;
    private boolean mDTMFToneEnabled;

    /** An active call which this DialpadFragment is serving for. */
    @Nullable
    private Call mActiveCall;

    /**
     * Creates a new instance of the {@link DialpadFragment} which is used for dialing a number.
     */
    public static DialpadFragment newPlaceCallDialpad() {
        DialpadFragment fragment = newDialpad(MODE_DIAL);
        return fragment;
    }

    /** Creates a new instance used for emergency dialing. */
    public static DialpadFragment newEmergencyDialpad() {
        return newDialpad(MODE_EMERGENCY);
    }

    /**
     * Returns a new instance of the {@link DialpadFragment} which runs in an active call for
     * dialing extension number, etc.
     */
    public static DialpadFragment newInCallDialpad() {
        return newDialpad(MODE_IN_CALL);
    }

    private static DialpadFragment newDialpad(int mode) {
        DialpadFragment fragment = new DialpadFragment();

        Bundle args = new Bundle();
        args.putInt(DIALPAD_MODE_KEY, mode);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, TONE_RELATIVE_VOLUME);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mMode = getArguments().getInt(DIALPAD_MODE_KEY);
        if (savedInstanceState != null) {
            mNumber.append(savedInstanceState.getCharSequence(DIAL_NUMBER_KEY));
        }
        L.d(TAG, "onCreateView mode: %s, number: %s", mMode, mNumber);

        View rootView = inflater.inflate(R.layout.dialpad_fragment, container, false);
        // Offset the dialpad to under the tabs in dial mode.
        rootView.setPadding(0, getTopOffset(), 0, 0);

        mTitleView = rootView.findViewById(R.id.title);
        mTitleView.setTextAppearance(
                mMode == MODE_EMERGENCY ? R.style.EmergencyDialNumber : R.style.DialNumber);
        mTitleView.setGravity(Gravity.CENTER);
        mDisplayName = rootView.findViewById(R.id.display_name);
        ImageButton callButton = rootView.findViewById(R.id.call_button);
        mDeleteButton = rootView.findViewById(R.id.delete_button);

        if (mMode == MODE_IN_CALL) {
            mDeleteButton.setVisibility(View.GONE);
            callButton.setVisibility(View.GONE);
            mActiveCall = ViewModelProviders.of(getActivity()).get(
                    InCallViewModel.class).getPrimaryCall().getValue();
        } else {
            callButton.setVisibility(View.VISIBLE);
            mDeleteButton.setVisibility(View.GONE);
            Context context = getContext();
            callButton.setOnClickListener((unusedView) -> {
                if (!TextUtils.isEmpty(mNumber.toString())) {
                    UiCallManager.get().placeCall(mNumber.toString());
                    // Update dialed number UI later in onResume() when in call intent is handled.
                    mNumber.setLength(0);
                } else {
                    setDialedNumber(CallLog.Calls.getLastOutgoingCall(context));
                }
            });
            mDeleteButton.setOnClickListener(v -> removeLastDigit());
            mDeleteButton.setOnLongClickListener(v -> {
                clearDialedNumber();
                return true;
            });
        }

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        mDTMFToneEnabled = Settings.System.getInt(getContext().getContentResolver(),
                Settings.System.DTMF_TONE_WHEN_DIALING, 1) == PLAY_DTMF_TONE;
        L.d(TAG, "DTMF tone enabled = %s", String.valueOf(mDTMFToneEnabled));

        presentDialedNumber();
    }

    @Override
    protected void setActionBarTitle() {
        if (mMode == MODE_DIAL) {
            super.setActionBarTitle();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mToneGenerator.stopTone();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putCharSequence(DIAL_NUMBER_KEY, mNumber);
    }

    @Override
    public void onKeyLongPressed(@KeypadFragment.DialKeyCode int keycode) {
        switch (keycode) {
            case KeyEvent.KEYCODE_0:
                removeLastDigit();
                appendDialedNumber("+");
                break;
            case KeyEvent.KEYCODE_1:
                UiCallManager.get().callVoicemail();
        }
    }

    @Override
    public void onKeyDown(@KeypadFragment.DialKeyCode int keycode) {
        String digit = sDialValueMap.get(keycode).toString();
        appendDialedNumber(digit);

        if (!mDTMFToneEnabled) {
            return;
        }
        if (mActiveCall != null) {
            L.d(TAG, "start DTMF tone for %s", keycode);
            mActiveCall.playDtmfTone(sDialValueMap.get(keycode));
        } else {
            L.d(TAG, "start key pressed tone for %s", keycode);
            mToneGenerator.startTone(sToneMap.get(keycode), TONE_LENGTH_INFINITE);
        }
    }

    @Override
    public void onKeyUp(@KeypadFragment.DialKeyCode int keycode) {
        if (!mDTMFToneEnabled) {
            return;
        }

        if (mActiveCall != null) {
            L.d(TAG, "stop DTMF tone");
            mActiveCall.stopDtmfTone();
        } else {
            L.d(TAG, "stop key pressed tone");
            mToneGenerator.stopTone();
        }
    }

    /** Set the dialed number to the given number. Must be called after the fragment is added. */
    public void setDialedNumber(String number) {
        mNumber.setLength(0);
        if (!TextUtils.isEmpty(number)) {
            mNumber.append(number);
        }
        presentDialedNumber();
    }

    private void clearDialedNumber() {
        mNumber.setLength(0);
        presentDialedNumber();
    }

    private void removeLastDigit() {
        if (mNumber.length() != 0) {
            mNumber.deleteCharAt(mNumber.length() - 1);
        }
        presentDialedNumber();
    }

    private void appendDialedNumber(String number) {
        mNumber.append(number);
        presentDialedNumber();
    }

    private void presentDialedNumber() {
        if (getActivity() == null) {
            return;
        }

        if (mMode != MODE_IN_CALL) {
            presentContactName();
        }

        if (mNumber.length() == 0) {
            mTitleView.setGravity(Gravity.CENTER);
            mDeleteButton.setVisibility(View.GONE);

            if (mMode == MODE_DIAL) {
                mTitleView.setText(R.string.dial_a_number);
            } else if (mMode == MODE_EMERGENCY) {
                mTitleView.setText(R.string.emergency_call_description);
            } else {
                mTitleView.setText("");
            }
        } else if (mNumber.length() > 0 && mNumber.length() <= MAX_DIAL_NUMBER) {
            mTitleView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);

            if (mMode == MODE_IN_CALL) {
                mTitleView.setText(mNumber);
                mDeleteButton.setVisibility(View.GONE);
            } else {
                mTitleView.setText(
                        TelecomUtils.getFormattedNumber(getContext(), mNumber.toString()));
                mDeleteButton.setVisibility(View.VISIBLE);
            }
        } else {
            mTitleView.setText(mNumber.substring(mNumber.length() - MAX_DIAL_NUMBER));
            mTitleView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);

            if (mMode == MODE_IN_CALL) {
                mDeleteButton.setVisibility(View.GONE);
            } else {
                mDeleteButton.setVisibility(View.VISIBLE);
            }
        }
    }

    private void presentContactName() {
        if (mDisplayName == null) {
            // OEM may remove this view from resource file.
            return;
        }

        Contact contact = InMemoryPhoneBook.get().lookupContactEntry(mNumber.toString());
        if (contact == null) {
            mDisplayName.setText("");
            mDisplayName.setVisibility(View.GONE);
            return;
        }
        mDisplayName.setVisibility(View.VISIBLE);
        mDisplayName.setText(contact.getDisplayName());
    }

    private int getTopOffset() {
        if (mMode == MODE_DIAL) {
            return getTopBarHeight();
        }
        return 0;
    }
}