mobile/android/base/home/TwoLinePageRow.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.home;
michael@0 7
michael@0 8 import java.lang.ref.WeakReference;
michael@0 9
michael@0 10 import org.mozilla.gecko.R;
michael@0 11 import org.mozilla.gecko.Tab;
michael@0 12 import org.mozilla.gecko.Tabs;
michael@0 13 import org.mozilla.gecko.db.BrowserContract.Combined;
michael@0 14 import org.mozilla.gecko.db.BrowserDB.URLColumns;
michael@0 15 import org.mozilla.gecko.favicons.Favicons;
michael@0 16 import org.mozilla.gecko.favicons.OnFaviconLoadedListener;
michael@0 17 import org.mozilla.gecko.util.ThreadUtils;
michael@0 18 import org.mozilla.gecko.widget.FaviconView;
michael@0 19
michael@0 20 import android.content.Context;
michael@0 21 import android.database.Cursor;
michael@0 22 import android.graphics.Bitmap;
michael@0 23 import android.text.TextUtils;
michael@0 24 import android.util.AttributeSet;
michael@0 25 import android.view.Gravity;
michael@0 26 import android.view.LayoutInflater;
michael@0 27 import android.widget.LinearLayout;
michael@0 28 import android.widget.TextView;
michael@0 29
michael@0 30 public class TwoLinePageRow extends LinearLayout
michael@0 31 implements Tabs.OnTabsChangedListener {
michael@0 32
michael@0 33 protected static final int NO_ICON = 0;
michael@0 34
michael@0 35 private final TextView mTitle;
michael@0 36 private final TextView mUrl;
michael@0 37
michael@0 38 private int mSwitchToTabIconId;
michael@0 39 private int mPageTypeIconId;
michael@0 40
michael@0 41 private final FaviconView mFavicon;
michael@0 42
michael@0 43 private boolean mShowIcons;
michael@0 44 private int mLoadFaviconJobId = Favicons.NOT_LOADING;
michael@0 45
michael@0 46 // Only holds a reference to the FaviconView itself, so if the row gets
michael@0 47 // discarded while a task is outstanding, we'll leak less memory.
michael@0 48 private static class UpdateViewFaviconLoadedListener implements OnFaviconLoadedListener {
michael@0 49 private final WeakReference<FaviconView> view;
michael@0 50 public UpdateViewFaviconLoadedListener(FaviconView view) {
michael@0 51 this.view = new WeakReference<FaviconView>(view);
michael@0 52 }
michael@0 53
michael@0 54 @Override
michael@0 55 public void onFaviconLoaded(String url, String faviconURL, Bitmap favicon) {
michael@0 56 FaviconView v = view.get();
michael@0 57 if (v == null) {
michael@0 58 // Guess we stuck around after the TwoLinePageRow went away.
michael@0 59 return;
michael@0 60 }
michael@0 61
michael@0 62 if (favicon == null) {
michael@0 63 v.showDefaultFavicon();
michael@0 64 return;
michael@0 65 }
michael@0 66
michael@0 67 v.updateImage(favicon, faviconURL);
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 // Listener for handling Favicon loads.
michael@0 72 private final OnFaviconLoadedListener mFaviconListener;
michael@0 73
michael@0 74 // The URL for the page corresponding to this view.
michael@0 75 private String mPageUrl;
michael@0 76
michael@0 77 public TwoLinePageRow(Context context) {
michael@0 78 this(context, null);
michael@0 79 }
michael@0 80
michael@0 81 public TwoLinePageRow(Context context, AttributeSet attrs) {
michael@0 82 super(context, attrs);
michael@0 83
michael@0 84 setGravity(Gravity.CENTER_VERTICAL);
michael@0 85
michael@0 86 LayoutInflater.from(context).inflate(R.layout.two_line_page_row, this);
michael@0 87 mTitle = (TextView) findViewById(R.id.title);
michael@0 88 mUrl = (TextView) findViewById(R.id.url);
michael@0 89
michael@0 90 mSwitchToTabIconId = NO_ICON;
michael@0 91 mPageTypeIconId = NO_ICON;
michael@0 92 mShowIcons = true;
michael@0 93
michael@0 94 mFavicon = (FaviconView) findViewById(R.id.icon);
michael@0 95 mFaviconListener = new UpdateViewFaviconLoadedListener(mFavicon);
michael@0 96 }
michael@0 97
michael@0 98 @Override
michael@0 99 protected void onAttachedToWindow() {
michael@0 100 Tabs.registerOnTabsChangedListener(this);
michael@0 101 }
michael@0 102
michael@0 103 @Override
michael@0 104 protected void onDetachedFromWindow() {
michael@0 105 // Tabs' listener array is safe to modify during use: its
michael@0 106 // iteration pattern is based on snapshots.
michael@0 107 Tabs.unregisterOnTabsChangedListener(this);
michael@0 108 }
michael@0 109
michael@0 110 @Override
michael@0 111 public void onTabChanged(final Tab tab, final Tabs.TabEvents msg, final Object data) {
michael@0 112 switch(msg) {
michael@0 113 case ADDED:
michael@0 114 case CLOSED:
michael@0 115 case LOCATION_CHANGE:
michael@0 116 updateDisplayedUrl();
michael@0 117 break;
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 private void setTitle(String text) {
michael@0 122 mTitle.setText(text);
michael@0 123 }
michael@0 124
michael@0 125 protected void setUrl(String text) {
michael@0 126 mUrl.setText(text);
michael@0 127 }
michael@0 128
michael@0 129 protected void setUrl(int stringId) {
michael@0 130 mUrl.setText(stringId);
michael@0 131 }
michael@0 132
michael@0 133 protected String getUrl() {
michael@0 134 return mPageUrl;
michael@0 135 }
michael@0 136
michael@0 137 protected void setSwitchToTabIcon(int iconId) {
michael@0 138 if (mSwitchToTabIconId == iconId) {
michael@0 139 return;
michael@0 140 }
michael@0 141
michael@0 142 mSwitchToTabIconId = iconId;
michael@0 143 mUrl.setCompoundDrawablesWithIntrinsicBounds(mSwitchToTabIconId, 0, mPageTypeIconId, 0);
michael@0 144 }
michael@0 145
michael@0 146 private void setPageTypeIcon(int iconId) {
michael@0 147 if (mPageTypeIconId == iconId) {
michael@0 148 return;
michael@0 149 }
michael@0 150
michael@0 151 mPageTypeIconId = iconId;
michael@0 152 mUrl.setCompoundDrawablesWithIntrinsicBounds(mSwitchToTabIconId, 0, mPageTypeIconId, 0);
michael@0 153 }
michael@0 154
michael@0 155 /**
michael@0 156 * Stores the page URL, so that we can use it to replace "Switch to tab" if the open
michael@0 157 * tab changes or is closed.
michael@0 158 */
michael@0 159 private void updateDisplayedUrl(String url) {
michael@0 160 mPageUrl = url;
michael@0 161 updateDisplayedUrl();
michael@0 162 }
michael@0 163
michael@0 164 /**
michael@0 165 * Replaces the page URL with "Switch to tab" if there is already a tab open with that URL.
michael@0 166 * Only looks for tabs that are either private or non-private, depending on the current
michael@0 167 * selected tab.
michael@0 168 */
michael@0 169 protected void updateDisplayedUrl() {
michael@0 170 boolean isPrivate = Tabs.getInstance().getSelectedTab().isPrivate();
michael@0 171 Tab tab = Tabs.getInstance().getFirstTabForUrl(mPageUrl, isPrivate);
michael@0 172 if (!mShowIcons || tab == null) {
michael@0 173 setUrl(mPageUrl);
michael@0 174 setSwitchToTabIcon(NO_ICON);
michael@0 175 } else {
michael@0 176 setUrl(R.string.switch_to_tab);
michael@0 177 setSwitchToTabIcon(R.drawable.ic_url_bar_tab);
michael@0 178 }
michael@0 179 }
michael@0 180
michael@0 181 public void setShowIcons(boolean showIcons) {
michael@0 182 mShowIcons = showIcons;
michael@0 183 }
michael@0 184
michael@0 185 public void updateFromCursor(Cursor cursor) {
michael@0 186 if (cursor == null) {
michael@0 187 return;
michael@0 188 }
michael@0 189
michael@0 190 int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE);
michael@0 191 final String title = cursor.getString(titleIndex);
michael@0 192
michael@0 193 int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
michael@0 194 final String url = cursor.getString(urlIndex);
michael@0 195
michael@0 196 if (mShowIcons) {
michael@0 197 final int bookmarkIdIndex = cursor.getColumnIndex(Combined.BOOKMARK_ID);
michael@0 198 if (bookmarkIdIndex != -1) {
michael@0 199 final long bookmarkId = cursor.getLong(bookmarkIdIndex);
michael@0 200 final int displayIndex = cursor.getColumnIndex(Combined.DISPLAY);
michael@0 201
michael@0 202 final int display;
michael@0 203 if (displayIndex != -1) {
michael@0 204 display = cursor.getInt(displayIndex);
michael@0 205 } else {
michael@0 206 display = Combined.DISPLAY_NORMAL;
michael@0 207 }
michael@0 208
michael@0 209 // The bookmark id will be 0 (null in database) when the url
michael@0 210 // is not a bookmark.
michael@0 211 if (bookmarkId == 0) {
michael@0 212 setPageTypeIcon(NO_ICON);
michael@0 213 } else if (display == Combined.DISPLAY_READER) {
michael@0 214 setPageTypeIcon(R.drawable.ic_url_bar_reader);
michael@0 215 } else {
michael@0 216 setPageTypeIcon(R.drawable.ic_url_bar_star);
michael@0 217 }
michael@0 218 } else {
michael@0 219 setPageTypeIcon(NO_ICON);
michael@0 220 }
michael@0 221 }
michael@0 222
michael@0 223 // Use the URL instead of an empty title for consistency with the normal URL
michael@0 224 // bar view - this is the equivalent of getDisplayTitle() in Tab.java
michael@0 225 setTitle(TextUtils.isEmpty(title) ? url : title);
michael@0 226
michael@0 227 // No point updating the below things if URL has not changed. Prevents evil Favicon flicker.
michael@0 228 if (url.equals(mPageUrl)) {
michael@0 229 return;
michael@0 230 }
michael@0 231
michael@0 232 // Blank the Favicon, so we don't show the wrong Favicon if we scroll and miss DB.
michael@0 233 mFavicon.clearImage();
michael@0 234 mLoadFaviconJobId = Favicons.getSizedFaviconForPageFromLocal(url, mFaviconListener);
michael@0 235
michael@0 236 updateDisplayedUrl(url);
michael@0 237 }
michael@0 238 }

mercurial