1 /*
2  * Copyright (C) 2023 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.shortcuts;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.text.method.LinkMovementMethod;
22 import android.util.AttributeSet;
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.TextView;
26 
27 import androidx.annotation.DrawableRes;
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.annotation.RawRes;
31 import androidx.preference.CheckBoxPreference;
32 import androidx.preference.PreferenceViewHolder;
33 
34 import com.android.settings.R;
35 import com.android.settingslib.widget.LottieColorUtils;
36 
37 import com.airbnb.lottie.LottieAnimationView;
38 import com.airbnb.lottie.LottieDrawable;
39 
40 /**
41  * A preference represents an accessibility shortcut option with a checkbox and a tutorial image
42  */
43 public class ShortcutOptionPreference extends CheckBoxPreference {
44 
45     private static final String TAG = "ShortcutOptionPreference";
46 
47     @DrawableRes
48     private int mIntroImageResId = Resources.ID_NULL;
49     @RawRes
50     private int mIntroImageRawResId = Resources.ID_NULL;
51 
52     private int mSummaryTextLineHeight;
53 
ShortcutOptionPreference( @onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)54     public ShortcutOptionPreference(
55             @NonNull Context context, @Nullable AttributeSet attrs,
56             int defStyleAttr, int defStyleRes) {
57         super(context, attrs, defStyleAttr, defStyleRes);
58         init();
59     }
60 
ShortcutOptionPreference(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)61     public ShortcutOptionPreference(@NonNull Context context, @Nullable AttributeSet attrs,
62             int defStyleAttr) {
63         super(context, attrs, defStyleAttr);
64         init();
65     }
66 
ShortcutOptionPreference(@onNull Context context, @Nullable AttributeSet attrs)67     public ShortcutOptionPreference(@NonNull Context context, @Nullable AttributeSet attrs) {
68         super(context, attrs);
69         init();
70     }
71 
ShortcutOptionPreference(@onNull Context context)72     public ShortcutOptionPreference(@NonNull Context context) {
73         super(context);
74         init();
75     }
76 
init()77     private void init() {
78         setLayoutResource(R.layout.accessibility_shortcut_option_checkable);
79     }
80 
81     @Override
onBindViewHolder(@onNull PreferenceViewHolder holder)82     public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
83         super.onBindViewHolder(holder);
84 
85         if (mIntroImageResId == Resources.ID_NULL && mIntroImageRawResId == Resources.ID_NULL) {
86             holder.findViewById(R.id.image).setVisibility(View.GONE);
87         } else {
88             holder.findViewById(R.id.image).setVisibility(View.VISIBLE);
89             LottieAnimationView imageView = holder.itemView.findViewById(R.id.image);
90 
91             if (mIntroImageRawResId != Resources.ID_NULL) {
92                 imageView.setFailureListener(result ->
93                         Log.w(TAG,
94                                 "Invalid image raw resource id: "
95                                         + getContext().getResources()
96                                                 .getResourceEntryName(mIntroImageRawResId),
97                                 result));
98                 imageView.setAnimation(mIntroImageRawResId);
99                 imageView.setRepeatCount(LottieDrawable.INFINITE);
100                 LottieColorUtils.applyDynamicColors(imageView.getContext(), imageView);
101                 imageView.playAnimation();
102             } else {
103                 imageView.setImageResource(mIntroImageResId);
104             }
105         }
106 
107         final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
108         if (summaryView != null) {
109             mSummaryTextLineHeight = summaryView.getLineHeight();
110             summaryView.setMovementMethod(LinkMovementMethod.getInstance());
111         }
112 
113         syncSummaryView(holder);
114     }
115 
116 
117     /**
118      * Sets the introduction image for this preference with a drawable resource ID.
119      */
setIntroImageResId(@rawableRes int introImageResId)120     public void setIntroImageResId(@DrawableRes int introImageResId) {
121         if (introImageResId != mIntroImageResId) {
122             mIntroImageResId = introImageResId;
123             mIntroImageRawResId = Resources.ID_NULL;
124             notifyChanged();
125         }
126     }
127 
128     /**
129      * Sets the introduction image for this preference with a raw resource ID for an animated image.
130      */
setIntroImageRawResId(@awRes int introImageRawResId)131     public void setIntroImageRawResId(@RawRes int introImageRawResId) {
132         if (introImageRawResId != mIntroImageRawResId) {
133             mIntroImageRawResId = introImageRawResId;
134             mIntroImageResId = Resources.ID_NULL;
135             notifyChanged();
136         }
137     }
138 
getSummaryTextLineHeight()139     public int getSummaryTextLineHeight() {
140         return mSummaryTextLineHeight;
141     }
142 }
143