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 | |
| 19 | import com.android.internal.telephony.CommandException; |
| 20 | import com.android.internal.telephony.CommandsInterface; |
| 21 | import com.android.internal.telephony.Phone; |
| 22 | |
| 23 | import android.app.AlertDialog; |
| 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.content.res.TypedArray; |
| 27 | import android.os.AsyncResult; |
| 28 | import android.os.Handler; |
| 29 | import android.os.Message; |
| 30 | import android.preference.Preference; |
| 31 | import android.preference.PreferenceActivity; |
| 32 | import android.util.AttributeSet; |
| 33 | import android.util.Log; |
| 34 | |
| 35 | public class CdmaCallWaitingPreference extends Preference { |
| 36 | private static final String LOG_TAG = "CdmaCallWaitingPreference"; |
| 37 | private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); |
| 38 | |
| 39 | private int mButtonClicked; |
| 40 | private Context mContext; |
| 41 | private Phone mPhone; |
| 42 | private SubscriptionInfoHelper mSubscriptionInfoHelper; |
| 43 | private TimeConsumingPreferenceListener mTcpListener; |
| 44 | private MyHandler mHandler = new MyHandler(); |
| 45 | |
| 46 | public CdmaCallWaitingPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 47 | super(context, attrs, defStyleAttr); |
| 48 | mContext = context; |
| 49 | } |
| 50 | |
| 51 | public CdmaCallWaitingPreference(Context context, AttributeSet attrs) { |
| 52 | this(context, attrs, com.android.internal.R.attr.preferenceStyle); |
| 53 | } |
| 54 | |
| 55 | public CdmaCallWaitingPreference(Context context) { |
| 56 | this(context, null); |
| 57 | } |
| 58 | |
| 59 | public void init(TimeConsumingPreferenceListener listener, Phone phone) { |
| 60 | mPhone = phone; |
| 61 | mTcpListener = listener; |
| 62 | Log.d(LOG_TAG, "phone id= " + mPhone.getPhoneId()); |
| 63 | mPhone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING, |
| 64 | MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING)); |
| 65 | if (mTcpListener != null) { |
| 66 | mTcpListener.onStarted(this, true); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onClick() { |
| 72 | super.onClick(); |
| 73 | |
| 74 | AlertDialog.Builder builder = new AlertDialog.Builder(mContext); |
| 75 | builder.setTitle(mContext.getText(R.string.cdma_call_waiting)); |
| 76 | builder.setMessage(mContext.getText(R.string.enable_cdma_call_waiting_setting)); |
| 77 | builder.setPositiveButton(R.string.enable_cdma_cw, new DialogInterface.OnClickListener() { |
| 78 | public void onClick(DialogInterface dialog, int whichButton) { |
| 79 | mPhone.setCallWaiting(true, |
| 80 | mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING)); |
| 81 | if (mTcpListener != null) { |
| 82 | mTcpListener.onStarted(CdmaCallWaitingPreference.this, false); |
| 83 | } |
| 84 | } |
| 85 | }); |
| 86 | builder.setNegativeButton(R.string.disable_cdma_cw, new DialogInterface.OnClickListener() { |
| 87 | public void onClick(DialogInterface dialog, int whichButton) { |
| 88 | mPhone.setCallWaiting(false, |
| 89 | mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING)); |
| 90 | if (mTcpListener != null) { |
| 91 | mTcpListener.onStarted(CdmaCallWaitingPreference.this, false); |
| 92 | } |
| 93 | } |
| 94 | }); |
| 95 | builder.create().show(); |
| 96 | } |
| 97 | |
| 98 | private class MyHandler extends Handler { |
| 99 | static final int MESSAGE_GET_CALL_WAITING = 0; |
| 100 | static final int MESSAGE_SET_CALL_WAITING = 1; |
| 101 | |
| 102 | @Override |
| 103 | public void handleMessage(Message msg) { |
| 104 | switch (msg.what) { |
| 105 | case MESSAGE_GET_CALL_WAITING: |
| 106 | handleGetCallWaitingResponse(msg); |
| 107 | break; |
| 108 | case MESSAGE_SET_CALL_WAITING: |
| 109 | handleSetCallWaitingResponse(msg); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private void handleGetCallWaitingResponse(Message msg) { |
| 115 | AsyncResult ar = (AsyncResult) msg.obj; |
| 116 | |
| 117 | if (mTcpListener != null) { |
| 118 | if (msg.arg2 == MESSAGE_SET_CALL_WAITING) { |
| 119 | mTcpListener.onFinished(CdmaCallWaitingPreference.this, false); |
| 120 | } else { |
| 121 | mTcpListener.onFinished(CdmaCallWaitingPreference.this, true); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (ar.exception instanceof CommandException) { |
| 126 | if (DBG) { |
| 127 | Log.d(LOG_TAG, "handleGetCallWaitingResponse: CommandException=" + |
| 128 | ar.exception); |
| 129 | } |
| 130 | if (mTcpListener != null) { |
| 131 | mTcpListener.onException(CdmaCallWaitingPreference.this, |
| 132 | (CommandException)ar.exception); |
| 133 | } |
| 134 | } else if (ar.userObj instanceof Throwable || ar.exception != null) { |
| 135 | if (DBG) { |
| 136 | Log.d(LOG_TAG, "handleGetCallWaitingResponse: Exception" + ar.exception); |
| 137 | } |
| 138 | if (mTcpListener != null) { |
| 139 | mTcpListener.onError(CdmaCallWaitingPreference.this, |
| 140 | TimeConsumingPreferenceActivity.RESPONSE_ERROR); |
| 141 | } |
| 142 | } else { |
| 143 | if (DBG) { |
| 144 | Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried."); |
| 145 | } |
| 146 | int[] cwArray = (int[])ar.result; |
| 147 | if (cwArray == null) { |
| 148 | if (mTcpListener != null) { |
| 149 | mTcpListener.onError(CdmaCallWaitingPreference.this, |
| 150 | TimeConsumingPreferenceActivity.RESPONSE_ERROR); |
| 151 | } |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | try { |
| 156 | if (cwArray[0] == CommandsInterface.SS_STATUS_UNKNOWN) { |
| 157 | setSummary(""); |
| 158 | } else if(cwArray[0] == 1) { |
| 159 | setSummary(mContext.getString(R.string.cdma_call_waiting_in_ims_on)); |
| 160 | } else if(cwArray[0] == 0) { |
| 161 | setSummary(mContext.getString(R.string.cdma_call_waiting_in_ims_off)); |
| 162 | } |
| 163 | } catch (ArrayIndexOutOfBoundsException e) { |
| 164 | setSummary(""); |
| 165 | Log.e(LOG_TAG, "handleGetCallWaitingResponse: improper result: err =" |
| 166 | + e.getMessage()); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private void handleSetCallWaitingResponse(Message msg) { |
| 172 | AsyncResult ar = (AsyncResult) msg.obj; |
| 173 | |
| 174 | if (ar.exception != null) { |
| 175 | if (DBG) { |
| 176 | Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (ar.result != null) { |
| 181 | int arr = (int)ar.result; |
| 182 | if (arr == CommandsInterface.SS_STATUS_UNKNOWN) { |
| 183 | Log.d(LOG_TAG, "handleSetCallWaitingResponse: no need to re get in CDMA"); |
| 184 | mTcpListener.onFinished(CdmaCallWaitingPreference.this, false); |
| 185 | return; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get"); |
| 190 | mPhone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING, |
| 191 | MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception)); |
| 192 | } |
| 193 | } |
| 194 | } |