• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)47     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
48         // Width is driven by measurespec, height is derrived from aspect ratio
49         int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
50         int height = Math.round(mAspect * originalWidth);
51         super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
52     }
53 
getFingerprintLocationAnimation()54     protected Uri getFingerprintLocationAnimation() {
55         return resourceEntryToUri(getContext(), R.raw.fingerprint_location_animation);
56     }
57 
58     @Override
onFinishInflate()59     protected void onFinishInflate() {
60         super.onFinishInflate();
61         setSurfaceTextureListener(new SurfaceTextureListener() {
62             private SurfaceTexture mTextureToDestroy = null;
63 
64             @Override
65             public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
66                     int height) {
67                 setVisibility(View.INVISIBLE);
68                 Uri videoUri = getFingerprintLocationAnimation();
69                 if (mMediaPlayer != null) {
70                     mMediaPlayer.release();
71                 }
72                 if (mTextureToDestroy != null) {
73                     mTextureToDestroy.release();
74                     mTextureToDestroy = null;
75                 }
76                 mMediaPlayer = MediaPlayer.create(mContext, videoUri);
77                 mMediaPlayer.setSurface(new Surface(surfaceTexture));
78                 mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
79                     @Override
80                     public void onPrepared(MediaPlayer mediaPlayer) {
81                         mediaPlayer.setLooping(true);
82                     }
83                 });
84                 mMediaPlayer.setOnInfoListener(new OnInfoListener() {
85                     @Override
86                     public boolean onInfo(MediaPlayer mediaPlayer, int what, int extra) {
87                         if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
88                             // Keep the view hidden until video starts
89                             setVisibility(View.VISIBLE);
90                         }
91                         return false;
92                     }
93                 });
94                 mAspect = (float) mMediaPlayer.getVideoHeight() / mMediaPlayer.getVideoWidth();
95                 requestLayout();
96                 startAnimation();
97             }
98 
99             @Override
100             public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
101                     int width, int height) {
102             }
103 
104             @Override
105             public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
106                 mTextureToDestroy = surfaceTexture;
107                 return false;
108             }
109 
110             @Override
111             public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
112             }
113         });
114     }
115 
resourceEntryToUri(Context context, int id)116     protected static Uri resourceEntryToUri (Context context, int id) {
117         Resources res = context.getResources();
118         return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
119                 res.getResourcePackageName(id) + '/' +
120                 res.getResourceTypeName(id) + '/' +
121                 res.getResourceEntryName(id));
122     }
123 
124     @Override
startAnimation()125     public void startAnimation() {
126         if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
127             mMediaPlayer.start();
128         }
129     }
130 
131     @Override
stopAnimation()132     public void stopAnimation() {
133         if (mMediaPlayer != null) {
134             mMediaPlayer.stop();
135             mMediaPlayer.release();
136             mMediaPlayer = null;
137         }
138     }
139 
140     @Override
pauseAnimation()141     public void pauseAnimation() {
142         if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
143             mMediaPlayer.pause();
144         }
145     }
146 
147 }
148