1 /*
2  * Copyright (C) 2019 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.accessibility;
18 
19 import android.content.Context;
20 import android.graphics.drawable.AnimatedImageDrawable;
21 import android.graphics.drawable.Drawable;
22 import android.net.Uri;
23 import android.widget.ImageView;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settings.R;
29 
30 /**
31  * A custom {@link ImageView} preference for showing animated or static image, such as <a
32  * href="https://developers.google.com/speed/webp/">animated webp</a> and static png.
33  */
34 public class AnimatedImagePreference extends Preference {
35 
36     private Uri mImageUri;
37     private int mMaxHeight = -1;
38 
AnimatedImagePreference(Context context)39     AnimatedImagePreference(Context context) {
40         super(context);
41         setLayoutResource(R.layout.preference_animated_image);
42     }
43 
44     @Override
onBindViewHolder(PreferenceViewHolder holder)45     public void onBindViewHolder(PreferenceViewHolder holder) {
46         super.onBindViewHolder(holder);
47 
48         final ImageView imageView = holder.itemView.findViewById(R.id.animated_img);
49         if (imageView == null) {
50             return;
51         }
52 
53         if (mImageUri != null) {
54             imageView.setImageURI(mImageUri);
55 
56             final Drawable drawable = imageView.getDrawable();
57             if (drawable instanceof AnimatedImageDrawable) {
58                 ((AnimatedImageDrawable) drawable).start();
59             }
60         }
61 
62         if (mMaxHeight > -1) {
63             imageView.setMaxHeight(mMaxHeight);
64         }
65     }
66 
67     /**
68      * Sets image uri to display image in {@link ImageView}
69      *
70      * @param imageUri the Uri of an image
71      */
setImageUri(Uri imageUri)72     public void setImageUri(Uri imageUri) {
73         if (imageUri != null && !imageUri.equals(mImageUri)) {
74             mImageUri = imageUri;
75             notifyChanged();
76         }
77     }
78 
79     /**
80      * Sets the maximum height of the view.
81      *
82      * @param maxHeight the maximum height of ImageView in terms of pixels.
83      */
setMaxHeight(int maxHeight)84     public void setMaxHeight(int maxHeight) {
85         if (maxHeight != mMaxHeight) {
86             mMaxHeight = maxHeight;
87             notifyChanged();
88         }
89     }
90 }
91