2 * Copyright (C) 2016 The Android Open Source Project
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.android.settings.gestures;
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;
35 import com.android.settings.R;
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
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;
49 public GesturePreference(Context context, AttributeSet attrs) {
50 super(context, attrs);
52 setLayoutResource(R.layout.gesture_preference);
53 TypedArray attributes = context.getTheme().obtainStyledAttributes(
55 R.styleable.GesturePreference,
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
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);
76 video.setOnTouchListener(new View.OnTouchListener() {
78 public boolean onTouch(View v, MotionEvent event) {
79 if (event.getAction() == MotionEvent.ACTION_DOWN) {
80 if (mMediaPlayer != null) {
81 if (mMediaPlayer.isPlaying()) {
85 imageView.setVisibility(View.GONE);
94 video.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
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() {
102 public void onPrepared(MediaPlayer mediaPlayer) {
103 mediaPlayer.setLooping(true);
110 public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
114 public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
115 imageView.setVisibility(View.VISIBLE);
116 if (mMediaPlayer != null) {
118 mMediaPlayer.reset();
119 mMediaPlayer.release();
125 public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
129 if (mMediaPlayer == null) {
130 Bitmap bitmap = mMediaMetadata.getFrameAtTime(0);
131 if (bitmap != null) {
132 imageView.setImageDrawable(new BitmapDrawable(bitmap));
134 imageView.setVisibility(View.VISIBLE);