OSDN Git Service

Merge "Zen Condition text and primary click changes"
[android-x86/packages-apps-Settings.git] / src / com / android / settings / widget / HighlightablePreferenceGroupAdapter.java
1 /*
2  * Copyright (C) 2018 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.widget;
18
19 import android.content.Context;
20 import android.support.annotation.VisibleForTesting;
21 import android.support.v7.preference.PreferenceGroup;
22 import android.support.v7.preference.PreferenceGroupAdapter;
23 import android.support.v7.preference.PreferenceViewHolder;
24 import android.support.v7.widget.RecyclerView;
25 import android.text.TextUtils;
26 import android.util.TypedValue;
27 import android.view.View;
28
29 import com.android.settings.R;
30
31 public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
32
33     @VisibleForTesting
34     static final long DELAY_HIGHLIGHT_DURATION_MILLIS = 600L;
35     private static final long HIGHLIGHT_DURATION = 5000L;
36
37     private final int mHighlightColor;
38     private final int mNormalBackgroundRes;
39     private final String mHighlightKey;
40
41     private boolean mHighlightRequested;
42     private int mHighlightPosition = RecyclerView.NO_POSITION;
43
44     public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup, String key,
45             boolean highlightRequested) {
46         super(preferenceGroup);
47         mHighlightKey = key;
48         mHighlightRequested = highlightRequested;
49         final Context context = preferenceGroup.getContext();
50         final TypedValue outValue = new TypedValue();
51         context.getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
52                 outValue, true /* resolveRefs */);
53         mNormalBackgroundRes = outValue.resourceId;
54         mHighlightColor = context.getColor(R.color.preference_highligh_color);
55     }
56
57     @Override
58     public void onBindViewHolder(PreferenceViewHolder holder, int position) {
59         super.onBindViewHolder(holder, position);
60         updateBackground(holder, position);
61     }
62
63     @VisibleForTesting
64     void updateBackground(PreferenceViewHolder holder, int position) {
65         View v = holder.itemView;
66         if (position == mHighlightPosition) {
67             v.setBackgroundColor(mHighlightColor);
68             v.setTag(R.id.preference_highlighted, true);
69             v.postDelayed(() -> {
70                 mHighlightPosition = RecyclerView.NO_POSITION;
71                 removeHighlightBackground(v);
72             }, HIGHLIGHT_DURATION);
73         } else if (Boolean.TRUE.equals(v.getTag(R.id.preference_highlighted))) {
74             removeHighlightBackground(v);
75         }
76     }
77
78     public void requestHighlight(View root, RecyclerView recyclerView) {
79         if (mHighlightRequested || recyclerView == null || TextUtils.isEmpty(mHighlightKey)) {
80             return;
81         }
82         root.postDelayed(() -> {
83             final int position = getPreferenceAdapterPosition(mHighlightKey);
84             if (position < 0) {
85                 return;
86             }
87             mHighlightRequested = true;
88             recyclerView.getLayoutManager().scrollToPosition(position);
89             mHighlightPosition = position;
90             notifyItemChanged(position);
91         }, DELAY_HIGHLIGHT_DURATION_MILLIS);
92     }
93
94     public boolean isHighlightRequested() {
95         return mHighlightRequested;
96     }
97
98     private void removeHighlightBackground(View v) {
99         v.setBackgroundResource(mNormalBackgroundRes);
100         v.setTag(R.id.preference_highlighted, false);
101     }
102 }