• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.phone;
2 
3 import com.android.internal.telephony.CallForwardInfo;
4 import com.android.internal.telephony.CommandsInterface;
5 import com.android.internal.telephony.Phone;
6 import com.android.internal.telephony.PhoneFactory;
7 
8 import android.content.Context;
9 import android.content.DialogInterface;
10 import android.content.res.TypedArray;
11 import android.os.AsyncResult;
12 import android.os.Handler;
13 import android.os.Message;
14 import android.telephony.PhoneNumberUtils;
15 import android.text.TextUtils;
16 import android.util.AttributeSet;
17 import android.util.Log;
18 
19 import static com.android.phone.TimeConsumingPreferenceActivity.EXCEPTION_ERROR;
20 import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
21 
22 public class CallForwardEditPreference extends EditPhoneNumberPreference {
23     private static final String LOG_TAG = "CallForwardEditPreference";
24     private static final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
25 
26     private static final String SRC_TAGS[]       = {"{0}"};
27     private CharSequence mSummaryOnTemplate;
28     private int mButtonClicked;
29     private int mServiceClass;
30     private MyHandler mHandler = new MyHandler();
31     int reason;
32     Phone phone;
33     CallForwardInfo callForwardInfo;
34     TimeConsumingPreferenceListener tcpListener;
35 
CallForwardEditPreference(Context context, AttributeSet attrs)36     public CallForwardEditPreference(Context context, AttributeSet attrs) {
37         super(context, attrs);
38 
39         phone = PhoneFactory.getDefaultPhone();
40         mSummaryOnTemplate = this.getSummaryOn();
41 
42         TypedArray a = context.obtainStyledAttributes(attrs,
43                 R.styleable.CallForwardEditPreference, 0, R.style.EditPhoneNumberPreference);
44         mServiceClass = a.getInt(R.styleable.CallForwardEditPreference_serviceClass,
45                 CommandsInterface.SERVICE_CLASS_VOICE);
46         reason = a.getInt(R.styleable.CallForwardEditPreference_reason,
47                 CommandsInterface.CF_REASON_UNCONDITIONAL);
48         a.recycle();
49 
50         if (DBG) Log.d(LOG_TAG, "mServiceClass=" + mServiceClass + ", reason=" + reason);
51     }
52 
CallForwardEditPreference(Context context)53     public CallForwardEditPreference(Context context) {
54         this(context, null);
55     }
56 
init(TimeConsumingPreferenceListener listener, boolean skipReading)57     void init(TimeConsumingPreferenceListener listener, boolean skipReading) {
58         tcpListener = listener;
59         if (!skipReading) {
60             phone.getCallForwardingOption(reason,
61                     mHandler.obtainMessage(MyHandler.MESSAGE_GET_CF, reason,
62                     MyHandler.MESSAGE_GET_CF, null));
63             if (tcpListener != null) {
64                 tcpListener.onStarted(this, true);
65             }
66         }
67     }
68 
69     @Override
onClick(DialogInterface dialog, int which)70     public void onClick(DialogInterface dialog, int which) {
71         super.onClick(dialog, which);
72         mButtonClicked = which;
73     }
74 
75     @Override
onDialogClosed(boolean positiveResult)76     protected void onDialogClosed(boolean positiveResult) {
77         super.onDialogClosed(positiveResult);
78 
79         if (DBG) Log.d(LOG_TAG, "mButtonClicked=" + mButtonClicked
80                 + ", positiveResult=" + positiveResult);
81         if (this.mButtonClicked != DialogInterface.BUTTON_NEGATIVE) {
82             int action = (isToggled() || (mButtonClicked == DialogInterface.BUTTON_POSITIVE)) ?
83                     CommandsInterface.CF_ACTION_REGISTRATION :
84                     CommandsInterface.CF_ACTION_DISABLE;
85             int time = (reason != CommandsInterface.CF_REASON_NO_REPLY) ? 0 : 20;
86             String number = PhoneNumberUtils.stripSeparators(getPhoneNumber());
87 
88             if (DBG) Log.d(LOG_TAG, "callForwardInfo=" + callForwardInfo);
89 
90             if (action == CommandsInterface.CF_ACTION_REGISTRATION
91                     && callForwardInfo != null
92                     && number.equals(callForwardInfo.number)) {
93                 // no change, do nothing
94                 if (DBG) Log.d(LOG_TAG, "no change, do nothing");
95             } else {
96                 // set to network
97                 if (DBG) Log.d(LOG_TAG, "reason=" + reason + ", action=" + action
98                         + ", number=" + number);
99                 setSummaryOn("");
100                 // the interface of Phone.setCallForwardingOption has error:
101                 // should be action, reason...
102                 phone.setCallForwardingOption(action,
103                         reason,
104                         number,
105                         time,
106                         mHandler.obtainMessage(MyHandler.MESSAGE_SET_CF));
107 
108                 if (tcpListener != null) {
109                     tcpListener.onStarted(this, false);
110                 }
111             }
112         }
113     }
114 
handleCallForwardResult(CallForwardInfo cf)115     void handleCallForwardResult(CallForwardInfo cf) {
116         callForwardInfo = cf;
117         if (DBG) Log.d(LOG_TAG, "handleGetCFResponse done, callForwardInfo=" + callForwardInfo);
118 
119         CharSequence summaryOn = "";
120         boolean active = (callForwardInfo.status == 1);
121         if (active) {
122             if (callForwardInfo.number != null) {
123                 String values[] = {callForwardInfo.number};
124                 summaryOn = TextUtils.replace(mSummaryOnTemplate, SRC_TAGS, values);
125             }
126             setSummaryOn(summaryOn);
127         }
128 
129         setToggled(active);
130         setPhoneNumber(callForwardInfo.number);
131     }
132 
133     private class MyHandler extends Handler {
134         private static final int MESSAGE_GET_CF = 0;
135         private static final int MESSAGE_SET_CF = 1;
136 
137         @Override
handleMessage(Message msg)138         public void handleMessage(Message msg) {
139             switch (msg.what) {
140                 case MESSAGE_GET_CF:
141                     handleGetCFResponse(msg);
142                     break;
143                 case MESSAGE_SET_CF:
144                     handleSetCFResponse(msg);
145                     break;
146             }
147         }
148 
handleGetCFResponse(Message msg)149         private void handleGetCFResponse(Message msg) {
150             if (DBG) Log.d(LOG_TAG, "handleGetCFResponse: done");
151 
152             if (msg.arg2 == MESSAGE_SET_CF) {
153                 tcpListener.onFinished(CallForwardEditPreference.this, false);
154             } else {
155                 tcpListener.onFinished(CallForwardEditPreference.this, true);
156             }
157 
158             AsyncResult ar = (AsyncResult) msg.obj;
159 
160             callForwardInfo = null;
161             if (ar.exception != null) {
162                 if (DBG) Log.d(LOG_TAG, "handleGetCFResponse: ar.exception=" + ar.exception);
163                 setEnabled(false);
164                 tcpListener.onError(CallForwardEditPreference.this, EXCEPTION_ERROR);
165             } else {
166                 if (ar.userObj instanceof Throwable) {
167                     tcpListener.onError(CallForwardEditPreference.this, RESPONSE_ERROR);
168                 }
169                 CallForwardInfo cfInfoArray[] = (CallForwardInfo[]) ar.result;
170                 if (cfInfoArray.length == 0) {
171                     if (DBG) Log.d(LOG_TAG, "handleGetCFResponse: cfInfoArray.length==0");
172                     setEnabled(false);
173                     tcpListener.onError(CallForwardEditPreference.this, RESPONSE_ERROR);
174                 } else {
175                     for (int i = 0, length = cfInfoArray.length; i < length; i++) {
176                         if (DBG) Log.d(LOG_TAG, "handleGetCFResponse, cfInfoArray[" + i + "]="
177                                 + cfInfoArray[i]);
178                         if ((mServiceClass & cfInfoArray[i].serviceClass) != 0) {
179                             // corresponding class
180                             handleCallForwardResult(cfInfoArray[i]);
181                         }
182                     }
183                 }
184             }
185         }
186 
handleSetCFResponse(Message msg)187         private void handleSetCFResponse(Message msg) {
188             AsyncResult ar = (AsyncResult) msg.obj;
189 
190             if (ar.exception != null) {
191                 if (DBG) Log.d(LOG_TAG, "handleSetCFResponse: ar.exception=" + ar.exception);
192                 // setEnabled(false);
193             }
194             if (DBG) Log.d(LOG_TAG, "handleSetCFResponse: re get");
195             phone.getCallForwardingOption(reason,
196                     obtainMessage(MESSAGE_GET_CF, reason, MESSAGE_SET_CF, ar.exception));
197         }
198     }
199 }
200