| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.graphics; |
| 18 | |
| Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 19 | import android.content.res.AssetManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import java.io.InputStream; |
| 21 | import java.io.FileInputStream; |
| 22 | |
| 23 | public class Movie { |
| Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 24 | private final long mNativeMovie; |
| Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 25 | |
| Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 26 | private Movie(long nativeMovie) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| Derek Sollenberger | ab22c1c | 2014-09-03 14:12:47 -0400 | [diff] [blame] | 38 | public native boolean setTime(int relativeMilliseconds); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| Derek Sollenberger | 304cc53 | 2015-01-08 16:31:53 -0500 | [diff] [blame] | 40 | private native void nDraw(long nativeCanvas, float x, float y, long paintHandle); |
| Derek Sollenberger | ab22c1c | 2014-09-03 14:12:47 -0400 | [diff] [blame] | 41 | |
| 42 | public void draw(Canvas canvas, float x, float y, Paint paint) { |
| Derek Sollenberger | 304cc53 | 2015-01-08 16:31:53 -0500 | [diff] [blame] | 43 | nDraw(canvas.getNativeCanvasWrapper(), x, y, |
| 44 | paint != null ? paint.getNativeInstance() : 0); |
| Derek Sollenberger | ab22c1c | 2014-09-03 14:12:47 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | public void draw(Canvas canvas, float x, float y) { |
| Derek Sollenberger | 304cc53 | 2015-01-08 16:31:53 -0500 | [diff] [blame] | 48 | nDraw(canvas.getNativeCanvasWrapper(), x, y, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 51 | public static Movie decodeStream(InputStream is) { |
| 52 | if (is == null) { |
| 53 | return null; |
| 54 | } |
| 55 | if (is instanceof AssetManager.AssetInputStream) { |
| Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 56 | final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset(); |
| Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 57 | return nativeDecodeAsset(asset); |
| 58 | } |
| 59 | |
| 60 | return nativeDecodeStream(is); |
| 61 | } |
| 62 | |
| Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 63 | private static native Movie nativeDecodeAsset(long asset); |
| Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 64 | private static native Movie nativeDecodeStream(InputStream is); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | public static native Movie decodeByteArray(byte[] data, int offset, |
| 66 | int length); |
| 67 | |
| Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 68 | private static native void nativeDestructor(long nativeMovie); |
| Kimiyoshi Kusaka | 08d7778 | 2011-03-28 11:58:51 +0900 | [diff] [blame] | 69 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| Kimiyoshi Kusaka | 08d7778 | 2011-03-28 11:58:51 +0900 | [diff] [blame] | 81 | @Override |
| 82 | protected void finalize() throws Throwable { |
| 83 | try { |
| 84 | nativeDestructor(mNativeMovie); |
| 85 | } finally { |
| 86 | super.finalize(); |
| 87 | } |
| 88 | } |
| 89 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | 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 | } |