1/*
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);
66        if (mCM.getState() == PhoneConstants.State.OFFHOOK) {
67            Toast.makeText(this, R.string.incall_status_dialed_mmi, Toast.LENGTH_SHORT).show();
68        }
69        showMMIDialog();
70    }
71
72    private void showMMIDialog() {
73        final List<? extends MmiCode> codes = mPhone.getPendingMmiCodes();
74        if (codes.size() > 0) {
75            final MmiCode mmiCode = codes.get(0);
76            final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
77            mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
78        } else {
79            finish();
80        }
81    }
82
83    /**
84     * Handles an MMI_COMPLETE event, which is triggered by telephony
85     */
86    private void onMMIComplete(MmiCode mmiCode) {
87        // Check the code to see if the request is ready to
88        // finish, this includes any MMI state that is not
89        // PENDING.
90
91        // if phone is a CDMA phone display feature code completed message
92        int phoneType = mPhone.getPhoneType();
93        if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
94            PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
95        } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
96            if (mmiCode.getState() != MmiCode.State.PENDING) {
97                Log.d(TAG, "Got MMI_COMPLETE, finishing dialog activity...");
98                dismissDialogsAndFinish();
99            }
100        }
101    }
102
103    /**
104     * Handles an MMI_CANCEL event, which is triggered by the button
105     * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
106     * @see PhoneUtils#cancelMmiCode(Phone)
107     */
108    private void onMMICancel() {
109        Log.v(TAG, "onMMICancel()...");
110
111        // First of all, cancel the outstanding MMI code (if possible.)
112        PhoneUtils.cancelMmiCode(mPhone);
113
114        // Regardless of whether the current MMI code was cancelable, the
115        // PhoneApp will get an MMI_COMPLETE event very soon, which will
116        // take us to the MMI Complete dialog (see
117        // PhoneUtils.displayMMIComplete().)
118        //
119        // But until that event comes in, we *don't* want to stay here on
120        // the in-call screen, since we'll be visible in a
121        // partially-constructed state as soon as the "MMI Started" dialog
122        // gets dismissed. So let's forcibly bail out right now.
123        Log.d(TAG, "onMMICancel: finishing InCallScreen...");
124        dismissDialogsAndFinish();
125    }
126
127    private void dismissDialogsAndFinish() {
128        if (mMMIDialog != null) {
129            mMMIDialog.dismiss();
130        }
131        if (mHandler != null) {
132            mCM.unregisterForMmiComplete(mHandler);
133        }
134        finish();
135    }
136}
137