Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.Fragment; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.os.Bundle; |
| 22 | import android.text.TextUtils; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | |
| 26 | public class HighlightingFragment extends Fragment { |
| 27 | |
| 28 | private static final String TAG = "HighlightSettingsFragment"; |
| 29 | |
Fabrice Di Meglio | 1f41406 | 2014-04-21 09:31:36 -0700 | [diff] [blame] | 30 | private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 400; |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 31 | 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; |
Alan Viverette | ba348ca | 2014-05-19 15:10:36 -0700 | [diff] [blame^] | 86 | highlight.setHotspot(centerX, centerY); |
| 87 | view.setPressed(true); |
| 88 | view.setPressed(false); |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 89 | } |
| 90 | }, DELAY_HIGHLIGHT_DURATION_MILLIS); |
| 91 | |
| 92 | mViewHighlighted = true; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private View findViewForKey(View root, String key) { |
| 97 | if (checkTag(root, key)) { |
| 98 | return root; |
| 99 | } |
| 100 | if (root instanceof ViewGroup) { |
| 101 | final ViewGroup group = (ViewGroup) root; |
| 102 | final int count = group.getChildCount(); |
| 103 | for (int n = 0; n < count; n++) { |
| 104 | final View child = group.getChildAt(n); |
| 105 | final View view = findViewForKey(child, key); |
| 106 | if (view != null) { |
| 107 | return view; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | private boolean checkTag(View view, String key) { |
| 115 | final Object tag = view.getTag(); |
| 116 | if (tag == null || !(tag instanceof String)) { |
| 117 | return false; |
| 118 | } |
| 119 | final String viewKey = (String) tag; |
| 120 | return (!TextUtils.isEmpty(viewKey) && viewKey.equals(key)); |
| 121 | } |
| 122 | } |