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.content.Intent;
     21 import android.os.Bundle;
     22 import android.os.Handler;
     23 import android.os.Message;
     24 import android.os.Vibrator;
     25 import android.view.KeyEvent;
     26 import android.view.View;
     27 import android.widget.ImageView;
     28 import android.widget.TextView;
     29 
     30 import com.android.internal.telephony.cat.TextMessage;
     31 import com.android.internal.telephony.cat.ToneSettings;
     32 
     33 /**
     34  * Activity used for PLAY TONE command.
     35  *
     36  */
     37 public class ToneDialog extends Activity {
     38     TextMessage toneMsg = null;
     39     ToneSettings settings = null;
     40     TonePlayer player = null;
     41     boolean mIsResponseSent = false;
     42 
     43     /**
     44      * Handler used to stop tones from playing when the duration ends.
     45      */
     46     Handler mToneStopper = new Handler() {
     47         @Override
     48         public void handleMessage(Message msg) {
     49             switch (msg.what) {
     50             case MSG_ID_STOP_TONE:
     51                 sendResponse(StkAppService.RES_ID_DONE);
     52                 finish();
     53                 break;
     54             }
     55         }
     56     };
     57 
     58     Vibrator mVibrator;
     59 
     60     // Message id to signal tone duration timeout.
     61     private static final int MSG_ID_STOP_TONE = 0xda;
     62 
     63     @Override
     64     protected void onCreate(Bundle icicle) {
     65         super.onCreate(icicle);
     66 
     67         mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
     68 
     69         initFromIntent(getIntent());
     70 
     71         // remove window title
     72         View title = findViewById(com.android.internal.R.id.title);
     73         title.setVisibility(View.GONE);
     74         // set customized content view
     75         setContentView(R.layout.stk_tone_dialog);
     76 
     77         TextView tv = (TextView) findViewById(R.id.message);
     78         ImageView iv = (ImageView) findViewById(R.id.icon);
     79 
     80         // set text and icon
     81         tv.setText(toneMsg.text);
     82         if (toneMsg.icon == null) {
     83             iv.setImageResource(com.android.internal.R.drawable.ic_volume);
     84         } else {
     85             iv.setImageBitmap(toneMsg.icon);
     86         }
     87 
     88         // Start playing tone and vibration
     89         player = new TonePlayer();
     90         player.play(settings.tone);
     91         int timeout = StkApp.calculateDurationInMilis(settings.duration);
     92         if (timeout == 0) {
     93             timeout = StkApp.TONE_DFEAULT_TIMEOUT;
     94         }
     95         mToneStopper.sendEmptyMessageDelayed(MSG_ID_STOP_TONE, timeout);
     96         if (settings.vibrate) {
     97             mVibrator.vibrate(timeout);
     98         }
     99     }
    100 
    101     @Override
    102     protected void onDestroy() {
    103         super.onDestroy();
    104         if (mIsResponseSent) {
    105             mToneStopper.removeMessages(MSG_ID_STOP_TONE);
    106         }
    107         player.stop();
    108         player.release();
    109         mVibrator.cancel();
    110     }
    111 
    112     @Override
    113     public boolean onKeyDown(int keyCode, KeyEvent event) {
    114         switch (keyCode) {
    115         case KeyEvent.KEYCODE_BACK:
    116             sendResponse(StkAppService.RES_ID_END_SESSION);
    117             finish();
    118             break;
    119         }
    120         return false;
    121     }
    122 
    123     private void initFromIntent(Intent intent) {
    124         if (intent == null) {
    125             finish();
    126         }
    127         toneMsg = intent.getParcelableExtra("TEXT");
    128         settings = intent.getParcelableExtra("TONE");
    129     }
    130 
    131     private void sendResponse(int resId) {
    132         Bundle args = new Bundle();
    133         args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
    134         args.putInt(StkAppService.RES_ID, resId);
    135         startService(new Intent(this, StkAppService.class).putExtras(args));
    136         mIsResponseSent = true;
    137     }
    138 }
    139