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
72399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X    @Override
73399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X    protected void onDestroy() {
74399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        super.onDestroy();
75399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X
76399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        if (mMMIDialog != null) {
77399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mMMIDialog.dismiss();
78399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mMMIDialog = null;
79399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        }
80399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        if (mHandler != null) {
81399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mCM.unregisterForMmiComplete(mHandler);
82399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mHandler = null;
83399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        }
84399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X    }
85399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X
86598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void showMMIDialog() {
87598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        final List<? extends MmiCode> codes = mPhone.getPendingMmiCodes();
88598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (codes.size() > 0) {
89598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            final MmiCode mmiCode = codes.get(0);
90598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
91598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
92598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        } else {
93598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            finish();
94598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
95598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
96598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
97598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    /**
98598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * Handles an MMI_COMPLETE event, which is triggered by telephony
99598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     */
100598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void onMMIComplete(MmiCode mmiCode) {
101598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // Check the code to see if the request is ready to
102598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // finish, this includes any MMI state that is not
103598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PENDING.
104598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
105598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // if phone is a CDMA phone display feature code completed message
106598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        int phoneType = mPhone.getPhoneType();
107598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
108598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
109598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
110598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            if (mmiCode.getState() != MmiCode.State.PENDING) {
111598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                Log.d(TAG, "Got MMI_COMPLETE, finishing dialog activity...");
112598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                dismissDialogsAndFinish();
113598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            }
114598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
115598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
116598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
117598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    /**
118598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * Handles an MMI_CANCEL event, which is triggered by the button
119598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
120598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * @see PhoneUtils#cancelMmiCode(Phone)
121598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     */
122598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void onMMICancel() {
123598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        Log.v(TAG, "onMMICancel()...");
124598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
125598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // First of all, cancel the outstanding MMI code (if possible.)
126598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        PhoneUtils.cancelMmiCode(mPhone);
127598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
128598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // Regardless of whether the current MMI code was cancelable, the
129598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PhoneApp will get an MMI_COMPLETE event very soon, which will
130598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // take us to the MMI Complete dialog (see
131598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PhoneUtils.displayMMIComplete().)
132598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        //
133598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // But until that event comes in, we *don't* want to stay here on
134598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // the in-call screen, since we'll be visible in a
135598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // partially-constructed state as soon as the "MMI Started" dialog
136598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // gets dismissed. So let's forcibly bail out right now.
137598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        Log.d(TAG, "onMMICancel: finishing InCallScreen...");
138598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        dismissDialogsAndFinish();
139598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
140598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
141598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void dismissDialogsAndFinish() {
142598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (mMMIDialog != null) {
143598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mMMIDialog.dismiss();
144399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mMMIDialog = null;
145598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
146598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (mHandler != null) {
147598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mCM.unregisterForMmiComplete(mHandler);
148399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mHandler = null;
149598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
150598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        finish();
151598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
152598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee}
153