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
33    public BookmarkContainer(Context context) {
34        super(context);
35        init();
36    }
37
38    public BookmarkContainer(Context context, AttributeSet attrs) {
39        super(context, attrs);
40        init();
41    }
42
43    public BookmarkContainer(
44            Context context, AttributeSet attrs, int defStyle) {
45        super(context, attrs, defStyle);
46        init();
47    }
48
49    void init() {
50        setFocusable(true);
51        super.setOnClickListener(this);
52    }
53
54    @Override
55    public void setBackgroundDrawable(Drawable d) {
56        super.setBackgroundDrawable(d);
57    }
58
59    @Override
60    public void setOnClickListener(OnClickListener l) {
61        mClickListener = l;
62    }
63
64    @Override
65    protected void drawableStateChanged() {
66        super.drawableStateChanged();
67        updateTransitionDrawable(isPressed());
68    }
69
70    void updateTransitionDrawable(boolean pressed) {
71        final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
72        Drawable selector = getBackground();
73        if (selector != null && selector instanceof StateListDrawable) {
74            Drawable d = ((StateListDrawable)selector).getCurrent();
75            if (d != null && d instanceof TransitionDrawable) {
76                if (pressed && isLongClickable()) {
77                    ((TransitionDrawable) d).startTransition(longPressTimeout);
78                } else {
79                    ((TransitionDrawable) d).resetTransition();
80                }
81            }
82        }
83    }
84
85    @Override
86    public void onClick(View view) {
87        updateTransitionDrawable(false);
88        if (mClickListener != null) {
89            mClickListener.onClick(view);
90        }
91    }
92}
93