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