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