blob: 037093913b712850264d0c86f206ed5c86e040d5 [file] [log] [blame]
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001/*
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
17package com.android.stk;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.os.Bundle;
22import android.os.Handler;
23import android.os.Message;
24import android.os.Vibrator;
25import android.view.KeyEvent;
26import android.view.View;
27import android.widget.ImageView;
28import android.widget.TextView;
29
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070030import com.android.internal.telephony.cat.TextMessage;
31import com.android.internal.telephony.cat.ToneSettings;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080032
33/**
34 * Activity used for PLAY TONE command.
Wink Saville79085fc2009-06-09 10:27:23 -070035 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080036 */
37public class ToneDialog extends Activity {
38 TextMessage toneMsg = null;
39 ToneSettings settings = null;
40 TonePlayer player = null;
Pierre Fröjd99fccc12011-01-19 13:46:57 +010041 boolean mIsResponseSent = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080042
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 Browncdda1a62012-04-13 02:15:37 -070058 Vibrator mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080059
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öjd99fccc12011-01-19 13:46:57 +0100102 if (mIsResponseSent) {
103 mToneStopper.removeMessages(MSG_ID_STOP_TONE);
104 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800105 player.stop();
106 player.release();
107 mVibrator.cancel();
108 }
Wink Saville79085fc2009-06-09 10:27:23 -0700109
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800110 @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öjd99fccc12011-01-19 13:46:57 +0100134 mIsResponseSent = true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800135 }
136}