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