1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.ui; 19 20 import android.app.Activity; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.text.InputFilter; 24 import android.text.method.NumberKeyListener; 25 import android.util.Log; 26 import android.view.KeyEvent; 27 import android.view.View; 28 import android.view.Window; 29 import android.view.View.OnClickListener; 30 import android.view.View.OnKeyListener; 31 import android.view.inputmethod.EditorInfo; 32 import android.widget.Button; 33 import android.widget.EditText; 34 import android.widget.TextView; 35 import android.widget.Toast; 36 37 import com.android.mms.R; 38 39 /** 40 * This activity provides the function to edit the duration of given slide. 41 */ 42 public class EditSlideDurationActivity extends Activity { 43 public static final String SLIDE_INDEX = "slide_index"; 44 public static final String SLIDE_TOTAL = "slide_total"; 45 public static final String SLIDE_DUR = "dur"; 46 47 private TextView mLabel; 48 private Button mDone; 49 private EditText mDur; 50 51 private int mCurSlide; 52 private int mTotal; 53 54 private Bundle mState; 55 // State. 56 private final static String STATE = "state"; 57 private final static String TAG = "EditSlideDurationActivity"; 58 private static final boolean DEBUG = false; 59 private static final boolean LOCAL_LOGV = false; 60 61 @Override onCreate(Bundle icicle)62 protected void onCreate(Bundle icicle) { 63 super.onCreate(icicle); 64 requestWindowFeature(Window.FEATURE_NO_TITLE); 65 setContentView(R.layout.edit_slide_duration); 66 67 int dur; 68 if (icicle == null) { 69 // Get extra from intent. 70 Intent intent = getIntent(); 71 mCurSlide = intent.getIntExtra(SLIDE_INDEX, 1); 72 mTotal = intent.getIntExtra(SLIDE_TOTAL, 1); 73 dur = intent.getIntExtra(SLIDE_DUR, 8); 74 } else { 75 mState = icicle.getBundle(STATE); 76 77 mCurSlide = mState.getInt(SLIDE_INDEX, 1); 78 mTotal = mState.getInt(SLIDE_TOTAL, 1); 79 dur = mState.getInt(SLIDE_DUR, 8); 80 } 81 82 // Label. 83 mLabel = (TextView) findViewById(R.id.label); 84 mLabel.setText(getString(R.string.duration_selector_title) + " " + (mCurSlide + 1) + "/" + mTotal); 85 86 // Input text field. 87 mDur = (EditText) findViewById(R.id.text); 88 mDur.setText(String.valueOf(dur)); 89 mDur.setOnKeyListener(mOnKeyListener); 90 91 // Done button. 92 mDone = (Button) findViewById(R.id.done); 93 mDone.setOnClickListener(mOnDoneClickListener); 94 } 95 96 /* 97 * (non-Javadoc) 98 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle) 99 */ 100 @Override onSaveInstanceState(Bundle outState)101 protected void onSaveInstanceState(Bundle outState) { 102 super.onSaveInstanceState(outState); 103 104 mState = new Bundle(); 105 mState.putInt(SLIDE_INDEX, mCurSlide); 106 mState.putInt(SLIDE_TOTAL, mTotal); 107 108 int durValue; 109 try { 110 durValue = Integer.parseInt(mDur.getText().toString()); 111 } catch (NumberFormatException e) { 112 // On an illegal value, set the duration back to a default value. 113 durValue = 5; 114 } 115 mState.putInt(SLIDE_DUR, durValue); 116 117 outState.putBundle(STATE, mState); 118 } 119 120 private final OnKeyListener mOnKeyListener = new OnKeyListener() { 121 public boolean onKey(View v, int keyCode, KeyEvent event) { 122 if (event.getAction() != KeyEvent.ACTION_DOWN) { 123 return false; 124 } 125 126 switch (keyCode) { 127 case KeyEvent.KEYCODE_DPAD_CENTER: 128 // Edit complete. 129 editDone(); 130 break; 131 } 132 return false; 133 } 134 }; 135 136 private final OnClickListener mOnDoneClickListener = new OnClickListener() { 137 public void onClick(View v) { 138 // Edit complete. 139 editDone(); 140 } 141 }; 142 editDone()143 protected void editDone() { 144 // Set result to parent, and close window. 145 // Check the duration. 146 String dur = mDur.getText().toString(); 147 int durValue = 0; 148 try { 149 durValue = Integer.valueOf(dur); 150 } catch (NumberFormatException e) { 151 notifyUser(R.string.duration_not_a_number); 152 return; 153 } 154 if (durValue <= 0) { 155 notifyUser(R.string.duration_zero); 156 return; 157 } 158 159 // Set result. 160 setResult(RESULT_OK, new Intent(mDur.getText().toString())); 161 finish(); 162 } 163 notifyUser(int msgId)164 private void notifyUser(int msgId) { 165 mDur.requestFocus(); 166 mDur.selectAll(); 167 Toast.makeText(this, msgId, Toast.LENGTH_SHORT).show(); 168 return; 169 } 170 } 171