aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/car/calendar/common/Dialer.java
blob: 889a3c81279026efbc8a1bc8483971af884cc6e6 (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
/*
 * Copyright 2020 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.calendar.common;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.telephony.PhoneNumberUtils;
import android.util.Log;

import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;

import javax.annotation.Nullable;

/** Calls the default dialer with an optional access code. */
public class Dialer {

    private static final String TAG = "CarCalendarDialer";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    private final Context mContext;

    public Dialer(Context context) {
        mContext = context;
    }

    /** Calls a telephone using a phone number and access number. */
    public boolean dial(NumberAndAccess numberAndAccess) {
        StringBuilder sb = new StringBuilder(numberAndAccess.getNumber());
        String access = numberAndAccess.getAccess();
        if (!Strings.isNullOrEmpty(access)) {
            // Wait for the number to dial if required.
            char first = access.charAt(0);
            if (first != PhoneNumberUtils.PAUSE && first != PhoneNumberUtils.WAIT) {
                // Insert a wait so the number finishes dialing before using the access code.
                access = PhoneNumberUtils.WAIT + access;
            }
            sb.append(access);
        }
        Uri dialUri = Uri.fromParts("tel", sb.toString(), /* fragment= */ null);
        PackageManager packageManager = mContext.getPackageManager();
        boolean useActionCall = packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        Intent intent = new Intent(useActionCall ? Intent.ACTION_CALL : Intent.ACTION_DIAL);
        intent.setData(dialUri);
        if (intent.resolveActivity(packageManager) == null) {
            Log.i(TAG, "No dialler app found");
            return false;
        }
        if (DEBUG) Log.d(TAG, "Starting dialler activity");
        mContext.startActivity(intent);
        return true;
    }

    /** An immutable value representing the details required to enter a conference call. */
    public static class NumberAndAccess {
        private final String mNumber;

        @Nullable private final String mAccess;

        NumberAndAccess(String number, @Nullable String access) {
            this.mNumber = number;
            this.mAccess = access;
        }

        public String getNumber() {
            return mNumber;
        }

        @Nullable
        public String getAccess() {
            return mAccess;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this)
                    .add("mNumber", mNumber)
                    .add("mAccess", mAccess)
                    .toString();
        }
    }
}