1/* 2 * Copyright (C) 2006 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.graphics; 18 19import android.content.res.AssetManager; 20import java.io.InputStream; 21import java.io.FileInputStream; 22 23public class Movie { 24 private long mNativeMovie; 25 26 private Movie(long nativeMovie) { 27 if (nativeMovie == 0) { 28 throw new RuntimeException("native movie creation failed"); 29 } 30 mNativeMovie = nativeMovie; 31 } 32 33 public native int width(); 34 public native int height(); 35 public native boolean isOpaque(); 36 public native int duration(); 37 38 public native boolean setTime(int relativeMilliseconds); 39 40 private native void nDraw(long nativeCanvas, float x, float y, long paintHandle); 41 42 public void draw(Canvas canvas, float x, float y, Paint paint) { 43 nDraw(canvas.getNativeCanvasWrapper(), x, y, 44 paint != null ? paint.getNativeInstance() : 0); 45 } 46 47 public void draw(Canvas canvas, float x, float y) { 48 nDraw(canvas.getNativeCanvasWrapper(), x, y, 0); 49 } 50 51 public static Movie decodeStream(InputStream is) { 52 if (is == null) { 53 return null; 54 } 55 if (is instanceof AssetManager.AssetInputStream) { 56 final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset(); 57 return nativeDecodeAsset(asset); 58 } 59 60 return nativeDecodeStream(is); 61 } 62 63 private static native Movie nativeDecodeAsset(long asset); 64 private static native Movie nativeDecodeStream(InputStream is); 65 public static native Movie decodeByteArray(byte[] data, int offset, 66 int length); 67 68 private static native void nativeDestructor(long nativeMovie); 69 70 public static Movie decodeFile(String pathName) { 71 InputStream is; 72 try { 73 is = new FileInputStream(pathName); 74 } 75 catch (java.io.FileNotFoundException e) { 76 return null; 77 } 78 return decodeTempStream(is); 79 } 80 81 @Override 82 protected void finalize() throws Throwable { 83 try { 84 nativeDestructor(mNativeMovie); 85 mNativeMovie = 0; 86 } finally { 87 super.finalize(); 88 } 89 } 90 91 private static Movie decodeTempStream(InputStream is) { 92 Movie moov = null; 93 try { 94 moov = decodeStream(is); 95 is.close(); 96 } 97 catch (java.io.IOException e) { 98 /* do nothing. 99 If the exception happened on open, moov will be null. 100 If it happened on close, moov is still valid. 101 */ 102 } 103 return moov; 104 } 105} 106