OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.ntp; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.res.Resources; |
| 9 import android.graphics.drawable.LevelListDrawable; |
| 10 import android.util.AttributeSet; |
| 11 import android.view.View; |
| 12 import android.widget.ImageView; |
| 13 import android.widget.RelativeLayout; |
| 14 import android.widget.TextView; |
| 15 |
| 16 import com.google.android.apps.chrome.R; |
| 17 |
| 18 import org.chromium.chrome.browser.ForeignSessionHelper.ForeignSession; |
| 19 import org.chromium.chrome.browser.widget.TintedDrawable; |
| 20 import org.chromium.ui.base.DeviceFormFactor; |
| 21 |
| 22 import java.util.concurrent.TimeUnit; |
| 23 |
| 24 /** |
| 25 * Header view shown above each group of items on the Recent Tabs page. Shows th
e name of the |
| 26 * group (e.g. "Recently closed" or "Jim's Laptop"), an icon, last synced time,
and a button to |
| 27 * expand or collapse the group. |
| 28 */ |
| 29 public class RecentTabsGroupView extends RelativeLayout { |
| 30 |
| 31 /** Drawable levels for the device type icon and the expand/collapse arrow.
*/ |
| 32 private static final int DRAWABLE_LEVEL_COLLAPSED = 0; |
| 33 private static final int DRAWABLE_LEVEL_EXPANDED = 1; |
| 34 |
| 35 private ImageView mDeviceIcon; |
| 36 private ImageView mExpandCollapseIcon; |
| 37 private TextView mDeviceLabel; |
| 38 private TextView mTimeLabel; |
| 39 private long mInitializationTimestamp; |
| 40 private int mDeviceLabelExpandedColor; |
| 41 private int mDeviceLabelCollapsedColor; |
| 42 private int mTimeLabelExpandedColor; |
| 43 private int mTimeLabelCollapsedColor; |
| 44 |
| 45 /** |
| 46 * Constructor for inflating from XML. |
| 47 * |
| 48 * @param context The context this view will work in. |
| 49 * @param attrs The attribute set for this view. |
| 50 */ |
| 51 public RecentTabsGroupView(Context context, AttributeSet attrs) { |
| 52 super(context, attrs); |
| 53 Resources res = getResources(); |
| 54 mDeviceLabelExpandedColor = res.getColor(R.color.light_active_color); |
| 55 mDeviceLabelCollapsedColor = res.getColor(R.color.ntp_list_header_text); |
| 56 mTimeLabelExpandedColor = res.getColor(R.color.ntp_list_header_subtext_a
ctive); |
| 57 mTimeLabelCollapsedColor = res.getColor(R.color.ntp_list_header_subtext)
; |
| 58 } |
| 59 |
| 60 @Override |
| 61 public void onFinishInflate() { |
| 62 super.onFinishInflate(); |
| 63 mDeviceIcon = (ImageView) findViewById(R.id.device_icon); |
| 64 mTimeLabel = (TextView) findViewById(R.id.time_label); |
| 65 mDeviceLabel = (TextView) findViewById(R.id.device_label); |
| 66 mExpandCollapseIcon = (ImageView) findViewById(R.id.expand_collapse_icon
); |
| 67 |
| 68 // Create drawable for expand/collapse arrow. |
| 69 LevelListDrawable collapseIcon = new LevelListDrawable(); |
| 70 collapseIcon.addLevel(DRAWABLE_LEVEL_COLLAPSED, DRAWABLE_LEVEL_COLLAPSED
, |
| 71 TintedDrawable.constructTintedDrawable(getResources(), R.drawabl
e.ic_expand)); |
| 72 TintedDrawable collapse = |
| 73 TintedDrawable.constructTintedDrawable(getResources(), R.drawabl
e.ic_collapse); |
| 74 collapse.setTint(getResources().getColorStateList(R.color.blue_mode_tint
)); |
| 75 collapseIcon.addLevel(DRAWABLE_LEVEL_EXPANDED, DRAWABLE_LEVEL_EXPANDED,
collapse); |
| 76 mExpandCollapseIcon.setImageDrawable(collapseIcon); |
| 77 } |
| 78 |
| 79 /** |
| 80 * Initialize the state of the group view. Should be called immediatly after
object creation. |
| 81 * |
| 82 * @param initializationTimestamp The timestamp to compute the time since la
st session sync. |
| 83 */ |
| 84 public void initialize(long initializationTimestamp) { |
| 85 mInitializationTimestamp = initializationTimestamp; |
| 86 } |
| 87 |
| 88 /** |
| 89 * Configures the view for currently open tabs. |
| 90 * |
| 91 * @param isExpanded Whether the view is expanded or collapsed. |
| 92 */ |
| 93 public void configureForCurrentlyOpenTabs(boolean isExpanded) { |
| 94 mDeviceIcon.setVisibility(View.VISIBLE); |
| 95 mDeviceIcon.setImageResource(DeviceFormFactor.isTablet(getContext()) |
| 96 ? R.drawable.recent_tablet : R.drawable.recent_phone); |
| 97 String title = getResources().getString(R.string.recent_tabs_this_device
); |
| 98 mDeviceLabel.setText(title); |
| 99 setTimeLabelVisibility(View.GONE); |
| 100 configureExpandedCollapsed(isExpanded); |
| 101 } |
| 102 |
| 103 /** |
| 104 * Configures the view for a foreign session. |
| 105 * |
| 106 * @param session The session to configure the view for. |
| 107 * @param isExpanded Whether the view is expanded or collapsed. |
| 108 */ |
| 109 public void configureForForeignSession(ForeignSession session, boolean isExp
anded) { |
| 110 mDeviceIcon.setVisibility(View.VISIBLE); |
| 111 mDeviceLabel.setText(session.name); |
| 112 setTimeLabelVisibility(View.VISIBLE); |
| 113 mTimeLabel.setText(getTimeString(session)); |
| 114 switch (session.deviceType) { |
| 115 case ForeignSession.DEVICE_TYPE_PHONE: |
| 116 mDeviceIcon.setImageResource(R.drawable.recent_phone); |
| 117 break; |
| 118 case ForeignSession.DEVICE_TYPE_TABLET: |
| 119 mDeviceIcon.setImageResource(R.drawable.recent_tablet); |
| 120 break; |
| 121 default: |
| 122 mDeviceIcon.setImageResource(R.drawable.recent_laptop); |
| 123 break; |
| 124 } |
| 125 configureExpandedCollapsed(isExpanded); |
| 126 } |
| 127 |
| 128 /** |
| 129 * Configures the view for the recently closed tabs group. |
| 130 * |
| 131 * @param isExpanded Whether the view is expanded or collapsed. |
| 132 */ |
| 133 public void configureForRecentlyClosedTabs(boolean isExpanded) { |
| 134 mDeviceIcon.setVisibility(View.VISIBLE); |
| 135 mDeviceIcon.setImageResource(R.drawable.recent_recently_closed); |
| 136 mDeviceLabel.setText(R.string.recently_closed); |
| 137 setTimeLabelVisibility(View.GONE); |
| 138 configureExpandedCollapsed(isExpanded); |
| 139 } |
| 140 |
| 141 /** |
| 142 * Configures the view for the sync promo. |
| 143 * |
| 144 * @param isExpanded Whether the view is expanded or collapsed. |
| 145 */ |
| 146 public void configureForSyncPromo(boolean isExpanded) { |
| 147 mDeviceIcon.setVisibility(View.VISIBLE); |
| 148 mDeviceIcon.setImageResource(R.drawable.recent_laptop); |
| 149 mDeviceLabel.setText(R.string.ntp_recent_tabs_sync_promo_title); |
| 150 setTimeLabelVisibility(View.GONE); |
| 151 configureExpandedCollapsed(isExpanded); |
| 152 } |
| 153 |
| 154 private void configureExpandedCollapsed(boolean isExpanded) { |
| 155 String description = getResources().getString(isExpanded |
| 156 ? R.string.ntp_recent_tabs_accessibility_expanded_group |
| 157 : R.string.ntp_recent_tabs_accessibility_collapsed_group); |
| 158 mExpandCollapseIcon.setContentDescription(description); |
| 159 |
| 160 int level = isExpanded ? DRAWABLE_LEVEL_EXPANDED : DRAWABLE_LEVEL_COLLAP
SED; |
| 161 mExpandCollapseIcon.getDrawable().setLevel(level); |
| 162 mDeviceIcon.setActivated(isExpanded); |
| 163 |
| 164 mDeviceLabel.setTextColor(isExpanded |
| 165 ? mDeviceLabelExpandedColor |
| 166 : mDeviceLabelCollapsedColor); |
| 167 mTimeLabel.setTextColor(isExpanded ? mTimeLabelExpandedColor : mTimeLabe
lCollapsedColor); |
| 168 } |
| 169 |
| 170 private String getTimeString(ForeignSession session) { |
| 171 long sessionModifiedTimeSeconds = |
| 172 TimeUnit.SECONDS.convert(session.modifiedTime, TimeUnit.MILLISEC
ONDS); |
| 173 long timeDelta = mInitializationTimestamp - sessionModifiedTimeSeconds; |
| 174 timeDelta = timeDelta > 0 ? timeDelta : 0; |
| 175 |
| 176 long daysElapsed = timeDelta / (24L * 60L * 60L); |
| 177 long hoursElapsed = timeDelta / (60L * 60L); |
| 178 long minutesElapsed = timeDelta / 60L; |
| 179 |
| 180 Resources resources = getContext().getResources(); |
| 181 if (daysElapsed > 0L) { |
| 182 return resources.getString(R.string.ntp_recent_tabs_last_synced_days
, daysElapsed); |
| 183 } else if (hoursElapsed > 0L) { |
| 184 return resources.getString(R.string.ntp_recent_tabs_last_synced_hour
s, hoursElapsed); |
| 185 } else if (minutesElapsed > 0L) { |
| 186 return resources.getString( |
| 187 R.string.ntp_recent_tabs_last_synced_minutes, minutesElapsed
); |
| 188 } else { |
| 189 return resources.getString(R.string.ntp_recent_tabs_last_synced_just
_now); |
| 190 } |
| 191 } |
| 192 |
| 193 /** |
| 194 * Shows or hides the time label (e.g. "Last synced: just now") and adjusts
the positions of the |
| 195 * icon and device label. In particular, the icon and device label are top-a
ligned when the time |
| 196 * is visible, but are vertically centered when the time is gone. |
| 197 */ |
| 198 private void setTimeLabelVisibility(int visibility) { |
| 199 if (mTimeLabel.getVisibility() == visibility) return; |
| 200 mTimeLabel.setVisibility(visibility); |
| 201 if (visibility == View.GONE) { |
| 202 replaceRule(mDeviceIcon, ALIGN_PARENT_TOP, CENTER_VERTICAL); |
| 203 replaceRule(mDeviceLabel, ALIGN_PARENT_TOP, CENTER_VERTICAL); |
| 204 } else { |
| 205 replaceRule(mDeviceIcon, CENTER_VERTICAL, ALIGN_PARENT_TOP); |
| 206 replaceRule(mDeviceLabel, CENTER_VERTICAL, ALIGN_PARENT_TOP); |
| 207 } |
| 208 } |
| 209 |
| 210 private static void replaceRule(View view, int oldRule, int newRule) { |
| 211 RelativeLayout.LayoutParams lp = ((RelativeLayout.LayoutParams) view.get
LayoutParams()); |
| 212 lp.addRule(oldRule, 0); |
| 213 lp.addRule(newRule); |
| 214 } |
| 215 } |
OLD | NEW |