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.Context; 21 import android.content.Intent; 22 import android.content.res.Resources; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.os.PersistableBundle; 26 import android.telecom.PhoneAccount; 27 import android.telephony.CarrierConfigManager; 28 import android.telephony.SubscriptionManager; 29 import android.text.TextUtils; 30 import android.view.Gravity; 31 import android.view.Menu; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.widget.ListView; 35 import android.widget.PopupMenu; 36 37 import com.android.phone.ADNList; 38 import com.android.phone.PhoneGlobals; 39 import com.android.phone.R; 40 import com.android.phone.SubscriptionInfoHelper; 41 42 /** 43 * Fixed Dialing Number (FDN) List UI for the Phone app. FDN is a feature of the service provider 44 * that allows a user to specify a limited set of phone numbers that the SIM can dial. 45 */ 46 public class FdnList extends ADNList { 47 private class SelectionPopupMenu extends PopupMenu { 48 private OnMenuItemClickListener mMenuItemListener = new OnMenuItemClickListener() { 49 50 @Override 51 public boolean onMenuItemClick(MenuItem item) { 52 if (item.getItemId() == MENU_EDIT) { 53 editSelected(position); 54 } else if (item.getItemId() == MENU_DELETE) { 55 deleteSelected(position); 56 } else if (item.getItemId() == MENU_DIAL) { 57 dialSelected(position); 58 } 59 return true; 60 } 61 }; 62 63 private final int position; 64 SelectionPopupMenu(Context context, View anchor, int position)65 public SelectionPopupMenu(Context context, View anchor, int position) { 66 super(context, anchor, Gravity.RIGHT); 67 this.position = position; 68 } 69 showPopUp()70 public void showPopUp() { 71 getMenu().add(0, MENU_EDIT, 0, getString(R.string.menu_edit)); 72 getMenu().add(0, MENU_DELETE, 0, getString(R.string.menu_delete)); 73 if (mFdnDialDirectlySupported) { 74 getMenu().add(0, MENU_DIAL, 0, getString(R.string.menu_dial)); 75 } 76 setOnMenuItemClickListener(mMenuItemListener); 77 show(); 78 } 79 } 80 81 private static final int MENU_ADD = 1; 82 private static final int MENU_EDIT = 2; 83 private static final int MENU_DELETE = 3; 84 private static final int MENU_DIAL = 4; 85 86 private static final String INTENT_EXTRA_NAME = "name"; 87 private static final String INTENT_EXTRA_NUMBER = "number"; 88 89 private static final Uri FDN_CONTENT_URI = Uri.parse("content://icc/fdn"); 90 private static final String FDN_CONTENT_PATH_WITH_SUB_ID = "content://icc/fdn/subId/"; 91 92 private SubscriptionInfoHelper mSubscriptionInfoHelper; 93 94 private boolean mFdnDialDirectlySupported = false; 95 private SelectionPopupMenu mPopup; 96 97 @Override onCreate(Bundle icicle)98 public void onCreate(Bundle icicle) { 99 super.onCreate(icicle); 100 101 ActionBar actionBar = getActionBar(); 102 if (actionBar != null) { 103 // android.R.id.home will be triggered in onOptionsItemSelected() 104 actionBar.setDisplayHomeAsUpEnabled(true); 105 } 106 107 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent()); 108 mSubscriptionInfoHelper.setActionBarTitle( 109 getActionBar(), getResources(), R.string.fdn_list_with_label); 110 } 111 112 @Override onResume()113 protected void onResume() { 114 super.onResume(); 115 mFdnDialDirectlySupported = getFdnDialDirectlySupported(); 116 } 117 118 @Override onStop()119 protected void onStop() { 120 super.onStop(); 121 if (mPopup != null) { 122 mPopup.dismiss(); 123 } 124 } 125 126 @Override resolveIntent()127 protected Uri resolveIntent() { 128 Intent intent = getIntent(); 129 intent.setData(getContentUri(mSubscriptionInfoHelper)); 130 return intent.getData(); 131 } 132 133 @Override onCreateOptionsMenu(Menu menu)134 public boolean onCreateOptionsMenu(Menu menu) { 135 super.onCreateOptionsMenu(menu); 136 137 Resources r = getResources(); 138 139 // Added the icons to the context menu 140 menu.add(0, MENU_ADD, 0, r.getString(R.string.menu_add)) 141 .setIcon(android.R.drawable.ic_menu_add); 142 menu.add(0, MENU_EDIT, 0, r.getString(R.string.menu_edit)) 143 .setIcon(android.R.drawable.ic_menu_edit); 144 menu.add(0, MENU_DELETE, 0, r.getString(R.string.menu_delete)) 145 .setIcon(android.R.drawable.ic_menu_delete); 146 menu.add(0, MENU_DIAL, 0, r.getString(R.string.menu_dial)); 147 return true; 148 } 149 150 @Override onPrepareOptionsMenu(Menu menu)151 public boolean onPrepareOptionsMenu(Menu menu) { 152 super.onPrepareOptionsMenu(menu); 153 boolean hasSelection = (getSelectedItemPosition() >= 0); 154 155 menu.findItem(MENU_ADD).setVisible(true); 156 menu.findItem(MENU_EDIT).setVisible(hasSelection); 157 menu.findItem(MENU_DELETE).setVisible(hasSelection); 158 menu.findItem(MENU_DIAL).setVisible(hasSelection && mFdnDialDirectlySupported); 159 160 return true; 161 } 162 163 @Override onOptionsItemSelected(MenuItem item)164 public boolean onOptionsItemSelected(MenuItem item) { 165 switch (item.getItemId()) { 166 case android.R.id.home: // See ActionBar#setDisplayHomeAsUpEnabled() 167 Intent intent = mSubscriptionInfoHelper.getIntent(FdnSetting.class); 168 intent.setAction(Intent.ACTION_MAIN); 169 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 170 startActivity(intent); 171 finish(); 172 return true; 173 174 case MENU_ADD: 175 addContact(); 176 return true; 177 178 case MENU_EDIT: 179 editSelected(); 180 return true; 181 182 case MENU_DELETE: 183 deleteSelected(); 184 return true; 185 186 case MENU_DIAL: 187 dialSelected(); 188 return true; 189 } 190 191 return super.onOptionsItemSelected(item); 192 } 193 194 @Override onListItemClick(ListView l, View v, int position, long id)195 public void onListItemClick(ListView l, View v, int position, long id) { 196 mPopup = new SelectionPopupMenu(this, v, position); 197 mPopup.showPopUp(); 198 } 199 addContact()200 private void addContact() { 201 //If there is no INTENT_EXTRA_NAME provided, EditFdnContactScreen treats it as an "add". 202 Intent intent = mSubscriptionInfoHelper.getIntent(EditFdnContactScreen.class); 203 startActivity(intent); 204 } 205 206 /** 207 * Overloaded to call editSelected with the current selection 208 * by default. This method may have a problem with touch UI 209 * since touch UI does not really have a concept of "selected" 210 * items. 211 */ editSelected()212 private void editSelected() { 213 editSelected(getSelectedItemPosition()); 214 } 215 216 /** 217 * Edit the item at the selected position in the list. 218 */ editSelected(int position)219 private void editSelected(int position) { 220 if (mCursor.moveToPosition(position)) { 221 String name = mCursor.getString(NAME_COLUMN); 222 String number = mCursor.getString(NUMBER_COLUMN); 223 224 Intent intent = mSubscriptionInfoHelper.getIntent(EditFdnContactScreen.class); 225 intent.putExtra(INTENT_EXTRA_NAME, name); 226 intent.putExtra(INTENT_EXTRA_NUMBER, number); 227 startActivity(intent); 228 } 229 } 230 deleteSelected()231 private void deleteSelected() { 232 deleteSelected(getSelectedItemPosition()); 233 } 234 deleteSelected(int position)235 private void deleteSelected(int position) { 236 if (mCursor.moveToPosition(position)) { 237 String name = mCursor.getString(NAME_COLUMN); 238 String number = mCursor.getString(NUMBER_COLUMN); 239 240 Intent intent = mSubscriptionInfoHelper.getIntent(DeleteFdnContactScreen.class); 241 intent.putExtra(INTENT_EXTRA_NAME, name); 242 intent.putExtra(INTENT_EXTRA_NUMBER, number); 243 startActivity(intent); 244 } 245 } 246 dialSelected()247 private void dialSelected() { 248 dialSelected(getSelectedItemPosition()); 249 } 250 dialSelected(int position)251 private void dialSelected(int position) { 252 if (mCursor.moveToPosition(position)) { 253 String number = mCursor.getString(NUMBER_COLUMN); 254 if (!TextUtils.isEmpty(number)) { 255 Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null); 256 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, uri); 257 startActivity(intent); 258 } 259 } 260 } 261 262 /** 263 * Returns the uri for updating the ICC FDN entry, taking into account the subscription id. 264 */ getContentUri(SubscriptionInfoHelper subscriptionInfoHelper)265 public static Uri getContentUri(SubscriptionInfoHelper subscriptionInfoHelper) { 266 return subscriptionInfoHelper.hasSubId() 267 ? Uri.parse(FDN_CONTENT_PATH_WITH_SUB_ID + subscriptionInfoHelper.getSubId()) 268 : FDN_CONTENT_URI; 269 } 270 271 /* 272 * Get the config of whether dialing FDN number from FDN list directly is supported 273 * from carrier config manager. 274 * 275 * @return boolean value of the config 276 */ getFdnDialDirectlySupported()277 private boolean getFdnDialDirectlySupported() { 278 int subId = mSubscriptionInfoHelper.hasSubId() 279 ? mSubscriptionInfoHelper.getSubId() 280 : SubscriptionManager.getDefaultSubscriptionId(); 281 PersistableBundle carrierConfig = 282 PhoneGlobals.getInstance().getCarrierConfigForSubId(subId); 283 return carrierConfig.getBoolean(CarrierConfigManager.KEY_SUPPORT_DIRECT_FDN_DIALING_BOOL); 284 } 285 } 286