1/*
2 * Copyright (C) 2010 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.videoeditor;
18
19import android.app.ListActivity;
20import android.content.Intent;
21import android.os.Bundle;
22import android.view.View;
23import android.widget.ListView;
24import android.widget.TextView;
25
26/**
27 * Activity that lists all transition effects for user to choose.
28 */
29public class TransitionsActivity extends ListActivity {
30    // Input transition category
31    public static final String PARAM_AFTER_MEDIA_ITEM_ID = "media_item_id";
32    public static final String PARAM_TRANSITION_ID = "transition_id";
33    public static final String PARAM_MINIMUM_DURATION = "min_duration";
34    public static final String PARAM_DEFAULT_DURATION = "default_duration";
35    public static final String PARAM_MAXIMUM_DURATION = "max_duration";
36
37    // Increment transition duration in milliseconds
38    private static final long INCREMENT_TRANSITION = 100;
39
40    // Output transition type
41    public static final String PARAM_TRANSITION_TYPE = "transition";
42    public static final String PARAM_TRANSITION_DURATION = "duration";
43
44    // State keys
45    private static final String STATE_KEY_TRANSITION_DURATION = "duration";
46
47    // Instance variables
48    private TextView mTransitionDurationView;
49    private View mTransitionLeftBtn, mTransitionRightBtn;
50    private TransitionsAdapter mAdapter;
51    private long mMinTransitionDurationMs, mMaxTransitionDurationMs, mTransitionDurationMs;
52
53    @Override
54    public void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56        setContentView(R.layout.transition_list_view);
57        setFinishOnTouchOutside(true);
58
59        mTransitionDurationView = (TextView)findViewById(R.id.transition_duration);
60        mTransitionLeftBtn = findViewById(R.id.duration_left);
61        mTransitionRightBtn = findViewById(R.id.duration_right);
62
63        mMinTransitionDurationMs = getIntent().getLongExtra(PARAM_MINIMUM_DURATION, 0);
64        mMinTransitionDurationMs = (mMinTransitionDurationMs / INCREMENT_TRANSITION) *
65            INCREMENT_TRANSITION;
66
67        mMaxTransitionDurationMs = getIntent().getLongExtra(PARAM_MAXIMUM_DURATION, 0);
68        mMaxTransitionDurationMs = (mMaxTransitionDurationMs / INCREMENT_TRANSITION) *
69            INCREMENT_TRANSITION;
70
71        if (savedInstanceState == null) {
72            mTransitionDurationMs = getIntent().getLongExtra(PARAM_DEFAULT_DURATION, 0);
73        } else {
74            mTransitionDurationMs = savedInstanceState.getLong(STATE_KEY_TRANSITION_DURATION);
75        }
76        mTransitionDurationMs = (mTransitionDurationMs / INCREMENT_TRANSITION) *
77            INCREMENT_TRANSITION;
78
79        updateTransitionDuration();
80
81        // Create the list adapter
82        mAdapter = new TransitionsAdapter(this, getListView());
83        setListAdapter(mAdapter);
84
85        final int transitionType = getIntent().getIntExtra(PARAM_TRANSITION_TYPE, -1);
86        if (transitionType >= 0) {
87            // Select the current transition
88            final TransitionType[] transitions = mAdapter.getTransitions();
89            for (int i = 0; i < transitions.length; i++) {
90                if (transitions[i].getType() == transitionType) {
91                    setSelection(i);
92                    break;
93                }
94            }
95        }
96    }
97
98    @Override
99    public void onPause() {
100        super.onPause();
101
102        if (mAdapter != null) {
103            mAdapter.onPause();
104        }
105    }
106
107    @Override
108    public void onDestroy() {
109        super.onDestroy();
110
111        if (mAdapter != null) {
112            mAdapter.onDestroy();
113            mAdapter = null;
114        }
115    }
116
117    @Override
118    public void onSaveInstanceState(Bundle outState) {
119        super.onSaveInstanceState(outState);
120
121        outState.putLong(STATE_KEY_TRANSITION_DURATION, mTransitionDurationMs);
122    }
123
124    @Override
125    public void onListItemClick(ListView l, View v, int position, long id) {
126        final Intent extras = new Intent();
127        extras.putExtra(PARAM_TRANSITION_TYPE,
128                ((TransitionType)mAdapter.getItem(position)).getType());
129        extras.putExtra(PARAM_AFTER_MEDIA_ITEM_ID,
130                getIntent().getStringExtra(PARAM_AFTER_MEDIA_ITEM_ID));
131        extras.putExtra(PARAM_TRANSITION_ID,
132                getIntent().getStringExtra(PARAM_TRANSITION_ID));
133        extras.putExtra(PARAM_TRANSITION_DURATION, mTransitionDurationMs);
134
135        setResult(RESULT_OK, extras);
136        finish();
137    }
138
139    public void onClickHandler(View target) {
140        switch (target.getId()) {
141            case R.id.duration_left: {
142                if (mTransitionDurationMs > mMinTransitionDurationMs) {
143                    mTransitionDurationMs -= INCREMENT_TRANSITION;
144                    updateTransitionDuration();
145                }
146                break;
147            }
148
149            case R.id.duration_right: {
150                if (mTransitionDurationMs < mMaxTransitionDurationMs) {
151                    mTransitionDurationMs += INCREMENT_TRANSITION;
152                    updateTransitionDuration();
153                }
154                break;
155            }
156
157            default: {
158                break;
159            }
160        }
161    }
162
163    @Override
164    public boolean onSearchRequested() {
165        return false;
166    }
167
168    /**
169     * Updates the transition duration and the state of the buttons.
170     */
171    private void updateTransitionDuration() {
172        mTransitionDurationView.setText(getString(R.string.transitions_duration,
173                (((float)mTransitionDurationMs) / 1000)));
174
175        mTransitionLeftBtn.setEnabled(mTransitionDurationMs > mMinTransitionDurationMs);
176        mTransitionRightBtn.setEnabled(mTransitionDurationMs < mMaxTransitionDurationMs);
177    }
178}
179