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 java.io.InputStream;
20import java.io.FileInputStream;
21
22public class Movie {
23    private final int mNativeMovie;
24
25    private Movie(int nativeMovie) {
26        if (nativeMovie == 0) {
27            throw new RuntimeException("native movie creation failed");
28        }
29        mNativeMovie = nativeMovie;
30    }
31
32    public native int width();
33    public native int height();
34    public native boolean isOpaque();
35    public native int duration();
36
37    public native boolean setTime(int relativeMilliseconds);
38
39    public native void draw(Canvas canvas, float x, float y, Paint paint);
40
41    public void draw(Canvas canvas, float x, float y) {
42        draw(canvas, x, y, null);
43    }
44
45    public static native Movie decodeStream(InputStream is);
46    public static native Movie decodeByteArray(byte[] data, int offset,
47                                               int length);
48
49    private static native void nativeDestructor(int nativeMovie);
50
51    public static Movie decodeFile(String pathName) {
52        InputStream is;
53        try {
54            is = new FileInputStream(pathName);
55        }
56        catch (java.io.FileNotFoundException e) {
57            return null;
58        }
59        return decodeTempStream(is);
60    }
61
62    @Override
63    protected void finalize() throws Throwable {
64        try {
65            nativeDestructor(mNativeMovie);
66        } finally {
67            super.finalize();
68        }
69    }
70
71    private static Movie decodeTempStream(InputStream is) {
72        Movie moov = null;
73        try {
74            moov = decodeStream(is);
75            is.close();
76        }
77        catch (java.io.IOException e) {
78            /*  do nothing.
79                If the exception happened on open, moov will be null.
80                If it happened on close, moov is still valid.
81            */
82        }
83        return moov;
84    }
85}
86