1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 package org.mozilla.gecko.home; 6 7 import android.content.Context; 8 import android.database.Cursor; 9 import android.util.AttributeSet; 10 import android.widget.LinearLayout; 11 import android.widget.TextView; 12 13 import org.mozilla.gecko.R; 14 import org.mozilla.gecko.db.BrowserContract.UrlAnnotations; 15 16 import java.text.DateFormat; 17 import java.text.FieldPosition; 18 import java.util.Date; 19 20 /** 21 * An entry of the screenshot list in the bookmarks panel. 22 */ 23 class BookmarkScreenshotRow extends LinearLayout { 24 private TextView titleView; 25 private TextView dateView; 26 27 // This DateFormat uses the current locale at instantiation time, which won't get updated if the locale is changed. 28 // Since it's just a date, it's probably not worth the code complexity to fix that. 29 private static final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); 30 private static final DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT); 31 32 // This parameter to DateFormat.format has no impact on the result but rather gets mutated by the method to 33 // identify where a certain field starts and ends (by index). This is useful if you want to later modify the String; 34 // I'm not sure why this argument isn't optional. 35 private static final FieldPosition dummyFieldPosition = new FieldPosition(-1); 36 BookmarkScreenshotRow(Context context, AttributeSet attrs)37 public BookmarkScreenshotRow(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 41 @Override onFinishInflate()42 public void onFinishInflate() { 43 super.onFinishInflate(); 44 titleView = (TextView) findViewById(R.id.title); 45 dateView = (TextView) findViewById(R.id.date); 46 } 47 updateFromCursor(final Cursor c)48 public void updateFromCursor(final Cursor c) { 49 titleView.setText(getTitleFromCursor(c)); 50 dateView.setText(getDateFromCursor(c)); 51 } 52 getTitleFromCursor(final Cursor c)53 private static String getTitleFromCursor(final Cursor c) { 54 final int index = c.getColumnIndexOrThrow(UrlAnnotations.URL); 55 return c.getString(index); 56 } 57 getDateFromCursor(final Cursor c)58 private static String getDateFromCursor(final Cursor c) { 59 final long timestamp = c.getLong(c.getColumnIndexOrThrow(UrlAnnotations.DATE_CREATED)); 60 final Date date = new Date(timestamp); 61 final StringBuffer sb = new StringBuffer(); 62 dateFormat.format(date, sb, dummyFieldPosition) 63 .append(" - "); 64 timeFormat.format(date, sb, dummyFieldPosition); 65 return sb.toString(); 66 } 67 } 68