OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.ntp; | 5 package org.chromium.chrome.browser.ntp; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
9 import android.graphics.drawable.LevelListDrawable; | 9 import android.graphics.drawable.LevelListDrawable; |
10 import android.util.AttributeSet; | 10 import android.util.AttributeSet; |
11 import android.view.View; | 11 import android.view.View; |
12 import android.widget.ImageView; | 12 import android.widget.ImageView; |
13 import android.widget.RelativeLayout; | 13 import android.widget.RelativeLayout; |
14 import android.widget.TextView; | 14 import android.widget.TextView; |
15 | 15 |
16 import org.chromium.base.ApiCompatibilityUtils; | 16 import org.chromium.base.ApiCompatibilityUtils; |
17 import org.chromium.chrome.R; | 17 import org.chromium.chrome.R; |
18 import org.chromium.chrome.browser.ntp.ForeignSessionHelper.ForeignSession; | 18 import org.chromium.chrome.browser.ntp.ForeignSessionHelper.ForeignSession; |
19 import org.chromium.chrome.browser.widget.TintedDrawable; | 19 import org.chromium.chrome.browser.widget.TintedDrawable; |
20 import org.chromium.ui.base.DeviceFormFactor; | 20 import org.chromium.ui.base.DeviceFormFactor; |
21 | 21 |
22 import java.util.concurrent.TimeUnit; | |
23 | |
24 /** | 22 /** |
25 * Header view shown above each group of items on the Recent Tabs page. Shows th e name of the | 23 * 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 | 24 * group (e.g. "Recently closed" or "Jim's Laptop"), an icon, last synced time, and a button to |
27 * expand or collapse the group. | 25 * expand or collapse the group. |
28 */ | 26 */ |
29 public class RecentTabsGroupView extends RelativeLayout { | 27 public class RecentTabsGroupView extends RelativeLayout { |
30 | 28 |
31 /** Drawable levels for the device type icon and the expand/collapse arrow. */ | 29 /** Drawable levels for the device type icon and the expand/collapse arrow. */ |
32 private static final int DRAWABLE_LEVEL_COLLAPSED = 0; | 30 private static final int DRAWABLE_LEVEL_COLLAPSED = 0; |
33 private static final int DRAWABLE_LEVEL_EXPANDED = 1; | 31 private static final int DRAWABLE_LEVEL_EXPANDED = 1; |
34 | 32 |
35 private ImageView mDeviceIcon; | 33 private ImageView mDeviceIcon; |
36 private ImageView mExpandCollapseIcon; | 34 private ImageView mExpandCollapseIcon; |
37 private TextView mDeviceLabel; | 35 private TextView mDeviceLabel; |
38 private TextView mTimeLabel; | 36 private TextView mTimeLabel; |
39 private long mInitializationTimestamp; | |
40 private int mDeviceLabelExpandedColor; | 37 private int mDeviceLabelExpandedColor; |
41 private int mDeviceLabelCollapsedColor; | 38 private int mDeviceLabelCollapsedColor; |
42 private int mTimeLabelExpandedColor; | 39 private int mTimeLabelExpandedColor; |
43 private int mTimeLabelCollapsedColor; | 40 private int mTimeLabelCollapsedColor; |
44 | 41 |
45 /** | 42 /** |
46 * Constructor for inflating from XML. | 43 * Constructor for inflating from XML. |
47 * | 44 * |
48 * @param context The context this view will work in. | 45 * @param context The context this view will work in. |
49 * @param attrs The attribute set for this view. | 46 * @param attrs The attribute set for this view. |
(...skipping 24 matching lines...) Expand all Loading... | |
74 TintedDrawable.constructTintedDrawable(getResources(), R.drawabl e.ic_expanded)); | 71 TintedDrawable.constructTintedDrawable(getResources(), R.drawabl e.ic_expanded)); |
75 TintedDrawable collapse = | 72 TintedDrawable collapse = |
76 TintedDrawable.constructTintedDrawable(getResources(), R.drawabl e.ic_collapsed); | 73 TintedDrawable.constructTintedDrawable(getResources(), R.drawabl e.ic_collapsed); |
77 collapse.setTint( | 74 collapse.setTint( |
78 ApiCompatibilityUtils.getColorStateList(getResources(), R.color. blue_mode_tint)); | 75 ApiCompatibilityUtils.getColorStateList(getResources(), R.color. blue_mode_tint)); |
79 collapseIcon.addLevel(DRAWABLE_LEVEL_EXPANDED, DRAWABLE_LEVEL_EXPANDED, collapse); | 76 collapseIcon.addLevel(DRAWABLE_LEVEL_EXPANDED, DRAWABLE_LEVEL_EXPANDED, collapse); |
80 mExpandCollapseIcon.setImageDrawable(collapseIcon); | 77 mExpandCollapseIcon.setImageDrawable(collapseIcon); |
81 } | 78 } |
82 | 79 |
83 /** | 80 /** |
84 * Initialize the state of the group view. Should be called immediatly after object creation. | |
85 * | |
86 * @param initializationTimestamp The timestamp to compute the time since la st session sync. | |
87 */ | |
88 public void initialize(long initializationTimestamp) { | |
89 mInitializationTimestamp = initializationTimestamp; | |
90 } | |
91 | |
92 /** | |
93 * Configures the view for currently open tabs. | 81 * Configures the view for currently open tabs. |
94 * | 82 * |
95 * @param isExpanded Whether the view is expanded or collapsed. | 83 * @param isExpanded Whether the view is expanded or collapsed. |
96 */ | 84 */ |
97 public void configureForCurrentlyOpenTabs(boolean isExpanded) { | 85 public void configureForCurrentlyOpenTabs(boolean isExpanded) { |
98 mDeviceIcon.setVisibility(View.VISIBLE); | 86 mDeviceIcon.setVisibility(View.VISIBLE); |
99 mDeviceIcon.setImageResource(DeviceFormFactor.isTablet(getContext()) | 87 mDeviceIcon.setImageResource(DeviceFormFactor.isTablet(getContext()) |
100 ? R.drawable.recent_tablet : R.drawable.recent_phone); | 88 ? R.drawable.recent_tablet : R.drawable.recent_phone); |
101 String title = getResources().getString(R.string.recent_tabs_this_device ); | 89 String title = getResources().getString(R.string.recent_tabs_this_device ); |
102 mDeviceLabel.setText(title); | 90 mDeviceLabel.setText(title); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 int level = isExpanded ? DRAWABLE_LEVEL_EXPANDED : DRAWABLE_LEVEL_COLLAP SED; | 152 int level = isExpanded ? DRAWABLE_LEVEL_EXPANDED : DRAWABLE_LEVEL_COLLAP SED; |
165 mExpandCollapseIcon.getDrawable().setLevel(level); | 153 mExpandCollapseIcon.getDrawable().setLevel(level); |
166 mDeviceIcon.setActivated(isExpanded); | 154 mDeviceIcon.setActivated(isExpanded); |
167 | 155 |
168 mDeviceLabel.setTextColor(isExpanded | 156 mDeviceLabel.setTextColor(isExpanded |
169 ? mDeviceLabelExpandedColor | 157 ? mDeviceLabelExpandedColor |
170 : mDeviceLabelCollapsedColor); | 158 : mDeviceLabelCollapsedColor); |
171 mTimeLabel.setTextColor(isExpanded ? mTimeLabelExpandedColor : mTimeLabe lCollapsedColor); | 159 mTimeLabel.setTextColor(isExpanded ? mTimeLabelExpandedColor : mTimeLabe lCollapsedColor); |
172 } | 160 } |
173 | 161 |
174 private String getTimeString(ForeignSession session) { | 162 private CharSequence getTimeString(ForeignSession session) { |
175 long sessionModifiedTimeSeconds = | 163 long timeDeltaMs = System.currentTimeMillis() - session.modifiedTime; |
176 TimeUnit.SECONDS.convert(session.modifiedTime, TimeUnit.MILLISEC ONDS); | 164 if (timeDeltaMs < 0) timeDeltaMs = 0; |
177 long timeDelta = mInitializationTimestamp - sessionModifiedTimeSeconds; | |
178 timeDelta = timeDelta > 0 ? timeDelta : 0; | |
179 | 165 |
180 long daysElapsed = timeDelta / (24L * 60L * 60L); | 166 int daysElapsed = (int) (timeDeltaMs / (24L * 60L * 60L * 1000L)); |
181 long hoursElapsed = timeDelta / (60L * 60L); | 167 int hoursElapsed = (int) (timeDeltaMs / (60L * 60L * 1000L)); |
182 long minutesElapsed = timeDelta / 60L; | 168 int minutesElapsed = (int) (timeDeltaMs / (60L * 1000L)); |
183 | 169 |
184 Resources resources = getContext().getResources(); | 170 Resources res = getResources(); |
171 String relativeTime; | |
185 if (daysElapsed > 0L) { | 172 if (daysElapsed > 0L) { |
186 return resources.getString(R.string.ntp_recent_tabs_last_synced_days , daysElapsed); | 173 relativeTime = res.getQuantityString(R.plurals.n_days_ago, daysElaps ed, daysElapsed); |
187 } else if (hoursElapsed > 0L) { | 174 } else if (hoursElapsed > 0L) { |
188 return resources.getString(R.string.ntp_recent_tabs_last_synced_hour s, hoursElapsed); | 175 relativeTime = res.getQuantityString(R.plurals.n_hours_ago, hoursEla psed, hoursElapsed); |
189 } else if (minutesElapsed > 0L) { | 176 } else if (minutesElapsed > 0L) { |
190 return resources.getString( | 177 relativeTime = res.getQuantityString(R.plurals.n_minutes_ago, minute sElapsed, |
191 R.string.ntp_recent_tabs_last_synced_minutes, minutesElapsed ); | 178 minutesElapsed); |
Ian Wen
2015/12/21 13:03:44
Remove the third parameter?
newt (away)
2015/12/21 16:55:55
Oddly, the third parameter is required. The second
| |
192 } else { | 179 } else { |
193 return resources.getString(R.string.ntp_recent_tabs_last_synced_just _now); | 180 relativeTime = res.getString(R.string.just_now); |
194 } | 181 } |
182 | |
183 return getResources().getString(R.string.ntp_recent_tabs_last_synced, re lativeTime); | |
195 } | 184 } |
196 | 185 |
197 /** | 186 /** |
198 * Shows or hides the time label (e.g. "Last synced: just now") and adjusts the positions of the | 187 * Shows or hides the time label (e.g. "Last synced: just now") and adjusts the positions of the |
199 * icon and device label. In particular, the icon and device label are top-a ligned when the time | 188 * icon and device label. In particular, the icon and device label are top-a ligned when the time |
200 * is visible, but are vertically centered when the time is gone. | 189 * is visible, but are vertically centered when the time is gone. |
201 */ | 190 */ |
202 private void setTimeLabelVisibility(int visibility) { | 191 private void setTimeLabelVisibility(int visibility) { |
203 if (mTimeLabel.getVisibility() == visibility) return; | 192 if (mTimeLabel.getVisibility() == visibility) return; |
204 mTimeLabel.setVisibility(visibility); | 193 mTimeLabel.setVisibility(visibility); |
205 if (visibility == View.GONE) { | 194 if (visibility == View.GONE) { |
206 replaceRule(mDeviceIcon, ALIGN_PARENT_TOP, CENTER_VERTICAL); | 195 replaceRule(mDeviceIcon, ALIGN_PARENT_TOP, CENTER_VERTICAL); |
207 replaceRule(mDeviceLabel, ALIGN_PARENT_TOP, CENTER_VERTICAL); | 196 replaceRule(mDeviceLabel, ALIGN_PARENT_TOP, CENTER_VERTICAL); |
208 } else { | 197 } else { |
209 replaceRule(mDeviceIcon, CENTER_VERTICAL, ALIGN_PARENT_TOP); | 198 replaceRule(mDeviceIcon, CENTER_VERTICAL, ALIGN_PARENT_TOP); |
210 replaceRule(mDeviceLabel, CENTER_VERTICAL, ALIGN_PARENT_TOP); | 199 replaceRule(mDeviceLabel, CENTER_VERTICAL, ALIGN_PARENT_TOP); |
211 } | 200 } |
212 } | 201 } |
213 | 202 |
214 private static void replaceRule(View view, int oldRule, int newRule) { | 203 private static void replaceRule(View view, int oldRule, int newRule) { |
215 RelativeLayout.LayoutParams lp = ((RelativeLayout.LayoutParams) view.get LayoutParams()); | 204 RelativeLayout.LayoutParams lp = ((RelativeLayout.LayoutParams) view.get LayoutParams()); |
216 lp.addRule(oldRule, 0); | 205 lp.addRule(oldRule, 0); |
217 lp.addRule(newRule); | 206 lp.addRule(newRule); |
218 } | 207 } |
219 } | 208 } |
OLD | NEW |