package com.grundfos.android.people; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts; import android.provider.Contacts.People; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.TextView; import android.widget.Toast; public class PeopleDetails extends Activity { static final int CALL_PHONE = 1000; static final int CALL_CELL = 1001; static final int TEXT_CELL = 1002; static final int WRITE_EMAIL = 1003; static final int CONTACTS = 1004; PeopleBean people = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.details); long id = getIntent().getLongExtra("id", -1); Log.i("PeopleDatails", "ID:"+id); people = new PeopleDatabase(this).getPeople(id); if (people != null) { View details = findViewById(R.id.detailsview); registerForContextMenu(details); TextView tv = (TextView) findViewById(R.id.details_name); tv.setText(people.name); tv = (TextView) findViewById(R.id.details_inits); tv.setText(people.inits); tv = (TextView) findViewById(R.id.details_title); tv.setText(people.title); tv = (TextView) findViewById(R.id.details_dept); tv.setText(people.dept); tv = (TextView) findViewById(R.id.details_company); tv.setText(people.company); tv = (TextView) findViewById(R.id.details_phone); tv.setText(people.phone); tv = (TextView) findViewById(R.id.details_cell); tv.setText(people.cell); tv = (TextView) findViewById(R.id.details_email); tv.setText(people.email); } else { Log.e("PeopleDetails", "Person not found!"); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuItem callPhone = menu.add(0, CALL_PHONE, 0, "Call Phone"); MenuItem callCell = menu.add(0, CALL_CELL, 0, "Call Cell"); MenuItem textCell = menu.add(0, TEXT_CELL, 0, "Text Cell"); MenuItem writeMail = menu.add(0, WRITE_EMAIL, 0, "Write e-mail"); menu.add(0, CONTACTS, 0, "Copy to contacts"); if (people.phone.length() == 0) callPhone.setEnabled(false); if (people.cell.length() == 0) { callCell.setEnabled(false); textCell.setEnabled(false); } if (people.email.length() == 0) writeMail.setEnabled(false); } public boolean onContextItemSelected(MenuItem item) { Intent intent; switch(item.getItemId()) { case CALL_PHONE: intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + people.phone)); startActivity(intent); return true; case CALL_CELL: intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + people.cell)); startActivity(intent); return true; case TEXT_CELL: intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("sms:" + people.cell)); startActivity(intent); return true; case WRITE_EMAIL: intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:" + people.email)); startActivity(intent); return true; case CONTACTS: intent = new Intent(Intent.ACTION_INSERT, People.CONTENT_URI); intent.putExtra(Contacts.Intents.Insert.NAME, people.name); intent.putExtra(Contacts.Intents.Insert.PHONE, people.phone); intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_WORK); intent.putExtra(Contacts.Intents.Insert.SECONDARY_PHONE , people.cell); intent.putExtra(Contacts.Intents.Insert.SECONDARY_PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE); intent.putExtra(Contacts.Intents.Insert.EMAIL, people.email); intent.putExtra(Contacts.Intents.Insert.JOB_TITLE, people.title); startActivity(intent); return true; } return super.onContextItemSelected(item); } }