CallDialTest.java revision 65454c803eb305c4740885ad4995a871b034a58a
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.telephony.PhoneNumberUtils;
25import android.text.TextUtils;
26import android.util.Log;
27import android.view.View;
28import android.widget.Button;
29import android.widget.EditText;
30import android.widget.TextView;
31import android.widget.Toast;
32
33import com.android.phone.Constants;
34
35/**
36 * Test activity that mimics the behavior of 3rd party apps firing off
37 * CALL and DIAL intents.
38 */
39public class CallDialTest extends Activity implements View.OnClickListener {
40    private static final String LOG_TAG = "CallDialTest";
41
42    // UI elements
43    private TextView mLabel;
44    private EditText mNumber;
45    private Button mCallButton;
46    private Button mDialButton;
47
48    @Override
49    protected void onCreate(Bundle savedInstanceState) {
50        Intent intent = getIntent();
51        log("onCreate: intent = " + intent);
52
53        // Construct our basic UI:
54        super.onCreate(savedInstanceState);
55        setContentView(R.layout.call_dial_test);
56
57        mLabel = (TextView) findViewById(R.id.label1);
58
59        mNumber = (EditText) findViewById(R.id.number);
60        mNumber.setText("6505551234");  // Preload it with something useful
61
62        mCallButton = (Button) findViewById(R.id.callButton);
63        mCallButton.setOnClickListener(this);
64
65        mDialButton = (Button) findViewById(R.id.dialButton);
66        mDialButton.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            default:
97                Log.w(LOG_TAG, "onClick: unexpected View: " + view);
98                break;
99        }
100    }
101
102    private void fireIntent(String action) {
103        log("fireIntent(action = '" + action + "')...");
104
105        // Get a phone number or SIP address from the EditText widget
106        String number = mNumber.getText().toString();
107        log("==> number: '" + number + "'");
108
109        // Based on the number, fire off a CALL or DIAL intent:
110        // - if it's a fully qualified URI (with scheme), use it directly
111        // - if it looks like a SIP address, prepend sip:
112        // - if it's just a number, prepend tel: automatically
113        // - if it's blank, fire off a blank CALL or DIAL intent
114
115        Uri uri = null;
116        if (!TextUtils.isEmpty(number)) {
117            if (number.contains(":")) {
118                uri = Uri.parse(number);
119            } else if (PhoneNumberUtils.isUriNumber(number)) {
120                uri = Uri.fromParts(Constants.SCHEME_SIP, number, null);
121            } else {
122                uri = Uri.fromParts(Constants.SCHEME_TEL, number, null);
123            }
124        }
125        log("==> uri: " + uri);
126
127        Intent intent = new Intent(action, uri);
128        log("==> intent: " + intent);
129
130        try {
131            startActivity(intent);
132            Toast.makeText(this, "Starting activity...", Toast.LENGTH_SHORT).show();
133        } catch (ActivityNotFoundException e) {
134            Log.w(LOG_TAG, "testCall: ActivityNotFoundException for intent: " + intent);
135            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
136        } catch (Exception e) {
137            Log.w(LOG_TAG, "testCall: Unexpected exception from startActivity(): " + e);
138            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
139        }
140    }
141
142    private void log(String msg) {
143        Log.i(LOG_TAG, msg);
144    }
145}
146