blob: 4afa6bec1679ae5abcdfc064f8b28090b84fc026 [file] [log] [blame]
Yorke Lee598dac52013-11-01 11:30:55 -07001/*
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
17package com.android.phone;
18
19import android.app.Activity;
20import android.app.Dialog;
Srikanth Chintala3cdd9022015-09-03 14:12:55 +053021import android.content.Intent;
Yorke Lee598dac52013-11-01 11:30:55 -070022import android.os.AsyncResult;
23import android.os.Bundle;
24import android.os.Handler;
25import android.os.Message;
Srikanth Chintala3cdd9022015-09-03 14:12:55 +053026import android.telephony.SubscriptionManager;
Yorke Lee598dac52013-11-01 11:30:55 -070027import android.util.Log;
28import android.widget.Toast;
29
30import com.android.internal.telephony.CallManager;
31import com.android.internal.telephony.MmiCode;
32import com.android.internal.telephony.Phone;
33import com.android.internal.telephony.PhoneConstants;
34
Tyler Gunnabb06242017-06-02 18:33:25 -070035import java.util.ArrayList;
Yorke Lee598dac52013-11-01 11:30:55 -070036import java.util.List;
37
38/**
39 * Used to display a dialog from within the Telephony service when running an USSD code
40 */
41public class MMIDialogActivity extends Activity {
42 private static final String TAG = MMIDialogActivity.class.getSimpleName();
43
44 private Dialog mMMIDialog;
45
46 private Handler mHandler;
47
48 private CallManager mCM = PhoneGlobals.getInstance().getCallManager();
Srikanth Chintala3cdd9022015-09-03 14:12:55 +053049 private Phone mPhone;
Yorke Lee598dac52013-11-01 11:30:55 -070050
51
52 @Override
53 protected void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
Srikanth Chintala3cdd9022015-09-03 14:12:55 +053055 Intent intent = getIntent();
56 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
57 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
58 mPhone = PhoneGlobals.getPhone(subId);
Yorke Lee598dac52013-11-01 11:30:55 -070059 mHandler = new Handler() {
60 @Override
61 public void handleMessage(Message msg) {
62 switch (msg.what) {
63 case PhoneGlobals.MMI_COMPLETE:
64 onMMIComplete((MmiCode) ((AsyncResult) msg.obj).result);
65 break;
66 case PhoneGlobals.MMI_CANCEL:
67 onMMICancel();
68 break;
69 }
70 }
71 };
Tyler Gunn13fe2492017-05-22 14:14:20 -070072 Log.d(TAG, "onCreate; registering for mmi complete.");
Yorke Lee598dac52013-11-01 11:30:55 -070073 mCM.registerForMmiComplete(mHandler, PhoneGlobals.MMI_COMPLETE, null);
Yorke Lee598dac52013-11-01 11:30:55 -070074 showMMIDialog();
75 }
76
Luo, Honggang X399d1e92015-03-03 11:04:28 +010077 @Override
78 protected void onDestroy() {
79 super.onDestroy();
80
81 if (mMMIDialog != null) {
82 mMMIDialog.dismiss();
83 mMMIDialog = null;
84 }
85 if (mHandler != null) {
86 mCM.unregisterForMmiComplete(mHandler);
87 mHandler = null;
88 }
89 }
90
Yorke Lee598dac52013-11-01 11:30:55 -070091 private void showMMIDialog() {
Tyler Gunnabb06242017-06-02 18:33:25 -070092 final List<MmiCode> codes = new ArrayList<>(mPhone.getPendingMmiCodes());
93 // If the phone has an IMS phone, also get pending imS MMIsl.
94 if (mPhone.getImsPhone() != null) {
95 codes.addAll(mPhone.getImsPhone().getPendingMmiCodes());
96 }
Yorke Lee598dac52013-11-01 11:30:55 -070097 if (codes.size() > 0) {
98 final MmiCode mmiCode = codes.get(0);
99 final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
Tyler Gunn13fe2492017-05-22 14:14:20 -0700100 Log.d(TAG, "showMMIDialog: mmiCode = " + mmiCode);
Yorke Lee598dac52013-11-01 11:30:55 -0700101 mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
102 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700103 Log.d(TAG, "showMMIDialog: no pending MMIs; finishing");
Yorke Lee598dac52013-11-01 11:30:55 -0700104 finish();
105 }
106 }
107
108 /**
109 * Handles an MMI_COMPLETE event, which is triggered by telephony
110 */
111 private void onMMIComplete(MmiCode mmiCode) {
112 // Check the code to see if the request is ready to
113 // finish, this includes any MMI state that is not
114 // PENDING.
Tyler Gunn13fe2492017-05-22 14:14:20 -0700115 Log.d(TAG, "onMMIComplete: mmi=" + mmiCode);
Yorke Lee598dac52013-11-01 11:30:55 -0700116
117 // if phone is a CDMA phone display feature code completed message
118 int phoneType = mPhone.getPhoneType();
119 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
120 PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
121 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
122 if (mmiCode.getState() != MmiCode.State.PENDING) {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700123 Log.d(TAG, "onMMIComplete: Got MMI_COMPLETE, finishing dialog activity...");
Yorke Lee598dac52013-11-01 11:30:55 -0700124 dismissDialogsAndFinish();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700125 } else {
126 Log.d(TAG, "onMMIComplete: still pending.");
Yorke Lee598dac52013-11-01 11:30:55 -0700127 }
128 }
129 }
130
131 /**
132 * Handles an MMI_CANCEL event, which is triggered by the button
133 * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
134 * @see PhoneUtils#cancelMmiCode(Phone)
135 */
136 private void onMMICancel() {
137 Log.v(TAG, "onMMICancel()...");
138
139 // First of all, cancel the outstanding MMI code (if possible.)
140 PhoneUtils.cancelMmiCode(mPhone);
141
142 // Regardless of whether the current MMI code was cancelable, the
143 // PhoneApp will get an MMI_COMPLETE event very soon, which will
144 // take us to the MMI Complete dialog (see
145 // PhoneUtils.displayMMIComplete().)
146 //
147 // But until that event comes in, we *don't* want to stay here on
148 // the in-call screen, since we'll be visible in a
149 // partially-constructed state as soon as the "MMI Started" dialog
150 // gets dismissed. So let's forcibly bail out right now.
Tyler Gunn13fe2492017-05-22 14:14:20 -0700151 Log.d(TAG, "onMMICancel: finishing MMI dialog...");
Yorke Lee598dac52013-11-01 11:30:55 -0700152 dismissDialogsAndFinish();
153 }
154
155 private void dismissDialogsAndFinish() {
156 if (mMMIDialog != null) {
157 mMMIDialog.dismiss();
Luo, Honggang X399d1e92015-03-03 11:04:28 +0100158 mMMIDialog = null;
Yorke Lee598dac52013-11-01 11:30:55 -0700159 }
160 if (mHandler != null) {
161 mCM.unregisterForMmiComplete(mHandler);
Luo, Honggang X399d1e92015-03-03 11:04:28 +0100162 mHandler = null;
Yorke Lee598dac52013-11-01 11:30:55 -0700163 }
Tyler Gunn13fe2492017-05-22 14:14:20 -0700164 Log.v(TAG, "dismissDialogsAndFinish");
Yorke Lee598dac52013-11-01 11:30:55 -0700165 finish();
166 }
167}