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.gsm.stk.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 
    130         startTimeOut();
    131     }
    132 
    133     @Override
    134     public void onPause() {
    135         super.onPause();
    136 
    137         cancelTimeOut();
    138     }
    139 
    140     @Override
    141     public void onSaveInstanceState(Bundle outState) {
    142         super.onSaveInstanceState(outState);
    143 
    144         outState.putParcelable(TEXT, mTextMsg);
    145     }
    146 
    147     @Override
    148     public void onRestoreInstanceState(Bundle savedInstanceState) {
    149         super.onRestoreInstanceState(savedInstanceState);
    150 
    151         mTextMsg = savedInstanceState.getParcelable(TEXT);
    152     }
    153 
    154     private void sendResponse(int resId, boolean confirmed) {
    155         Bundle args = new Bundle();
    156         args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
    157         args.putInt(StkAppService.RES_ID, resId);
    158         args.putBoolean(StkAppService.CONFIRMATION, confirmed);
    159         startService(new Intent(this, StkAppService.class).putExtras(args));
    160     }
    161 
    162     private void sendResponse(int resId) {
    163         sendResponse(resId, true);
    164     }
    165 
    166     private void initFromIntent(Intent intent) {
    167 
    168         if (intent != null) {
    169             mTextMsg = intent.getParcelableExtra("TEXT");
    170         } else {
    171             finish();
    172         }
    173     }
    174 
    175     private void cancelTimeOut() {
    176         mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
    177     }
    178 
    179     private void startTimeOut() {
    180         // Reset timeout.
    181         cancelTimeOut();
    182         int dialogDuration = StkApp.calculateDurationInMilis(mTextMsg.duration);
    183         if (dialogDuration == 0) {
    184             dialogDuration = StkApp.UI_TIMEOUT;
    185         }
    186         mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
    187                 .obtainMessage(MSG_ID_TIMEOUT), dialogDuration);
    188     }
    189 }
    190