SongFerngWang | c63cf52 | 2021-03-31 22:08:45 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 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 static com.android.internal.util.Preconditions.checkNotNull; |
| 20 | |
| 21 | import android.os.Bundle; |
| 22 | import android.os.Handler; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ResultReceiver; |
| 25 | import android.telephony.TelephonyManager; |
| 26 | import android.telephony.UssdResponse; |
| 27 | import android.text.TextUtils; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | import com.android.internal.telephony.IIntegerConsumer; |
| 31 | |
| 32 | import java.util.HashMap; |
| 33 | |
| 34 | /** |
| 35 | * Handling the call waiting USSD result. |
| 36 | */ |
| 37 | public class CallWaitingUssdResultReceiver extends ResultReceiver { |
| 38 | private static final String LOG_TAG = "CwUssdResultReceiver"; |
| 39 | |
| 40 | private IIntegerConsumer mCallback; |
| 41 | private CarrierXmlParser mCarrierXmlParser; |
| 42 | private CarrierXmlParser.SsEntry.SSAction mSsAction; |
| 43 | |
| 44 | CallWaitingUssdResultReceiver(Handler handler, IIntegerConsumer callback, |
| 45 | CarrierXmlParser carrierXmlParser, CarrierXmlParser.SsEntry.SSAction action) { |
| 46 | super(handler); |
| 47 | mCallback = callback; |
| 48 | mCarrierXmlParser = carrierXmlParser; |
| 49 | mSsAction = action; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected void onReceiveResult(int resultCode, Bundle ussdResponse) { |
| 54 | log("USSD:" + resultCode); |
| 55 | checkNotNull(ussdResponse, "ussdResponse cannot be null."); |
| 56 | UssdResponse response = ussdResponse.getParcelable( |
| 57 | TelephonyManager.USSD_RESPONSE); |
| 58 | |
| 59 | if (resultCode == TelephonyManager.USSD_RETURN_SUCCESS) { |
| 60 | int callWaitingStatus = getStatusFromResponse(response); |
| 61 | try { |
| 62 | mCallback.accept(callWaitingStatus); |
| 63 | } catch (RemoteException e) { |
| 64 | log("Fail to notify getCallWaitingStatus due to " + e); |
| 65 | } |
| 66 | } else { |
| 67 | try { |
| 68 | mCallback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR); |
| 69 | } catch (RemoteException e) { |
| 70 | log("Fail to notify getCallWaitingStatus due to " + e); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private int getStatusFromResponse(UssdResponse response) { |
| 76 | int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR; |
| 77 | |
| 78 | CarrierXmlParser.SsFeature callWaitingFeature = mCarrierXmlParser.getFeature( |
| 79 | CarrierXmlParser.FEATURE_CALL_WAITING); |
| 80 | if (callWaitingFeature == null) { |
| 81 | return callWaitingStatus; |
| 82 | } |
| 83 | |
| 84 | HashMap<String, String> analysisResult = callWaitingFeature |
| 85 | .getResponseSet(mSsAction, response.getReturnMessage().toString()); |
| 86 | if (analysisResult.get(CarrierXmlParser.TAG_RESPONSE_STATUS_ERROR) != null) { |
| 87 | return callWaitingStatus; |
| 88 | } |
| 89 | |
| 90 | if (analysisResult != null && analysisResult.size() != 0) { |
| 91 | String tmpStatusStr = analysisResult.get( |
| 92 | CarrierXmlParser.TAG_RESPONSE_STATUS); |
| 93 | |
| 94 | if (!TextUtils.isEmpty(tmpStatusStr)) { |
| 95 | if (tmpStatusStr.equals( |
| 96 | CarrierXmlParser.TAG_COMMAND_RESULT_DEFINITION_ACTIVATE)) { |
| 97 | callWaitingStatus = |
| 98 | TelephonyManager.CALL_WAITING_STATUS_ENABLED; |
| 99 | } else if (tmpStatusStr.equals( |
| 100 | CarrierXmlParser.TAG_COMMAND_RESULT_DEFINITION_DEACTIVATE)) { |
| 101 | callWaitingStatus = |
| 102 | TelephonyManager.CALL_WAITING_STATUS_DISABLED; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return callWaitingStatus; |
| 107 | } |
| 108 | |
| 109 | private static void log(String msg) { |
| 110 | Log.d(LOG_TAG, msg); |
| 111 | } |
| 112 | } |