blob: ecb42553e09507250d4b15c3c0536287811bda57 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Leon Scroggins III7315f1b2013-09-10 20:26:05 -040019import android.content.res.AssetManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import java.io.InputStream;
21import java.io.FileInputStream;
22
23public class Movie {
Ashok Bhatdcaf5592014-01-08 16:47:08 +000024 private final long mNativeMovie;
Leon Scroggins III7315f1b2013-09-10 20:26:05 -040025
Ashok Bhatdcaf5592014-01-08 16:47:08 +000026 private Movie(long nativeMovie) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 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
Derek Sollenbergerab22c1c2014-09-03 14:12:47 -040038 public native boolean setTime(int relativeMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Derek Sollenberger304cc532015-01-08 16:31:53 -050040 private native void nDraw(long nativeCanvas, float x, float y, long paintHandle);
Derek Sollenbergerab22c1c2014-09-03 14:12:47 -040041
42 public void draw(Canvas canvas, float x, float y, Paint paint) {
Derek Sollenberger304cc532015-01-08 16:31:53 -050043 nDraw(canvas.getNativeCanvasWrapper(), x, y,
44 paint != null ? paint.getNativeInstance() : 0);
Derek Sollenbergerab22c1c2014-09-03 14:12:47 -040045 }
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 public void draw(Canvas canvas, float x, float y) {
Derek Sollenberger304cc532015-01-08 16:31:53 -050048 nDraw(canvas.getNativeCanvasWrapper(), x, y, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 }
50
Leon Scroggins III7315f1b2013-09-10 20:26:05 -040051 public static Movie decodeStream(InputStream is) {
52 if (is == null) {
53 return null;
54 }
55 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhatdcaf5592014-01-08 16:47:08 +000056 final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Leon Scroggins III7315f1b2013-09-10 20:26:05 -040057 return nativeDecodeAsset(asset);
58 }
59
60 return nativeDecodeStream(is);
61 }
62
Ashok Bhatdcaf5592014-01-08 16:47:08 +000063 private static native Movie nativeDecodeAsset(long asset);
Leon Scroggins III7315f1b2013-09-10 20:26:05 -040064 private static native Movie nativeDecodeStream(InputStream is);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 public static native Movie decodeByteArray(byte[] data, int offset,
66 int length);
67
Ashok Bhatdcaf5592014-01-08 16:47:08 +000068 private static native void nativeDestructor(long nativeMovie);
Kimiyoshi Kusaka08d77782011-03-28 11:58:51 +090069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 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
Kimiyoshi Kusaka08d77782011-03-28 11:58:51 +090081 @Override
82 protected void finalize() throws Throwable {
83 try {
84 nativeDestructor(mNativeMovie);
85 } finally {
86 super.finalize();
87 }
88 }
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 private static Movie decodeTempStream(InputStream is) {
91 Movie moov = null;
92 try {
93 moov = decodeStream(is);
94 is.close();
95 }
96 catch (java.io.IOException e) {
97 /* do nothing.
98 If the exception happened on open, moov will be null.
99 If it happened on close, moov is still valid.
100 */
101 }
102 return moov;
103 }
104}