1 package com.android.server.telecom.testapps; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.provider.CallLog.Calls; 10 import android.telecom.PhoneAccount; 11 import android.telecom.TelecomManager; 12 import android.util.Log; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.widget.EditText; 16 import android.widget.Toast; 17 18 import com.android.server.telecom.testapps.R; 19 20 public class TestDialerActivity extends Activity { 21 private static final int REQUEST_CODE_SET_DEFAULT_DIALER = 1; 22 23 private EditText mNumberView; 24 25 @Override onCreate(Bundle savedInstanceState)26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.testdialer_main); 29 findViewById(R.id.set_default_button).setOnClickListener(new OnClickListener() { 30 @Override 31 public void onClick(View v) { 32 setDefault(); 33 } 34 }); 35 36 findViewById(R.id.place_call_button).setOnClickListener(new OnClickListener() { 37 @Override 38 public void onClick(View v) { 39 placeCall(); 40 } 41 }); 42 43 findViewById(R.id.test_voicemail_button).setOnClickListener(new OnClickListener() { 44 @Override 45 public void onClick(View v) { 46 testVoicemail(); 47 } 48 }); 49 50 findViewById(R.id.cancel_missed_button).setOnClickListener(new OnClickListener() { 51 @Override 52 public void onClick(View v) { 53 cancelMissedCallNotification(); 54 } 55 }); 56 57 mNumberView = (EditText) findViewById(R.id.number); 58 updateEditTextWithNumber(); 59 } 60 61 @Override onActivityResult(int requestCode, int resultCode, Intent data)62 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 63 if (requestCode == REQUEST_CODE_SET_DEFAULT_DIALER) { 64 if (resultCode == RESULT_OK) { 65 showToast("User accepted request to become default dialer"); 66 } else if (resultCode == RESULT_CANCELED) { 67 showToast("User declined request to become default dialer"); 68 } 69 } 70 } 71 72 @Override onNewIntent(Intent intent)73 protected void onNewIntent(Intent intent) { 74 super.onNewIntent(intent); 75 updateEditTextWithNumber(); 76 } 77 updateEditTextWithNumber()78 private void updateEditTextWithNumber() { 79 Intent intent = getIntent(); 80 if (intent != null) { 81 mNumberView.setText(intent.getDataString()); 82 } 83 } 84 setDefault()85 private void setDefault() { 86 final Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER); 87 intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName()); 88 startActivityForResult(intent, REQUEST_CODE_SET_DEFAULT_DIALER); 89 } 90 placeCall()91 private void placeCall() { 92 final TelecomManager telecomManager = 93 (TelecomManager) getSystemService(Context.TELECOM_SERVICE); 94 telecomManager.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, 95 mNumberView.getText().toString(), null), createCallIntentExtras()); 96 } 97 testVoicemail()98 private void testVoicemail() { 99 try { 100 // Test read 101 getContentResolver().query(Calls.CONTENT_URI_WITH_VOICEMAIL, null, null, null, null); 102 // Test write 103 final ContentValues values = new ContentValues(); 104 values.put(Calls.CACHED_NAME, "hello world"); 105 getContentResolver().update(Calls.CONTENT_URI_WITH_VOICEMAIL, values, "1=0", null); 106 } catch (SecurityException e) { 107 showToast("Permission check failed"); 108 return; 109 } 110 showToast("Permission check succeeded"); 111 } 112 showToast(String message)113 private void showToast(String message) { 114 Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 115 } 116 cancelMissedCallNotification()117 private void cancelMissedCallNotification() { 118 try { 119 final TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE); 120 tm.cancelMissedCallsNotification(); 121 } catch (SecurityException e) { 122 Toast.makeText(this, "Privileged dialer operation failed", Toast.LENGTH_SHORT).show(); 123 return; 124 } 125 Toast.makeText(this, "Privileged dialer operation succeeded", Toast.LENGTH_SHORT).show(); 126 } 127 createCallIntentExtras()128 private Bundle createCallIntentExtras() { 129 Bundle extras = new Bundle(); 130 extras.putString("com.android.server.telecom.testapps.CALL_EXTRAS", "Yorke was here"); 131 132 Bundle intentExtras = new Bundle(); 133 intentExtras.putBundle(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras); 134 Log.i("Santos xtr", intentExtras.toString()); 135 return intentExtras; 136 } 137 } 138