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