• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.phone;
2 
3 import java.util.ArrayList;
4 
5 import com.android.internal.telephony.CommandsInterface;
6 
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 com.android.internal.telephony.CallForwardInfo;
14 import android.util.Log;
15 
16 
17 public class GsmUmtsCallForwardOptions extends TimeConsumingPreferenceActivity {
18     private static final String LOG_TAG = "GsmUmtsCallForwardOptions";
19     private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
20 
21     private static final String NUM_PROJECTION[] = {Phone.NUMBER};
22 
23     private static final String BUTTON_CFU_KEY   = "button_cfu_key";
24     private static final String BUTTON_CFB_KEY   = "button_cfb_key";
25     private static final String BUTTON_CFNRY_KEY = "button_cfnry_key";
26     private static final String BUTTON_CFNRC_KEY = "button_cfnrc_key";
27 
28     private static final String KEY_TOGGLE = "toggle";
29     private static final String KEY_STATUS = "status";
30     private static final String KEY_NUMBER = "number";
31 
32     private CallForwardEditPreference mButtonCFU;
33     private CallForwardEditPreference mButtonCFB;
34     private CallForwardEditPreference mButtonCFNRy;
35     private CallForwardEditPreference mButtonCFNRc;
36 
37     private ArrayList<CallForwardEditPreference> mPreferences =
38             new ArrayList<CallForwardEditPreference> ();
39     private int mInitIndex= 0;
40     @Override
onCreate(Bundle icicle)41     protected void onCreate(Bundle icicle) {
42         super.onCreate(icicle);
43 
44         addPreferencesFromResource(R.xml.callforward_options);
45 
46         PreferenceScreen prefSet = getPreferenceScreen();
47         mButtonCFU   = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFU_KEY);
48         mButtonCFB   = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFB_KEY);
49         mButtonCFNRy = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRY_KEY);
50         mButtonCFNRc = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRC_KEY);
51 
52         mButtonCFU.setParentActivity(this, mButtonCFU.reason);
53         mButtonCFB.setParentActivity(this, mButtonCFB.reason);
54         mButtonCFNRy.setParentActivity(this, mButtonCFNRy.reason);
55         mButtonCFNRc.setParentActivity(this, mButtonCFNRc.reason);
56 
57         mPreferences.add(mButtonCFU);
58         mPreferences.add(mButtonCFB);
59         mPreferences.add(mButtonCFNRy);
60         mPreferences.add(mButtonCFNRc);
61 
62         if (icicle == null) {
63             if (DBG) Log.d(LOG_TAG, "start to init ");
64             mPreferences.get(mInitIndex).init(this, false);
65         } else {
66             mInitIndex = mPreferences.size();
67 
68             for (CallForwardEditPreference pref : mPreferences) {
69                 Bundle bundle = icicle.getParcelable(pref.getKey());
70                 pref.setToggled(bundle.getBoolean(KEY_TOGGLE));
71                 CallForwardInfo cf = new CallForwardInfo();
72                 cf.number = bundle.getString(KEY_NUMBER);
73                 cf.status = bundle.getInt(KEY_STATUS);
74                 pref.handleCallForwardResult(cf);
75                 pref.init(this, true);
76             }
77         }
78     }
79 
80     @Override
onSaveInstanceState(Bundle outState)81     protected void onSaveInstanceState(Bundle outState) {
82         super.onSaveInstanceState(outState);
83 
84         for (CallForwardEditPreference pref : mPreferences) {
85             Bundle bundle = new Bundle();
86             bundle.putBoolean(KEY_TOGGLE, pref.isToggled());
87             if (pref.callForwardInfo != null) {
88                 bundle.putString(KEY_NUMBER, pref.callForwardInfo.number);
89                 bundle.putInt(KEY_STATUS, pref.callForwardInfo.status);
90             }
91             outState.putParcelable(pref.getKey(), bundle);
92         }
93     }
94 
95     @Override
onFinished(Preference preference, boolean reading)96     public void onFinished(Preference preference, boolean reading) {
97         if (mInitIndex < mPreferences.size()-1 && !isFinishing()) {
98             mInitIndex++;
99             mPreferences.get(mInitIndex).init(this, false);
100         }
101 
102         super.onFinished(preference, reading);
103     }
104 
105     @Override
onActivityResult(int requestCode, int resultCode, Intent data)106     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
107         if (DBG) Log.d(LOG_TAG, "onActivityResult: done");
108         if (resultCode != RESULT_OK) {
109             if (DBG) Log.d(LOG_TAG, "onActivityResult: contact picker result not OK.");
110             return;
111         }
112         Cursor cursor = getContentResolver().query(data.getData(),
113                 NUM_PROJECTION, null, null, null);
114         if ((cursor == null) || (!cursor.moveToFirst())) {
115             if (DBG) Log.d(LOG_TAG, "onActivityResult: bad contact data, no results found.");
116             return;
117         }
118 
119         switch (requestCode) {
120             case CommandsInterface.CF_REASON_UNCONDITIONAL:
121                 mButtonCFU.onPickActivityResult(cursor.getString(0));
122                 break;
123             case CommandsInterface.CF_REASON_BUSY:
124                 mButtonCFB.onPickActivityResult(cursor.getString(0));
125                 break;
126             case CommandsInterface.CF_REASON_NO_REPLY:
127                 mButtonCFNRy.onPickActivityResult(cursor.getString(0));
128                 break;
129             case CommandsInterface.CF_REASON_NOT_REACHABLE:
130                 mButtonCFNRc.onPickActivityResult(cursor.getString(0));
131                 break;
132             default:
133                 // TODO: may need exception here.
134         }
135     }
136 }
137