Home | History | Annotate | Download | only in phone
      1 package com.android.phone;
      2 
      3 import com.android.internal.telephony.CallForwardInfo;
      4 import com.android.internal.telephony.CommandsInterface;
      5 
      6 import android.app.ActionBar;
      7 import android.content.Intent;
      8 import android.database.Cursor;
      9 import android.os.Bundle;
     10 import android.preference.Preference;
     11 import android.preference.PreferenceScreen;
     12 import android.provider.ContactsContract.CommonDataKinds.Phone;
     13 import android.util.Log;
     14 import android.view.MenuItem;
     15 
     16 import java.util.ArrayList;
     17 
     18 
     19 public class GsmUmtsCallForwardOptions extends TimeConsumingPreferenceActivity {
     20     private static final String LOG_TAG = "GsmUmtsCallForwardOptions";
     21     private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
     22 
     23     private static final String NUM_PROJECTION[] = {Phone.NUMBER};
     24 
     25     private static final String BUTTON_CFU_KEY   = "button_cfu_key";
     26     private static final String BUTTON_CFB_KEY   = "button_cfb_key";
     27     private static final String BUTTON_CFNRY_KEY = "button_cfnry_key";
     28     private static final String BUTTON_CFNRC_KEY = "button_cfnrc_key";
     29 
     30     private static final String KEY_TOGGLE = "toggle";
     31     private static final String KEY_STATUS = "status";
     32     private static final String KEY_NUMBER = "number";
     33 
     34     private CallForwardEditPreference mButtonCFU;
     35     private CallForwardEditPreference mButtonCFB;
     36     private CallForwardEditPreference mButtonCFNRy;
     37     private CallForwardEditPreference mButtonCFNRc;
     38 
     39     private final ArrayList<CallForwardEditPreference> mPreferences =
     40             new ArrayList<CallForwardEditPreference> ();
     41     private int mInitIndex= 0;
     42 
     43     private boolean mFirstResume;
     44     private Bundle mIcicle;
     45 
     46     @Override
     47     protected void onCreate(Bundle icicle) {
     48         super.onCreate(icicle);
     49 
     50         addPreferencesFromResource(R.xml.callforward_options);
     51 
     52         PreferenceScreen prefSet = getPreferenceScreen();
     53         mButtonCFU   = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFU_KEY);
     54         mButtonCFB   = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFB_KEY);
     55         mButtonCFNRy = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRY_KEY);
     56         mButtonCFNRc = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRC_KEY);
     57 
     58         mButtonCFU.setParentActivity(this, mButtonCFU.reason);
     59         mButtonCFB.setParentActivity(this, mButtonCFB.reason);
     60         mButtonCFNRy.setParentActivity(this, mButtonCFNRy.reason);
     61         mButtonCFNRc.setParentActivity(this, mButtonCFNRc.reason);
     62 
     63         mPreferences.add(mButtonCFU);
     64         mPreferences.add(mButtonCFB);
     65         mPreferences.add(mButtonCFNRy);
     66         mPreferences.add(mButtonCFNRc);
     67 
     68         // we wait to do the initialization until onResume so that the
     69         // TimeConsumingPreferenceActivity dialog can display as it
     70         // relies on onResume / onPause to maintain its foreground state.
     71 
     72         mFirstResume = true;
     73         mIcicle = icicle;
     74 
     75         ActionBar actionBar = getActionBar();
     76         if (actionBar != null) {
     77             // android.R.id.home will be triggered in onOptionsItemSelected()
     78             actionBar.setDisplayHomeAsUpEnabled(true);
     79         }
     80     }
     81 
     82     @Override
     83     public void onResume() {
     84         super.onResume();
     85 
     86         if (mFirstResume) {
     87             if (mIcicle == null) {
     88                 if (DBG) Log.d(LOG_TAG, "start to init ");
     89                 mPreferences.get(mInitIndex).init(this, false);
     90             } else {
     91                 mInitIndex = mPreferences.size();
     92 
     93                 for (CallForwardEditPreference pref : mPreferences) {
     94                     Bundle bundle = mIcicle.getParcelable(pref.getKey());
     95                     pref.setToggled(bundle.getBoolean(KEY_TOGGLE));
     96                     CallForwardInfo cf = new CallForwardInfo();
     97                     cf.number = bundle.getString(KEY_NUMBER);
     98                     cf.status = bundle.getInt(KEY_STATUS);
     99                     pref.handleCallForwardResult(cf);
    100                     pref.init(this, true);
    101                 }
    102             }
    103             mFirstResume = false;
    104             mIcicle=null;
    105         }
    106     }
    107 
    108     @Override
    109     protected void onSaveInstanceState(Bundle outState) {
    110         super.onSaveInstanceState(outState);
    111 
    112         for (CallForwardEditPreference pref : mPreferences) {
    113             Bundle bundle = new Bundle();
    114             bundle.putBoolean(KEY_TOGGLE, pref.isToggled());
    115             if (pref.callForwardInfo != null) {
    116                 bundle.putString(KEY_NUMBER, pref.callForwardInfo.number);
    117                 bundle.putInt(KEY_STATUS, pref.callForwardInfo.status);
    118             }
    119             outState.putParcelable(pref.getKey(), bundle);
    120         }
    121     }
    122 
    123     @Override
    124     public void onFinished(Preference preference, boolean reading) {
    125         if (mInitIndex < mPreferences.size()-1 && !isFinishing()) {
    126             mInitIndex++;
    127             mPreferences.get(mInitIndex).init(this, false);
    128         }
    129 
    130         super.onFinished(preference, reading);
    131     }
    132 
    133     @Override
    134     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    135         if (DBG) Log.d(LOG_TAG, "onActivityResult: done");
    136         if (resultCode != RESULT_OK) {
    137             if (DBG) Log.d(LOG_TAG, "onActivityResult: contact picker result not OK.");
    138             return;
    139         }
    140         Cursor cursor = null;
    141         try {
    142             cursor = getContentResolver().query(data.getData(),
    143                 NUM_PROJECTION, null, null, null);
    144             if ((cursor == null) || (!cursor.moveToFirst())) {
    145                 if (DBG) Log.d(LOG_TAG, "onActivityResult: bad contact data, no results found.");
    146                 return;
    147             }
    148 
    149             switch (requestCode) {
    150                 case CommandsInterface.CF_REASON_UNCONDITIONAL:
    151                     mButtonCFU.onPickActivityResult(cursor.getString(0));
    152                     break;
    153                 case CommandsInterface.CF_REASON_BUSY:
    154                     mButtonCFB.onPickActivityResult(cursor.getString(0));
    155                     break;
    156                 case CommandsInterface.CF_REASON_NO_REPLY:
    157                     mButtonCFNRy.onPickActivityResult(cursor.getString(0));
    158                     break;
    159                 case CommandsInterface.CF_REASON_NOT_REACHABLE:
    160                     mButtonCFNRc.onPickActivityResult(cursor.getString(0));
    161                     break;
    162                 default:
    163                     // TODO: may need exception here.
    164             }
    165         } finally {
    166             if (cursor != null) {
    167                 cursor.close();
    168             }
    169         }
    170     }
    171 
    172     @Override
    173     public boolean onOptionsItemSelected(MenuItem item) {
    174         final int itemId = item.getItemId();
    175         if (itemId == android.R.id.home) {  // See ActionBar#setDisplayHomeAsUpEnabled()
    176             CallFeaturesSetting.goUpToTopLevelSetting(this);
    177             return true;
    178         }
    179         return super.onOptionsItemSelected(item);
    180     }
    181 }
    182