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