Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2008 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;
     18 
     19 import android.content.Context;
     20 import android.graphics.Bitmap;
     21 import android.graphics.drawable.Drawable;
     22 import android.view.LayoutInflater;
     23 import android.view.MotionEvent;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.HorizontalScrollView;
     27 import android.widget.ImageView;
     28 import android.widget.TextView;
     29 
     30 /**
     31  *  Custom layout for an item representing a bookmark in the browser.
     32  */
     33 class BookmarkItem extends HorizontalScrollView {
     34 
     35     final static int MAX_TEXTVIEW_LEN = 80;
     36 
     37     protected TextView    mTextView;
     38     protected TextView    mUrlText;
     39     protected ImageView   mImageView;
     40     protected String      mUrl;
     41     protected String      mTitle;
     42     protected boolean mEnableScrolling = false;
     43 
     44     /**
     45      *  Instantiate a bookmark item, including a default favicon.
     46      *
     47      *  @param context  The application context for the item.
     48      */
     49     BookmarkItem(Context context) {
     50         super(context);
     51 
     52         setClickable(false);
     53         setEnableScrolling(false);
     54         LayoutInflater factory = LayoutInflater.from(context);
     55         factory.inflate(R.layout.history_item, this);
     56         mTextView = (TextView) findViewById(R.id.title);
     57         mUrlText = (TextView) findViewById(R.id.url);
     58         mImageView = (ImageView) findViewById(R.id.favicon);
     59         View star = findViewById(R.id.star);
     60         star.setVisibility(View.GONE);
     61     }
     62 
     63     /**
     64      *  Copy this BookmarkItem to item.
     65      *  @param item BookmarkItem to receive the info from this BookmarkItem.
     66      */
     67     /* package */ void copyTo(BookmarkItem item) {
     68         item.mTextView.setText(mTextView.getText());
     69         item.mUrlText.setText(mUrlText.getText());
     70         item.mImageView.setImageDrawable(mImageView.getDrawable());
     71     }
     72 
     73     /**
     74      * Return the name assigned to this bookmark item.
     75      */
     76     /* package */ String getName() {
     77         return mTitle;
     78     }
     79 
     80     /* package */ String getUrl() {
     81         return mUrl;
     82     }
     83 
     84     /**
     85      *  Set the favicon for this item.
     86      *
     87      *  @param b    The new bitmap for this item.
     88      *              If it is null, will use the default.
     89      */
     90     /* package */ void setFavicon(Bitmap b) {
     91         if (b != null) {
     92             mImageView.setImageBitmap(b);
     93         } else {
     94             mImageView.setImageResource(R.drawable.app_web_browser_sm);
     95         }
     96     }
     97 
     98     void setFaviconBackground(Drawable d) {
     99         mImageView.setBackgroundDrawable(d);
    100     }
    101 
    102     /**
    103      *  Set the new name for the bookmark item.
    104      *
    105      *  @param name The new name for the bookmark item.
    106      */
    107     /* package */ void setName(String name) {
    108         if (name == null) {
    109             return;
    110         }
    111 
    112         mTitle = name;
    113 
    114         if (name.length() > MAX_TEXTVIEW_LEN) {
    115             name = name.substring(0, MAX_TEXTVIEW_LEN);
    116         }
    117 
    118         mTextView.setText(name);
    119     }
    120 
    121     /**
    122      *  Set the new url for the bookmark item.
    123      *  @param url  The new url for the bookmark item.
    124      */
    125     /* package */ void setUrl(String url) {
    126         if (url == null) {
    127             return;
    128         }
    129 
    130         mUrl = url;
    131 
    132         url = UrlUtils.stripUrl(url);
    133         if (url.length() > MAX_TEXTVIEW_LEN) {
    134             url = url.substring(0, MAX_TEXTVIEW_LEN);
    135         }
    136 
    137         mUrlText.setText(url);
    138     }
    139 
    140     void setEnableScrolling(boolean enable) {
    141         mEnableScrolling = enable;
    142         setFocusable(mEnableScrolling);
    143         setFocusableInTouchMode(mEnableScrolling);
    144         requestDisallowInterceptTouchEvent(!mEnableScrolling);
    145         requestLayout();
    146     }
    147 
    148     @Override
    149     public boolean onTouchEvent(MotionEvent ev) {
    150         if (mEnableScrolling) {
    151             return super.onTouchEvent(ev);
    152         }
    153         return false;
    154     }
    155 
    156     @Override
    157     protected void measureChild(View child, int parentWidthMeasureSpec,
    158             int parentHeightMeasureSpec) {
    159         if (mEnableScrolling) {
    160             super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
    161             return;
    162         }
    163 
    164         final ViewGroup.LayoutParams lp = child.getLayoutParams();
    165 
    166         final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
    167                 mPaddingLeft + mPaddingRight, lp.width);
    168         final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
    169                 mPaddingTop + mPaddingBottom, lp.height);
    170 
    171         child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    172     }
    173 
    174     @Override
    175     protected void measureChildWithMargins(View child,
    176             int parentWidthMeasureSpec, int widthUsed,
    177             int parentHeightMeasureSpec, int heightUsed) {
    178         if (mEnableScrolling) {
    179             super.measureChildWithMargins(child, parentWidthMeasureSpec,
    180                     widthUsed, parentHeightMeasureSpec, heightUsed);
    181             return;
    182         }
    183 
    184         final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
    185 
    186         final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
    187                 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
    188                         + widthUsed, lp.width);
    189         final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
    190                 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
    191                         + heightUsed, lp.height);
    192 
    193         child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    194     }
    195 }
    196