blob: b68340ba8fa286f40d48de7d3c3cf35210ee9374 [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;
Li Qiang075340e2013-08-13 09:59:44 +020026import android.view.MotionEvent;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080027import android.view.View;
28import android.widget.ImageView;
29import android.widget.TextView;
30
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070031import com.android.internal.telephony.cat.TextMessage;
32import com.android.internal.telephony.cat.ToneSettings;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080033
34/**
35 * Activity used for PLAY TONE command.
Wink Saville79085fc2009-06-09 10:27:23 -070036 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080037 */
38public class ToneDialog extends Activity {
39 TextMessage toneMsg = null;
40 ToneSettings settings = null;
41 TonePlayer player = null;
Pierre Fröjd99fccc12011-01-19 13:46:57 +010042 boolean mIsResponseSent = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080043
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 Savillea8e4f502012-09-26 16:43:29 -070059 Vibrator mVibrator;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080060
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 Savillea8e4f502012-09-26 16:43:29 -070068 mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
69
70 initFromIntent(getIntent());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080071
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öjd99fccc12011-01-19 13:46:57 +0100105 if (mIsResponseSent) {
106 mToneStopper.removeMessages(MSG_ID_STOP_TONE);
107 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800108 player.stop();
109 player.release();
110 mVibrator.cancel();
111 }
Wink Saville79085fc2009-06-09 10:27:23 -0700112
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800113 @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 Qiang075340e2013-08-13 09:59:44 +0200124 @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 Project9d9730a2009-03-03 19:32:37 -0800135 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öjd99fccc12011-01-19 13:46:57 +0100148 mIsResponseSent = true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800149 }
150}