summaryrefslogtreecommitdiff
path: root/tests/src/com/android/phone/tests/CallDialTest.java
blob: 5ea80d1d10a94f9fe7acecde95c2a870077f30f0 (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
/*
 * Copyright (C) 2010 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.phone.tests;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;
import com.android.phone.Constants;

/**
 * Test activity that mimics the behavior of 3rd party apps firing off
 * CALL and DIAL intents.
 */
public class CallDialTest extends Activity implements View.OnClickListener {
    private static final String LOG_TAG = "CallDialTest";

    // UI elements
    private TextView mLabel;
    private EditText mNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        log("onCreate: intent = " + intent);

        // Construct our basic UI:
        super.onCreate(savedInstanceState);
        setContentView(R.layout.call_dial_test);

        mLabel = (TextView) findViewById(R.id.label1);

        mNumber = (EditText) findViewById(R.id.number);
        mNumber.setText("6505551234");  // Preload it with something useful

        ((Button) findViewById(R.id.callButton)).setOnClickListener(this);
        ((Button) findViewById(R.id.dialButton)).setOnClickListener(this);
        ((Button) findViewById(R.id.itelephonyCallButton)).setOnClickListener(this);
        ((Button) findViewById(R.id.itelephonyDialButton)).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        log("onResume()...");
        super.onResume();
    }

    @Override
    protected void onPause() {
        log("onPause()...");
        super.onPause();
    }

    // View.OnClickListener implementation
    @Override
    public void onClick(View view) {
        int id = view.getId();
        log("onClick(View " + view + ", id " + id + ")...");

        switch (id) {
            case R.id.callButton:
                log("onClick: CALL...");
                fireIntent(Intent.ACTION_CALL);
                break;
            case R.id.dialButton:
                log("onClick: DIAL...");
                fireIntent(Intent.ACTION_DIAL);
                break;
            case R.id.itelephonyCallButton:
                log("onClick: ITelephony.call()...");
                doITelephonyCall();
                break;
            case R.id.itelephonyDialButton:
                log("onClick: ITelephony.dial()...");
                doITelephonyDial();
                break;
            default:
                Log.wtf(LOG_TAG, "onClick: unexpected View: " + view);
                break;
        }
    }

    private void fireIntent(String action) {
        log("fireIntent(action = '" + action + "')...");

        // Get a phone number or SIP address from the EditText widget
        String number = mNumber.getText().toString();
        log("==> number: '" + number + "'");

        // Based on the number, fire off a CALL or DIAL intent:
        // - if it's a fully qualified URI (with scheme), use it directly
        // - if it looks like a SIP address, prepend sip:
        // - if it's just a number, prepend tel: automatically
        // - if it's blank, fire off a blank CALL or DIAL intent

        Uri uri = null;
        if (!TextUtils.isEmpty(number)) {
            if (number.contains(":")) {
                uri = Uri.parse(number);
            } else if (PhoneNumberUtils.isUriNumber(number)) {
                uri = Uri.fromParts(Constants.SCHEME_SIP, number, null);
            } else {
                uri = Uri.fromParts(Constants.SCHEME_TEL, number, null);
            }
        }
        log("==> uri: " + uri);

        Intent intent = new Intent(action, uri);
        log("==> intent: " + intent);

        try {
            startActivity(intent);
            Toast.makeText(this, "Starting activity...", Toast.LENGTH_SHORT).show();
        } catch (ActivityNotFoundException e) {
            Log.w(LOG_TAG, "testCall: ActivityNotFoundException for intent: " + intent);
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Log.w(LOG_TAG, "testCall: Unexpected exception from startActivity(): " + e);
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    private void doITelephonyCall() {
        log("doITelephonyCall()...");

        // Get a phone number from the EditText widget
        String number = mNumber.getText().toString();
        log("==> number: '" + number + "'");

        try {
            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
            log("- phone: " + phone);
            log("- calling call()...");
            phone.call(getPackageName(), number);
            log("  Done.");
        } catch (RemoteException ex) {
            Log.w(LOG_TAG, "RemoteException!", ex);
        }
    }

    private void doITelephonyDial() {
        log("doITelephonyDial()...");

        // Get a phone number from the EditText widget
        String number = mNumber.getText().toString();
        log("==> number: '" + number + "'");

        try {
            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
            log("- phone: " + phone);
            log("- calling dial()...");
            phone.dial(number);
            log("  Done.");
        } catch (RemoteException ex) {
            Log.w(LOG_TAG, "RemoteException!", ex);
        }
    }

    private void log(String msg) {
        Log.i(LOG_TAG, msg);
    }
}