171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck/*
271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * Copyright (C) 2011 The Android Open Source Project
371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck *
471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * Licensed under the Apache License, Version 2.0 (the "License");
571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * you may not use this file except in compliance with the License.
671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * You may obtain a copy of the License at
771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck *
871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck *      http://www.apache.org/licenses/LICENSE-2.0
971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck *
1071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * Unless required by applicable law or agreed to in writing, software
1171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * distributed under the License is distributed on an "AS IS" BASIS,
1271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * See the License for the specific language governing permissions and
1471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck * limitations under the License.
1571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck */
1671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
1771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckpackage com.android.browser.view;
1871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
1971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.content.Context;
2071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.graphics.drawable.Drawable;
2171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.graphics.drawable.StateListDrawable;
2271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.graphics.drawable.TransitionDrawable;
2371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.util.AttributeSet;
2471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.view.View;
2571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.view.View.OnClickListener;
2671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.view.ViewConfiguration;
2771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckimport android.widget.RelativeLayout;
2871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
2971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reckpublic class BookmarkContainer extends RelativeLayout implements OnClickListener {
3071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
3171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    private OnClickListener mClickListener;
32f94abcf44fc1611f76e55461f48220e621fc31b7John Reck    private boolean mIgnoreRequestLayout = false;
33f94abcf44fc1611f76e55461f48220e621fc31b7John Reck
3471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    public BookmarkContainer(Context context) {
3571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        super(context);
3671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        init();
3771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
3871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
3971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    public BookmarkContainer(Context context, AttributeSet attrs) {
4071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        super(context, attrs);
4171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        init();
4271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
4371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
4471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    public BookmarkContainer(
4571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck            Context context, AttributeSet attrs, int defStyle) {
4671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        super(context, attrs, defStyle);
4771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        init();
4871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
4971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
5071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    void init() {
5171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        setFocusable(true);
5271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        super.setOnClickListener(this);
5371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
5471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
5571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    @Override
5671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    public void setBackgroundDrawable(Drawable d) {
5771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        super.setBackgroundDrawable(d);
5871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
5971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
6071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    @Override
6171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    public void setOnClickListener(OnClickListener l) {
6271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        mClickListener = l;
6371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
6471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
6571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    @Override
6671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    protected void drawableStateChanged() {
6771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        super.drawableStateChanged();
6871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        updateTransitionDrawable(isPressed());
6971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
7071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
7171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    void updateTransitionDrawable(boolean pressed) {
7271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
7371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        Drawable selector = getBackground();
7471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        if (selector != null && selector instanceof StateListDrawable) {
7571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck            Drawable d = ((StateListDrawable)selector).getCurrent();
7671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck            if (d != null && d instanceof TransitionDrawable) {
7771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck                if (pressed && isLongClickable()) {
7871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck                    ((TransitionDrawable) d).startTransition(longPressTimeout);
7971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck                } else {
8071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck                    ((TransitionDrawable) d).resetTransition();
8171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck                }
8271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck            }
8371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        }
8471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
8571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck
8671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    @Override
8771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    public void onClick(View view) {
8871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        updateTransitionDrawable(false);
8971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        if (mClickListener != null) {
9071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck            mClickListener.onClick(view);
9171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck        }
9271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck    }
93f94abcf44fc1611f76e55461f48220e621fc31b7John Reck
94f94abcf44fc1611f76e55461f48220e621fc31b7John Reck    public void setIgnoreRequestLayout(boolean ignore) {
95f94abcf44fc1611f76e55461f48220e621fc31b7John Reck        mIgnoreRequestLayout = ignore;
96f94abcf44fc1611f76e55461f48220e621fc31b7John Reck    }
97f94abcf44fc1611f76e55461f48220e621fc31b7John Reck
98f94abcf44fc1611f76e55461f48220e621fc31b7John Reck    @Override
99f94abcf44fc1611f76e55461f48220e621fc31b7John Reck    public void requestLayout() {
100f94abcf44fc1611f76e55461f48220e621fc31b7John Reck        if (!mIgnoreRequestLayout) {
101f94abcf44fc1611f76e55461f48220e621fc31b7John Reck            super.requestLayout();
102f94abcf44fc1611f76e55461f48220e621fc31b7John Reck        }
103f94abcf44fc1611f76e55461f48220e621fc31b7John Reck    }
10471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck}
105