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