Home | History | Annotate | Download | only in settings
      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 
     30     private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 400;
     31     private static final String SAVE_HIGHLIGHTED_KEY = "android:view_highlighted";
     32 
     33     private String mViewKey;
     34     private boolean mViewHighlighted = false;
     35     private Drawable mHighlightDrawable;
     36 
     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() {
     71         if (mHighlightDrawable == null) {
     72             mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
     73         }
     74         return mHighlightDrawable;
     75     }
     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;
     90                     highlight.setHotspot(centerX, centerY);
     91                     view.setPressed(true);
     92                     view.setPressed(false);
     93                 }
     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) {
    119         final Object tag = view.getTag(R.id.preference_highlight_key);
    120         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 }
    127