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
18package android.media.videoeditor;
19
20import java.util.HashMap;
21import java.util.Map;
22
23/**
24 * This is the super class for all Overlay classes.
25 * {@hide}
26 */
27public abstract class Overlay {
28    /**
29     *  Instance variables
30     */
31    private final String mUniqueId;
32    /**
33     *  The overlay owner
34     */
35    private final MediaItem mMediaItem;
36    /**
37     *  user attributes
38     */
39    private final Map<String, String> mUserAttributes;
40
41    protected long mStartTimeMs;
42    protected long mDurationMs;
43
44    /**
45     * Default constructor
46     */
47    @SuppressWarnings("unused")
48    private Overlay() {
49        this(null, null, 0, 0);
50    }
51
52    /**
53     * Constructor
54     *
55     * @param mediaItem The media item owner
56     * @param overlayId The overlay id
57     * @param startTimeMs The start time relative to the media item start time
58     * @param durationMs The duration
59     *
60     * @throws IllegalArgumentException if the file type is not PNG or the
61     *      startTimeMs and durationMs are incorrect.
62     */
63    public Overlay(MediaItem mediaItem, String overlayId, long startTimeMs,
64           long durationMs) {
65        if (mediaItem == null) {
66            throw new IllegalArgumentException("Media item cannot be null");
67        }
68
69        if ((startTimeMs<0) || (durationMs<0) ) {
70            throw new IllegalArgumentException("Invalid start time and/OR duration");
71        }
72
73        if (startTimeMs + durationMs > mediaItem.getDuration()) {
74            throw new IllegalArgumentException("Invalid start time and duration");
75        }
76
77        mMediaItem = mediaItem;
78        mUniqueId = overlayId;
79        mStartTimeMs = startTimeMs;
80        mDurationMs = durationMs;
81        mUserAttributes = new HashMap<String, String>();
82    }
83
84    /**
85     * Get the overlay ID.
86     *
87     * @return The of the overlay
88     */
89    public String getId() {
90        return mUniqueId;
91    }
92
93    /**
94     * Get the duration of overlay.
95     *
96     * @return The duration of the overlay effect
97     */
98    public long getDuration() {
99        return mDurationMs;
100    }
101
102    /**
103     * If a preview or export is in progress, then this change is effective for
104     * next preview or export session.
105     *
106     * @param durationMs The duration in milliseconds
107     */
108    public void setDuration(long durationMs) {
109        if (durationMs < 0) {
110            throw new IllegalArgumentException("Invalid duration");
111        }
112
113        if (mStartTimeMs + durationMs > mMediaItem.getDuration()) {
114            throw new IllegalArgumentException("Duration is too large");
115        }
116
117        getMediaItem().getNativeContext().setGeneratePreview(true);
118
119        final long oldDurationMs = mDurationMs;
120        mDurationMs = durationMs;
121
122        mMediaItem.invalidateTransitions(mStartTimeMs, oldDurationMs, mStartTimeMs, mDurationMs);
123    }
124
125    /**
126     * Get the start time of overlay.
127     *
128     * @return the start time of the overlay
129     */
130    public long getStartTime() {
131        return mStartTimeMs;
132    }
133
134    /**
135     * Set the start time for the overlay. If a preview or export is in
136     * progress, then this change is effective for next preview or export
137     * session.
138     *
139     * @param startTimeMs start time in milliseconds
140     */
141    public void setStartTime(long startTimeMs) {
142        if (startTimeMs + mDurationMs > mMediaItem.getDuration()) {
143            throw new IllegalArgumentException("Start time is too large");
144        }
145
146        getMediaItem().getNativeContext().setGeneratePreview(true);
147
148        final long oldStartTimeMs = mStartTimeMs;
149        mStartTimeMs = startTimeMs;
150
151        mMediaItem.invalidateTransitions(oldStartTimeMs, mDurationMs, mStartTimeMs, mDurationMs);
152    }
153
154    /**
155     * Set the start time and duration
156     *
157     * @param startTimeMs start time in milliseconds
158     * @param durationMs The duration in milliseconds
159     */
160    public void setStartTimeAndDuration(long startTimeMs, long durationMs) {
161        if (startTimeMs + durationMs > mMediaItem.getDuration()) {
162            throw new IllegalArgumentException("Invalid start time or duration");
163        }
164
165        getMediaItem().getNativeContext().setGeneratePreview(true);
166
167        final long oldStartTimeMs = mStartTimeMs;
168        final long oldDurationMs = mDurationMs;
169
170        mStartTimeMs = startTimeMs;
171        mDurationMs = durationMs;
172
173        mMediaItem.invalidateTransitions(oldStartTimeMs, oldDurationMs, mStartTimeMs, mDurationMs);
174    }
175
176    /**
177     * Get the media item owner.
178     *
179     * @return The media item owner.
180     */
181    public MediaItem getMediaItem() {
182        return mMediaItem;
183    }
184
185    /**
186     * Set a user attribute
187     *
188     * @param name The attribute name
189     * @param value The attribute value
190     */
191    public void setUserAttribute(String name, String value) {
192        mUserAttributes.put(name, value);
193    }
194
195    /**
196     * Get the current user attributes set.
197     *
198     * @return The user attributes
199     */
200    public Map<String, String> getUserAttributes() {
201        return mUserAttributes;
202    }
203
204    /*
205     * {@inheritDoc}
206     */
207    @Override
208    public boolean equals(Object object) {
209        if (!(object instanceof Overlay)) {
210            return false;
211        }
212        return mUniqueId.equals(((Overlay)object).mUniqueId);
213    }
214
215    /*
216     * {@inheritDoc}
217     */
218    @Override
219    public int hashCode() {
220        return mUniqueId.hashCode();
221    }
222}
223