• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 package com.android.settings.fuelgauge;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.View;
21 import android.widget.ImageView;
22 import android.widget.TextView;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settings.R;
29 
30 /** A preference for expandable section divider. */
31 public class ExpandDividerPreference extends Preference {
32     private static final String TAG = "ExpandDividerPreference";
33     @VisibleForTesting
34     static final String PREFERENCE_KEY = "expandable_divider";
35 
36     @VisibleForTesting TextView mTextView;
37     @VisibleForTesting ImageView mImageView;
38     private OnExpandListener mOnExpandListener;
39 
40     private boolean mIsExpanded = false;
41     private String mTitleContent = null;
42 
43     /** A callback listener for expand state is changed by users. */
44     public interface OnExpandListener {
onExpand(boolean isExpanded)45         void onExpand(boolean isExpanded);
46     }
47 
ExpandDividerPreference(Context context)48     public ExpandDividerPreference(Context context) {
49         this(context, /*attrs=*/ null);
50     }
51 
ExpandDividerPreference(Context context, AttributeSet attrs)52     public ExpandDividerPreference(Context context, AttributeSet attrs) {
53         super(context, attrs);
54         setLayoutResource(R.layout.preference_expand_divider);
55         setKey(PREFERENCE_KEY);
56     }
57 
58     @Override
onBindViewHolder(PreferenceViewHolder view)59     public void onBindViewHolder(PreferenceViewHolder view) {
60         super.onBindViewHolder(view);
61         mTextView = (TextView) view.findViewById(R.id.expand_title);
62         mImageView = (ImageView) view.findViewById(R.id.expand_icon);
63         refreshState();
64     }
65 
66     @Override
onClick()67     public void onClick() {
68         mIsExpanded = !mIsExpanded;
69         refreshState();
70         if (mOnExpandListener != null) {
71             mOnExpandListener.onExpand(mIsExpanded);
72         }
73     }
74 
setTitle(final String titleContent)75     void setTitle(final String titleContent) {
76         mTitleContent = titleContent;
77         if (mTextView != null) {
78             mTextView.postDelayed(
79                 () -> mTextView.setText(titleContent), 50);
80         }
81     }
82 
setIsExpanded(boolean isExpanded)83     void setIsExpanded(boolean isExpanded) {
84         mIsExpanded = isExpanded;
85         refreshState();
86     }
87 
setOnExpandListener(OnExpandListener listener)88     void setOnExpandListener(OnExpandListener listener) {
89         mOnExpandListener = listener;
90     }
91 
refreshState()92     private void refreshState() {
93         final int iconId =
94             mIsExpanded
95                 ? R.drawable.ic_settings_expand_less
96                 : R.drawable.ic_settings_expand_more;
97         if (mImageView != null) {
98             mImageView.setImageResource(iconId);
99         }
100         setTitle(mTitleContent);
101     }
102 }
103