1 /*
2  * Copyright (C) 2015 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 com.android.settings.fingerprint;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.graphics.SurfaceTexture;
23 import android.media.MediaPlayer;
24 import android.media.MediaPlayer.OnInfoListener;
25 import android.media.MediaPlayer.OnPreparedListener;
26 import android.net.Uri;
27 import android.util.AttributeSet;
28 import android.view.Surface;
29 import android.view.TextureView;
30 import android.view.View;
31 
32 import com.android.settings.R;
33 
34 /**
35  * A view containing a VideoView for showing the user how to enroll a fingerprint
36  */
37 public class FingerprintLocationAnimationVideoView extends TextureView
38         implements FingerprintFindSensorAnimation {
39     protected float mAspect = 1.0f; // initial guess until we know
40     protected MediaPlayer mMediaPlayer;
41 
FingerprintLocationAnimationVideoView(Context context, AttributeSet attrs)42     public FingerprintLocationAnimationVideoView(Context context, AttributeSet attrs) {
43         super(context, attrs);
44 
45     }
46 
47     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)48     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
49         // Width is driven by measurespec, height is derrived from aspect ratio
50         int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
51         int height = Math.round(mAspect * originalWidth);
52         super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
53     }
54 
55     @Override
onFinishInflate()56     protected void onFinishInflate() {
57         super.onFinishInflate();
58         setSurfaceTextureListener(new SurfaceTextureListener() {
59             @Override
60             public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
61                     int height) {
62                 setVisibility(View.INVISIBLE);
63                 Uri videoUri = resourceEntryToUri(mContext, R.raw.fingerprint_location_animation);
64                 mMediaPlayer = MediaPlayer.create(mContext, videoUri);
65                 mMediaPlayer.setSurface(new Surface(surfaceTexture));
66                 mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
67                     @Override
68                     public void onPrepared(MediaPlayer mediaPlayer) {
69                         mediaPlayer.setLooping(true);
70                     }
71                 });
72                 mMediaPlayer.setOnInfoListener(new OnInfoListener() {
73                     @Override
74                     public boolean onInfo(MediaPlayer mediaPlayer, int what, int extra) {
75                         if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
76                             // Keep the view hidden until video starts
77                             setVisibility(View.VISIBLE);
78                         }
79                         return false;
80                     }
81                 });
82                 mAspect = (float) mMediaPlayer.getVideoHeight() / mMediaPlayer.getVideoWidth();
83                 requestLayout();
84                 startAnimation();
85             }
86 
87             @Override
88             public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
89                     int width, int height) {
90             }
91 
92             @Override
93             public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
94                 return false;
95             }
96 
97             @Override
98             public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
99             }
100         });
101     }
102 
resourceEntryToUri(Context context, int id)103     private static Uri resourceEntryToUri (Context context, int id) {
104         Resources res = context.getResources();
105         return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
106                 res.getResourcePackageName(id) + '/' +
107                 res.getResourceTypeName(id) + '/' +
108                 res.getResourceEntryName(id));
109     }
110 
111     @Override
startAnimation()112     public void startAnimation() {
113         if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
114             mMediaPlayer.start();
115         }
116     }
117 
118     @Override
stopAnimation()119     public void stopAnimation() {
120         if (mMediaPlayer != null) {
121             mMediaPlayer.stop();
122             mMediaPlayer.release();
123             mMediaPlayer = null;
124         }
125     }
126 
127     @Override
pauseAnimation()128     public void pauseAnimation() {
129         if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
130             mMediaPlayer.pause();
131         }
132     }
133 
134 }
135