ToneDialog.java revision 99fccc1c113d9b7a030dc7c945d0750748cc4af9
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    boolean mIsResponseSent = false;
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
58    Vibrator mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
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();
102        if (mIsResponseSent) {
103            mToneStopper.removeMessages(MSG_ID_STOP_TONE);
104        }
105        player.stop();
106        player.release();
107        mVibrator.cancel();
108    }
109
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));
134        mIsResponseSent = true;
135    }
136}
137