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; 20 import android.graphics.Bitmap; 21 import android.util.AttributeSet; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.webkit.WebView; 26 import android.widget.ImageView; 27 import android.widget.LinearLayout; 28 import android.widget.TextView; 29 30 public class NavTabView extends LinearLayout { 31 32 private ViewGroup mContent; 33 private Tab mTab; 34 private ImageView mClose; 35 private TextView mTitle; 36 private View mTitleBar; 37 ImageView mImage; 38 private OnClickListener mClickListener; 39 private boolean mHighlighted; 40 NavTabView(Context context, AttributeSet attrs, int defStyle)41 public NavTabView(Context context, AttributeSet attrs, int defStyle) { 42 super(context, attrs, defStyle); 43 init(); 44 } 45 NavTabView(Context context, AttributeSet attrs)46 public NavTabView(Context context, AttributeSet attrs) { 47 super(context, attrs); 48 init(); 49 } 50 NavTabView(Context context)51 public NavTabView(Context context) { 52 super(context); 53 init(); 54 } 55 init()56 private void init() { 57 LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, this); 58 mContent = (ViewGroup) findViewById(R.id.main); 59 mClose = (ImageView) findViewById(R.id.closetab); 60 mTitle = (TextView) findViewById(R.id.title); 61 mTitleBar = findViewById(R.id.titlebar); 62 mImage = (ImageView) findViewById(R.id.tab_view); 63 } 64 isClose(View v)65 protected boolean isClose(View v) { 66 return v == mClose; 67 } 68 isTitle(View v)69 protected boolean isTitle(View v) { 70 return v == mTitleBar; 71 } 72 isWebView(View v)73 protected boolean isWebView(View v) { 74 return v == mImage; 75 } 76 setHighlighted(boolean highlighted)77 protected void setHighlighted(boolean highlighted) { 78 if (highlighted == mHighlighted) return; 79 mHighlighted = highlighted; 80 } 81 setTitle()82 private void setTitle() { 83 if (mTab == null) return; 84 if (mHighlighted) { 85 mTitle.setText(mTab.getUrl()); 86 } else { 87 String txt = mTab.getTitle(); 88 if (txt == null) { 89 txt = mTab.getUrl(); 90 } 91 mTitle.setText(txt); 92 } 93 if (mTab.isSnapshot()) { 94 setTitleIcon(R.drawable.ic_history_holo_dark); 95 } else if (mTab.isPrivateBrowsingEnabled()) { 96 setTitleIcon(R.drawable.ic_incognito_holo_dark); 97 } else { 98 setTitleIcon(0); 99 } 100 } 101 setTitleIcon(int id)102 private void setTitleIcon(int id) { 103 if (id == 0) { 104 mTitle.setPadding(mTitle.getCompoundDrawablePadding(), 0, 0, 0); 105 } else { 106 mTitle.setPadding(0, 0, 0, 0); 107 } 108 mTitle.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0); 109 } 110 isHighlighted()111 protected boolean isHighlighted() { 112 return mHighlighted; 113 } 114 setWebView(WebView w)115 protected void setWebView(WebView w) { 116 mContent.addView(w, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 117 } 118 setWebView(Tab tab)119 protected void setWebView(Tab tab) { 120 mTab = tab; 121 setTitle(); 122 Bitmap image = tab.getScreenshot(); 123 if (image != null) { 124 mImage.setImageBitmap(image); 125 } 126 } 127 hideTitle()128 protected void hideTitle() { 129 mTitleBar.setVisibility(View.INVISIBLE); 130 } 131 132 @Override setOnClickListener(OnClickListener listener)133 public void setOnClickListener(OnClickListener listener) { 134 mClickListener = listener; 135 mTitleBar.setOnClickListener(mClickListener); 136 mClose.setOnClickListener(mClickListener); 137 if (mImage != null) { 138 mImage.setOnClickListener(mClickListener); 139 } 140 } 141 142 } 143