ToneDialog.java revision d41f1d98d7bf42e9e867819593ad79f685604796
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
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
30import com.android.internal.telephony.cat.TextMessage;
31import com.android.internal.telephony.cat.ToneSettings;
32
33/**
34 * Activity used for PLAY TONE command.
35 *
36 */
37public class ToneDialog extends Activity {
38    TextMessage toneMsg = null;
39    ToneSettings settings = null;
40    TonePlayer player = null;
41
42    /**
43     * Handler used to stop tones from playing when the duration ends.
44     */
45    Handler mToneStopper = new Handler() {
46        @Override
47        public void handleMessage(Message msg) {
48            switch (msg.what) {
49            case MSG_ID_STOP_TONE:
50                sendResponse(StkAppService.RES_ID_DONE);
51                finish();
52                break;
53            }
54        }
55    };
56
57    Vibrator mVibrator = new Vibrator();
58
59    // Message id to signal tone duration timeout.
60    private static final int MSG_ID_STOP_TONE = 0xda;
61
62    @Override
63    protected void onCreate(Bundle icicle) {
64        super.onCreate(icicle);
65
66       initFromIntent(getIntent());
67
68        // remove window title
69        View title = findViewById(com.android.internal.R.id.title);
70        title.setVisibility(View.GONE);
71        // set customized content view
72        setContentView(R.layout.stk_tone_dialog);
73
74        TextView tv = (TextView) findViewById(R.id.message);
75        ImageView iv = (ImageView) findViewById(R.id.icon);
76
77        // set text and icon
78        tv.setText(toneMsg.text);
79        if (toneMsg.icon == null) {
80            iv.setImageResource(com.android.internal.R.drawable.ic_volume);
81        } else {
82            iv.setImageBitmap(toneMsg.icon);
83        }
84
85        // Start playing tone and vibration
86        player = new TonePlayer();
87        player.play(settings.tone);
88        int timeout = StkApp.calculateDurationInMilis(settings.duration);
89        if (timeout == 0) {
90            timeout = StkApp.TONE_DFEAULT_TIMEOUT;
91        }
92        mToneStopper.sendEmptyMessageDelayed(MSG_ID_STOP_TONE, timeout);
93        if (settings.vibrate) {
94            mVibrator.vibrate(timeout);
95        }
96    }
97
98    @Override
99    protected void onDestroy() {
100        super.onDestroy();
101
102        mToneStopper.removeMessages(MSG_ID_STOP_TONE);
103        player.stop();
104        player.release();
105        mVibrator.cancel();
106    }
107
108    @Override
109    public boolean onKeyDown(int keyCode, KeyEvent event) {
110        switch (keyCode) {
111        case KeyEvent.KEYCODE_BACK:
112            sendResponse(StkAppService.RES_ID_END_SESSION);
113            finish();
114            break;
115        }
116        return false;
117    }
118
119    private void initFromIntent(Intent intent) {
120        if (intent == null) {
121            finish();
122        }
123        toneMsg = intent.getParcelableExtra("TEXT");
124        settings = intent.getParcelableExtra("TONE");
125    }
126
127    private void sendResponse(int resId) {
128        Bundle args = new Bundle();
129        args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
130        args.putInt(StkAppService.RES_ID, resId);
131        startService(new Intent(this, StkAppService.class).putExtras(args));
132    }
133}
134