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.TextMessage;
     20 
     21 import android.app.Activity;
     22 import android.content.Intent;
     23 import android.graphics.drawable.BitmapDrawable;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.os.Message;
     27 import android.view.KeyEvent;
     28 import android.view.View;
     29 import android.view.Window;
     30 import android.widget.Button;
     31 import android.widget.TextView;
     32 
     33 /**
     34  * AlretDialog used for DISPLAY TEXT commands.
     35  *
     36  */
     37 public class StkDialogActivity extends Activity implements View.OnClickListener {
     38     // members
     39     TextMessage mTextMsg;
     40 
     41     Handler mTimeoutHandler = new Handler() {
     42         @Override
     43         public void handleMessage(Message msg) {
     44             switch(msg.what) {
     45             case MSG_ID_TIMEOUT:
     46                 sendResponse(StkAppService.RES_ID_TIMEOUT);
     47                 finish();
     48                 break;
     49             }
     50         }
     51     };
     52 
     53     //keys) for saving the state of the dialog in the icicle
     54     private static final String TEXT = "text";
     55 
     56     // message id for time out
     57     private static final int MSG_ID_TIMEOUT = 1;
     58 
     59     // buttons id
     60     public static final int OK_BUTTON = R.id.button_ok;
     61     public static final int CANCEL_BUTTON = R.id.button_cancel;
     62 
     63     @Override
     64     protected void onCreate(Bundle icicle) {
     65         super.onCreate(icicle);
     66 
     67         initFromIntent(getIntent());
     68         if (mTextMsg == null) {
     69             finish();
     70             return;
     71         }
     72 
     73         requestWindowFeature(Window.FEATURE_LEFT_ICON);
     74         Window window = getWindow();
     75 
     76         setContentView(R.layout.stk_msg_dialog);
     77         TextView mMessageView = (TextView) window
     78                 .findViewById(R.id.dialog_message);
     79 
     80         Button okButton = (Button) findViewById(R.id.button_ok);
     81         Button cancelButton = (Button) findViewById(R.id.button_cancel);
     82 
     83         okButton.setOnClickListener(this);
     84         cancelButton.setOnClickListener(this);
     85 
     86         setTitle(mTextMsg.title);
     87         if (!(mTextMsg.iconSelfExplanatory && mTextMsg.icon != null)) {
     88             mMessageView.setText(mTextMsg.text);
     89         }
     90 
     91         if (mTextMsg.icon == null) {
     92             window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
     93                     com.android.internal.R.drawable.stat_notify_sim_toolkit);
     94         } else {
     95             window.setFeatureDrawable(Window.FEATURE_LEFT_ICON,
     96                     new BitmapDrawable(mTextMsg.icon));
     97         }
     98     }
     99 
    100     public void onClick(View v) {
    101         String input = null;
    102 
    103         switch (v.getId()) {
    104         case OK_BUTTON:
    105             sendResponse(StkAppService.RES_ID_CONFIRM, true);
    106             finish();
    107             break;
    108         case CANCEL_BUTTON:
    109             sendResponse(StkAppService.RES_ID_CONFIRM, false);
    110             finish();
    111             break;
    112         }
    113     }
    114 
    115     @Override
    116     public boolean onKeyDown(int keyCode, KeyEvent event) {
    117         switch (keyCode) {
    118         case KeyEvent.KEYCODE_BACK:
    119             sendResponse(StkAppService.RES_ID_BACKWARD);
    120             finish();
    121             break;
    122         }
    123         return false;
    124     }
    125 
    126     @Override
    127     public void onResume() {
    128         super.onResume();
    129         startTimeOut(mTextMsg.userClear);
    130     }
    131 
    132     @Override
    133     public void onPause() {
    134         super.onPause();
    135 
    136         cancelTimeOut();
    137     }
    138 
    139     @Override
    140     public void onSaveInstanceState(Bundle outState) {
    141         super.onSaveInstanceState(outState);
    142 
    143         outState.putParcelable(TEXT, mTextMsg);
    144     }
    145 
    146     @Override
    147     public void onRestoreInstanceState(Bundle savedInstanceState) {
    148         super.onRestoreInstanceState(savedInstanceState);
    149 
    150         mTextMsg = savedInstanceState.getParcelable(TEXT);
    151     }
    152 
    153     private void sendResponse(int resId, boolean confirmed) {
    154         Bundle args = new Bundle();
    155         args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
    156         args.putInt(StkAppService.RES_ID, resId);
    157         args.putBoolean(StkAppService.CONFIRMATION, confirmed);
    158         startService(new Intent(this, StkAppService.class).putExtras(args));
    159     }
    160 
    161     private void sendResponse(int resId) {
    162         sendResponse(resId, true);
    163     }
    164 
    165     private void initFromIntent(Intent intent) {
    166 
    167         if (intent != null) {
    168             mTextMsg = intent.getParcelableExtra("TEXT");
    169         } else {
    170             finish();
    171         }
    172     }
    173 
    174     private void cancelTimeOut() {
    175         mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
    176     }
    177 
    178     private void startTimeOut(boolean waitForUserToClear) {
    179         // Reset timeout.
    180         cancelTimeOut();
    181         int dialogDuration = StkApp.calculateDurationInMilis(mTextMsg.duration);
    182         // If duration is specified, this has priority. If not, set timeout
    183         // according to condition given by the card.
    184         if (dialogDuration == 0) {
    185             if (waitForUserToClear) {
    186                 dialogDuration = StkApp.DISP_TEXT_WAIT_FOR_USER_TIMEOUT;
    187             } else {
    188                 dialogDuration = StkApp.DISP_TEXT_CLEAR_AFTER_DELAY_TIMEOUT;
    189             }
    190         }
    191         mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
    192                 .obtainMessage(MSG_ID_TIMEOUT), dialogDuration);
    193     }
    194 }
    195