StkDialogActivity.java revision cfe4d7f06d8e023ddc153cd042682957dfd85de2
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    TextMessage mTextMsg;
41
42    Handler mTimeoutHandler = new Handler() {
43        @Override
44        public void handleMessage(Message msg) {
45            switch(msg.what) {
46            case MSG_ID_TIMEOUT:
47                sendResponse(StkAppService.RES_ID_TIMEOUT);
48                finish();
49                break;
50            }
51        }
52    };
53
54    //keys) for saving the state of the dialog in the icicle
55    private static final String TEXT = "text";
56
57    // message id for time out
58    private static final int MSG_ID_TIMEOUT = 1;
59
60    // buttons id
61    public static final int OK_BUTTON = R.id.button_ok;
62    public static final int CANCEL_BUTTON = R.id.button_cancel;
63
64    @Override
65    protected void onCreate(Bundle icicle) {
66        super.onCreate(icicle);
67
68        initFromIntent(getIntent());
69        if (mTextMsg == null) {
70            finish();
71            return;
72        }
73
74        requestWindowFeature(Window.FEATURE_LEFT_ICON);
75        Window window = getWindow();
76
77        setContentView(R.layout.stk_msg_dialog);
78        TextView mMessageView = (TextView) window
79                .findViewById(R.id.dialog_message);
80
81        Button okButton = (Button) findViewById(R.id.button_ok);
82        Button cancelButton = (Button) findViewById(R.id.button_cancel);
83
84        okButton.setOnClickListener(this);
85        cancelButton.setOnClickListener(this);
86
87        setTitle(mTextMsg.title);
88        if (!(mTextMsg.iconSelfExplanatory && mTextMsg.icon != null)) {
89            mMessageView.setText(mTextMsg.text);
90        }
91
92        if (mTextMsg.icon == null) {
93            window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
94                    com.android.internal.R.drawable.stat_notify_sim_toolkit);
95        } else {
96            window.setFeatureDrawable(Window.FEATURE_LEFT_ICON,
97                    new BitmapDrawable(mTextMsg.icon));
98        }
99    }
100
101    public void onClick(View v) {
102        String input = null;
103
104        switch (v.getId()) {
105        case OK_BUTTON:
106            sendResponse(StkAppService.RES_ID_CONFIRM, true);
107            finish();
108            break;
109        case CANCEL_BUTTON:
110            sendResponse(StkAppService.RES_ID_CONFIRM, false);
111            finish();
112            break;
113        }
114    }
115
116    @Override
117    public boolean onKeyDown(int keyCode, KeyEvent event) {
118        switch (keyCode) {
119        case KeyEvent.KEYCODE_BACK:
120            sendResponse(StkAppService.RES_ID_BACKWARD);
121            finish();
122            break;
123        }
124        return false;
125    }
126
127    @Override
128    public void onResume() {
129        super.onResume();
130
131        /*
132         * The user should be shown the message forever or until some high
133         * priority event occurs (such as incoming call, MMI code execution
134         * etc as mentioned in ETSI 102.223, 6.4.1).
135         *
136         * Since mTextMsg.responseNeeded is false (because the response has
137         * already been sent) and duration of the dialog is zero and userClear
138         * is true, don't set the timeout.
139         */
140        if (!mTextMsg.responseNeeded &&
141                StkApp.calculateDurationInMilis(mTextMsg.duration) == 0 &&
142                mTextMsg.userClear) {
143            CatLog.d(this, "User should clear text..show message forever");
144            return;
145        }
146
147        startTimeOut(mTextMsg.userClear);
148    }
149
150    @Override
151    public void onPause() {
152        super.onPause();
153
154        cancelTimeOut();
155    }
156
157    @Override
158    public void onSaveInstanceState(Bundle outState) {
159        super.onSaveInstanceState(outState);
160
161        outState.putParcelable(TEXT, mTextMsg);
162    }
163
164    @Override
165    public void onRestoreInstanceState(Bundle savedInstanceState) {
166        super.onRestoreInstanceState(savedInstanceState);
167
168        mTextMsg = savedInstanceState.getParcelable(TEXT);
169    }
170
171    private void sendResponse(int resId, boolean confirmed) {
172        Bundle args = new Bundle();
173        args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
174        args.putInt(StkAppService.RES_ID, resId);
175        args.putBoolean(StkAppService.CONFIRMATION, confirmed);
176        startService(new Intent(this, StkAppService.class).putExtras(args));
177    }
178
179    private void sendResponse(int resId) {
180        sendResponse(resId, true);
181    }
182
183    private void initFromIntent(Intent intent) {
184
185        if (intent != null) {
186            mTextMsg = intent.getParcelableExtra("TEXT");
187        } else {
188            finish();
189        }
190    }
191
192    private void cancelTimeOut() {
193        mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
194    }
195
196    private void startTimeOut(boolean waitForUserToClear) {
197        // Reset timeout.
198        cancelTimeOut();
199        int dialogDuration = StkApp.calculateDurationInMilis(mTextMsg.duration);
200        // If duration is specified, this has priority. If not, set timeout
201        // according to condition given by the card.
202        if (dialogDuration == 0) {
203            if (waitForUserToClear) {
204                dialogDuration = StkApp.DISP_TEXT_WAIT_FOR_USER_TIMEOUT;
205            } else {
206                dialogDuration = StkApp.DISP_TEXT_CLEAR_AFTER_DELAY_TIMEOUT;
207            }
208        }
209        mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
210                .obtainMessage(MSG_ID_TIMEOUT), dialogDuration);
211    }
212}
213