StkDialogActivity.java revision c46d8a06f810a3b5b570e43e7e0ed8b36efd23e5
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.gsm.stk.TextMessage; 20 21import android.app.Activity; 22import android.content.Intent; 23import android.graphics.drawable.BitmapDrawable; 24import android.os.Bundle; 25import android.os.Handler; 26import android.os.Message; 27import android.view.KeyEvent; 28import android.view.View; 29import android.view.Window; 30import android.widget.Button; 31import android.widget.TextView; 32 33/** 34 * AlretDialog used for DISPLAY TEXT commands. 35 * 36 */ 37public class StkDialogActivity extends Activity implements View.OnClickListener { 38 // members 39 TextMessage mTextMsg; 40 41 Handler mTimeoutHandler = new Handler() { 42 public void handleMessage(Message msg) { 43 switch(msg.what) { 44 case MSG_ID_TIMEOUT: 45 sendResponse(StkAppService.RES_ID_TIMEOUT); 46 finish(); 47 break; 48 } 49 } 50 }; 51 52 // constants 53 static final String TAG = "STK Dialog"; 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 startTimeOut(); 133 } 134 135 @Override 136 public void onPause() { 137 super.onPause(); 138 139 cancelTimeOut(); 140 } 141 142 @Override 143 public void onSaveInstanceState(Bundle outState) { 144 super.onSaveInstanceState(outState); 145 146 outState.putParcelable(TEXT, mTextMsg); 147 } 148 149 @Override 150 public void onRestoreInstanceState(Bundle savedInstanceState) { 151 super.onRestoreInstanceState(savedInstanceState); 152 153 mTextMsg = savedInstanceState.getParcelable(TEXT); 154 } 155 156 private void sendResponse(int resId, boolean confirmed) { 157 Bundle args = new Bundle(); 158 args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE); 159 args.putInt(StkAppService.RES_ID, resId); 160 args.putBoolean(StkAppService.CONFIRMATION, confirmed); 161 startService(new Intent(this, StkAppService.class).putExtras(args)); 162 } 163 164 private void sendResponse(int resId) { 165 sendResponse(resId, true); 166 } 167 168 private void initFromIntent(Intent intent) { 169 170 if (intent != null) { 171 mTextMsg = intent.getParcelableExtra("TEXT"); 172 } else { 173 finish(); 174 } 175 } 176 177 private void cancelTimeOut() { 178 mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT); 179 } 180 181 private void startTimeOut() { 182 // Reset timeout. 183 cancelTimeOut(); 184 int dialogDuration = StkApp.calculateDurationInMilis(mTextMsg.duration); 185 if (dialogDuration == 0) { 186 dialogDuration = StkApp.UI_TIMEOUT; 187 } 188 mTimeoutHandler.sendMessageDelayed(mTimeoutHandler 189 .obtainMessage(MSG_ID_TIMEOUT), dialogDuration); 190 } 191} 192