Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.Activity; |
| 20 | import android.app.Dialog; |
Srikanth Chintala | 3cdd902 | 2015-09-03 14:12:55 +0530 | [diff] [blame^] | 21 | import android.content.Intent; |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 22 | import android.os.AsyncResult; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
Srikanth Chintala | 3cdd902 | 2015-09-03 14:12:55 +0530 | [diff] [blame^] | 26 | import android.telephony.SubscriptionManager; |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.widget.Toast; |
| 29 | |
| 30 | import com.android.internal.telephony.CallManager; |
| 31 | import com.android.internal.telephony.MmiCode; |
| 32 | import com.android.internal.telephony.Phone; |
| 33 | import com.android.internal.telephony.PhoneConstants; |
| 34 | |
| 35 | import java.util.List; |
| 36 | |
| 37 | /** |
| 38 | * Used to display a dialog from within the Telephony service when running an USSD code |
| 39 | */ |
| 40 | public class MMIDialogActivity extends Activity { |
| 41 | private static final String TAG = MMIDialogActivity.class.getSimpleName(); |
| 42 | |
| 43 | private Dialog mMMIDialog; |
| 44 | |
| 45 | private Handler mHandler; |
| 46 | |
| 47 | private CallManager mCM = PhoneGlobals.getInstance().getCallManager(); |
Srikanth Chintala | 3cdd902 | 2015-09-03 14:12:55 +0530 | [diff] [blame^] | 48 | private Phone mPhone; |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | @Override |
| 52 | protected void onCreate(Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
Srikanth Chintala | 3cdd902 | 2015-09-03 14:12:55 +0530 | [diff] [blame^] | 54 | Intent intent = getIntent(); |
| 55 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 56 | SubscriptionManager.DEFAULT_SUBSCRIPTION_ID); |
| 57 | mPhone = PhoneGlobals.getPhone(subId); |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 58 | mHandler = new Handler() { |
| 59 | @Override |
| 60 | public void handleMessage(Message msg) { |
| 61 | switch (msg.what) { |
| 62 | case PhoneGlobals.MMI_COMPLETE: |
| 63 | onMMIComplete((MmiCode) ((AsyncResult) msg.obj).result); |
| 64 | break; |
| 65 | case PhoneGlobals.MMI_CANCEL: |
| 66 | onMMICancel(); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | }; |
| 71 | mCM.registerForMmiComplete(mHandler, PhoneGlobals.MMI_COMPLETE, null); |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 72 | showMMIDialog(); |
| 73 | } |
| 74 | |
Luo, Honggang X | 399d1e9 | 2015-03-03 11:04:28 +0100 | [diff] [blame] | 75 | @Override |
| 76 | protected void onDestroy() { |
| 77 | super.onDestroy(); |
| 78 | |
| 79 | if (mMMIDialog != null) { |
| 80 | mMMIDialog.dismiss(); |
| 81 | mMMIDialog = null; |
| 82 | } |
| 83 | if (mHandler != null) { |
| 84 | mCM.unregisterForMmiComplete(mHandler); |
| 85 | mHandler = null; |
| 86 | } |
| 87 | } |
| 88 | |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 89 | private void showMMIDialog() { |
| 90 | final List<? extends MmiCode> codes = mPhone.getPendingMmiCodes(); |
| 91 | if (codes.size() > 0) { |
| 92 | final MmiCode mmiCode = codes.get(0); |
| 93 | final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL); |
| 94 | mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog); |
| 95 | } else { |
| 96 | finish(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Handles an MMI_COMPLETE event, which is triggered by telephony |
| 102 | */ |
| 103 | private void onMMIComplete(MmiCode mmiCode) { |
| 104 | // Check the code to see if the request is ready to |
| 105 | // finish, this includes any MMI state that is not |
| 106 | // PENDING. |
| 107 | |
| 108 | // if phone is a CDMA phone display feature code completed message |
| 109 | int phoneType = mPhone.getPhoneType(); |
| 110 | if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) { |
| 111 | PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null); |
| 112 | } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) { |
| 113 | if (mmiCode.getState() != MmiCode.State.PENDING) { |
| 114 | Log.d(TAG, "Got MMI_COMPLETE, finishing dialog activity..."); |
| 115 | dismissDialogsAndFinish(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Handles an MMI_CANCEL event, which is triggered by the button |
| 122 | * (labeled either "OK" or "Cancel") on the "MMI Started" dialog. |
| 123 | * @see PhoneUtils#cancelMmiCode(Phone) |
| 124 | */ |
| 125 | private void onMMICancel() { |
| 126 | Log.v(TAG, "onMMICancel()..."); |
| 127 | |
| 128 | // First of all, cancel the outstanding MMI code (if possible.) |
| 129 | PhoneUtils.cancelMmiCode(mPhone); |
| 130 | |
| 131 | // Regardless of whether the current MMI code was cancelable, the |
| 132 | // PhoneApp will get an MMI_COMPLETE event very soon, which will |
| 133 | // take us to the MMI Complete dialog (see |
| 134 | // PhoneUtils.displayMMIComplete().) |
| 135 | // |
| 136 | // But until that event comes in, we *don't* want to stay here on |
| 137 | // the in-call screen, since we'll be visible in a |
| 138 | // partially-constructed state as soon as the "MMI Started" dialog |
| 139 | // gets dismissed. So let's forcibly bail out right now. |
| 140 | Log.d(TAG, "onMMICancel: finishing InCallScreen..."); |
| 141 | dismissDialogsAndFinish(); |
| 142 | } |
| 143 | |
| 144 | private void dismissDialogsAndFinish() { |
| 145 | if (mMMIDialog != null) { |
| 146 | mMMIDialog.dismiss(); |
Luo, Honggang X | 399d1e9 | 2015-03-03 11:04:28 +0100 | [diff] [blame] | 147 | mMMIDialog = null; |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 148 | } |
| 149 | if (mHandler != null) { |
| 150 | mCM.unregisterForMmiComplete(mHandler); |
Luo, Honggang X | 399d1e9 | 2015-03-03 11:04:28 +0100 | [diff] [blame] | 151 | mHandler = null; |
Yorke Lee | 598dac5 | 2013-11-01 11:30:55 -0700 | [diff] [blame] | 152 | } |
| 153 | finish(); |
| 154 | } |
| 155 | } |