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
17package com.android.browser.view;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.graphics.drawable.StateListDrawable;
22import android.graphics.drawable.TransitionDrawable;
23import android.util.AttributeSet;
24import android.view.View;
25import android.view.View.OnClickListener;
26import android.view.ViewConfiguration;
27import android.widget.RelativeLayout;
28
29public class BookmarkContainer extends RelativeLayout implements OnClickListener {
30
31    private OnClickListener mClickListener;
32    private boolean mIgnoreRequestLayout = false;
33
34    public BookmarkContainer(Context context) {
35        super(context);
36        init();
37    }
38
39    public BookmarkContainer(Context context, AttributeSet attrs) {
40        super(context, attrs);
41        init();
42    }
43
44    public BookmarkContainer(
45            Context context, AttributeSet attrs, int defStyle) {
46        super(context, attrs, defStyle);
47        init();
48    }
49
50    void init() {
51        setFocusable(true);
52        super.setOnClickListener(this);
53    }
54
55    @Override
56    public void setBackgroundDrawable(Drawable d) {
57        super.setBackgroundDrawable(d);
58    }
59
60    @Override
61    public void setOnClickListener(OnClickListener l) {
62        mClickListener = l;
63    }
64
65    @Override
66    protected void drawableStateChanged() {
67        super.drawableStateChanged();
68        updateTransitionDrawable(isPressed());
69    }
70
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
87    public void onClick(View view) {
88        updateTransitionDrawable(false);
89        if (mClickListener != null) {
90            mClickListener.onClick(view);
91        }
92    }
93
94    public void setIgnoreRequestLayout(boolean ignore) {
95        mIgnoreRequestLayout = ignore;
96    }
97
98    @Override
99    public void requestLayout() {
100        if (!mIgnoreRequestLayout) {
101            super.requestLayout();
102        }
103    }
104}
105