2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.widget;
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;
29 import com.android.settings.R;
31 public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
34 static final long DELAY_HIGHLIGHT_DURATION_MILLIS = 600L;
35 private static final long HIGHLIGHT_DURATION = 5000L;
37 private final int mHighlightColor;
38 private final int mNormalBackgroundRes;
39 private final String mHighlightKey;
41 private boolean mHighlightRequested;
42 private int mHighlightPosition = RecyclerView.NO_POSITION;
44 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup, String key,
45 boolean highlightRequested) {
46 super(preferenceGroup);
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);
58 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
59 super.onBindViewHolder(holder, position);
60 updateBackground(holder, position);
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);
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);
78 public void requestHighlight(View root, RecyclerView recyclerView) {
79 if (mHighlightRequested || recyclerView == null || TextUtils.isEmpty(mHighlightKey)) {
82 root.postDelayed(() -> {
83 final int position = getPreferenceAdapterPosition(mHighlightKey);
87 mHighlightRequested = true;
88 recyclerView.getLayoutManager().scrollToPosition(position);
89 mHighlightPosition = position;
90 notifyItemChanged(position);
91 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
94 public boolean isHighlightRequested() {
95 return mHighlightRequested;
98 private void removeHighlightBackground(View v) {
99 v.setBackgroundResource(mNormalBackgroundRes);
100 v.setTag(R.id.preference_highlighted, false);