blob: 5ac6b85986561f5dbe4b5b100e691de876ce3b40 [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;
21import android.os.AsyncResult;
22import android.os.Bundle;
23import android.os.Handler;
24import android.os.Message;
25import android.util.Log;
26import android.widget.Toast;
27
28import com.android.internal.telephony.CallManager;
29import com.android.internal.telephony.MmiCode;
30import com.android.internal.telephony.Phone;
31import com.android.internal.telephony.PhoneConstants;
32
33import java.util.List;
34
35/**
36 * Used to display a dialog from within the Telephony service when running an USSD code
37 */
38public 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
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);
Yorke Lee598dac52013-11-01 11:30:55 -070066 showMMIDialog();
67 }
68
Luo, Honggang X399d1e92015-03-03 11:04:28 +010069 @Override
70 protected void onDestroy() {
71 super.onDestroy();
72
73 if (mMMIDialog != null) {
74 mMMIDialog.dismiss();
75 mMMIDialog = null;
76 }
77 if (mHandler != null) {
78 mCM.unregisterForMmiComplete(mHandler);
79 mHandler = null;
80 }
81 }
82
Yorke Lee598dac52013-11-01 11:30:55 -070083 private void showMMIDialog() {
84 final List<? extends MmiCode> codes = mPhone.getPendingMmiCodes();
85 if (codes.size() > 0) {
86 final MmiCode mmiCode = codes.get(0);
87 final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
88 mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
89 } else {
90 finish();
91 }
92 }
93
94 /**
95 * Handles an MMI_COMPLETE event, which is triggered by telephony
96 */
97 private void onMMIComplete(MmiCode mmiCode) {
98 // Check the code to see if the request is ready to
99 // finish, this includes any MMI state that is not
100 // PENDING.
101
102 // if phone is a CDMA phone display feature code completed message
103 int phoneType = mPhone.getPhoneType();
104 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
105 PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
106 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
107 if (mmiCode.getState() != MmiCode.State.PENDING) {
108 Log.d(TAG, "Got MMI_COMPLETE, finishing dialog activity...");
109 dismissDialogsAndFinish();
110 }
111 }
112 }
113
114 /**
115 * Handles an MMI_CANCEL event, which is triggered by the button
116 * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
117 * @see PhoneUtils#cancelMmiCode(Phone)
118 */
119 private void onMMICancel() {
120 Log.v(TAG, "onMMICancel()...");
121
122 // First of all, cancel the outstanding MMI code (if possible.)
123 PhoneUtils.cancelMmiCode(mPhone);
124
125 // Regardless of whether the current MMI code was cancelable, the
126 // PhoneApp will get an MMI_COMPLETE event very soon, which will
127 // take us to the MMI Complete dialog (see
128 // PhoneUtils.displayMMIComplete().)
129 //
130 // But until that event comes in, we *don't* want to stay here on
131 // the in-call screen, since we'll be visible in a
132 // partially-constructed state as soon as the "MMI Started" dialog
133 // gets dismissed. So let's forcibly bail out right now.
134 Log.d(TAG, "onMMICancel: finishing InCallScreen...");
135 dismissDialogsAndFinish();
136 }
137
138 private void dismissDialogsAndFinish() {
139 if (mMMIDialog != null) {
140 mMMIDialog.dismiss();
Luo, Honggang X399d1e92015-03-03 11:04:28 +0100141 mMMIDialog = null;
Yorke Lee598dac52013-11-01 11:30:55 -0700142 }
143 if (mHandler != null) {
144 mCM.unregisterForMmiComplete(mHandler);
Luo, Honggang X399d1e92015-03-03 11:04:28 +0100145 mHandler = null;
Yorke Lee598dac52013-11-01 11:30:55 -0700146 }
147 finish();
148 }
149}