Use default switch preference view if gesture animations video is not
available.

Check to see if animation video is available. If so, use the customized
view. Otherwise, use the default switch preference layout.

Test: make and run SettingsGoogleTests

Bug: 30701118
Change-Id: I0b25f41f8425b92834b45f706690648fb35a885d
diff --git a/res/layout/gesture_preference.xml b/res/layout/gesture_preference.xml
index 883b94e..d5685ea 100644
--- a/res/layout/gesture_preference.xml
+++ b/res/layout/gesture_preference.xml
@@ -51,6 +51,7 @@
     </LinearLayout>
 
     <LinearLayout
+        android:id="@+id/gesture_animation_view"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@color/gestures_setting_background_color"
@@ -58,7 +59,6 @@
         android:orientation="horizontal">
 
         <com.android.settings.widget.AspectRatioFrameLayout
-            android:id="@+id/gesture_animation_frame"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1"
diff --git a/src/com/android/settings/gestures/GesturePreference.java b/src/com/android/settings/gestures/GesturePreference.java
index 2d921bf..4536aa2 100644
--- a/src/com/android/settings/gestures/GesturePreference.java
+++ b/src/com/android/settings/gestures/GesturePreference.java
@@ -58,7 +58,6 @@
     public GesturePreference(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
-        setLayoutResource(R.layout.gesture_preference);
         TypedArray attributes = context.getTheme().obtainStyledAttributes(
                 attrs,
                 R.styleable.GesturePreference,
@@ -70,7 +69,8 @@
                     .appendPath(String.valueOf(animation))
                     .build();
             mMediaPlayer = MediaPlayer.create(mContext, mVideoPath);
-            if (mMediaPlayer != null) {
+            if (mMediaPlayer != null && mMediaPlayer.getDuration() > 0) {
+                setLayoutResource(R.layout.gesture_preference);
                 mMediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                     @Override
                     public void onSeekComplete(MediaPlayer mp) {
@@ -84,9 +84,8 @@
                         mediaPlayer.setLooping(true);
                     }
                 });
+                mAnimationAvailable = true;
             }
-            mAnimationAvailable = true;
-
         } catch (Exception e) {
             Log.w(TAG, "Animation resource not found. Will not show animation.");
         } finally {
@@ -97,15 +96,14 @@
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
+
+        if (!mAnimationAvailable) {
+            return;
+        }
+
         final TextureView video = (TextureView) holder.findViewById(R.id.gesture_video);
         final ImageView imageView = (ImageView) holder.findViewById(R.id.gesture_image);
         final ImageView playButton = (ImageView) holder.findViewById(R.id.gesture_play_button);
-        final View animationFrame = holder.findViewById(R.id.gesture_animation_frame);
-
-        if (!mAnimationAvailable) {
-            animationFrame.setVisibility(View.GONE);
-            return;
-        }
 
         video.setOnClickListener(new View.OnClickListener() {
             @Override
@@ -182,7 +180,9 @@
     }
 
     void loadPreview(LoaderManager manager, int id) {
-        Loader<Bitmap> loader = manager.initLoader(id, Bundle.EMPTY, this);
+        if (mAnimationAvailable) {
+            Loader<Bitmap> loader = manager.initLoader(id, Bundle.EMPTY, this);
+        }
     }
 
     void onViewVisible() {