blob: 2d305e7d4af78cd90548674489118ab527128921 [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
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070019import android.graphics.drawable.Drawable;
20import android.os.Bundle;
21import android.text.TextUtils;
22import android.view.View;
23import android.view.ViewGroup;
24
Chris Wren8a963ba2015-03-20 10:29:14 -040025public abstract class HighlightingFragment extends InstrumentedFragment {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070026
27 private static final String TAG = "HighlightSettingsFragment";
28
Fabrice Di Meglio1f414062014-04-21 09:31:36 -070029 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 400;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070030 private static final String SAVE_HIGHLIGHTED_KEY = "android:view_highlighted";
31
32 private String mViewKey;
33 private boolean mViewHighlighted = false;
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070034 private Drawable mHighlightDrawable;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070035
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() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070070 if (mHighlightDrawable == null) {
71 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
72 }
73 return mHighlightDrawable;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070074 }
75
76 private void highlightView(String key) {
77 final Drawable highlight = getHighlightDrawable();
78
79 // Try locating the View thru its Tag / Key
80 final View view = findViewForKey(getView(), key);
81 if (view != null ) {
82 view.setBackground(highlight);
83
84 getView().postDelayed(new Runnable() {
85 @Override
86 public void run() {
87 final int centerX = view.getWidth() / 2;
88 final int centerY = view.getHeight() / 2;
Alan Viveretteba348ca2014-05-19 15:10:36 -070089 highlight.setHotspot(centerX, centerY);
90 view.setPressed(true);
91 view.setPressed(false);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070092 }
93 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
94
95 mViewHighlighted = true;
96 }
97 }
98
99 private View findViewForKey(View root, String key) {
100 if (checkTag(root, key)) {
101 return root;
102 }
103 if (root instanceof ViewGroup) {
104 final ViewGroup group = (ViewGroup) root;
105 final int count = group.getChildCount();
106 for (int n = 0; n < count; n++) {
107 final View child = group.getChildAt(n);
108 final View view = findViewForKey(child, key);
109 if (view != null) {
110 return view;
111 }
112 }
113 }
114 return null;
115 }
116
117 private boolean checkTag(View view, String key) {
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700118 final Object tag = view.getTag(R.id.preference_highlight_key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700119 if (tag == null || !(tag instanceof String)) {
120 return false;
121 }
122 final String viewKey = (String) tag;
123 return (!TextUtils.isEmpty(viewKey) && viewKey.equals(key));
124 }
125}