MMIDialogActivity.java revision 598dac56af25319ec4aa0f2a5d98adfc023d7ac4
1598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee/*
2598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * Copyright (C) 2013 The Android Open Source Project
3598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee *
4598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
5598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * you may not use this file except in compliance with the License.
6598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * You may obtain a copy of the License at
7598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee *
8598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
9598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee *
10598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * Unless required by applicable law or agreed to in writing, software
11598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
12598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * See the License for the specific language governing permissions and
14598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * limitations under the License.
15598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee */
16598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
17598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leepackage com.android.phone;
18598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
19598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.app.Activity;
20598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.app.Dialog;
21598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.AsyncResult;
22598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.Bundle;
23598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.Handler;
24598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.Message;
25598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.util.Log;
26598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.widget.Toast;
27598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
28598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.CallManager;
29598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.MmiCode;
30598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.Phone;
31598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.PhoneConstants;
32598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
33598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport java.util.List;
34598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
35598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee/**
36598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * Used to display a dialog from within the Telephony service when running an USSD code
37598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee */
38598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leepublic class MMIDialogActivity extends Activity {
39598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private static final String TAG = MMIDialogActivity.class.getSimpleName();
40598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
41598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private Dialog mMMIDialog;
42598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
43598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private Handler mHandler;
44598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
45598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private CallManager mCM = PhoneGlobals.getInstance().getCallManager();
46598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private Phone mPhone = PhoneGlobals.getPhone();
47598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
48598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
49598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    @Override
50598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    protected void onCreate(Bundle savedInstanceState) {
51598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        super.onCreate(savedInstanceState);
52598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        mHandler = new Handler() {
53598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                @Override
54598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                public void handleMessage(Message msg) {
55598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                    switch (msg.what) {
56598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                        case PhoneGlobals.MMI_COMPLETE:
57598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            onMMIComplete((MmiCode) ((AsyncResult) msg.obj).result);
58598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            break;
59598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                        case PhoneGlobals.MMI_CANCEL:
60598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            onMMICancel();
61598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            break;
62598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                    }
63598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                }
64598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        };
65598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        mCM.registerForMmiComplete(mHandler, PhoneGlobals.MMI_COMPLETE, null);
66598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (mCM.getState() == PhoneConstants.State.OFFHOOK) {
67598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            Toast.makeText(this, R.string.incall_status_dialed_mmi, Toast.LENGTH_SHORT).show();
68598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
69598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        showMMIDialog();
70598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
71598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
72598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void showMMIDialog() {
73598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        final List<? extends MmiCode> codes = mPhone.getPendingMmiCodes();
74598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (codes.size() > 0) {
75598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            final MmiCode mmiCode = codes.get(0);
76598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
77598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
78598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        } else {
79598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            finish();
80598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
81598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
82598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
83598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    /**
84598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * Handles an MMI_COMPLETE event, which is triggered by telephony
85598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     */
86598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void onMMIComplete(MmiCode mmiCode) {
87598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // Check the code to see if the request is ready to
88598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // finish, this includes any MMI state that is not
89598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PENDING.
90598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
91598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // if phone is a CDMA phone display feature code completed message
92598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        int phoneType = mPhone.getPhoneType();
93598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
94598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
95598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
96598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            if (mmiCode.getState() != MmiCode.State.PENDING) {
97598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                Log.d(TAG, "Got MMI_COMPLETE, finishing dialog activity...");
98598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                dismissDialogsAndFinish();
99598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            }
100598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
101598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
102598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
103598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    /**
104598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * Handles an MMI_CANCEL event, which is triggered by the button
105598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
106598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * @see PhoneUtils#cancelMmiCode(Phone)
107598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     */
108598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void onMMICancel() {
109598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        Log.v(TAG, "onMMICancel()...");
110598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
111598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // First of all, cancel the outstanding MMI code (if possible.)
112598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        PhoneUtils.cancelMmiCode(mPhone);
113598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
114598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // Regardless of whether the current MMI code was cancelable, the
115598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PhoneApp will get an MMI_COMPLETE event very soon, which will
116598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // take us to the MMI Complete dialog (see
117598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PhoneUtils.displayMMIComplete().)
118598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        //
119598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // But until that event comes in, we *don't* want to stay here on
120598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // the in-call screen, since we'll be visible in a
121598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // partially-constructed state as soon as the "MMI Started" dialog
122598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // gets dismissed. So let's forcibly bail out right now.
123598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        Log.d(TAG, "onMMICancel: finishing InCallScreen...");
124598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        dismissDialogsAndFinish();
125598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
126598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
127598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void dismissDialogsAndFinish() {
128598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (mMMIDialog != null) {
129598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mMMIDialog.dismiss();
130598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
131598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (mHandler != null) {
132598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mCM.unregisterForMmiComplete(mHandler);
133598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
134598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        finish();
135598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
136598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee}
137