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; 3271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 3371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck public BookmarkContainer(Context context) { 3471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck super(context); 3571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck init(); 3671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 3771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 3871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck public BookmarkContainer(Context context, AttributeSet attrs) { 3971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck super(context, attrs); 4071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck init(); 4171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 4271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 4371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck public BookmarkContainer( 4471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck Context context, AttributeSet attrs, int defStyle) { 4571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck super(context, attrs, defStyle); 4671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck init(); 4771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 4871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 4971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck void init() { 5071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck setFocusable(true); 5171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck super.setOnClickListener(this); 5271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 5371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 5471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck @Override 5571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck public void setBackgroundDrawable(Drawable d) { 5671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck super.setBackgroundDrawable(d); 5771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 5871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 5971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck @Override 6071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck public void setOnClickListener(OnClickListener l) { 6171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck mClickListener = l; 6271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 6371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 6471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck @Override 6571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck protected void drawableStateChanged() { 6671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck super.drawableStateChanged(); 6771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck updateTransitionDrawable(isPressed()); 6871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 6971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 7071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck void updateTransitionDrawable(boolean pressed) { 7171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck final int longPressTimeout = ViewConfiguration.getLongPressTimeout(); 7271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck Drawable selector = getBackground(); 7371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck if (selector != null && selector instanceof StateListDrawable) { 7471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck Drawable d = ((StateListDrawable)selector).getCurrent(); 7571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck if (d != null && d instanceof TransitionDrawable) { 7671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck if (pressed && isLongClickable()) { 7771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck ((TransitionDrawable) d).startTransition(longPressTimeout); 7871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } else { 7971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck ((TransitionDrawable) d).resetTransition(); 8071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 8171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 8271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 8371efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 8471efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck 8571efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck @Override 8671efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck public void onClick(View view) { 8771efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck updateTransitionDrawable(false); 8871efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck if (mClickListener != null) { 8971efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck mClickListener.onClick(view); 9071efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 9171efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck } 9271efc2bbf08574425a387c992e24cb9eaf0a6e6cJohn Reck} 93