Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2007 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;
     18 
     19 import android.content.Intent;
     20 import android.content.res.Resources;
     21 import android.net.Uri;
     22 import android.view.Menu;
     23 import android.view.MenuItem;
     24 import android.view.View;
     25 import android.widget.ListView;
     26 
     27 /**
     28  * FDN List UI for the Phone app.
     29  */
     30 public class FdnList extends ADNList {
     31     private static final int MENU_ADD = 1;
     32     private static final int MENU_EDIT = 2;
     33     private static final int MENU_DELETE = 3;
     34 
     35     private static final String INTENT_EXTRA_NAME = "name";
     36     private static final String INTENT_EXTRA_NUMBER = "number";
     37 
     38 
     39     @Override
     40     protected Uri resolveIntent() {
     41         Intent intent = getIntent();
     42         intent.setData(Uri.parse("content://icc/fdn"));
     43         return intent.getData();
     44     }
     45 
     46     @Override
     47     public boolean onCreateOptionsMenu(Menu menu) {
     48         super.onCreateOptionsMenu(menu);
     49 
     50         Resources r = getResources();
     51 
     52         // Added the icons to the context menu
     53         menu.add(0, MENU_ADD, 0, r.getString(R.string.menu_add))
     54                 .setIcon(android.R.drawable.ic_menu_add);
     55         menu.add(0, MENU_EDIT, 0, r.getString(R.string.menu_edit))
     56                 .setIcon(android.R.drawable.ic_menu_edit);
     57         menu.add(0, MENU_DELETE, 0, r.getString(R.string.menu_delete))
     58                 .setIcon(android.R.drawable.ic_menu_delete);
     59         return true;
     60     }
     61 
     62     @Override
     63     public boolean onPrepareOptionsMenu(Menu menu) {
     64         super.onPrepareOptionsMenu(menu);
     65         boolean hasSelection = (getSelectedItemPosition() >= 0);
     66 
     67         menu.findItem(MENU_ADD).setVisible(true);
     68         menu.findItem(MENU_EDIT).setVisible(hasSelection);
     69         menu.findItem(MENU_DELETE).setVisible(hasSelection);
     70 
     71         return true;
     72     }
     73 
     74     @Override
     75     public boolean onOptionsItemSelected(MenuItem item) {
     76         switch (item.getItemId()) {
     77             case MENU_ADD:
     78                 addContact();
     79                 return true;
     80 
     81             case MENU_EDIT:
     82                 editSelected();
     83                 return true;
     84 
     85             case MENU_DELETE:
     86                 deleteSelected();
     87                 return true;
     88         }
     89 
     90         return super.onOptionsItemSelected(item);
     91     }
     92 
     93     @Override
     94     public void onListItemClick(ListView l, View v, int position, long id) {
     95         // TODO: is this what we really want?
     96         editSelected(position);
     97     }
     98 
     99     private void addContact() {
    100         // if we don't put extras "name" when starting this activity, then
    101         // EditFdnContactScreen treats it like add contact.
    102         Intent intent = new Intent();
    103         intent.setClass(this, EditFdnContactScreen.class);
    104         startActivity(intent);
    105     }
    106 
    107     /**
    108      * Overloaded to call editSelected with the current selection
    109      * by default.  This method may have a problem with touch UI
    110      * since touch UI does not really have a concept of "selected"
    111      * items.
    112      */
    113     private void editSelected() {
    114         editSelected(getSelectedItemPosition());
    115     }
    116 
    117     /**
    118      * Edit the item at the selected position in the list.
    119      */
    120     private void editSelected(int position) {
    121         if (mCursor.moveToPosition(position)) {
    122             String name = mCursor.getString(NAME_COLUMN);
    123             String number = mCursor.getString(NUMBER_COLUMN);
    124 
    125             Intent intent = new Intent();
    126             intent.setClass(this, EditFdnContactScreen.class);
    127             intent.putExtra(INTENT_EXTRA_NAME, name);
    128             intent.putExtra(INTENT_EXTRA_NUMBER, number);
    129             startActivity(intent);
    130         }
    131     }
    132 
    133     private void deleteSelected() {
    134         if (mCursor.moveToPosition(getSelectedItemPosition())) {
    135             String name = mCursor.getString(NAME_COLUMN);
    136             String number = mCursor.getString(NUMBER_COLUMN);
    137 
    138             Intent intent = new Intent();
    139             intent.setClass(this, DeleteFdnContactScreen.class);
    140             intent.putExtra(INTENT_EXTRA_NAME, name);
    141             intent.putExtra(INTENT_EXTRA_NUMBER, number);
    142             startActivity(intent);
    143         }
    144     }
    145 }
    146