• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.phone;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.os.AsyncResult;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.preference.Preference;
26 import android.util.AttributeSet;
27 import android.util.Log;
28 
29 import com.android.internal.telephony.CommandException;
30 import com.android.internal.telephony.CommandsInterface;
31 import com.android.internal.telephony.Phone;
32 
33 public class CdmaCallWaitingPreference extends Preference {
34     private static final String LOG_TAG = "CdmaCallWaitingPreference";
35     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
36 
37     private Context mContext;
38     private Phone mPhone;
39     private TimeConsumingPreferenceListener mTcpListener;
40     private MyHandler mHandler = new MyHandler();
41     private boolean mIsActionAvailable = true;
42 
CdmaCallWaitingPreference(Context context, AttributeSet attrs, int defStyleAttr)43     public CdmaCallWaitingPreference(Context context, AttributeSet attrs, int defStyleAttr) {
44         super(context, attrs, defStyleAttr);
45         mContext = context;
46     }
47 
CdmaCallWaitingPreference(Context context, AttributeSet attrs)48     public CdmaCallWaitingPreference(Context context, AttributeSet attrs) {
49         this(context, attrs, com.android.internal.R.attr.preferenceStyle);
50     }
51 
CdmaCallWaitingPreference(Context context)52     public CdmaCallWaitingPreference(Context context) {
53         this(context, null);
54     }
55 
init(TimeConsumingPreferenceListener listener, Phone phone)56     public void init(TimeConsumingPreferenceListener listener, Phone phone) {
57         mPhone = phone;
58         mTcpListener = listener;
59         Log.d(LOG_TAG, "phone id= " + mPhone.getPhoneId());
60         mPhone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING,
61                     MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING));
62         if (mTcpListener != null) {
63             mTcpListener.onStarted(this, true);
64         }
65     }
66 
67     /**
68      * Enables this preference if Call waiting is available in the platform. If not, this will
69      * override all attempts to enable the preference from the associated
70      * TimeConsumingPreferenceActivity.
71      */
setActionAvailable(boolean isAvailable)72     public void setActionAvailable(boolean isAvailable) {
73         mIsActionAvailable = isAvailable;
74         super.setEnabled(mIsActionAvailable);
75     }
76 
77     @Override
onClick()78     public void onClick() {
79         super.onClick();
80 
81         AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
82         builder.setTitle(mContext.getText(R.string.cdma_call_waiting));
83         builder.setMessage(mContext.getText(R.string.enable_cdma_call_waiting_setting));
84         builder.setPositiveButton(R.string.enable_cdma_cw, new DialogInterface.OnClickListener() {
85             public void onClick(DialogInterface dialog, int whichButton) {
86                 mPhone.setCallWaiting(true,
87                         mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING));
88                 if (mTcpListener != null) {
89                     mTcpListener.onStarted(CdmaCallWaitingPreference.this, false);
90                 }
91             }
92         });
93         builder.setNegativeButton(R.string.disable_cdma_cw, new DialogInterface.OnClickListener() {
94             public void onClick(DialogInterface dialog, int whichButton) {
95                 mPhone.setCallWaiting(false,
96                         mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING));
97                 if (mTcpListener != null) {
98                     mTcpListener.onStarted(CdmaCallWaitingPreference.this, false);
99                 }
100             }
101         });
102         builder.create().show();
103     }
104 
105     @Override
setEnabled(boolean enabled)106     public void setEnabled(boolean enabled) {
107         // If this action is currently disabled due to configuration changes, do not allow anything
108         // to enable it.
109         if (!mIsActionAvailable) return;
110         super.setEnabled(enabled);
111     }
112 
113     private class MyHandler extends Handler {
114         static final int MESSAGE_GET_CALL_WAITING = 0;
115         static final int MESSAGE_SET_CALL_WAITING = 1;
116 
117         @Override
handleMessage(Message msg)118         public void handleMessage(Message msg) {
119             switch (msg.what) {
120                 case MESSAGE_GET_CALL_WAITING:
121                     handleGetCallWaitingResponse(msg);
122                     break;
123                 case MESSAGE_SET_CALL_WAITING:
124                     handleSetCallWaitingResponse(msg);
125                     break;
126             }
127         }
128 
handleGetCallWaitingResponse(Message msg)129         private void handleGetCallWaitingResponse(Message msg) {
130             AsyncResult ar = (AsyncResult) msg.obj;
131 
132             if (mTcpListener != null) {
133                 if (msg.arg2 == MESSAGE_SET_CALL_WAITING) {
134                     mTcpListener.onFinished(CdmaCallWaitingPreference.this, false);
135                 } else {
136                     mTcpListener.onFinished(CdmaCallWaitingPreference.this, true);
137                 }
138             }
139 
140             if (ar.exception instanceof CommandException) {
141                 if (DBG) {
142                     Log.d(LOG_TAG, "handleGetCallWaitingResponse: CommandException=" +
143                             ar.exception);
144                 }
145                 if (mTcpListener != null) {
146                     mTcpListener.onException(CdmaCallWaitingPreference.this,
147                                              (CommandException)ar.exception);
148                 }
149             } else if (ar.userObj instanceof Throwable || ar.exception != null) {
150                 if (DBG) {
151                     Log.d(LOG_TAG, "handleGetCallWaitingResponse: Exception" + ar.exception);
152                 }
153                 if (mTcpListener != null) {
154                     mTcpListener.onError(CdmaCallWaitingPreference.this,
155                                          TimeConsumingPreferenceActivity.RESPONSE_ERROR);
156                 }
157             } else {
158                 if (DBG) {
159                     Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried.");
160                 }
161                 int[] cwArray = (int[])ar.result;
162                 if (cwArray == null) {
163                     if (mTcpListener != null) {
164                         mTcpListener.onError(CdmaCallWaitingPreference.this,
165                                              TimeConsumingPreferenceActivity.RESPONSE_ERROR);
166                     }
167                     return;
168                 }
169 
170                 try {
171                     if (cwArray[0] == CommandsInterface.SS_STATUS_UNKNOWN) {
172                         setSummary("");
173                     } else if(cwArray[0] == 1) {
174                         setSummary(mContext.getString(R.string.cdma_call_waiting_in_ims_on));
175                     } else if(cwArray[0] == 0) {
176                         setSummary(mContext.getString(R.string.cdma_call_waiting_in_ims_off));
177                     }
178                 } catch (ArrayIndexOutOfBoundsException e) {
179                     setSummary("");
180                     Log.e(LOG_TAG, "handleGetCallWaitingResponse: improper result: err ="
181                             + e.getMessage());
182                 }
183             }
184         }
185 
handleSetCallWaitingResponse(Message msg)186         private void handleSetCallWaitingResponse(Message msg) {
187             AsyncResult ar = (AsyncResult) msg.obj;
188 
189             if (ar.exception != null) {
190                 if (DBG) {
191                     Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception);
192                 }
193             }
194 
195             if (ar.result != null) {
196                 int arr = (int)ar.result;
197                 if (arr == CommandsInterface.SS_STATUS_UNKNOWN) {
198                     Log.d(LOG_TAG, "handleSetCallWaitingResponse: no need to re get in CDMA");
199                     mTcpListener.onFinished(CdmaCallWaitingPreference.this, false);
200                     return;
201                 }
202             }
203 
204             if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
205             mPhone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING,
206                         MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception));
207         }
208     }
209 }
210