Home | History | Annotate | Download | only in videoeditor
      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 
     17 package com.android.videoeditor;
     18 
     19 import android.app.ListActivity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.view.View;
     23 import android.widget.ListView;
     24 import android.widget.TextView;
     25 
     26 /**
     27  * The transitions activity
     28  */
     29 public 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     /*
     54      * {@inheritDoc}
     55      */
     56     @Override
     57     public void onCreate(Bundle savedInstanceState) {
     58         super.onCreate(savedInstanceState);
     59         setContentView(R.layout.transition_list_view);
     60         setFinishOnTouchOutside(true);
     61 
     62         mTransitionDurationView = (TextView)findViewById(R.id.transition_duration);
     63         mTransitionLeftBtn = findViewById(R.id.duration_left);
     64         mTransitionRightBtn = findViewById(R.id.duration_right);
     65 
     66         mMinTransitionDurationMs = getIntent().getLongExtra(PARAM_MINIMUM_DURATION, 0);
     67         mMinTransitionDurationMs = (mMinTransitionDurationMs / INCREMENT_TRANSITION) *
     68             INCREMENT_TRANSITION;
     69 
     70         mMaxTransitionDurationMs = getIntent().getLongExtra(PARAM_MAXIMUM_DURATION, 0);
     71         mMaxTransitionDurationMs = (mMaxTransitionDurationMs / INCREMENT_TRANSITION) *
     72             INCREMENT_TRANSITION;
     73 
     74         if (savedInstanceState == null) {
     75             mTransitionDurationMs = getIntent().getLongExtra(PARAM_DEFAULT_DURATION, 0);
     76         } else {
     77             mTransitionDurationMs = savedInstanceState.getLong(STATE_KEY_TRANSITION_DURATION);
     78         }
     79         mTransitionDurationMs = (mTransitionDurationMs / INCREMENT_TRANSITION) *
     80             INCREMENT_TRANSITION;
     81 
     82         updateTransitionDuration();
     83 
     84         // Create the list adapter
     85         mAdapter = new TransitionsAdapter(this, getListView());
     86         setListAdapter(mAdapter);
     87 
     88         final int transitionType = getIntent().getIntExtra(PARAM_TRANSITION_TYPE, -1);
     89         if (transitionType >= 0) {
     90             // Select the current transition
     91             final TransitionType[] transitions = mAdapter.getTransitions();
     92             for (int i = 0; i < transitions.length; i++) {
     93                 if (transitions[i].getType() == transitionType) {
     94                     setSelection(i);
     95                     break;
     96                 }
     97             }
     98         }
     99     }
    100 
    101     /*
    102      * {@inheritDoc}
    103      */
    104     @Override
    105     public void onPause() {
    106         super.onPause();
    107 
    108         if (mAdapter != null) {
    109             mAdapter.onPause();
    110         }
    111     }
    112 
    113     /*
    114      * {@inheritDoc}
    115      */
    116     @Override
    117     public void onDestroy() {
    118         super.onDestroy();
    119 
    120         if (mAdapter != null) {
    121             mAdapter.onDestroy();
    122         }
    123     }
    124 
    125     /*
    126      * {@inheritDoc}
    127      */
    128     @Override
    129     public void onSaveInstanceState(Bundle outState) {
    130         super.onSaveInstanceState(outState);
    131 
    132         outState.putLong(STATE_KEY_TRANSITION_DURATION, mTransitionDurationMs);
    133     }
    134 
    135     /*
    136      * {@inheritDoc}
    137      */
    138     @Override
    139     public void onListItemClick(ListView l, View v, int position, long id) {
    140         final Intent extras = new Intent();
    141         extras.putExtra(PARAM_TRANSITION_TYPE,
    142                 ((TransitionType)mAdapter.getItem(position)).getType());
    143         extras.putExtra(PARAM_AFTER_MEDIA_ITEM_ID,
    144                 getIntent().getStringExtra(PARAM_AFTER_MEDIA_ITEM_ID));
    145         extras.putExtra(PARAM_TRANSITION_ID,
    146                 getIntent().getStringExtra(PARAM_TRANSITION_ID));
    147         extras.putExtra(PARAM_TRANSITION_DURATION, mTransitionDurationMs);
    148 
    149         // Release the adapter now
    150         mAdapter.onDestroy();
    151         mAdapter = null;
    152 
    153         setResult(RESULT_OK, extras);
    154         finish();
    155     }
    156 
    157     /*
    158      * {@inheritDoc}
    159      */
    160     public void onClickHandler(View target) {
    161         switch (target.getId()) {
    162             case R.id.duration_left: {
    163                 if (mTransitionDurationMs > mMinTransitionDurationMs) {
    164                     mTransitionDurationMs -= INCREMENT_TRANSITION;
    165                     updateTransitionDuration();
    166                 }
    167                 break;
    168             }
    169 
    170             case R.id.duration_right: {
    171                 if (mTransitionDurationMs < mMaxTransitionDurationMs) {
    172                     mTransitionDurationMs += INCREMENT_TRANSITION;
    173                     updateTransitionDuration();
    174                 }
    175                 break;
    176             }
    177 
    178             default: {
    179                 break;
    180             }
    181         }
    182     }
    183 
    184     /*
    185      * {@inheritDoc}
    186      */
    187     @Override
    188     public boolean onSearchRequested() {
    189         return false;
    190     }
    191 
    192     /**
    193      * Update the transition duration and the state of the buttons
    194      */
    195     private void updateTransitionDuration() {
    196         mTransitionDurationView.setText(getString(R.string.transitions_duration,
    197                 (((float)mTransitionDurationMs) / 1000)));
    198 
    199         mTransitionLeftBtn.setEnabled(mTransitionDurationMs > mMinTransitionDurationMs);
    200         mTransitionRightBtn.setEnabled(mTransitionDurationMs < mMaxTransitionDurationMs);
    201     }
    202 }
    203