blob: 165118ba343dbcc1fadc9934706f59ae62a07e4a [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
35import java.util.List;
36
37/**
38 * Used to display a dialog from within the Telephony service when running an USSD code
39 */
40public 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 Chintala3cdd9022015-09-03 14:12:55 +053048 private Phone mPhone;
Yorke Lee598dac52013-11-01 11:30:55 -070049
50
51 @Override
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
Srikanth Chintala3cdd9022015-09-03 14:12:55 +053054 Intent intent = getIntent();
55 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
56 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
57 mPhone = PhoneGlobals.getPhone(subId);
Yorke Lee598dac52013-11-01 11:30:55 -070058 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 };
Tyler Gunn13fe2492017-05-22 14:14:20 -070071 Log.d(TAG, "onCreate; registering for mmi complete.");
Yorke Lee598dac52013-11-01 11:30:55 -070072 mCM.registerForMmiComplete(mHandler, PhoneGlobals.MMI_COMPLETE, null);
Yorke Lee598dac52013-11-01 11:30:55 -070073 showMMIDialog();
74 }
75
Luo, Honggang X399d1e92015-03-03 11:04:28 +010076 @Override
77 protected void onDestroy() {
78 super.onDestroy();
79
80 if (mMMIDialog != null) {
81 mMMIDialog.dismiss();
82 mMMIDialog = null;
83 }
84 if (mHandler != null) {
85 mCM.unregisterForMmiComplete(mHandler);
86 mHandler = null;
87 }
88 }
89
Yorke Lee598dac52013-11-01 11:30:55 -070090 private void showMMIDialog() {
91 final List<? extends MmiCode> codes = mPhone.getPendingMmiCodes();
92 if (codes.size() > 0) {
93 final MmiCode mmiCode = codes.get(0);
94 final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
Tyler Gunn13fe2492017-05-22 14:14:20 -070095 Log.d(TAG, "showMMIDialog: mmiCode = " + mmiCode);
Yorke Lee598dac52013-11-01 11:30:55 -070096 mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
97 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -070098 Log.d(TAG, "showMMIDialog: no pending MMIs; finishing");
Yorke Lee598dac52013-11-01 11:30:55 -070099 finish();
100 }
101 }
102
103 /**
104 * Handles an MMI_COMPLETE event, which is triggered by telephony
105 */
106 private void onMMIComplete(MmiCode mmiCode) {
107 // Check the code to see if the request is ready to
108 // finish, this includes any MMI state that is not
109 // PENDING.
Tyler Gunn13fe2492017-05-22 14:14:20 -0700110 Log.d(TAG, "onMMIComplete: mmi=" + mmiCode);
Yorke Lee598dac52013-11-01 11:30:55 -0700111
112 // if phone is a CDMA phone display feature code completed message
113 int phoneType = mPhone.getPhoneType();
114 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
115 PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
116 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
117 if (mmiCode.getState() != MmiCode.State.PENDING) {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700118 Log.d(TAG, "onMMIComplete: Got MMI_COMPLETE, finishing dialog activity...");
Yorke Lee598dac52013-11-01 11:30:55 -0700119 dismissDialogsAndFinish();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700120 } else {
121 Log.d(TAG, "onMMIComplete: still pending.");
Yorke Lee598dac52013-11-01 11:30:55 -0700122 }
123 }
124 }
125
126 /**
127 * Handles an MMI_CANCEL event, which is triggered by the button
128 * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
129 * @see PhoneUtils#cancelMmiCode(Phone)
130 */
131 private void onMMICancel() {
132 Log.v(TAG, "onMMICancel()...");
133
134 // First of all, cancel the outstanding MMI code (if possible.)
135 PhoneUtils.cancelMmiCode(mPhone);
136
137 // Regardless of whether the current MMI code was cancelable, the
138 // PhoneApp will get an MMI_COMPLETE event very soon, which will
139 // take us to the MMI Complete dialog (see
140 // PhoneUtils.displayMMIComplete().)
141 //
142 // But until that event comes in, we *don't* want to stay here on
143 // the in-call screen, since we'll be visible in a
144 // partially-constructed state as soon as the "MMI Started" dialog
145 // gets dismissed. So let's forcibly bail out right now.
Tyler Gunn13fe2492017-05-22 14:14:20 -0700146 Log.d(TAG, "onMMICancel: finishing MMI dialog...");
Yorke Lee598dac52013-11-01 11:30:55 -0700147 dismissDialogsAndFinish();
148 }
149
150 private void dismissDialogsAndFinish() {
151 if (mMMIDialog != null) {
152 mMMIDialog.dismiss();
Luo, Honggang X399d1e92015-03-03 11:04:28 +0100153 mMMIDialog = null;
Yorke Lee598dac52013-11-01 11:30:55 -0700154 }
155 if (mHandler != null) {
156 mCM.unregisterForMmiComplete(mHandler);
Luo, Honggang X399d1e92015-03-03 11:04:28 +0100157 mHandler = null;
Yorke Lee598dac52013-11-01 11:30:55 -0700158 }
Tyler Gunn13fe2492017-05-22 14:14:20 -0700159 Log.v(TAG, "dismissDialogsAndFinish");
Yorke Lee598dac52013-11-01 11:30:55 -0700160 finish();
161 }
162}