blob: cf81f03084a8a16893be6c34bf08e115c1206d8e [file] [log] [blame]
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001/*
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
Christopher Posselwhitecfe4d7f2013-09-04 09:01:50 +020019import com.android.internal.telephony.cat.CatLog;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070020import com.android.internal.telephony.cat.TextMessage;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080021
22import android.app.Activity;
23import android.content.Intent;
24import android.graphics.drawable.BitmapDrawable;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.Message;
28import android.view.KeyEvent;
29import android.view.View;
30import android.view.Window;
31import android.widget.Button;
32import android.widget.TextView;
33
34/**
35 * AlretDialog used for DISPLAY TEXT commands.
Wink Saville79085fc2009-06-09 10:27:23 -070036 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080037 */
38public class StkDialogActivity extends Activity implements View.OnClickListener {
39 // members
40 TextMessage mTextMsg;
41
Johannes Carlssonef243a22011-10-25 10:37:24 +020042 private boolean mIsResponseSent = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080043 Handler mTimeoutHandler = new Handler() {
44 @Override
45 public void handleMessage(Message msg) {
46 switch(msg.what) {
47 case MSG_ID_TIMEOUT:
48 sendResponse(StkAppService.RES_ID_TIMEOUT);
49 finish();
50 break;
51 }
52 }
53 };
54
55 //keys) for saving the state of the dialog in the icicle
56 private static final String TEXT = "text";
57
Wink Saville79085fc2009-06-09 10:27:23 -070058 // message id for time out
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080059 private static final int MSG_ID_TIMEOUT = 1;
60
61 // buttons id
62 public static final int OK_BUTTON = R.id.button_ok;
63 public static final int CANCEL_BUTTON = R.id.button_cancel;
64
65 @Override
66 protected void onCreate(Bundle icicle) {
67 super.onCreate(icicle);
68
69 initFromIntent(getIntent());
70 if (mTextMsg == null) {
71 finish();
72 return;
73 }
74
75 requestWindowFeature(Window.FEATURE_LEFT_ICON);
76 Window window = getWindow();
77
78 setContentView(R.layout.stk_msg_dialog);
79 TextView mMessageView = (TextView) window
80 .findViewById(R.id.dialog_message);
81
82 Button okButton = (Button) findViewById(R.id.button_ok);
83 Button cancelButton = (Button) findViewById(R.id.button_cancel);
84
85 okButton.setOnClickListener(this);
86 cancelButton.setOnClickListener(this);
87
88 setTitle(mTextMsg.title);
89 if (!(mTextMsg.iconSelfExplanatory && mTextMsg.icon != null)) {
Wink Saville79085fc2009-06-09 10:27:23 -070090 mMessageView.setText(mTextMsg.text);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080091 }
92
93 if (mTextMsg.icon == null) {
94 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
95 com.android.internal.R.drawable.stat_notify_sim_toolkit);
96 } else {
97 window.setFeatureDrawable(Window.FEATURE_LEFT_ICON,
98 new BitmapDrawable(mTextMsg.icon));
99 }
100 }
101
102 public void onClick(View v) {
103 String input = null;
104
105 switch (v.getId()) {
106 case OK_BUTTON:
107 sendResponse(StkAppService.RES_ID_CONFIRM, true);
108 finish();
109 break;
110 case CANCEL_BUTTON:
111 sendResponse(StkAppService.RES_ID_CONFIRM, false);
112 finish();
113 break;
114 }
115 }
116
117 @Override
118 public boolean onKeyDown(int keyCode, KeyEvent event) {
119 switch (keyCode) {
120 case KeyEvent.KEYCODE_BACK:
121 sendResponse(StkAppService.RES_ID_BACKWARD);
122 finish();
123 break;
124 }
125 return false;
126 }
127
128 @Override
129 public void onResume() {
130 super.onResume();
Christopher Posselwhitecfe4d7f2013-09-04 09:01:50 +0200131
132 /*
133 * The user should be shown the message forever or until some high
134 * priority event occurs (such as incoming call, MMI code execution
135 * etc as mentioned in ETSI 102.223, 6.4.1).
136 *
137 * Since mTextMsg.responseNeeded is false (because the response has
138 * already been sent) and duration of the dialog is zero and userClear
139 * is true, don't set the timeout.
140 */
141 if (!mTextMsg.responseNeeded &&
142 StkApp.calculateDurationInMilis(mTextMsg.duration) == 0 &&
143 mTextMsg.userClear) {
144 CatLog.d(this, "User should clear text..show message forever");
145 return;
146 }
147
Christopher.Posselwhite1772c042012-11-22 12:15:54 +0100148 startTimeOut(mTextMsg.userClear);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800149 }
150
151 @Override
152 public void onPause() {
153 super.onPause();
154
155 cancelTimeOut();
156 }
157
158 @Override
Johannes Carlssonef243a22011-10-25 10:37:24 +0200159 protected void onStart() {
160 super.onStart();
161 mIsResponseSent = false;
162 }
163
164 @Override
165 public void onStop() {
166 super.onStop();
167 if (!mIsResponseSent) {
168 sendResponse(StkAppService.RES_ID_TIMEOUT);
169 }
170 }
171
172 @Override
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800173 public void onSaveInstanceState(Bundle outState) {
174 super.onSaveInstanceState(outState);
175
176 outState.putParcelable(TEXT, mTextMsg);
177 }
178
179 @Override
180 public void onRestoreInstanceState(Bundle savedInstanceState) {
181 super.onRestoreInstanceState(savedInstanceState);
Wink Saville79085fc2009-06-09 10:27:23 -0700182
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800183 mTextMsg = savedInstanceState.getParcelable(TEXT);
184 }
185
186 private void sendResponse(int resId, boolean confirmed) {
187 Bundle args = new Bundle();
188 args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
189 args.putInt(StkAppService.RES_ID, resId);
190 args.putBoolean(StkAppService.CONFIRMATION, confirmed);
191 startService(new Intent(this, StkAppService.class).putExtras(args));
Johannes Carlssonef243a22011-10-25 10:37:24 +0200192 mIsResponseSent = true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800193 }
194
195 private void sendResponse(int resId) {
196 sendResponse(resId, true);
197 }
198
199 private void initFromIntent(Intent intent) {
200
201 if (intent != null) {
202 mTextMsg = intent.getParcelableExtra("TEXT");
203 } else {
204 finish();
205 }
206 }
207
208 private void cancelTimeOut() {
209 mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
210 }
211
Christopher.Posselwhite1772c042012-11-22 12:15:54 +0100212 private void startTimeOut(boolean waitForUserToClear) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800213 // Reset timeout.
214 cancelTimeOut();
215 int dialogDuration = StkApp.calculateDurationInMilis(mTextMsg.duration);
Christopher.Posselwhite1772c042012-11-22 12:15:54 +0100216 // If duration is specified, this has priority. If not, set timeout
217 // according to condition given by the card.
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800218 if (dialogDuration == 0) {
Christopher.Posselwhite1772c042012-11-22 12:15:54 +0100219 if (waitForUserToClear) {
220 dialogDuration = StkApp.DISP_TEXT_WAIT_FOR_USER_TIMEOUT;
221 } else {
222 dialogDuration = StkApp.DISP_TEXT_CLEAR_AFTER_DELAY_TIMEOUT;
223 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800224 }
225 mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
226 .obtainMessage(MSG_ID_TIMEOUT), dialogDuration);
227 }
228}