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