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