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;
213cdd90262077a4a5b779d6d1ae5249978fdf7992Srikanth Chintalaimport android.content.Intent;
22598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.AsyncResult;
23598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.Bundle;
24598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.Handler;
25598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.os.Message;
263cdd90262077a4a5b779d6d1ae5249978fdf7992Srikanth Chintalaimport android.telephony.SubscriptionManager;
27598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.util.Log;
28598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport android.widget.Toast;
29598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
30598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.CallManager;
31598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.MmiCode;
32598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.Phone;
33598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport com.android.internal.telephony.PhoneConstants;
34598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
35abb0624423c82785a377fd41287545e5f8d21d4bTyler Gunnimport java.util.ArrayList;
36598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leeimport java.util.List;
37598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
38598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee/**
39598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee * Used to display a dialog from within the Telephony service when running an USSD code
40598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee */
41598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Leepublic class MMIDialogActivity extends Activity {
42598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private static final String TAG = MMIDialogActivity.class.getSimpleName();
43598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
44598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private Dialog mMMIDialog;
45598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
46598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private Handler mHandler;
47598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
48598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private CallManager mCM = PhoneGlobals.getInstance().getCallManager();
493cdd90262077a4a5b779d6d1ae5249978fdf7992Srikanth Chintala    private Phone mPhone;
50598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
51598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
52598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    @Override
53598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    protected void onCreate(Bundle savedInstanceState) {
54598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        super.onCreate(savedInstanceState);
553cdd90262077a4a5b779d6d1ae5249978fdf7992Srikanth Chintala        Intent intent = getIntent();
563cdd90262077a4a5b779d6d1ae5249978fdf7992Srikanth Chintala        int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
573cdd90262077a4a5b779d6d1ae5249978fdf7992Srikanth Chintala                SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
583cdd90262077a4a5b779d6d1ae5249978fdf7992Srikanth Chintala        mPhone = PhoneGlobals.getPhone(subId);
59598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        mHandler = new Handler() {
60598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                @Override
61598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                public void handleMessage(Message msg) {
62598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                    switch (msg.what) {
63598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                        case PhoneGlobals.MMI_COMPLETE:
64598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            onMMIComplete((MmiCode) ((AsyncResult) msg.obj).result);
65598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            break;
66598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                        case PhoneGlobals.MMI_CANCEL:
67598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            onMMICancel();
68598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                            break;
69598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                    }
70598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                }
71598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        };
7213fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn        Log.d(TAG, "onCreate; registering for mmi complete.");
73598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        mCM.registerForMmiComplete(mHandler, PhoneGlobals.MMI_COMPLETE, null);
74598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        showMMIDialog();
75598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
76598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
77399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X    @Override
78399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X    protected void onDestroy() {
79399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        super.onDestroy();
80399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X
81399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        if (mMMIDialog != null) {
82399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mMMIDialog.dismiss();
83399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mMMIDialog = null;
84399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        }
85399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        if (mHandler != null) {
86399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mCM.unregisterForMmiComplete(mHandler);
87399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mHandler = null;
88399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X        }
89399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X    }
90399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X
91598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void showMMIDialog() {
92abb0624423c82785a377fd41287545e5f8d21d4bTyler Gunn        final List<MmiCode> codes = new ArrayList<>(mPhone.getPendingMmiCodes());
93abb0624423c82785a377fd41287545e5f8d21d4bTyler Gunn        // If the phone has an IMS phone, also get pending imS MMIsl.
94abb0624423c82785a377fd41287545e5f8d21d4bTyler Gunn        if (mPhone.getImsPhone() != null) {
95abb0624423c82785a377fd41287545e5f8d21d4bTyler Gunn            codes.addAll(mPhone.getImsPhone().getPendingMmiCodes());
96abb0624423c82785a377fd41287545e5f8d21d4bTyler Gunn        }
97598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (codes.size() > 0) {
98598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            final MmiCode mmiCode = codes.get(0);
99598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
10013fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn            Log.d(TAG, "showMMIDialog: mmiCode = " + mmiCode);
101598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
102598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        } else {
10313fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn            Log.d(TAG, "showMMIDialog: no pending MMIs; finishing");
104598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            finish();
105598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
106598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
107598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
108598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    /**
109598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * Handles an MMI_COMPLETE event, which is triggered by telephony
110598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     */
111598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void onMMIComplete(MmiCode mmiCode) {
112598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // Check the code to see if the request is ready to
113598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // finish, this includes any MMI state that is not
114598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PENDING.
11513fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn        Log.d(TAG, "onMMIComplete: mmi=" + mmiCode);
116598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
117598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // if phone is a CDMA phone display feature code completed message
118598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        int phoneType = mPhone.getPhoneType();
119598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
120598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
121598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
122598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            if (mmiCode.getState() != MmiCode.State.PENDING) {
12313fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn                Log.d(TAG, "onMMIComplete: Got MMI_COMPLETE, finishing dialog activity...");
124598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee                dismissDialogsAndFinish();
12513fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn            } else {
12613fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn                Log.d(TAG, "onMMIComplete: still pending.");
127598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            }
128598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
129598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
130598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
131598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    /**
132598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * Handles an MMI_CANCEL event, which is triggered by the button
133598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
134598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     * @see PhoneUtils#cancelMmiCode(Phone)
135598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee     */
136598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void onMMICancel() {
137598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        Log.v(TAG, "onMMICancel()...");
138598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
139598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // First of all, cancel the outstanding MMI code (if possible.)
140598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        PhoneUtils.cancelMmiCode(mPhone);
141598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
142598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // Regardless of whether the current MMI code was cancelable, the
143598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PhoneApp will get an MMI_COMPLETE event very soon, which will
144598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // take us to the MMI Complete dialog (see
145598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // PhoneUtils.displayMMIComplete().)
146598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        //
147598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // But until that event comes in, we *don't* want to stay here on
148598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // the in-call screen, since we'll be visible in a
149598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // partially-constructed state as soon as the "MMI Started" dialog
150598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        // gets dismissed. So let's forcibly bail out right now.
15113fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn        Log.d(TAG, "onMMICancel: finishing MMI dialog...");
152598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        dismissDialogsAndFinish();
153598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
154598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee
155598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    private void dismissDialogsAndFinish() {
156598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (mMMIDialog != null) {
157598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mMMIDialog.dismiss();
158399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mMMIDialog = null;
159598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
160598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        if (mHandler != null) {
161598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee            mCM.unregisterForMmiComplete(mHandler);
162399d1e9218cef40d27f4252cd71c6ec3847ef242Luo, Honggang X            mHandler = null;
163598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        }
16413fe249b0ba05e6652a4ce2d5271be5b8f3a3672Tyler Gunn        Log.v(TAG, "dismissDialogsAndFinish");
165598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee        finish();
166598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee    }
167598dac56af25319ec4aa0f2a5d98adfc023d7ac4Yorke Lee}
168