Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 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 | |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
| 21 | import android.content.DialogInterface; |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 22 | import android.os.AsyncResult; |
| 23 | import android.os.Handler; |
| 24 | import android.os.Message; |
| 25 | import android.preference.Preference; |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
| 28 | |
Brad Ebinger | 9df0456 | 2021-07-15 16:16:17 -0700 | [diff] [blame^] | 29 | import com.android.internal.telephony.CommandException; |
| 30 | import com.android.internal.telephony.CommandsInterface; |
| 31 | import com.android.internal.telephony.Phone; |
| 32 | |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 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 | |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 37 | private Context mContext; |
| 38 | private Phone mPhone; |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 39 | private TimeConsumingPreferenceListener mTcpListener; |
| 40 | private MyHandler mHandler = new MyHandler(); |
Brad Ebinger | 9df0456 | 2021-07-15 16:16:17 -0700 | [diff] [blame^] | 41 | private boolean mIsActionAvailable = true; |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 42 | |
| 43 | public CdmaCallWaitingPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 44 | super(context, attrs, defStyleAttr); |
| 45 | mContext = context; |
| 46 | } |
| 47 | |
| 48 | public CdmaCallWaitingPreference(Context context, AttributeSet attrs) { |
| 49 | this(context, attrs, com.android.internal.R.attr.preferenceStyle); |
| 50 | } |
| 51 | |
| 52 | public CdmaCallWaitingPreference(Context context) { |
| 53 | this(context, null); |
| 54 | } |
| 55 | |
| 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 | |
Brad Ebinger | 9df0456 | 2021-07-15 16:16:17 -0700 | [diff] [blame^] | 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 | */ |
| 72 | public void setActionAvailable(boolean isAvailable) { |
| 73 | mIsActionAvailable = isAvailable; |
| 74 | super.setEnabled(mIsActionAvailable); |
| 75 | } |
| 76 | |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 77 | @Override |
| 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 | |
Brad Ebinger | 9df0456 | 2021-07-15 16:16:17 -0700 | [diff] [blame^] | 105 | @Override |
| 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 | |
Jang Hayeong | 4022b38 | 2019-07-16 20:05:17 +0900 | [diff] [blame] | 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 |
| 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 | |
| 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 | |
| 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 | } |