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