StkDialogActivity.java revision 9591934f86a8e10c1f9c4612883f8b6164ea427f
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
17package com.android.stk;
18
19import com.android.internal.telephony.cat.CatLog;
20import com.android.internal.telephony.cat.TextMessage;
21
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.
36 *
37 */
38public class StkDialogActivity extends Activity implements View.OnClickListener {
39    // members
40    private static final String className = new Object(){}.getClass().getEnclosingClass().getName();
41    private static final String LOG_TAG = className.substring(className.lastIndexOf('.') + 1);
42    TextMessage mTextMsg = null;
43    private int mSlotId = -1;
44    private StkAppService appService = StkAppService.getInstance();
45    private boolean mIsResponseSent = false;
46
47    Handler mTimeoutHandler = new Handler() {
48        @Override
49        public void handleMessage(Message msg) {
50            switch(msg.what) {
51            case MSG_ID_TIMEOUT:
52                CatLog.d(LOG_TAG, "MSG_ID_TIMEOUT finish.");
53                sendResponse(StkAppService.RES_ID_TIMEOUT);
54                finish();
55                break;
56            }
57        }
58    };
59
60    //keys) for saving the state of the dialog in the icicle
61    private static final String TEXT = "text";
62
63    // message id for time out
64    private static final int MSG_ID_TIMEOUT = 1;
65
66    // buttons id
67    public static final int OK_BUTTON = R.id.button_ok;
68    public static final int CANCEL_BUTTON = R.id.button_cancel;
69
70    @Override
71    protected void onCreate(Bundle icicle) {
72        super.onCreate(icicle);
73
74        CatLog.d(LOG_TAG, "onCreate");
75        initFromIntent(getIntent());
76        if (mTextMsg == null) {
77            finish();
78            return;
79        }
80
81        requestWindowFeature(Window.FEATURE_LEFT_ICON);
82        Window window = getWindow();
83
84        setContentView(R.layout.stk_msg_dialog);
85        TextView mMessageView = (TextView) window
86                .findViewById(R.id.dialog_message);
87
88        Button okButton = (Button) findViewById(R.id.button_ok);
89        Button cancelButton = (Button) findViewById(R.id.button_cancel);
90
91        okButton.setOnClickListener(this);
92        cancelButton.setOnClickListener(this);
93
94        setTitle(mTextMsg.title);
95        if (!(mTextMsg.iconSelfExplanatory && mTextMsg.icon != null)) {
96            mMessageView.setText(mTextMsg.text);
97        }
98
99        if (mTextMsg.icon == null) {
100            CatLog.d(LOG_TAG, "onCreate icon is null");
101            window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
102                    com.android.internal.R.drawable.stat_notify_sim_toolkit);
103        } else {
104            window.setFeatureDrawable(Window.FEATURE_LEFT_ICON,
105                    new BitmapDrawable(mTextMsg.icon));
106        }
107    }
108
109    public void onClick(View v) {
110        String input = null;
111
112        switch (v.getId()) {
113        case OK_BUTTON:
114            CatLog.d(LOG_TAG, "OK Clicked!, mSlotId: " + mSlotId);
115            sendResponse(StkAppService.RES_ID_CONFIRM, true);
116            cancelTimeOut();
117            finish();
118            break;
119        case CANCEL_BUTTON:
120            CatLog.d(LOG_TAG, "Cancel Clicked!, mSlotId: " + mSlotId);
121            sendResponse(StkAppService.RES_ID_CONFIRM, false);
122            cancelTimeOut();
123            finish();
124            break;
125        }
126    }
127
128    @Override
129    public boolean onKeyDown(int keyCode, KeyEvent event) {
130        switch (keyCode) {
131        case KeyEvent.KEYCODE_BACK:
132            CatLog.d(LOG_TAG, "onKeyDown - KEYCODE_BACK");
133            cancelTimeOut();
134            sendResponse(StkAppService.RES_ID_BACKWARD);
135            finish();
136            break;
137        }
138        return false;
139    }
140
141    @Override
142    public void onResume() {
143        super.onResume();
144        CatLog.d(LOG_TAG, "onResume - mIsResponseSent[" + mIsResponseSent +
145                "], sim id: " + mSlotId);
146        /*
147         * The user should be shown the message forever or until some high
148         * priority event occurs (such as incoming call, MMI code execution
149         * etc as mentioned in ETSI 102.223, 6.4.1).
150         *
151         * Since mTextMsg.responseNeeded is false (because the response has
152         * already been sent) and duration of the dialog is zero and userClear
153         * is true, don't set the timeout.
154         */
155        if (!mTextMsg.responseNeeded &&
156                StkApp.calculateDurationInMilis(mTextMsg.duration) == 0 &&
157                mTextMsg.userClear) {
158            CatLog.d(this, "User should clear text..show message forever");
159            return;
160        }
161
162        appService.setDisplayTextDlgVisibility(true, mSlotId);
163
164        startTimeOut(mTextMsg.userClear);
165    }
166
167    @Override
168    public void onPause() {
169        super.onPause();
170        CatLog.d(LOG_TAG, "onPause, sim id: " + mSlotId);
171        appService.setDisplayTextDlgVisibility(false, mSlotId);
172        cancelTimeOut();
173    }
174
175    @Override
176    protected void onStart() {
177        super.onStart();
178        mIsResponseSent = false;
179    }
180
181    @Override
182    public void onStop() {
183        super.onStop();
184        CatLog.d(LOG_TAG, "onStop - before Send CONFIRM false mIsResponseSent[" +
185                mIsResponseSent + "], sim id: " + mSlotId);
186        if (!mIsResponseSent) {
187            appService.getStkContext(mSlotId).setPendingDialogInstance(this);
188        } else {
189            CatLog.d(LOG_TAG, "finish.");
190            appService.getStkContext(mSlotId).setPendingDialogInstance(null);
191            cancelTimeOut();
192            finish();
193            CatLog.d(LOG_TAG, "finish.");
194        }
195    }
196
197    @Override
198    public void onDestroy() {
199        super.onDestroy();
200        CatLog.d(LOG_TAG, "onDestroy - mIsResponseSent[" + mIsResponseSent +
201                "], sim id: " + mSlotId);
202        // if dialog activity is finished by stkappservice
203        // when receiving OP_LAUNCH_APP from the other SIM, we can not send TR here
204        // , since the dialog cmd is waiting user to process.
205        if (!mIsResponseSent && !appService.isDialogPending(mSlotId)) {
206            sendResponse(StkAppService.RES_ID_CONFIRM, false);
207        }
208        cancelTimeOut();
209    }
210
211    @Override
212    public void onSaveInstanceState(Bundle outState) {
213        CatLog.d(LOG_TAG, "onSaveInstanceState");
214
215        super.onSaveInstanceState(outState);
216
217        outState.putParcelable(TEXT, mTextMsg);
218    }
219
220    @Override
221    public void onRestoreInstanceState(Bundle savedInstanceState) {
222        super.onRestoreInstanceState(savedInstanceState);
223
224        mTextMsg = savedInstanceState.getParcelable(TEXT);
225        CatLog.d(LOG_TAG, "onRestoreInstanceState - [" + mTextMsg + "]");
226    }
227
228    private void sendResponse(int resId, boolean confirmed) {
229        if (mSlotId == -1) {
230            CatLog.d(LOG_TAG, "sim id is invalid");
231            return;
232        }
233
234        if (StkAppService.getInstance() == null) {
235            CatLog.d(LOG_TAG, "Ignore response: id is " + resId);
236            return;
237        }
238
239        CatLog.d(LOG_TAG, "sendResponse resID[" + resId + "] confirmed[" + confirmed + "]");
240
241        Bundle args = new Bundle();
242        args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
243        args.putInt(StkAppService.SLOT_ID, mSlotId);
244        args.putInt(StkAppService.RES_ID, resId);
245        args.putBoolean(StkAppService.CONFIRMATION, confirmed);
246        startService(new Intent(this, StkAppService.class).putExtras(args));
247        mIsResponseSent = true;
248    }
249
250    private void sendResponse(int resId) {
251        sendResponse(resId, true);
252    }
253
254    private void initFromIntent(Intent intent) {
255
256        if (intent != null) {
257            mTextMsg = intent.getParcelableExtra("TEXT");
258            mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
259        } else {
260            finish();
261        }
262
263        CatLog.d(LOG_TAG, "initFromIntent - [" + mTextMsg + "], sim id: " + mSlotId);
264    }
265
266    private void cancelTimeOut() {
267        CatLog.d(LOG_TAG, "cancelTimeOut: " + mSlotId);
268        mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
269    }
270
271    private void startTimeOut(boolean waitForUserToClear) {
272        // Reset timeout.
273        cancelTimeOut();
274        int dialogDuration = StkApp.calculateDurationInMilis(mTextMsg.duration);
275        // If duration is specified, this has priority. If not, set timeout
276        // according to condition given by the card.
277        if (mTextMsg.userClear == true && mTextMsg.responseNeeded == false) {
278            return;
279        } else {
280            // userClear = false. will dissapear after a while.
281            if (dialogDuration == 0) {
282                if (waitForUserToClear) {
283                    dialogDuration = StkApp.DISP_TEXT_WAIT_FOR_USER_TIMEOUT;
284                } else {
285                    dialogDuration = StkApp.DISP_TEXT_CLEAR_AFTER_DELAY_TIMEOUT;
286                }
287            }
288            CatLog.d(LOG_TAG, "startTimeOut: " + mSlotId);
289            mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
290                .obtainMessage(MSG_ID_TIMEOUT), dialogDuration);
291        }
292    }
293}
294