Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.LayoutInflater; |
| 23 | import android.view.View; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 24 | import android.view.ViewGroup; |
| 25 | import android.webkit.WebView; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 26 | import android.widget.ImageView; |
| 27 | import android.widget.LinearLayout; |
| 28 | import android.widget.TextView; |
| 29 | |
| 30 | public class NavTabView extends LinearLayout { |
| 31 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 32 | private ViewGroup mContent; |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 33 | private Tab mTab; |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 34 | private ImageView mClose; |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 35 | private TextView mTitle; |
| 36 | private View mTitleBar; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 37 | ImageView mImage; |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 38 | private OnClickListener mClickListener; |
| 39 | private boolean mHighlighted; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 40 | |
| 41 | public NavTabView(Context context, AttributeSet attrs, int defStyle) { |
| 42 | super(context, attrs, defStyle); |
| 43 | init(); |
| 44 | } |
| 45 | |
| 46 | public NavTabView(Context context, AttributeSet attrs) { |
| 47 | super(context, attrs); |
| 48 | init(); |
| 49 | } |
| 50 | |
| 51 | public NavTabView(Context context) { |
| 52 | super(context); |
| 53 | init(); |
| 54 | } |
| 55 | |
| 56 | private void init() { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 57 | LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, this); |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 58 | mContent = (ViewGroup) findViewById(R.id.main); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 59 | mClose = (ImageView) findViewById(R.id.closetab); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 60 | mTitle = (TextView) findViewById(R.id.title); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 61 | mTitleBar = findViewById(R.id.titlebar); |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 62 | mImage = (ImageView) findViewById(R.id.tab_view); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | protected boolean isClose(View v) { |
| 66 | return v == mClose; |
| 67 | } |
| 68 | |
| 69 | protected boolean isTitle(View v) { |
| 70 | return v == mTitleBar; |
| 71 | } |
| 72 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 73 | protected boolean isWebView(View v) { |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 74 | return v == mImage; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 77 | private void setTitle() { |
| 78 | if (mTab == null) return; |
| 79 | if (mHighlighted) { |
| 80 | mTitle.setText(mTab.getUrl()); |
| 81 | } else { |
| 82 | String txt = mTab.getTitle(); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 83 | if (txt == null) { |
| 84 | txt = mTab.getUrl(); |
| 85 | } |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 86 | mTitle.setText(txt); |
| 87 | } |
John Reck | 502a353 | 2011-08-16 14:21:46 -0700 | [diff] [blame] | 88 | if (mTab.isSnapshot()) { |
| 89 | setTitleIcon(R.drawable.ic_history_holo_dark); |
| 90 | } else if (mTab.isPrivateBrowsingEnabled()) { |
| 91 | setTitleIcon(R.drawable.ic_incognito_holo_dark); |
| 92 | } else { |
| 93 | setTitleIcon(0); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private void setTitleIcon(int id) { |
| 98 | if (id == 0) { |
| 99 | mTitle.setPadding(mTitle.getCompoundDrawablePadding(), 0, 0, 0); |
| 100 | } else { |
| 101 | mTitle.setPadding(0, 0, 0, 0); |
| 102 | } |
| 103 | mTitle.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | protected boolean isHighlighted() { |
| 107 | return mHighlighted; |
| 108 | } |
| 109 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 110 | protected void setWebView(Tab tab) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 111 | mTab = tab; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 112 | setTitle(); |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 113 | Bitmap image = tab.getScreenshot(); |
| 114 | if (image != null) { |
| 115 | mImage.setImageBitmap(image); |
Michael Kolb | a9a5967 | 2011-11-17 12:50:10 -0800 | [diff] [blame] | 116 | if (tab != null) { |
| 117 | mImage.setContentDescription(tab.getTitle()); |
| 118 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 119 | } |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 122 | @Override |
| 123 | public void setOnClickListener(OnClickListener listener) { |
| 124 | mClickListener = listener; |
| 125 | mTitleBar.setOnClickListener(mClickListener); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 126 | mClose.setOnClickListener(mClickListener); |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 127 | if (mImage != null) { |
| 128 | mImage.setOnClickListener(mClickListener); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 132 | } |