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 final 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    public native void draw(Canvas canvas, float x, float y, Paint paint);
41
42    public void draw(Canvas canvas, float x, float y) {
43        draw(canvas, x, y, null);
44    }
45
46    public static Movie decodeStream(InputStream is) {
47        if (is == null) {
48            return null;
49        }
50        if (is instanceof AssetManager.AssetInputStream) {
51            final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
52            return nativeDecodeAsset(asset);
53        }
54
55        return nativeDecodeStream(is);
56    }
57
58    private static native Movie nativeDecodeAsset(long asset);
59    private static native Movie nativeDecodeStream(InputStream is);
60    public static native Movie decodeByteArray(byte[] data, int offset,
61                                               int length);
62
63    private static native void nativeDestructor(long nativeMovie);
64
65    public static Movie decodeFile(String pathName) {
66        InputStream is;
67        try {
68            is = new FileInputStream(pathName);
69        }
70        catch (java.io.FileNotFoundException e) {
71            return null;
72        }
73        return decodeTempStream(is);
74    }
75
76    @Override
77    protected void finalize() throws Throwable {
78        try {
79            nativeDestructor(mNativeMovie);
80        } finally {
81            super.finalize();
82        }
83    }
84
85    private static Movie decodeTempStream(InputStream is) {
86        Movie moov = null;
87        try {
88            moov = decodeStream(is);
89            is.close();
90        }
91        catch (java.io.IOException e) {
92            /*  do nothing.
93                If the exception happened on open, moov will be null.
94                If it happened on close, moov is still valid.
95            */
96        }
97        return moov;
98    }
99}
100