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.view.KeyEvent;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.view.View.OnKeyListener;
27 import android.view.Window;
28 import android.widget.Button;
29 import android.widget.EditText;
30 import android.widget.TextView;
31 import android.widget.Toast;
32 
33 import com.android.mms.LogTag;
34 import com.android.mms.R;
35 
36 /**
37  * This activity provides the function to edit the duration of given slide.
38  */
39 public class EditSlideDurationActivity  extends Activity {
40     public static final String SLIDE_INDEX = "slide_index";
41     public static final String SLIDE_TOTAL = "slide_total";
42     public static final String SLIDE_DUR   = "dur";
43 
44     private TextView mLabel;
45     private Button mDone;
46     private EditText mDur;
47 
48     private int mCurSlide;
49     private int mTotal;
50 
51     private Bundle mState;
52     //  State.
53     private final static String STATE = "state";
54     private final static String TAG = LogTag.TAG;
55     private static final boolean DEBUG = false;
56     private static final boolean LOCAL_LOGV = false;
57 
58     @Override
onCreate(Bundle icicle)59     protected void onCreate(Bundle icicle) {
60         super.onCreate(icicle);
61         requestWindowFeature(Window.FEATURE_NO_TITLE);
62         setContentView(R.layout.edit_slide_duration);
63 
64         int dur;
65         if (icicle == null) {
66             // Get extra from intent.
67             Intent intent = getIntent();
68             mCurSlide = intent.getIntExtra(SLIDE_INDEX, 1);
69             mTotal = intent.getIntExtra(SLIDE_TOTAL, 1);
70             dur = intent.getIntExtra(SLIDE_DUR, 8);
71         } else {
72             mState = icicle.getBundle(STATE);
73 
74             mCurSlide = mState.getInt(SLIDE_INDEX, 1);
75             mTotal = mState.getInt(SLIDE_TOTAL, 1);
76             dur = mState.getInt(SLIDE_DUR, 8);
77         }
78 
79         // Label.
80         mLabel = (TextView) findViewById(R.id.label);
81         mLabel.setText(getString(R.string.duration_selector_title) + " " + (mCurSlide + 1) + "/" + mTotal);
82 
83         // Input text field.
84         mDur = (EditText) findViewById(R.id.text);
85         mDur.setText(String.valueOf(dur));
86         mDur.setOnKeyListener(mOnKeyListener);
87 
88         // Done button.
89         mDone = (Button) findViewById(R.id.done);
90         mDone.setOnClickListener(mOnDoneClickListener);
91     }
92 
93     /*
94      * (non-Javadoc)
95      * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
96      */
97     @Override
onSaveInstanceState(Bundle outState)98     protected void onSaveInstanceState(Bundle outState) {
99         super.onSaveInstanceState(outState);
100 
101         mState = new Bundle();
102         mState.putInt(SLIDE_INDEX, mCurSlide);
103         mState.putInt(SLIDE_TOTAL, mTotal);
104 
105         int durValue;
106         try {
107             durValue = Integer.parseInt(mDur.getText().toString());
108         } catch (NumberFormatException e) {
109             // On an illegal value, set the duration back to a default value.
110             durValue = 5;
111         }
112         mState.putInt(SLIDE_DUR, durValue);
113 
114         outState.putBundle(STATE, mState);
115     }
116 
117     private final OnKeyListener mOnKeyListener = new OnKeyListener() {
118         public boolean onKey(View v, int keyCode, KeyEvent event) {
119             if (event.getAction() != KeyEvent.ACTION_DOWN) {
120                 return false;
121             }
122 
123             switch (keyCode) {
124                 case KeyEvent.KEYCODE_DPAD_CENTER:
125                     // Edit complete.
126                     editDone();
127                     break;
128             }
129             return false;
130         }
131     };
132 
133     private final OnClickListener mOnDoneClickListener = new OnClickListener() {
134         public void onClick(View v) {
135             // Edit complete.
136             editDone();
137         }
138     };
139 
editDone()140     protected void editDone() {
141         // Set result to parent, and close window.
142         // Check the duration.
143         String dur = mDur.getText().toString();
144         int durValue = 0;
145         try {
146             durValue = Integer.valueOf(dur);
147         } catch (NumberFormatException e) {
148             notifyUser(R.string.duration_not_a_number);
149             return;
150         }
151         if (durValue <= 0) {
152             notifyUser(R.string.duration_zero);
153             return;
154         }
155 
156         // Set result.
157         setResult(RESULT_OK, new Intent(mDur.getText().toString()));
158         finish();
159     }
160 
notifyUser(int msgId)161     private void notifyUser(int msgId) {
162         mDur.requestFocus();
163         mDur.selectAll();
164         Toast.makeText(this, msgId, Toast.LENGTH_SHORT).show();
165         return;
166     }
167 }
168