1/*
2 * Copyright (C) 2010 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
17package com.android.phone.tests;
18
19import android.app.Activity;
20import android.content.ActivityNotFoundException;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.Bundle;
24import android.os.RemoteException;
25import android.os.ServiceManager;
26import android.telephony.PhoneNumberUtils;
27import android.text.TextUtils;
28import android.util.Log;
29import android.view.View;
30import android.widget.Button;
31import android.widget.EditText;
32import android.widget.TextView;
33import android.widget.Toast;
34
35import com.android.internal.telephony.ITelephony;
36import com.android.phone.Constants;
37
38/**
39 * Test activity that mimics the behavior of 3rd party apps firing off
40 * CALL and DIAL intents.
41 */
42public class CallDialTest extends Activity implements View.OnClickListener {
43    private static final String LOG_TAG = "CallDialTest";
44
45    // UI elements
46    private TextView mLabel;
47    private EditText mNumber;
48
49    @Override
50    protected void onCreate(Bundle savedInstanceState) {
51        Intent intent = getIntent();
52        log("onCreate: intent = " + intent);
53
54        // Construct our basic UI:
55        super.onCreate(savedInstanceState);
56        setContentView(R.layout.call_dial_test);
57
58        mLabel = (TextView) findViewById(R.id.label1);
59
60        mNumber = (EditText) findViewById(R.id.number);
61        mNumber.setText("6505551234");  // Preload it with something useful
62
63        ((Button) findViewById(R.id.callButton)).setOnClickListener(this);
64        ((Button) findViewById(R.id.dialButton)).setOnClickListener(this);
65        ((Button) findViewById(R.id.itelephonyCallButton)).setOnClickListener(this);
66        ((Button) findViewById(R.id.itelephonyDialButton)).setOnClickListener(this);
67    }
68
69    @Override
70    protected void onResume() {
71        log("onResume()...");
72        super.onResume();
73    }
74
75    @Override
76    protected void onPause() {
77        log("onPause()...");
78        super.onPause();
79    }
80
81    // View.OnClickListener implementation
82    @Override
83    public void onClick(View view) {
84        int id = view.getId();
85        log("onClick(View " + view + ", id " + id + ")...");
86
87        switch (id) {
88            case R.id.callButton:
89                log("onClick: CALL...");
90                fireIntent(Intent.ACTION_CALL);
91                break;
92            case R.id.dialButton:
93                log("onClick: DIAL...");
94                fireIntent(Intent.ACTION_DIAL);
95                break;
96            case R.id.itelephonyCallButton:
97                log("onClick: ITelephony.call()...");
98                doITelephonyCall();
99                break;
100            case R.id.itelephonyDialButton:
101                log("onClick: ITelephony.dial()...");
102                doITelephonyDial();
103                break;
104            default:
105                Log.wtf(LOG_TAG, "onClick: unexpected View: " + view);
106                break;
107        }
108    }
109
110    private void fireIntent(String action) {
111        log("fireIntent(action = '" + action + "')...");
112
113        // Get a phone number or SIP address from the EditText widget
114        String number = mNumber.getText().toString();
115        log("==> number: '" + number + "'");
116
117        // Based on the number, fire off a CALL or DIAL intent:
118        // - if it's a fully qualified URI (with scheme), use it directly
119        // - if it looks like a SIP address, prepend sip:
120        // - if it's just a number, prepend tel: automatically
121        // - if it's blank, fire off a blank CALL or DIAL intent
122
123        Uri uri = null;
124        if (!TextUtils.isEmpty(number)) {
125            if (number.contains(":")) {
126                uri = Uri.parse(number);
127            } else if (PhoneNumberUtils.isUriNumber(number)) {
128                uri = Uri.fromParts(Constants.SCHEME_SIP, number, null);
129            } else {
130                uri = Uri.fromParts(Constants.SCHEME_TEL, number, null);
131            }
132        }
133        log("==> uri: " + uri);
134
135        Intent intent = new Intent(action, uri);
136        log("==> intent: " + intent);
137
138        try {
139            startActivity(intent);
140            Toast.makeText(this, "Starting activity...", Toast.LENGTH_SHORT).show();
141        } catch (ActivityNotFoundException e) {
142            Log.w(LOG_TAG, "testCall: ActivityNotFoundException for intent: " + intent);
143            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
144        } catch (Exception e) {
145            Log.w(LOG_TAG, "testCall: Unexpected exception from startActivity(): " + e);
146            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
147        }
148    }
149
150    private void doITelephonyCall() {
151        log("doITelephonyCall()...");
152
153        // Get a phone number from the EditText widget
154        String number = mNumber.getText().toString();
155        log("==> number: '" + number + "'");
156
157        try {
158            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
159            log("- phone: " + phone);
160            log("- calling call()...");
161            phone.call(number);
162            log("  Done.");
163        } catch (RemoteException ex) {
164            Log.w(LOG_TAG, "RemoteException!", ex);
165        }
166    }
167
168    private void doITelephonyDial() {
169        log("doITelephonyDial()...");
170
171        // Get a phone number from the EditText widget
172        String number = mNumber.getText().toString();
173        log("==> number: '" + number + "'");
174
175        try {
176            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
177            log("- phone: " + phone);
178            log("- calling dial()...");
179            phone.dial(number);
180            log("  Done.");
181        } catch (RemoteException ex) {
182            Log.w(LOG_TAG, "RemoteException!", ex);
183        }
184    }
185
186    private void log(String msg) {
187        Log.i(LOG_TAG, msg);
188    }
189}
190