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.app.AlertDialog;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.Message;
28import android.view.KeyEvent;
29import android.view.LayoutInflater;
30import android.view.MotionEvent;
31import android.view.View;
32import android.widget.ImageView;
33import android.widget.TextView;
34import com.android.internal.telephony.cat.CatLog;
35import com.android.internal.telephony.cat.TextMessage;
36import com.android.internal.telephony.cat.CatLog;
37
38/**
39 * Activity used to display tone dialog.
40 *
41 */
42public class ToneDialog extends Activity {
43    TextMessage toneMsg = null;
44    int mSlotId = -1;
45
46    private static final String LOG_TAG = new Object(){}.getClass().getEnclosingClass().getName();
47
48    @Override
49    protected void onCreate(Bundle icicle) {
50        super.onCreate(icicle);
51
52        CatLog.d(LOG_TAG, "onCreate");
53        initFromIntent(getIntent());
54        // Register receiver
55        IntentFilter filter = new IntentFilter();
56        filter.addAction(StkAppService.FINISH_TONE_ACTIVITY_ACTION);
57        registerReceiver(mFinishActivityReceiver, filter);
58
59        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
60        LayoutInflater inflater = this.getLayoutInflater();
61        View dialogView = inflater.inflate(R.layout.stk_tone_dialog, null);
62        alertDialogBuilder.setView(dialogView);
63
64        TextView tv = (TextView) dialogView.findViewById(R.id.message);
65        ImageView iv = (ImageView) dialogView.findViewById(R.id.icon);
66
67        // set text and icon
68        if ((null == toneMsg) || (null == toneMsg.text) || (toneMsg.text.equals(""))) {
69            CatLog.d(LOG_TAG, "onCreate - null tone text");
70        } else {
71            tv.setText(toneMsg.text);
72        }
73
74        if (toneMsg.icon == null) {
75            iv.setImageResource(com.android.internal.R.drawable.ic_volume);
76        } else {
77            iv.setImageBitmap(toneMsg.icon);
78        }
79
80        if (toneMsg.iconSelfExplanatory && toneMsg.icon != null) {
81            tv.setVisibility(View.GONE);
82        }
83    }
84
85    @Override
86    protected void onDestroy() {
87        CatLog.d(LOG_TAG, "onDestroy");
88        // Unregister receiver
89        unregisterReceiver(mFinishActivityReceiver);
90        super.onDestroy();
91    }
92
93    @Override
94    public boolean onKeyDown(int keyCode, KeyEvent event) {
95        switch (keyCode) {
96        case KeyEvent.KEYCODE_BACK:
97            sendStopTone();
98            finish();
99            break;
100        }
101        return false;
102    }
103
104    @Override
105    public boolean onTouchEvent(MotionEvent event) {
106        switch (event.getAction()) {
107        case MotionEvent.ACTION_DOWN:
108            sendStopTone();
109            finish();
110            return true;
111        }
112        return super.onTouchEvent(event);
113    }
114
115    private BroadcastReceiver mFinishActivityReceiver = new BroadcastReceiver() {
116        @Override
117        public void onReceive(Context context, Intent intent) {
118            // Intent received from StkAppService to finish ToneDialog activity,
119            // after finishing off playing the tone.
120            if (intent.getAction().equals(StkAppService.FINISH_TONE_ACTIVITY_ACTION)) {
121                CatLog.d(this, "Finishing Tone dialog activity");
122                finish();
123            }
124        }
125    };
126
127    private void initFromIntent(Intent intent) {
128        if (intent == null) {
129            finish();
130        }
131        toneMsg = intent.getParcelableExtra("TEXT");
132        mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
133    }
134
135    // Send stop playing tone to StkAppService, when user presses back key.
136    private void sendStopTone() {
137        Bundle args = new Bundle();
138        args.putInt(StkAppService.OPCODE, StkAppService.OP_STOP_TONE_USER);
139        args.putInt(StkAppService.SLOT_ID, mSlotId);
140        startService(new Intent(this, StkAppService.class).putExtras(args));
141    }
142}
143