1cd0189299f484dcbd51400b994daf585923e9e8dDavid Krausepackage com.android.phone;
2cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
3cd0189299f484dcbd51400b994daf585923e9e8dDavid Krauseimport android.content.Intent;
4cd0189299f484dcbd51400b994daf585923e9e8dDavid Krauseimport android.database.Cursor;
5cd0189299f484dcbd51400b994daf585923e9e8dDavid Krauseimport android.os.Bundle;
6cd0189299f484dcbd51400b994daf585923e9e8dDavid Krauseimport android.preference.Preference;
7cd0189299f484dcbd51400b994daf585923e9e8dDavid Krauseimport android.preference.PreferenceScreen;
8849bc5bfd940cea4e55332e805e0a7da4a765bcdDmitri Plotnikovimport android.provider.ContactsContract.CommonDataKinds.Phone;
9cd0189299f484dcbd51400b994daf585923e9e8dDavid Krauseimport android.util.Log;
10cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
114a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Parkimport com.android.internal.telephony.CallForwardInfo;
124a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Parkimport com.android.internal.telephony.CommandsInterface;
134a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
144a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Parkimport java.util.ArrayList;
154a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
16cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
17cd0189299f484dcbd51400b994daf585923e9e8dDavid Krausepublic class GsmUmtsCallForwardOptions extends TimeConsumingPreferenceActivity {
18cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String LOG_TAG = "GsmUmtsCallForwardOptions";
19cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
20cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
21849bc5bfd940cea4e55332e805e0a7da4a765bcdDmitri Plotnikov    private static final String NUM_PROJECTION[] = {Phone.NUMBER};
22cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
23cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String BUTTON_CFU_KEY   = "button_cfu_key";
24cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String BUTTON_CFB_KEY   = "button_cfb_key";
25cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String BUTTON_CFNRY_KEY = "button_cfnry_key";
26cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String BUTTON_CFNRC_KEY = "button_cfnrc_key";
27cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
28cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String KEY_TOGGLE = "toggle";
29cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String KEY_STATUS = "status";
30cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private static final String KEY_NUMBER = "number";
31cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
32cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private CallForwardEditPreference mButtonCFU;
33cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private CallForwardEditPreference mButtonCFB;
34cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private CallForwardEditPreference mButtonCFNRy;
35cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private CallForwardEditPreference mButtonCFNRc;
36cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
374a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park    private final ArrayList<CallForwardEditPreference> mPreferences =
38cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            new ArrayList<CallForwardEditPreference> ();
39cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    private int mInitIndex= 0;
404a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
414a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park    private boolean mFirstResume;
424a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park    private Bundle mIcicle;
434a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
44cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    @Override
45cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    protected void onCreate(Bundle icicle) {
46cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        super.onCreate(icicle);
47cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
48cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        addPreferencesFromResource(R.xml.callforward_options);
49cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
50cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        PreferenceScreen prefSet = getPreferenceScreen();
51cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFU   = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFU_KEY);
52cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFB   = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFB_KEY);
53cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFNRy = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRY_KEY);
54cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFNRc = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRC_KEY);
55cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
56cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFU.setParentActivity(this, mButtonCFU.reason);
57cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFB.setParentActivity(this, mButtonCFB.reason);
58cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFNRy.setParentActivity(this, mButtonCFNRy.reason);
59cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mButtonCFNRc.setParentActivity(this, mButtonCFNRc.reason);
60cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
61cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mPreferences.add(mButtonCFU);
62cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mPreferences.add(mButtonCFB);
63cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mPreferences.add(mButtonCFNRy);
64cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        mPreferences.add(mButtonCFNRc);
65cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
664a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park        // we wait to do the initialization until onResume so that the
674a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park        // TimeConsumingPreferenceActivity dialog can display as it
684a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park        // relies on onResume / onPause to maintain its foreground state.
694a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
704a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park        mFirstResume = true;
714a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park        mIcicle = icicle;
724a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park    }
734a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
744a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park    @Override
754a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park    public void onResume() {
764a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park        super.onResume();
774a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
784a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park        if (mFirstResume) {
794a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park            if (mIcicle == null) {
804a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                if (DBG) Log.d(LOG_TAG, "start to init ");
814a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                mPreferences.get(mInitIndex).init(this, false);
824a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park            } else {
834a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                mInitIndex = mPreferences.size();
844a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park
854a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                for (CallForwardEditPreference pref : mPreferences) {
864a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                    Bundle bundle = mIcicle.getParcelable(pref.getKey());
874a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                    pref.setToggled(bundle.getBoolean(KEY_TOGGLE));
884a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                    CallForwardInfo cf = new CallForwardInfo();
894a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                    cf.number = bundle.getString(KEY_NUMBER);
904a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                    cf.status = bundle.getInt(KEY_STATUS);
914a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                    pref.handleCallForwardResult(cf);
924a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                    pref.init(this, true);
934a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park                }
94cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            }
954a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park            mFirstResume = false;
964a0b3738f8a710d2ffc8c49b3ae7e44100c88839Tim Park            mIcicle=null;
97cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        }
98cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    }
99cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
100cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    @Override
101cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    protected void onSaveInstanceState(Bundle outState) {
102cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        super.onSaveInstanceState(outState);
103cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
104cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        for (CallForwardEditPreference pref : mPreferences) {
105cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            Bundle bundle = new Bundle();
106cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            bundle.putBoolean(KEY_TOGGLE, pref.isToggled());
107cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            if (pref.callForwardInfo != null) {
108cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                bundle.putString(KEY_NUMBER, pref.callForwardInfo.number);
109cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                bundle.putInt(KEY_STATUS, pref.callForwardInfo.status);
110cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            }
111cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            outState.putParcelable(pref.getKey(), bundle);
112cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        }
113cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    }
114cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
115cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    @Override
116cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    public void onFinished(Preference preference, boolean reading) {
117948a17dc246520345b81455fdae0e1d628adc263Li Zhe        if (mInitIndex < mPreferences.size()-1 && !isFinishing()) {
118cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            mInitIndex++;
119cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            mPreferences.get(mInitIndex).init(this, false);
120cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        }
121cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
122cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        super.onFinished(preference, reading);
123cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    }
124cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
125cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    @Override
126cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
127cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        if (DBG) Log.d(LOG_TAG, "onActivityResult: done");
128cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        if (resultCode != RESULT_OK) {
129cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            if (DBG) Log.d(LOG_TAG, "onActivityResult: contact picker result not OK.");
130cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            return;
131cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        }
132cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        Cursor cursor = getContentResolver().query(data.getData(),
133cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                NUM_PROJECTION, null, null, null);
134cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        if ((cursor == null) || (!cursor.moveToFirst())) {
135cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            if (DBG) Log.d(LOG_TAG, "onActivityResult: bad contact data, no results found.");
136cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            return;
137cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        }
138cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
139cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        switch (requestCode) {
140cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            case CommandsInterface.CF_REASON_UNCONDITIONAL:
141cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                mButtonCFU.onPickActivityResult(cursor.getString(0));
142cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                break;
143cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            case CommandsInterface.CF_REASON_BUSY:
144cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                mButtonCFB.onPickActivityResult(cursor.getString(0));
145cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                break;
146cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            case CommandsInterface.CF_REASON_NO_REPLY:
147cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                mButtonCFNRy.onPickActivityResult(cursor.getString(0));
148cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                break;
149cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            case CommandsInterface.CF_REASON_NOT_REACHABLE:
150cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                mButtonCFNRc.onPickActivityResult(cursor.getString(0));
151cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                break;
152cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            default:
153cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause                // TODO: may need exception here.
154cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause        }
155cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause    }
156cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause}
157