Home | History | Annotate | Download | only in phone
      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 
     17 package com.android.phone;
     18 
     19 import android.app.Activity;
     20 import android.app.Dialog;
     21 import android.content.Intent;
     22 import android.os.AsyncResult;
     23 import android.os.Bundle;
     24 import android.os.Handler;
     25 import android.os.Message;
     26 import android.telephony.SubscriptionManager;
     27 import android.util.Log;
     28 import android.widget.Toast;
     29 
     30 import com.android.internal.telephony.CallManager;
     31 import com.android.internal.telephony.MmiCode;
     32 import com.android.internal.telephony.Phone;
     33 import com.android.internal.telephony.PhoneConstants;
     34 
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 
     38 /**
     39  * Used to display a dialog from within the Telephony service when running an USSD code
     40  */
     41 public class MMIDialogActivity extends Activity {
     42     private static final String TAG = MMIDialogActivity.class.getSimpleName();
     43 
     44     private Dialog mMMIDialog;
     45 
     46     private Handler mHandler;
     47 
     48     private CallManager mCM = PhoneGlobals.getInstance().getCallManager();
     49     private Phone mPhone;
     50 
     51 
     52     @Override
     53     protected void onCreate(Bundle savedInstanceState) {
     54         super.onCreate(savedInstanceState);
     55         Intent intent = getIntent();
     56         int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
     57                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
     58         mPhone = PhoneGlobals.getPhone(subId);
     59         mHandler = new Handler() {
     60                 @Override
     61                 public void handleMessage(Message msg) {
     62                     switch (msg.what) {
     63                         case PhoneGlobals.MMI_COMPLETE:
     64                             onMMIComplete((MmiCode) ((AsyncResult) msg.obj).result);
     65                             break;
     66                         case PhoneGlobals.MMI_CANCEL:
     67                             onMMICancel();
     68                             break;
     69                     }
     70                 }
     71         };
     72         Log.d(TAG, "onCreate; registering for mmi complete.");
     73         mCM.registerForMmiComplete(mHandler, PhoneGlobals.MMI_COMPLETE, null);
     74         showMMIDialog();
     75     }
     76 
     77     @Override
     78     protected void onDestroy() {
     79         super.onDestroy();
     80 
     81         if (mMMIDialog != null) {
     82             mMMIDialog.dismiss();
     83             mMMIDialog = null;
     84         }
     85         if (mHandler != null) {
     86             mCM.unregisterForMmiComplete(mHandler);
     87             mHandler = null;
     88         }
     89     }
     90 
     91     private void showMMIDialog() {
     92         final List<MmiCode> codes = new ArrayList<>(mPhone.getPendingMmiCodes());
     93         // If the phone has an IMS phone, also get pending imS MMIsl.
     94         if (mPhone.getImsPhone() != null) {
     95             codes.addAll(mPhone.getImsPhone().getPendingMmiCodes());
     96         }
     97         if (codes.size() > 0) {
     98             final MmiCode mmiCode = codes.get(0);
     99             final Message message = Message.obtain(mHandler, PhoneGlobals.MMI_CANCEL);
    100             Log.d(TAG, "showMMIDialog: mmiCode = " + mmiCode);
    101             mMMIDialog = PhoneUtils.displayMMIInitiate(this, mmiCode, message, mMMIDialog);
    102         } else {
    103             Log.d(TAG, "showMMIDialog: no pending MMIs; finishing");
    104             finish();
    105         }
    106     }
    107 
    108     /**
    109      * Handles an MMI_COMPLETE event, which is triggered by telephony
    110      */
    111     private void onMMIComplete(MmiCode mmiCode) {
    112         // Check the code to see if the request is ready to
    113         // finish, this includes any MMI state that is not
    114         // PENDING.
    115         Log.d(TAG, "onMMIComplete: mmi=" + mmiCode);
    116 
    117         // if phone is a CDMA phone display feature code completed message
    118         int phoneType = mPhone.getPhoneType();
    119         if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
    120             PhoneUtils.displayMMIComplete(mPhone, this, mmiCode, null, null);
    121         } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
    122             if (mmiCode.getState() != MmiCode.State.PENDING) {
    123                 Log.d(TAG, "onMMIComplete: Got MMI_COMPLETE, finishing dialog activity...");
    124                 dismissDialogsAndFinish();
    125             } else {
    126                 Log.d(TAG, "onMMIComplete: still pending.");
    127             }
    128         }
    129     }
    130 
    131     /**
    132      * Handles an MMI_CANCEL event, which is triggered by the button
    133      * (labeled either "OK" or "Cancel") on the "MMI Started" dialog.
    134      * @see PhoneUtils#cancelMmiCode(Phone)
    135      */
    136     private void onMMICancel() {
    137         Log.v(TAG, "onMMICancel()...");
    138 
    139         // First of all, cancel the outstanding MMI code (if possible.)
    140         PhoneUtils.cancelMmiCode(mPhone);
    141 
    142         // Regardless of whether the current MMI code was cancelable, the
    143         // PhoneApp will get an MMI_COMPLETE event very soon, which will
    144         // take us to the MMI Complete dialog (see
    145         // PhoneUtils.displayMMIComplete().)
    146         //
    147         // But until that event comes in, we *don't* want to stay here on
    148         // the in-call screen, since we'll be visible in a
    149         // partially-constructed state as soon as the "MMI Started" dialog
    150         // gets dismissed. So let's forcibly bail out right now.
    151         Log.d(TAG, "onMMICancel: finishing MMI dialog...");
    152         dismissDialogsAndFinish();
    153     }
    154 
    155     private void dismissDialogsAndFinish() {
    156         if (mMMIDialog != null) {
    157             mMMIDialog.dismiss();
    158             mMMIDialog = null;
    159         }
    160         if (mHandler != null) {
    161             mCM.unregisterForMmiComplete(mHandler);
    162             mHandler = null;
    163         }
    164         Log.v(TAG, "dismissDialogsAndFinish");
    165         finish();
    166     }
    167 }
    168