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