1package com.android.phone;
2
3import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
4import com.android.internal.telephony.CommandException;
5import com.android.internal.telephony.CommandsInterface;
6import com.android.internal.telephony.Phone;
7
8import android.content.Context;
9import android.os.AsyncResult;
10import android.os.Handler;
11import android.os.Message;
12import android.os.Parcelable;
13import android.preference.ListPreference;
14import android.util.AttributeSet;
15import android.util.Log;
16
17/**
18 * {@link ListPreference} for CLIR (Calling Line Identification Restriction).
19 * Right now this is used for "Caller ID" setting.
20 */
21public class CLIRListPreference extends ListPreference {
22    private static final String LOG_TAG = "CLIRListPreference";
23    private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
24
25    private final MyHandler mHandler = new MyHandler();
26    private Phone mPhone;
27    private TimeConsumingPreferenceListener mTcpListener;
28
29    int clirArray[];
30
31    public CLIRListPreference(Context context, AttributeSet attrs) {
32        super(context, attrs);
33    }
34
35    public CLIRListPreference(Context context) {
36        this(context, null);
37    }
38
39    @Override
40    protected void onDialogClosed(boolean positiveResult) {
41        super.onDialogClosed(positiveResult);
42
43        mPhone.setOutgoingCallerIdDisplay(findIndexOfValue(getValue()),
44                mHandler.obtainMessage(MyHandler.MESSAGE_SET_CLIR));
45        if (mTcpListener != null) {
46            mTcpListener.onStarted(this, false);
47        }
48    }
49
50    /* package */ void init(
51            TimeConsumingPreferenceListener listener, boolean skipReading, Phone phone) {
52        mPhone = phone;
53        mTcpListener = listener;
54        if (!skipReading) {
55            mPhone.getOutgoingCallerIdDisplay(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CLIR,
56                    MyHandler.MESSAGE_GET_CLIR, MyHandler.MESSAGE_GET_CLIR));
57            if (mTcpListener != null) {
58                mTcpListener.onStarted(this, true);
59            }
60        }
61    }
62
63    /* package */ void handleGetCLIRResult(int tmpClirArray[]) {
64        clirArray = tmpClirArray;
65        final boolean enabled =
66                tmpClirArray[1] == 1 || tmpClirArray[1] == 3 || tmpClirArray[1] == 4;
67        setEnabled(enabled);
68
69        // set the value of the preference based upon the clirArgs.
70        int value = CommandsInterface.CLIR_DEFAULT;
71        switch (tmpClirArray[1]) {
72            case 1: // Permanently provisioned
73            case 3: // Temporary presentation disallowed
74            case 4: // Temporary presentation allowed
75                switch (tmpClirArray[0]) {
76                    case 1: // CLIR invoked
77                        value = CommandsInterface.CLIR_INVOCATION;
78                        break;
79                    case 2: // CLIR suppressed
80                        value = CommandsInterface.CLIR_SUPPRESSION;
81                        break;
82                    case 0: // Network default
83                    default:
84                        value = CommandsInterface.CLIR_DEFAULT;
85                        break;
86                }
87                break;
88            case 0: // Not Provisioned
89            case 2: // Unknown (network error, etc)
90            default:
91                value = CommandsInterface.CLIR_DEFAULT;
92                break;
93        }
94        setValueIndex(value);
95
96        // set the string summary to reflect the value
97        int summary = R.string.sum_default_caller_id;
98        switch (value) {
99            case CommandsInterface.CLIR_SUPPRESSION:
100                summary = R.string.sum_show_caller_id;
101                break;
102            case CommandsInterface.CLIR_INVOCATION:
103                summary = R.string.sum_hide_caller_id;
104                break;
105            case CommandsInterface.CLIR_DEFAULT:
106                summary = R.string.sum_default_caller_id;
107                break;
108        }
109        setSummary(summary);
110    }
111
112    private class MyHandler extends Handler {
113        static final int MESSAGE_GET_CLIR = 0;
114        static final int MESSAGE_SET_CLIR = 1;
115
116        @Override
117        public void handleMessage(Message msg) {
118            switch (msg.what) {
119                case MESSAGE_GET_CLIR:
120                    handleGetCLIRResponse(msg);
121                    break;
122                case MESSAGE_SET_CLIR:
123                    handleSetCLIRResponse(msg);
124                    break;
125            }
126        }
127
128        private void handleGetCLIRResponse(Message msg) {
129            AsyncResult ar = (AsyncResult) msg.obj;
130
131            if (msg.arg2 == MESSAGE_SET_CLIR) {
132                mTcpListener.onFinished(CLIRListPreference.this, false);
133            } else {
134                mTcpListener.onFinished(CLIRListPreference.this, true);
135            }
136            clirArray = null;
137            if (ar.exception != null) {
138                if (DBG) Log.d(LOG_TAG, "handleGetCLIRResponse: ar.exception="+ar.exception);
139                mTcpListener.onException(CLIRListPreference.this, (CommandException) ar.exception);
140            } else if (ar.userObj instanceof Throwable) {
141                mTcpListener.onError(CLIRListPreference.this, RESPONSE_ERROR);
142            } else {
143                int clirArray[] = (int[]) ar.result;
144                if (clirArray.length != 2) {
145                    mTcpListener.onError(CLIRListPreference.this, RESPONSE_ERROR);
146                } else {
147                    if (DBG) {
148                        Log.d(LOG_TAG, "handleGetCLIRResponse: CLIR successfully queried,"
149                                + " clirArray[0]=" + clirArray[0]
150                                + ", clirArray[1]=" + clirArray[1]);
151                    }
152                    handleGetCLIRResult(clirArray);
153                }
154            }
155        }
156
157        private void handleSetCLIRResponse(Message msg) {
158            AsyncResult ar = (AsyncResult) msg.obj;
159
160            if (ar.exception != null) {
161                if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception="+ar.exception);
162                //setEnabled(false);
163            }
164            if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
165
166            mPhone.getOutgoingCallerIdDisplay(obtainMessage(MESSAGE_GET_CLIR,
167                    MESSAGE_SET_CLIR, MESSAGE_SET_CLIR, ar.exception));
168        }
169    }
170}
171