The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 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 | |
Alex Yakavenka | d41f1d9 | 2010-07-12 14:13:13 -0700 | [diff] [blame] | 30 | import com.android.internal.telephony.cat.TextMessage; |
| 31 | import com.android.internal.telephony.cat.ToneSettings; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Activity used for PLAY TONE command. |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 35 | * |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 36 | */ |
| 37 | public class ToneDialog extends Activity { |
| 38 | TextMessage toneMsg = null; |
| 39 | ToneSettings settings = null; |
| 40 | TonePlayer player = null; |
Pierre Fröjd | 99fccc1 | 2011-01-19 13:46:57 +0100 | [diff] [blame^] | 41 | boolean mIsResponseSent = false; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 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 | |
Jeff Brown | cdda1a6 | 2012-04-13 02:15:37 -0700 | [diff] [blame] | 58 | Vibrator mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 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 | initFromIntent(getIntent()); |
| 68 | |
| 69 | // remove window title |
| 70 | View title = findViewById(com.android.internal.R.id.title); |
| 71 | title.setVisibility(View.GONE); |
| 72 | // set customized content view |
| 73 | setContentView(R.layout.stk_tone_dialog); |
| 74 | |
| 75 | TextView tv = (TextView) findViewById(R.id.message); |
| 76 | ImageView iv = (ImageView) findViewById(R.id.icon); |
| 77 | |
| 78 | // set text and icon |
| 79 | tv.setText(toneMsg.text); |
| 80 | if (toneMsg.icon == null) { |
| 81 | iv.setImageResource(com.android.internal.R.drawable.ic_volume); |
| 82 | } else { |
| 83 | iv.setImageBitmap(toneMsg.icon); |
| 84 | } |
| 85 | |
| 86 | // Start playing tone and vibration |
| 87 | player = new TonePlayer(); |
| 88 | player.play(settings.tone); |
| 89 | int timeout = StkApp.calculateDurationInMilis(settings.duration); |
| 90 | if (timeout == 0) { |
| 91 | timeout = StkApp.TONE_DFEAULT_TIMEOUT; |
| 92 | } |
| 93 | mToneStopper.sendEmptyMessageDelayed(MSG_ID_STOP_TONE, timeout); |
| 94 | if (settings.vibrate) { |
| 95 | mVibrator.vibrate(timeout); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected void onDestroy() { |
| 101 | super.onDestroy(); |
Pierre Fröjd | 99fccc1 | 2011-01-19 13:46:57 +0100 | [diff] [blame^] | 102 | if (mIsResponseSent) { |
| 103 | mToneStopper.removeMessages(MSG_ID_STOP_TONE); |
| 104 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 105 | player.stop(); |
| 106 | player.release(); |
| 107 | mVibrator.cancel(); |
| 108 | } |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 109 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 110 | @Override |
| 111 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 112 | switch (keyCode) { |
| 113 | case KeyEvent.KEYCODE_BACK: |
| 114 | sendResponse(StkAppService.RES_ID_END_SESSION); |
| 115 | finish(); |
| 116 | break; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | private void initFromIntent(Intent intent) { |
| 122 | if (intent == null) { |
| 123 | finish(); |
| 124 | } |
| 125 | toneMsg = intent.getParcelableExtra("TEXT"); |
| 126 | settings = intent.getParcelableExtra("TONE"); |
| 127 | } |
| 128 | |
| 129 | private void sendResponse(int resId) { |
| 130 | Bundle args = new Bundle(); |
| 131 | args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE); |
| 132 | args.putInt(StkAppService.RES_ID, resId); |
| 133 | startService(new Intent(this, StkAppService.class).putExtras(args)); |
Pierre Fröjd | 99fccc1 | 2011-01-19 13:46:57 +0100 | [diff] [blame^] | 134 | mIsResponseSent = true; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 135 | } |
| 136 | } |