Home | History | Annotate | Download | only in stk
      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 com.android.internal.telephony.cat.CatLog;
     20 import com.android.internal.telephony.cat.TextMessage;
     21 
     22 import android.app.Activity;
     23 import android.content.Intent;
     24 import android.graphics.drawable.BitmapDrawable;
     25 import android.os.Bundle;
     26 import android.os.Handler;
     27 import android.os.Message;
     28 import android.view.KeyEvent;
     29 import android.view.View;
     30 import android.view.Window;
     31 import android.widget.Button;
     32 import android.widget.TextView;
     33 
     34 /**
     35  * AlretDialog used for DISPLAY TEXT commands.
     36  *
     37  */
     38 public class StkDialogActivity extends Activity implements View.OnClickListener {
     39     // members
     40     TextMessage mTextMsg;
     41 
     42     private boolean mIsResponseSent = false;
     43     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 
     58     // message id for time out
     59     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)) {
     90             mMessageView.setText(mTextMsg.text);
     91         }
     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();
    131 
    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 
    148         startTimeOut(mTextMsg.userClear);
    149     }
    150 
    151     @Override
    152     public void onPause() {
    153         super.onPause();
    154 
    155         cancelTimeOut();
    156     }
    157 
    158     @Override
    159     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
    173     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);
    182 
    183         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));
    192         mIsResponseSent = true;
    193     }
    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 
    212     private void startTimeOut(boolean waitForUserToClear) {
    213         // Reset timeout.
    214         cancelTimeOut();
    215         int dialogDuration = StkApp.calculateDurationInMilis(mTextMsg.duration);
    216         // If duration is specified, this has priority. If not, set timeout
    217         // according to condition given by the card.
    218         if (dialogDuration == 0) {
    219             if (waitForUserToClear) {
    220                 dialogDuration = StkApp.DISP_TEXT_WAIT_FOR_USER_TIMEOUT;
    221             } else {
    222                 dialogDuration = StkApp.DISP_TEXT_CLEAR_AFTER_DELAY_TIMEOUT;
    223             }
    224         }
    225         mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
    226                 .obtainMessage(MSG_ID_TIMEOUT), dialogDuration);
    227     }
    228 }
    229