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 
97     @Override
onDestroy()98     protected void onDestroy() {
99         CatLog.d(LOG_TAG, "onDestroy");
100         super.onDestroy();
101 
102         unregisterReceiver(mFinishActivityReceiver);
103 
104         if (mAlertDialog != null && mAlertDialog.isShowing()) {
105             mAlertDialog.dismiss();
106             mAlertDialog = null;
107         }
108     }
109 
110     private BroadcastReceiver mFinishActivityReceiver = new BroadcastReceiver() {
111         @Override
112         public void onReceive(Context context, Intent intent) {
113             // Intent received from StkAppService to finish ToneDialog activity,
114             // after finishing off playing the tone.
115             if (intent.getAction().equals(StkAppService.FINISH_TONE_ACTIVITY_ACTION)) {
116                 CatLog.d(LOG_TAG, "Finishing Tone dialog activity");
117                 finish();
118             }
119         }
120     };
121 
initFromIntent(Intent intent)122     private void initFromIntent(Intent intent) {
123         if (intent == null) {
124             finish();
125         }
126         toneMsg = intent.getParcelableExtra("TEXT");
127         mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
128     }
129 
130     // Send stop playing tone to StkAppService, when user presses back key.
sendStopTone()131     private void sendStopTone() {
132         Bundle args = new Bundle();
133         args.putInt(StkAppService.OPCODE, StkAppService.OP_STOP_TONE_USER);
134         args.putInt(StkAppService.SLOT_ID, mSlotId);
135         startService(new Intent(this, StkAppService.class).putExtras(args));
136     }
137 }
138