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 
17 package com.android.stk;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.os.Vibrator;
26 import android.view.KeyEvent;
27 import android.view.LayoutInflater;
28 import android.view.MotionEvent;
29 import android.view.View;
30 import android.widget.ImageView;
31 import android.widget.TextView;
32 import com.android.internal.telephony.cat.CatLog;
33 import com.android.internal.telephony.cat.TextMessage;
34 import com.android.internal.telephony.cat.ToneSettings;
35 
36 /**
37  * Activity used for PLAY TONE command.
38  *
39  */
40 public class ToneDialog extends Activity {
41     TextMessage toneMsg = null;
42     ToneSettings settings = null;
43     TonePlayer player = null;
44     int mSlotId = -1;
45     boolean mIsResponseSent = false;
46 
47     private static final String LOG_TAG = new Object(){}.getClass().getEnclosingClass().getName();
48 
49     /**
50      * Handler used to stop tones from playing when the duration ends.
51      */
52     Handler mToneStopper = new Handler() {
53         @Override
54         public void handleMessage(Message msg) {
55             switch (msg.what) {
56             case MSG_ID_STOP_TONE:
57                 sendResponse(StkAppService.RES_ID_DONE);
58                 finish();
59                 break;
60             }
61         }
62     };
63 
64     Vibrator mVibrator;
65 
66     // Message id to signal tone duration timeout.
67     private static final int MSG_ID_STOP_TONE = 0xda;
68 
69     @Override
onCreate(Bundle icicle)70     protected void onCreate(Bundle icicle) {
71         super.onCreate(icicle);
72 
73         CatLog.d(LOG_TAG, "onCreate");
74         mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
75 
76         initFromIntent(getIntent());
77 
78         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
79         LayoutInflater inflater = this.getLayoutInflater();
80         View dialogView = inflater.inflate(R.layout.stk_tone_dialog, null);
81         alertDialogBuilder.setView(dialogView);
82 
83         TextView tv = (TextView) dialogView.findViewById(R.id.message);
84         ImageView iv = (ImageView) dialogView.findViewById(R.id.icon);
85 
86         // set text and icon
87         if ((null == toneMsg) || (null == toneMsg.text) || (toneMsg.text.equals(""))) {
88             CatLog.d(LOG_TAG, "onCreate - null tone text");
89         } else {
90             tv.setText(toneMsg.text);
91         }
92 
93         if (toneMsg.icon == null) {
94             iv.setImageResource(com.android.internal.R.drawable.ic_volume);
95         } else {
96             iv.setImageBitmap(toneMsg.icon);
97         }
98 
99         if (toneMsg.iconSelfExplanatory && toneMsg.icon != null) {
100             tv.setVisibility(View.GONE);
101         }
102 
103         // Start playing tone and vibration
104         if (null == settings) {
105             CatLog.d(LOG_TAG, "onCreate - null settings - finish");
106             finish();
107             return;
108         }
109 
110         player = new TonePlayer();
111         player.play(settings.tone);
112         int timeout = StkApp.calculateDurationInMilis(settings.duration);
113         if (timeout == 0) {
114             timeout = StkApp.TONE_DFEAULT_TIMEOUT;
115         }
116         mToneStopper.sendEmptyMessageDelayed(MSG_ID_STOP_TONE, timeout);
117         if (settings.vibrate && mVibrator != null) {
118             mVibrator.vibrate(timeout);
119         }
120 
121         alertDialogBuilder.create().show();
122     }
123 
124     @Override
onDestroy()125     protected void onDestroy() {
126         super.onDestroy();
127         CatLog.d(LOG_TAG, "onDestroy");
128 
129         mToneStopper.removeMessages(MSG_ID_STOP_TONE);
130         if (!mIsResponseSent) {
131             sendResponse(StkAppService.RES_ID_END_SESSION);
132         }
133 
134         if (null != player) {
135             player.stop();
136             player.release();
137         }
138         if (null != mVibrator) {
139             mVibrator.cancel();
140         }
141     }
142 
143     @Override
onKeyDown(int keyCode, KeyEvent event)144     public boolean onKeyDown(int keyCode, KeyEvent event) {
145         switch (keyCode) {
146         case KeyEvent.KEYCODE_BACK:
147             sendResponse(StkAppService.RES_ID_END_SESSION);
148             finish();
149             break;
150         }
151         return false;
152     }
153 
154     @Override
onTouchEvent(MotionEvent event)155     public boolean onTouchEvent(MotionEvent event) {
156         switch (event.getAction()) {
157         case MotionEvent.ACTION_DOWN:
158             sendResponse(StkAppService.RES_ID_END_SESSION);
159             finish();
160             return true;
161         }
162         return super.onTouchEvent(event);
163     }
164 
initFromIntent(Intent intent)165     private void initFromIntent(Intent intent) {
166         if (intent == null) {
167             finish();
168         }
169         toneMsg = intent.getParcelableExtra("TEXT");
170         settings = intent.getParcelableExtra("TONE");
171         mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
172     }
173 
sendResponse(int resId)174     private void sendResponse(int resId) {
175         Bundle args = new Bundle();
176         args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
177         args.putInt(StkAppService.SLOT_ID, mSlotId);
178         args.putInt(StkAppService.RES_ID, resId);
179         startService(new Intent(this, StkAppService.class).putExtras(args));
180         mIsResponseSent = true;
181     }
182 }
183