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