1 package com.android.phone;
2 
3 import android.app.ActionBar;
4 import android.content.Intent;
5 import android.database.Cursor;
6 import android.os.Bundle;
7 import android.os.PersistableBundle;
8 import android.preference.Preference;
9 import android.preference.PreferenceScreen;
10 import android.telephony.CarrierConfigManager;
11 import android.util.Log;
12 import android.view.MenuItem;
13 
14 import com.android.internal.telephony.CallForwardInfo;
15 import com.android.internal.telephony.CommandsInterface;
16 import com.android.internal.telephony.Phone;
17 
18 import java.util.ArrayList;
19 
20 public class GsmUmtsCallForwardOptions extends TimeConsumingPreferenceActivity {
21     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
22     private static final String LOG_TAG = "GsmUmtsCallForwardOptions";
23 
24     private static final String NUM_PROJECTION[] = {
25         android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER
26     };
27 
28     private static final String BUTTON_CFU_KEY   = "button_cfu_key";
29     private static final String BUTTON_CFB_KEY   = "button_cfb_key";
30     private static final String BUTTON_CFNRY_KEY = "button_cfnry_key";
31     private static final String BUTTON_CFNRC_KEY = "button_cfnrc_key";
32 
33     private static final String KEY_TOGGLE = "toggle";
34     private static final String KEY_STATUS = "status";
35     private static final String KEY_NUMBER = "number";
36     private static final String KEY_ENABLE = "enable";
37 
38     private CallForwardEditPreference mButtonCFU;
39     private CallForwardEditPreference mButtonCFB;
40     private CallForwardEditPreference mButtonCFNRy;
41     private CallForwardEditPreference mButtonCFNRc;
42 
43     private final ArrayList<CallForwardEditPreference> mPreferences =
44             new ArrayList<CallForwardEditPreference> ();
45     private int mInitIndex= 0;
46 
47     private boolean mFirstResume;
48     private Bundle mIcicle;
49     private Phone mPhone;
50     private SubscriptionInfoHelper mSubscriptionInfoHelper;
51     private boolean mReplaceInvalidCFNumbers;
52     private boolean mCallForwardByUssd;
53 
54     @Override
onCreate(Bundle icicle)55     protected void onCreate(Bundle icicle) {
56         super.onCreate(icicle);
57 
58         addPreferencesFromResource(R.xml.callforward_options);
59 
60         mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
61         mSubscriptionInfoHelper.setActionBarTitle(
62                 getActionBar(), getResources(), R.string.call_forwarding_settings_with_label);
63         mPhone = mSubscriptionInfoHelper.getPhone();
64 
65         PersistableBundle b = null;
66         boolean supportCFB = true;
67         boolean supportCFNRc = true;
68         boolean supportCFNRy = true;
69         if (mSubscriptionInfoHelper.hasSubId()) {
70             b = PhoneGlobals.getInstance().getCarrierConfigForSubId(
71                     mSubscriptionInfoHelper.getSubId());
72         } else {
73             b = PhoneGlobals.getInstance().getCarrierConfig();
74         }
75         if (b != null) {
76             mReplaceInvalidCFNumbers = b.getBoolean(
77                     CarrierConfigManager.KEY_CALL_FORWARDING_MAP_NON_NUMBER_TO_VOICEMAIL_BOOL);
78             mCallForwardByUssd = b.getBoolean(
79                     CarrierConfigManager.KEY_USE_CALL_FORWARDING_USSD_BOOL);
80             supportCFB = b.getBoolean(
81                     CarrierConfigManager.KEY_CALL_FORWARDING_WHEN_BUSY_SUPPORTED_BOOL);
82             supportCFNRc = b.getBoolean(
83                     CarrierConfigManager.KEY_CALL_FORWARDING_WHEN_UNREACHABLE_SUPPORTED_BOOL);
84             supportCFNRy = b.getBoolean(
85                     CarrierConfigManager.KEY_CALL_FORWARDING_WHEN_UNANSWERED_SUPPORTED_BOOL);
86         }
87 
88         PreferenceScreen prefSet = getPreferenceScreen();
89         mButtonCFU = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFU_KEY);
90         mButtonCFB = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFB_KEY);
91         mButtonCFNRy = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRY_KEY);
92         mButtonCFNRc = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRC_KEY);
93 
94         mButtonCFU.setParentActivity(this, mButtonCFU.reason);
95         mButtonCFB.setParentActivity(this, mButtonCFB.reason);
96         mButtonCFNRy.setParentActivity(this, mButtonCFNRy.reason);
97         mButtonCFNRc.setParentActivity(this, mButtonCFNRc.reason);
98 
99         mPreferences.add(mButtonCFU);
100         layoutCallForwardItem(supportCFB, mButtonCFB, prefSet);
101         layoutCallForwardItem(supportCFNRy, mButtonCFNRy, prefSet);
102         layoutCallForwardItem(supportCFNRc, mButtonCFNRc, prefSet);
103 
104         if (mCallForwardByUssd) {
105             //the call forwarding ussd command's behavior is similar to the call forwarding when
106             //unanswered,so only display the call forwarding when unanswered item.
107             prefSet.removePreference(mButtonCFU);
108             prefSet.removePreference(mButtonCFB);
109             prefSet.removePreference(mButtonCFNRc);
110             mPreferences.remove(mButtonCFU);
111             mPreferences.remove(mButtonCFB);
112             mPreferences.remove(mButtonCFNRc);
113             mButtonCFNRy.setDependency(null);
114         }
115 
116         // we wait to do the initialization until onResume so that the
117         // TimeConsumingPreferenceActivity dialog can display as it
118         // relies on onResume / onPause to maintain its foreground state.
119 
120         mFirstResume = true;
121         mIcicle = icicle;
122 
123         ActionBar actionBar = getActionBar();
124         if (actionBar != null) {
125             // android.R.id.home will be triggered in onOptionsItemSelected()
126             actionBar.setDisplayHomeAsUpEnabled(true);
127         }
128     }
129 
layoutCallForwardItem(boolean support, CallForwardEditPreference preference, PreferenceScreen prefSet)130     private void layoutCallForwardItem(boolean support, CallForwardEditPreference preference,
131             PreferenceScreen prefSet) {
132         if (support) {
133             mPreferences.add(preference);
134         } else {
135             prefSet.removePreference(preference);
136         }
137     }
138 
139     @Override
onResume()140     public void onResume() {
141         super.onResume();
142 
143         if (mFirstResume) {
144             if (mIcicle == null) {
145                 Log.d(LOG_TAG, "start to init ");
146                 CallForwardEditPreference pref = mPreferences.get(mInitIndex);
147                 pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd);
148                 pref.startCallForwardOptionsQuery();
149 
150             } else {
151                 mInitIndex = mPreferences.size();
152 
153                 for (CallForwardEditPreference pref : mPreferences) {
154                     Bundle bundle = mIcicle.getParcelable(pref.getKey());
155                     pref.setToggled(bundle.getBoolean(KEY_TOGGLE));
156                     pref.setEnabled(bundle.getBoolean(KEY_ENABLE));
157                     CallForwardInfo cf = new CallForwardInfo();
158                     cf.number = bundle.getString(KEY_NUMBER);
159                     cf.status = bundle.getInt(KEY_STATUS);
160                     pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd);
161                     pref.restoreCallForwardInfo(cf);
162                 }
163             }
164             mFirstResume = false;
165             mIcicle = null;
166         }
167     }
168 
169     @Override
onSaveInstanceState(Bundle outState)170     protected void onSaveInstanceState(Bundle outState) {
171         super.onSaveInstanceState(outState);
172 
173         for (CallForwardEditPreference pref : mPreferences) {
174             Bundle bundle = new Bundle();
175             bundle.putBoolean(KEY_TOGGLE, pref.isToggled());
176             bundle.putBoolean(KEY_ENABLE, pref.isEnabled());
177             if (pref.callForwardInfo != null) {
178                 bundle.putString(KEY_NUMBER, pref.callForwardInfo.number);
179                 bundle.putInt(KEY_STATUS, pref.callForwardInfo.status);
180             }
181             outState.putParcelable(pref.getKey(), bundle);
182         }
183     }
184 
185     @Override
onFinished(Preference preference, boolean reading)186     public void onFinished(Preference preference, boolean reading) {
187         if (mInitIndex < mPreferences.size()-1 && !isFinishing()) {
188             mInitIndex++;
189             CallForwardEditPreference pref = mPreferences.get(mInitIndex);
190             pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd);
191             pref.startCallForwardOptionsQuery();
192         }
193 
194         super.onFinished(preference, reading);
195     }
196 
197     @Override
onActivityResult(int requestCode, int resultCode, Intent data)198     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
199         Log.d(LOG_TAG, "onActivityResult: done");
200         if (resultCode != RESULT_OK) {
201             Log.d(LOG_TAG, "onActivityResult: contact picker result not OK.");
202             return;
203         }
204         Cursor cursor = null;
205         try {
206             cursor = getContentResolver().query(data.getData(),
207                 NUM_PROJECTION, null, null, null);
208             if ((cursor == null) || (!cursor.moveToFirst())) {
209                 Log.d(LOG_TAG, "onActivityResult: bad contact data, no results found.");
210                 return;
211             }
212 
213             switch (requestCode) {
214                 case CommandsInterface.CF_REASON_UNCONDITIONAL:
215                     mButtonCFU.onPickActivityResult(cursor.getString(0));
216                     break;
217                 case CommandsInterface.CF_REASON_BUSY:
218                     mButtonCFB.onPickActivityResult(cursor.getString(0));
219                     break;
220                 case CommandsInterface.CF_REASON_NO_REPLY:
221                     mButtonCFNRy.onPickActivityResult(cursor.getString(0));
222                     break;
223                 case CommandsInterface.CF_REASON_NOT_REACHABLE:
224                     mButtonCFNRc.onPickActivityResult(cursor.getString(0));
225                     break;
226                 default:
227                     // TODO: may need exception here.
228             }
229         } finally {
230             if (cursor != null) {
231                 cursor.close();
232             }
233         }
234     }
235 
236     @Override
onOptionsItemSelected(MenuItem item)237     public boolean onOptionsItemSelected(MenuItem item) {
238         final int itemId = item.getItemId();
239         if (itemId == android.R.id.home) {  // See ActionBar#setDisplayHomeAsUpEnabled()
240             CallFeaturesSetting.goUpToTopLevelSetting(this, mSubscriptionInfoHelper);
241             return true;
242         }
243         return super.onOptionsItemSelected(item);
244     }
245 }
246