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 = ToneDialog.class.getSimpleName();
47 
48     @Override
onCreate(Bundle icicle)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, Context.RECEIVER_NOT_EXPORTED);
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         alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
85                     @Override
86                     public void onCancel(DialogInterface dialog) {
87                         sendStopTone();
88                         finish();
89                     }
90                 });
91 
92         mAlertDialog = alertDialogBuilder.create();
93         mAlertDialog.show();
94 
95         StkAppService appService = StkAppService.getInstance();
96         // Finish the activity if the specified duration is too short and timed-out already.
97         if (appService != null && (appService.isNoTonePlaying())) {
98             finish();
99         }
100     }
101 
102     @Override
onDestroy()103     protected void onDestroy() {
104         CatLog.d(LOG_TAG, "onDestroy");
105         super.onDestroy();
106 
107         unregisterReceiver(mFinishActivityReceiver);
108 
109         if (mAlertDialog != null && mAlertDialog.isShowing()) {
110             mAlertDialog.dismiss();
111             mAlertDialog = null;
112         }
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(LOG_TAG, "Finishing Tone dialog activity");
122                 finish();
123             }
124         }
125     };
126 
initFromIntent(Intent intent)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.
sendStopTone()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