OSDN Git Service

Merge \"Protect against crash in memory details\" into nyc-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / gestures / GesturePreference.java
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14
15 package com.android.settings.gestures;
16
17 import android.content.Context;
18 import android.content.res.TypedArray;
19 import android.graphics.Bitmap;
20 import android.graphics.drawable.BitmapDrawable;
21 import android.graphics.SurfaceTexture;
22 import android.media.MediaMetadataRetriever;
23 import android.media.MediaPlayer;
24 import android.net.Uri;
25 import android.support.v14.preference.SwitchPreference;
26 import android.support.v7.preference.PreferenceViewHolder;
27 import android.view.MotionEvent;
28 import android.view.View;
29 import android.view.Surface;
30 import android.view.TextureView;
31 import android.widget.ImageView;
32 import android.util.AttributeSet;
33 import android.util.Log;
34
35 import com.android.settings.R;
36
37 /**
38  * Preference item for a gesture with a switch to signify if it should be enabled.
39  * This shows the title and description of the gesture along with an animation showing how to do
40  * the gesture
41  */
42 public class GesturePreference extends SwitchPreference {
43     private static final String TAG = "GesturePreference";
44     private Uri mVideoPath;
45     private Context mContext;
46     private MediaPlayer mMediaPlayer;
47     private MediaMetadataRetriever mMediaMetadata;
48
49     public GesturePreference(Context context, AttributeSet attrs) {
50         super(context, attrs);
51         mContext = context;
52         setLayoutResource(R.layout.gesture_preference);
53         TypedArray attributes = context.getTheme().obtainStyledAttributes(
54                 attrs,
55                 R.styleable.GesturePreference,
56                 0, 0);
57         try {
58             int animation = attributes.getResourceId(R.styleable.GesturePreference_animation, 0);
59             mVideoPath = Uri.parse(
60                 "android.resource://" + context.getPackageName() + "/" + animation);
61             mMediaMetadata = new MediaMetadataRetriever();
62             mMediaMetadata.setDataSource(mContext, mVideoPath);
63         } catch (Exception e) {
64             // resource not available, show blank view
65         } finally {
66             attributes.recycle();
67         }
68     }
69
70     @Override
71     public void onBindViewHolder(PreferenceViewHolder holder) {
72         super.onBindViewHolder(holder);
73         final TextureView video = (TextureView) holder.findViewById(R.id.gesture_video);
74         final ImageView imageView = (ImageView) holder.findViewById(R.id.gesture_image);
75
76         video.setOnTouchListener(new View.OnTouchListener() {
77             @Override
78             public boolean onTouch(View v, MotionEvent event) {
79                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
80                     if (mMediaPlayer != null) {
81                         if (mMediaPlayer.isPlaying()) {
82                             mMediaPlayer.pause();
83                         } else {
84                             mMediaPlayer.start();
85                             imageView.setVisibility(View.GONE);
86                         }
87                     }
88                     return true;
89                 }
90                 return false;
91             }
92         });
93
94         video.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
95             @Override
96             public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
97                 mMediaPlayer = MediaPlayer.create(mContext, mVideoPath);
98                 if (mMediaPlayer != null) {
99                     mMediaPlayer.setSurface(new Surface(surfaceTexture));
100                     mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
101                         @Override
102                         public void onPrepared(MediaPlayer mediaPlayer) {
103                             mediaPlayer.setLooping(true);
104                         }
105                     });
106                 }
107             }
108
109             @Override
110             public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
111             }
112
113             @Override
114             public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
115                 imageView.setVisibility(View.VISIBLE);
116                 if (mMediaPlayer != null) {
117                     mMediaPlayer.stop();
118                     mMediaPlayer.reset();
119                     mMediaPlayer.release();
120                 }
121                 return false;
122             }
123
124             @Override
125             public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
126             }
127         });
128
129         if (mMediaPlayer == null) {
130             Bitmap bitmap = mMediaMetadata.getFrameAtTime(0);
131             if (bitmap != null) {
132                 imageView.setImageDrawable(new BitmapDrawable(bitmap));
133             }
134             imageView.setVisibility(View.VISIBLE);
135         }
136
137     }
138
139 }