Bug 1243558 - Add proper layout to bookmark_screenshot_row. r=sebastian draft
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 29 Feb 2016 16:32:43 -0800
changeset 335640 79aaede891025761122e154a9b8c51623eebcc53
parent 335635 393684f11e2d69f98f49eff9859e1ab21738979f
child 335641 e64e95e5f7701fdc7561ce23b60f465701de96e1
push id11840
push usermichael.l.comella@gmail.com
push dateTue, 01 Mar 2016 01:35:09 +0000
reviewerssebastian
bugs1243558, 1252309
milestone47.0a1
Bug 1243558 - Add proper layout to bookmark_screenshot_row. r=sebastian An alternative implementation may have re-used TwoLinePageRow by disabling some views. Disabling views is messy so I opted not to do that but I didn't realize we needed to add favicons to the view so this may end up occurring in bug 1252309 anyway. MozReview-Commit-ID: DOrvYryYYP4
mobile/android/base/java/org/mozilla/gecko/home/BookmarkScreenshotRow.java
mobile/android/base/java/org/mozilla/gecko/home/BookmarksListAdapter.java
mobile/android/base/moz.build
mobile/android/base/resources/layout/bookmark_screenshot_row.xml
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/home/BookmarkScreenshotRow.java
@@ -0,0 +1,67 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.home;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.util.AttributeSet;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import org.mozilla.gecko.R;
+import org.mozilla.gecko.db.BrowserContract.UrlAnnotations;
+
+import java.text.DateFormat;
+import java.text.FieldPosition;
+import java.util.Date;
+
+/**
+ * An entry of the screenshot list in the bookmarks panel.
+ */
+class BookmarkScreenshotRow extends LinearLayout {
+    private TextView titleView;
+    private TextView dateView;
+
+    // This DateFormat uses the current locale at instantiation time, which won't get updated if the locale is changed.
+    // Since it's just a date, it's probably not worth the code complexity to fix that.
+    private static final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
+    private static final DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
+
+    // This parameter to DateFormat.format has no impact on the result but rather gets mutated by the method to
+    // identify where a certain field starts and ends (by index). This is useful if you want to later modify the String;
+    // I'm not sure why this argument isn't optional.
+    private static final FieldPosition dummyFieldPosition = new FieldPosition(-1);
+
+    public BookmarkScreenshotRow(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    public void onFinishInflate() {
+        super.onFinishInflate();
+        titleView = (TextView) findViewById(R.id.title);
+        dateView = (TextView) findViewById(R.id.date);
+    }
+
+    public void updateFromCursor(final Cursor c) {
+        titleView.setText(getTitleFromCursor(c));
+        dateView.setText(getDateFromCursor(c));
+    }
+
+    private static String getTitleFromCursor(final Cursor c) {
+        final int index = c.getColumnIndexOrThrow(UrlAnnotations.URL);
+        return c.getString(index);
+    }
+
+    private static String getDateFromCursor(final Cursor c) {
+        final long timestamp = c.getLong(c.getColumnIndexOrThrow(UrlAnnotations.DATE_CREATED));
+        final Date date = new Date(timestamp);
+        final StringBuffer sb = new StringBuffer();
+        dateFormat.format(date, sb, dummyFieldPosition)
+                .append(" - ");
+        timeFormat.format(date, sb, dummyFieldPosition);
+        return sb.toString();
+    }
+}
--- a/mobile/android/base/java/org/mozilla/gecko/home/BookmarksListAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/BookmarksListAdapter.java
@@ -5,26 +5,24 @@
 
 package org.mozilla.gecko.home;
 
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.mozilla.gecko.R;
-import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.view.View;
-import android.widget.TextView;
 
 /**
  * Adapter to back the BookmarksListView with a list of bookmarks.
  */
 class BookmarksListAdapter extends MultiTypeCursorAdapter {
     private static final int VIEW_TYPE_BOOKMARK_ITEM = 0;
     private static final int VIEW_TYPE_FOLDER = 1;
     private static final int VIEW_TYPE_SCREENSHOT = 2;
@@ -311,19 +309,17 @@ class BookmarksListAdapter extends Multi
                 position--;
                 cursor = getCursor(position);
             }
         } else {
             cursor = getCursor(position);
         }
 
         if (viewType == VIEW_TYPE_SCREENSHOT) {
-            // TODO: Update view in a more robust way.
-            ((TextView) view.findViewById(R.id.title)).setText(cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.UrlAnnotations.URL)));
-            ((TextView) view.findViewById(R.id.date)).setText(Long.toString(cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.UrlAnnotations.DATE_CREATED))));
+            ((BookmarkScreenshotRow) view).updateFromCursor(cursor);
         } else if (viewType == VIEW_TYPE_BOOKMARK_ITEM) {
             final TwoLinePageRow row = (TwoLinePageRow) view;
             row.updateFromCursor(cursor);
         } else {
             final BookmarkFolderView row = (BookmarkFolderView) view;
             if (cursor == null) {
                 final Resources res = context.getResources();
                 row.setText(res.getString(R.string.home_move_up_to_filter, mParentStack.get(1).title));
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -354,16 +354,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'gfx/ViewTransform.java',
     'gfx/VirtualLayer.java',
     'GlobalHistory.java',
     'GuestSession.java',
     'health/HealthRecorder.java',
     'health/SessionInformation.java',
     'health/StubbedHealthRecorder.java',
     'home/BookmarkFolderView.java',
+    'home/BookmarkScreenshotRow.java',
     'home/BookmarksListAdapter.java',
     'home/BookmarksListView.java',
     'home/BookmarksPanel.java',
     'home/BrowserSearch.java',
     'home/DynamicPanel.java',
     'home/FramePanelLayout.java',
     'home/HistoryHeaderListCursorAdapter.java',
     'home/HistoryItemAdapter.java',
--- a/mobile/android/base/resources/layout/bookmark_screenshot_row.xml
+++ b/mobile/android/base/resources/layout/bookmark_screenshot_row.xml
@@ -1,23 +1,35 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- This Source Code Form is subject to the terms of the Mozilla Public
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
-<!-- TODO: create proper layout -->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              android:orientation="vertical"
-              android:layout_width="match_parent"
-              android:layout_height="wrap_content"
-              android:padding="4dp">
+<org.mozilla.gecko.home.BookmarkScreenshotRow
+        xmlns:android="http://schemas.android.com/apk/res/android" xmlns:gecko="http://schemas.android.com/apk/res-auto"
+        android:layout_width="match_parent"
+        android:layout_height="@dimen/page_row_height"
+        android:minHeight="@dimen/page_row_height"
+        android:orientation="vertical"
+        android:gravity="center_vertical"
+        android:paddingLeft="16dp"
+        android:paddingRight="16dp"
+>
 
-    <TextView android:id="@+id/title"
-              android:layout_width="wrap_content"
-              android:layout_height="wrap_content"
-              />
+    <org.mozilla.gecko.widget.FadedSingleColorTextView
+            android:id="@+id/title"
+            style="@style/Widget.TwoLinePageRow.Title"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            gecko:fadeWidth="30dp"
+    />
 
-    <TextView android:id="@+id/date"
-              android:layout_width="wrap_content"
-              android:layout_height="wrap_content"
-              />
+    <org.mozilla.gecko.widget.FadedSingleColorTextView
+            android:id="@+id/date"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:textAppearance="@style/TextAppearance.Widget.Home.ItemDescription"
+            android:textColor="@color/link_blue"
+            android:singleLine="true"
+            gecko:fadeWidth="30dp"
+    />
 
-</LinearLayout>
\ No newline at end of file
+</org.mozilla.gecko.home.BookmarkScreenshotRow>