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; |
Li Qiang | 075340e | 2013-08-13 09:59:44 +0200 | [diff] [blame] | 26 | import android.view.MotionEvent; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.widget.ImageView; |
| 29 | import android.widget.TextView; |
| 30 | |
Alex Yakavenka | d41f1d9 | 2010-07-12 14:13:13 -0700 | [diff] [blame] | 31 | import com.android.internal.telephony.cat.TextMessage; |
| 32 | import com.android.internal.telephony.cat.ToneSettings; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Activity used for PLAY TONE command. |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 36 | * |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 37 | */ |
| 38 | public class ToneDialog extends Activity { |
| 39 | TextMessage toneMsg = null; |
| 40 | ToneSettings settings = null; |
| 41 | TonePlayer player = null; |
Pierre Fröjd | 99fccc1 | 2011-01-19 13:46:57 +0100 | [diff] [blame] | 42 | boolean mIsResponseSent = false; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Handler used to stop tones from playing when the duration ends. |
| 46 | */ |
| 47 | Handler mToneStopper = new Handler() { |
| 48 | @Override |
| 49 | public void handleMessage(Message msg) { |
| 50 | switch (msg.what) { |
| 51 | case MSG_ID_STOP_TONE: |
| 52 | sendResponse(StkAppService.RES_ID_DONE); |
| 53 | finish(); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | }; |
| 58 | |
Wink Saville | a8e4f50 | 2012-09-26 16:43:29 -0700 | [diff] [blame] | 59 | Vibrator mVibrator; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 60 | |
| 61 | // Message id to signal tone duration timeout. |
| 62 | private static final int MSG_ID_STOP_TONE = 0xda; |
| 63 | |
| 64 | @Override |
| 65 | protected void onCreate(Bundle icicle) { |
| 66 | super.onCreate(icicle); |
| 67 | |
Wink Saville | a8e4f50 | 2012-09-26 16:43:29 -0700 | [diff] [blame] | 68 | mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE); |
| 69 | |
| 70 | initFromIntent(getIntent()); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 71 | |
| 72 | // remove window title |
| 73 | View title = findViewById(com.android.internal.R.id.title); |
| 74 | title.setVisibility(View.GONE); |
| 75 | // set customized content view |
| 76 | setContentView(R.layout.stk_tone_dialog); |
| 77 | |
| 78 | TextView tv = (TextView) findViewById(R.id.message); |
| 79 | ImageView iv = (ImageView) findViewById(R.id.icon); |
| 80 | |
| 81 | // set text and icon |
| 82 | tv.setText(toneMsg.text); |
| 83 | if (toneMsg.icon == null) { |
| 84 | iv.setImageResource(com.android.internal.R.drawable.ic_volume); |
| 85 | } else { |
| 86 | iv.setImageBitmap(toneMsg.icon); |
| 87 | } |
| 88 | |
| 89 | // Start playing tone and vibration |
| 90 | player = new TonePlayer(); |
| 91 | player.play(settings.tone); |
| 92 | int timeout = StkApp.calculateDurationInMilis(settings.duration); |
| 93 | if (timeout == 0) { |
| 94 | timeout = StkApp.TONE_DFEAULT_TIMEOUT; |
| 95 | } |
| 96 | mToneStopper.sendEmptyMessageDelayed(MSG_ID_STOP_TONE, timeout); |
| 97 | if (settings.vibrate) { |
| 98 | mVibrator.vibrate(timeout); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected void onDestroy() { |
| 104 | super.onDestroy(); |
Pierre Fröjd | 99fccc1 | 2011-01-19 13:46:57 +0100 | [diff] [blame] | 105 | if (mIsResponseSent) { |
| 106 | mToneStopper.removeMessages(MSG_ID_STOP_TONE); |
| 107 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 108 | player.stop(); |
| 109 | player.release(); |
| 110 | mVibrator.cancel(); |
| 111 | } |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 112 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 113 | @Override |
| 114 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 115 | switch (keyCode) { |
| 116 | case KeyEvent.KEYCODE_BACK: |
| 117 | sendResponse(StkAppService.RES_ID_END_SESSION); |
| 118 | finish(); |
| 119 | break; |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
Li Qiang | 075340e | 2013-08-13 09:59:44 +0200 | [diff] [blame] | 124 | @Override |
| 125 | public boolean onTouchEvent(MotionEvent event) { |
| 126 | switch (event.getAction()) { |
| 127 | case MotionEvent.ACTION_DOWN: |
| 128 | sendResponse(StkAppService.RES_ID_END_SESSION); |
| 129 | finish(); |
| 130 | return true; |
| 131 | } |
| 132 | return super.onTouchEvent(event); |
| 133 | } |
| 134 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 135 | private void initFromIntent(Intent intent) { |
| 136 | if (intent == null) { |
| 137 | finish(); |
| 138 | } |
| 139 | toneMsg = intent.getParcelableExtra("TEXT"); |
| 140 | settings = intent.getParcelableExtra("TONE"); |
| 141 | } |
| 142 | |
| 143 | private void sendResponse(int resId) { |
| 144 | Bundle args = new Bundle(); |
| 145 | args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE); |
| 146 | args.putInt(StkAppService.RES_ID, resId); |
| 147 | startService(new Intent(this, StkAppService.class).putExtras(args)); |
Pierre Fröjd | 99fccc1 | 2011-01-19 13:46:57 +0100 | [diff] [blame] | 148 | mIsResponseSent = true; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 149 | } |
| 150 | } |