blob: ef2fbf3107f7cdf46f9aac81c33abf03c162b52d [file] [log] [blame]
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07001/*
2 * Copyright (C) 2014 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
17package com.android.settings;
18
19import android.app.Fragment;
20import android.graphics.drawable.Drawable;
21import android.os.Bundle;
22import android.text.TextUtils;
23import android.view.View;
24import android.view.ViewGroup;
25
Chris Wren8a963ba2015-03-20 10:29:14 -040026public abstract class HighlightingFragment extends InstrumentedFragment {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070027
28 private static final String TAG = "HighlightSettingsFragment";
29
Fabrice Di Meglio1f414062014-04-21 09:31:36 -070030 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 400;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070031 private static final String SAVE_HIGHLIGHTED_KEY = "android:view_highlighted";
32
33 private String mViewKey;
34 private boolean mViewHighlighted = false;
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070035 private Drawable mHighlightDrawable;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070036
37 @Override
38 public void onCreate(Bundle icicle) {
39 super.onCreate(icicle);
40
41 if (icicle != null) {
42 mViewHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
43 }
44 }
45
46 @Override
47 public void onSaveInstanceState(Bundle outState) {
48 super.onSaveInstanceState(outState);
49
50 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mViewHighlighted);
51 }
52
53 @Override
54 public void onActivityCreated(Bundle savedInstanceState) {
55 super.onActivityCreated(savedInstanceState);
56
57 final Bundle args = getArguments();
58 if (args != null) {
59 mViewKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
60 highlightViewIfNeeded();
61 }
62 }
63
64 public void highlightViewIfNeeded() {
65 if (!mViewHighlighted &&!TextUtils.isEmpty(mViewKey)) {
66 highlightView(mViewKey);
67 }
68 }
69
70 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070071 if (mHighlightDrawable == null) {
72 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
73 }
74 return mHighlightDrawable;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070075 }
76
77 private void highlightView(String key) {
78 final Drawable highlight = getHighlightDrawable();
79
80 // Try locating the View thru its Tag / Key
81 final View view = findViewForKey(getView(), key);
82 if (view != null ) {
83 view.setBackground(highlight);
84
85 getView().postDelayed(new Runnable() {
86 @Override
87 public void run() {
88 final int centerX = view.getWidth() / 2;
89 final int centerY = view.getHeight() / 2;
Alan Viveretteba348ca2014-05-19 15:10:36 -070090 highlight.setHotspot(centerX, centerY);
91 view.setPressed(true);
92 view.setPressed(false);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070093 }
94 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
95
96 mViewHighlighted = true;
97 }
98 }
99
100 private View findViewForKey(View root, String key) {
101 if (checkTag(root, key)) {
102 return root;
103 }
104 if (root instanceof ViewGroup) {
105 final ViewGroup group = (ViewGroup) root;
106 final int count = group.getChildCount();
107 for (int n = 0; n < count; n++) {
108 final View child = group.getChildAt(n);
109 final View view = findViewForKey(child, key);
110 if (view != null) {
111 return view;
112 }
113 }
114 }
115 return null;
116 }
117
118 private boolean checkTag(View view, String key) {
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700119 final Object tag = view.getTag(R.id.preference_highlight_key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700120 if (tag == null || !(tag instanceof String)) {
121 return false;
122 }
123 final String viewKey = (String) tag;
124 return (!TextUtils.isEmpty(viewKey) && viewKey.equals(key));
125 }
126}