1 /*
2  * Copyright (C) 2011 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.browser.view;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.graphics.drawable.StateListDrawable;
22 import android.graphics.drawable.TransitionDrawable;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.view.ViewConfiguration;
27 import android.widget.RelativeLayout;
28 
29 public class BookmarkContainer extends RelativeLayout implements OnClickListener {
30 
31     private OnClickListener mClickListener;
32     private boolean mIgnoreRequestLayout = false;
33 
BookmarkContainer(Context context)34     public BookmarkContainer(Context context) {
35         super(context);
36         init();
37     }
38 
BookmarkContainer(Context context, AttributeSet attrs)39     public BookmarkContainer(Context context, AttributeSet attrs) {
40         super(context, attrs);
41         init();
42     }
43 
BookmarkContainer( Context context, AttributeSet attrs, int defStyle)44     public BookmarkContainer(
45             Context context, AttributeSet attrs, int defStyle) {
46         super(context, attrs, defStyle);
47         init();
48     }
49 
init()50     void init() {
51         setFocusable(true);
52         super.setOnClickListener(this);
53     }
54 
55     @Override
setBackgroundDrawable(Drawable d)56     public void setBackgroundDrawable(Drawable d) {
57         super.setBackgroundDrawable(d);
58     }
59 
60     @Override
setOnClickListener(OnClickListener l)61     public void setOnClickListener(OnClickListener l) {
62         mClickListener = l;
63     }
64 
65     @Override
drawableStateChanged()66     protected void drawableStateChanged() {
67         super.drawableStateChanged();
68         updateTransitionDrawable(isPressed());
69     }
70 
updateTransitionDrawable(boolean pressed)71     void updateTransitionDrawable(boolean pressed) {
72         final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
73         Drawable selector = getBackground();
74         if (selector != null && selector instanceof StateListDrawable) {
75             Drawable d = ((StateListDrawable)selector).getCurrent();
76             if (d != null && d instanceof TransitionDrawable) {
77                 if (pressed && isLongClickable()) {
78                     ((TransitionDrawable) d).startTransition(longPressTimeout);
79                 } else {
80                     ((TransitionDrawable) d).resetTransition();
81                 }
82             }
83         }
84     }
85 
86     @Override
onClick(View view)87     public void onClick(View view) {
88         updateTransitionDrawable(false);
89         if (mClickListener != null) {
90             mClickListener.onClick(view);
91         }
92     }
93 
setIgnoreRequestLayout(boolean ignore)94     public void setIgnoreRequestLayout(boolean ignore) {
95         mIgnoreRequestLayout = ignore;
96     }
97 
98     @Override
requestLayout()99     public void requestLayout() {
100         if (!mIgnoreRequestLayout) {
101             super.requestLayout();
102         }
103     }
104 }
105