Transition.java revision fdacc8be92cd36f712cfdb0fcf9b0e847f8eeb58
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 android.media.videoeditor;
18
19import java.io.File;
20
21/**
22 * This class is super class for all transitions. Transitions (with the
23 * exception of TransitionAtStart and TransitioAtEnd) can only be inserted
24 * between media items.
25 *
26 * Adding a transition between MediaItems makes the
27 * duration of the storyboard shorter by the duration of the Transition itself.
28 * As a result, if the duration of the transition is larger than the smaller
29 * duration of the two MediaItems associated with the Transition, an exception
30 * will be thrown.
31 *
32 * During a transition, the audio track are cross-fading
33 * automatically. {@hide}
34 */
35public abstract class Transition {
36    // The transition behavior
37    /** The transition starts slowly and speed up */
38    public static final int BEHAVIOR_SPEED_UP = 0;
39    /** The transition start fast and speed down */
40    public static final int BEHAVIOR_SPEED_DOWN = 1;
41    /** The transition speed is constant */
42    public static final int BEHAVIOR_LINEAR = 2;
43    /** The transition starts fast and ends fast with a slow middle */
44    public static final int BEHAVIOR_MIDDLE_SLOW = 3;
45    /** The transition starts slowly and ends slowly with a fast middle */
46    public static final int BEHAVIOR_MIDDLE_FAST = 4;
47
48    // The unique id of the transition
49    private final String mUniqueId;
50
51    // The transition is applied at the end of this media item
52    private final MediaItem mAfterMediaItem;
53    // The transition is applied at the beginning of this media item
54    private final MediaItem mBeforeMediaItem;
55
56    // The transition behavior
57    protected final int mBehavior;
58
59    // The transition duration
60    protected long mDurationMs;
61
62    // The transition filename
63    protected String mFilename;
64
65    /**
66     * An object of this type cannot be instantiated by using the default
67     * constructor
68     */
69    @SuppressWarnings("unused")
70    private Transition() {
71        this(null, null, null, 0, 0);
72    }
73
74    /**
75     * Constructor
76     *
77     * @param transitionId The transition id
78     * @param afterMediaItem The transition is applied to the end of this
79     *      media item
80     * @param beforeMediaItem The transition is applied to the beginning of
81     *      this media item
82     * @param durationMs The duration of the transition in milliseconds
83     * @param behavior The transition behavior
84     */
85    protected Transition(String transitionId, MediaItem afterMediaItem, MediaItem beforeMediaItem,
86            long durationMs, int behavior) {
87        mUniqueId = transitionId;
88        mAfterMediaItem = afterMediaItem;
89        mBeforeMediaItem = beforeMediaItem;
90        mDurationMs = durationMs;
91        mBehavior = behavior;
92    }
93
94    /**
95     * @return The of the transition
96     */
97    public String getId() {
98        return mUniqueId;
99    }
100
101    /**
102     * @return The media item at the end of which the transition is applied
103     */
104    public MediaItem getAfterMediaItem() {
105        return mAfterMediaItem;
106    }
107
108    /**
109     * @return The media item at the beginning of which the transition is applied
110     */
111    public MediaItem getBeforeMediaItem() {
112        return mBeforeMediaItem;
113    }
114
115    /**
116     * Set the duration of the transition.
117     *
118     * @param durationMs the duration of the transition in milliseconds
119     */
120    public void setDuration(long durationMs) {
121        mDurationMs = durationMs;
122    }
123
124    /**
125     * @return the duration of the transition in milliseconds
126     */
127    public long getDuration() {
128        return mDurationMs;
129    }
130
131    /**
132     * @return The behavior
133     */
134    public int getBehavior() {
135        return mBehavior;
136    }
137
138    /**
139     * Generate the video clip for the specified transition.
140     * This method may block for a significant amount of time.
141     *
142     * Before the method completes execution it sets the mFilename to
143     * the name of the newly generated transition video clip file.
144     */
145    abstract void generate();
146
147    /**
148     * Remove any resources associated with this transition
149     */
150    void invalidate() {
151        if (mFilename != null) {
152            new File(mFilename).delete();
153            mFilename = null;
154        }
155    }
156
157    /**
158     * @return true if the transition is generated
159     */
160    boolean isGenerated() {
161        return (mFilename != null);
162    }
163
164    /*
165     * {@inheritDoc}
166     */
167    @Override
168    public boolean equals(Object object) {
169        if (!(object instanceof Transition)) {
170            return false;
171        }
172        return mUniqueId.equals(((Transition)object).mUniqueId);
173    }
174
175    /*
176     * {@inheritDoc}
177     */
178    @Override
179    public int hashCode() {
180        return mUniqueId.hashCode();
181    }
182}
183