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.service;
18
19import com.android.videoeditor.EffectType;
20
21import android.graphics.Rect;
22import android.media.videoeditor.Effect;
23import android.media.videoeditor.EffectColor;
24import android.media.videoeditor.EffectKenBurns;
25
26/**
27 * An effect can only be applied to a single media item.
28 */
29public class MovieEffect {
30    // Instance variables
31    private final String mUniqueId;
32    private final int mType;
33    private long mDurationMs;
34    private long mStartTimeMs;
35    private Rect mStartRect, mEndRect;
36
37    /**
38     * Default constructor
39     */
40    @SuppressWarnings("unused")
41    private MovieEffect() {
42        this (null);
43    }
44
45    /**
46     * Constructor
47     *
48     * @param effect The effect
49     */
50    MovieEffect(Effect effect) {
51        mUniqueId = effect.getId();
52        mStartTimeMs = effect.getStartTime();
53        mDurationMs = effect.getDuration();
54        if (effect instanceof EffectKenBurns) {
55            mStartRect = ((EffectKenBurns)effect).getStartRect();
56            mEndRect = ((EffectKenBurns)effect).getEndRect();
57        } else {
58            mStartRect = null;
59            mEndRect = null;
60        }
61
62        mType = toType(effect);
63    }
64
65    /**
66     * @return The effect type
67     */
68    public int getType() {
69        return mType;
70    }
71    /**
72     * @return The id of the effect
73     */
74    public String getId() {
75        return mUniqueId;
76    }
77
78    /**
79     * Set the duration of the effect. If a preview or export is in progress,
80     * then this change is effective for next preview or export session. s
81     *
82     * @param durationMs of the effect in milliseconds
83     */
84    void setDuration(long durationMs) {
85        mDurationMs = durationMs;
86    }
87
88    /**
89     * Get the duration of the effect
90     *
91     * @return The duration of the effect in milliseconds
92     */
93    public long getDuration() {
94        return mDurationMs;
95    }
96
97    /**
98     * Set start time of the effect. If a preview or export is in progress, then
99     * this change is effective for next preview or export session.
100     *
101     * @param startTimeMs The start time of the effect relative to the beginning
102     *            of the media item in milliseconds
103     */
104    void setStartTime(long startTimeMs) {
105        mStartTimeMs = startTimeMs;
106    }
107
108    /**
109     * @return The start time in milliseconds
110     */
111    long getStartTime() {
112        return mStartTimeMs;
113    }
114
115    /**
116     * Set the Ken Burns start and end rectangles
117     *
118     * @param startRect The start rectangle
119     * @param endRect The end rectangle
120     */
121    void setRectangles(Rect startRect, Rect endRect) {
122        mStartRect = startRect;
123        mEndRect = endRect;
124    }
125
126    /**
127     * @return The start rectangle
128     */
129    public Rect getStartRect() {
130        return mStartRect;
131    }
132
133    /**
134     * @return The end rectangle
135     */
136    public Rect getEndRect() {
137        return mEndRect;
138    }
139
140    /**
141     * Get the effect type
142     *
143     * @param effect The effect
144     *
145     * @return The effect type
146     */
147    private static int toType(Effect effect) {
148        if (effect instanceof EffectKenBurns) {
149            return EffectType.EFFECT_KEN_BURNS;
150        } else if (effect instanceof EffectColor) {
151            final EffectColor colorEffect = (EffectColor)effect;
152            switch (colorEffect.getType()) {
153                case EffectColor.TYPE_GRADIENT: {
154                    return EffectType.EFFECT_COLOR_GRADIENT;
155                }
156
157                case EffectColor.TYPE_SEPIA: {
158                    return EffectType.EFFECT_COLOR_SEPIA;
159                }
160
161                case EffectColor.TYPE_NEGATIVE: {
162                    return EffectType.EFFECT_COLOR_NEGATIVE;
163                }
164
165                case EffectColor.TYPE_COLOR:
166                default: {
167                    throw new IllegalArgumentException("Unsupported color type effect: " +
168                            colorEffect.getType());
169                }
170            }
171        } else {
172            throw new IllegalArgumentException("Unsupported effect: " + effect.getClass());
173        }
174    }
175
176    @Override
177    public boolean equals(Object object) {
178        if (!(object instanceof MovieEffect)) {
179            return false;
180        }
181        return mUniqueId.equals(((MovieEffect)object).mUniqueId);
182    }
183
184    @Override
185    public int hashCode() {
186        return mUniqueId.hashCode();
187    }
188}
189