Home | History | Annotate | Download | only in tests
      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 
     17 package com.android.phone.tests;
     18 
     19 import android.app.Activity;
     20 import android.content.ActivityNotFoundException;
     21 import android.content.Intent;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.os.RemoteException;
     25 import android.os.ServiceManager;
     26 import android.telephony.PhoneNumberUtils;
     27 import android.text.TextUtils;
     28 import android.util.Log;
     29 import android.view.View;
     30 import android.widget.Button;
     31 import android.widget.EditText;
     32 import android.widget.TextView;
     33 import android.widget.Toast;
     34 
     35 import com.android.internal.telephony.ITelephony;
     36 import 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  */
     42 public 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