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