Home | History | Annotate | Download | only in stk
      1 /*
      2  * Copyright (C) 2007 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.stk;
     18 
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.DialogInterface;
     24 import android.content.Intent;
     25 import android.content.IntentFilter;
     26 import android.os.Bundle;
     27 import android.os.Handler;
     28 import android.os.Message;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.widget.ImageView;
     32 import android.widget.TextView;
     33 import com.android.internal.telephony.cat.CatLog;
     34 import com.android.internal.telephony.cat.TextMessage;
     35 import com.android.internal.telephony.cat.CatLog;
     36 
     37 /**
     38  * Activity used to display tone dialog.
     39  *
     40  */
     41 public class ToneDialog extends Activity {
     42     TextMessage toneMsg = null;
     43     int mSlotId = -1;
     44     private AlertDialog mAlertDialog;
     45 
     46     private static final String LOG_TAG = new Object(){}.getClass().getEnclosingClass().getName();
     47 
     48     @Override
     49     protected void onCreate(Bundle icicle) {
     50         super.onCreate(icicle);
     51 
     52         CatLog.d(LOG_TAG, "onCreate");
     53         initFromIntent(getIntent());
     54         // Register receiver
     55         IntentFilter filter = new IntentFilter();
     56         filter.addAction(StkAppService.FINISH_TONE_ACTIVITY_ACTION);
     57         registerReceiver(mFinishActivityReceiver, filter);
     58 
     59         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
     60         LayoutInflater inflater = this.getLayoutInflater();
     61         View dialogView = inflater.inflate(R.layout.stk_tone_dialog, null);
     62         alertDialogBuilder.setView(dialogView);
     63 
     64         TextView tv = (TextView) dialogView.findViewById(R.id.message);
     65         ImageView iv = (ImageView) dialogView.findViewById(R.id.icon);
     66 
     67         // set text and icon
     68         if ((null == toneMsg) || (null == toneMsg.text) || (toneMsg.text.equals(""))) {
     69             CatLog.d(LOG_TAG, "onCreate - null tone text");
     70         } else {
     71             tv.setText(toneMsg.text);
     72         }
     73 
     74         if (toneMsg.icon == null) {
     75             iv.setImageResource(com.android.internal.R.drawable.ic_volume);
     76         } else {
     77             iv.setImageBitmap(toneMsg.icon);
     78         }
     79 
     80         if (toneMsg.iconSelfExplanatory && toneMsg.icon != null) {
     81             tv.setVisibility(View.GONE);
     82         }
     83 
     84         alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
     85                     @Override
     86                     public void onCancel(DialogInterface dialog) {
     87                         sendStopTone();
     88                         finish();
     89                     }
     90                 });
     91 
     92         mAlertDialog = alertDialogBuilder.create();
     93         mAlertDialog.show();
     94     }
     95 
     96     @Override
     97     protected void onDestroy() {
     98         CatLog.d(LOG_TAG, "onDestroy");
     99         super.onDestroy();
    100 
    101         unregisterReceiver(mFinishActivityReceiver);
    102 
    103         if (mAlertDialog != null && mAlertDialog.isShowing()) {
    104             mAlertDialog.dismiss();
    105             mAlertDialog = null;
    106         }
    107     }
    108 
    109     private BroadcastReceiver mFinishActivityReceiver = new BroadcastReceiver() {
    110         @Override
    111         public void onReceive(Context context, Intent intent) {
    112             // Intent received from StkAppService to finish ToneDialog activity,
    113             // after finishing off playing the tone.
    114             if (intent.getAction().equals(StkAppService.FINISH_TONE_ACTIVITY_ACTION)) {
    115                 CatLog.d(this, "Finishing Tone dialog activity");
    116                 finish();
    117             }
    118         }
    119     };
    120 
    121     private void initFromIntent(Intent intent) {
    122         if (intent == null) {
    123             finish();
    124         }
    125         toneMsg = intent.getParcelableExtra("TEXT");
    126         mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
    127     }
    128 
    129     // Send stop playing tone to StkAppService, when user presses back key.
    130     private void sendStopTone() {
    131         Bundle args = new Bundle();
    132         args.putInt(StkAppService.OPCODE, StkAppService.OP_STOP_TONE_USER);
    133         args.putInt(StkAppService.SLOT_ID, mSlotId);
    134         startService(new Intent(this, StkAppService.class).putExtras(args));
    135     }
    136 }
    137