blob: a1d6be06574aed8f388aa02c603df8721ecdaa80 [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
26public class HighlightingFragment extends Fragment {
27
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;
35
36 @Override
37 public void onCreate(Bundle icicle) {
38 super.onCreate(icicle);
39
40 if (icicle != null) {
41 mViewHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
42 }
43 }
44
45 @Override
46 public void onSaveInstanceState(Bundle outState) {
47 super.onSaveInstanceState(outState);
48
49 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mViewHighlighted);
50 }
51
52 @Override
53 public void onActivityCreated(Bundle savedInstanceState) {
54 super.onActivityCreated(savedInstanceState);
55
56 final Bundle args = getArguments();
57 if (args != null) {
58 mViewKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
59 highlightViewIfNeeded();
60 }
61 }
62
63 public void highlightViewIfNeeded() {
64 if (!mViewHighlighted &&!TextUtils.isEmpty(mViewKey)) {
65 highlightView(mViewKey);
66 }
67 }
68
69 private Drawable getHighlightDrawable() {
70 return getResources().getDrawable(R.drawable.preference_highlight);
71 }
72
73 private void highlightView(String key) {
74 final Drawable highlight = getHighlightDrawable();
75
76 // Try locating the View thru its Tag / Key
77 final View view = findViewForKey(getView(), key);
78 if (view != null ) {
79 view.setBackground(highlight);
80
81 getView().postDelayed(new Runnable() {
82 @Override
83 public void run() {
84 final int centerX = view.getWidth() / 2;
85 final int centerY = view.getHeight() / 2;
86 highlight.setHotspot(0, centerX, centerY);
87 highlight.clearHotspots();
88 }
89 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
90
91 mViewHighlighted = true;
92 }
93 }
94
95 private View findViewForKey(View root, String key) {
96 if (checkTag(root, key)) {
97 return root;
98 }
99 if (root instanceof ViewGroup) {
100 final ViewGroup group = (ViewGroup) root;
101 final int count = group.getChildCount();
102 for (int n = 0; n < count; n++) {
103 final View child = group.getChildAt(n);
104 final View view = findViewForKey(child, key);
105 if (view != null) {
106 return view;
107 }
108 }
109 }
110 return null;
111 }
112
113 private boolean checkTag(View view, String key) {
114 final Object tag = view.getTag();
115 if (tag == null || !(tag instanceof String)) {
116 return false;
117 }
118 final String viewKey = (String) tag;
119 return (!TextUtils.isEmpty(viewKey) && viewKey.equals(key));
120 }
121}