1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 package org.mozilla.gecko.activitystream.homepanel.model;
7 
8 import android.database.Cursor;
9 import android.support.annotation.NonNull;
10 import android.support.annotation.Nullable;
11 
12 import org.mozilla.gecko.db.BrowserContract;
13 
14 public class TopSite implements WebpageModel {
15     private final long id;
16     private final String url;
17     private final String title;
18     private @Nullable Boolean isBookmarked;
19     private final @Nullable boolean isPinned;
20     private @BrowserContract.TopSites.TopSiteType final int type;
21     private final Metadata metadata;
22 
fromCursor(Cursor cursor)23     public static TopSite fromCursor(Cursor cursor) {
24         // The Combined View only contains pages that have been visited at least once, i.e. any
25         // page in the TopSites query will contain a HISTORY_ID. _ID however will be 0 for all rows.
26         final long id = cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Combined.HISTORY_ID));
27         final String url = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Combined.URL));
28         final String title = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Combined.TITLE));
29         final int type = cursor.getInt(cursor.getColumnIndexOrThrow(BrowserContract.TopSites.TYPE));
30         final Metadata metadata = Metadata.fromCursor(cursor, BrowserContract.TopSites.PAGE_METADATA_JSON);
31 
32         // We can't figure out bookmark state of a pin or suggested site, so we leave it as unknown to be queried later.
33         Boolean isBookmarked = null;
34         if (type != BrowserContract.TopSites.TYPE_PINNED &&
35                 type != BrowserContract.TopSites.TYPE_SUGGESTED) {
36             isBookmarked = !cursor.isNull(cursor.getColumnIndexOrThrow(BrowserContract.Combined.BOOKMARK_ID));
37         }
38 
39         return new TopSite(id, url, title, isBookmarked, type, metadata);
40     }
41 
TopSite(long id, String url, String title, @Nullable Boolean isBookmarked, int type, final Metadata metadata)42     private TopSite(long id, String url, String title, @Nullable Boolean isBookmarked, int type, final Metadata metadata) {
43         this.id = id;
44         this.url = url;
45         this.title = title;
46         this.isBookmarked = isBookmarked;
47         this.isPinned = type == BrowserContract.TopSites.TYPE_PINNED;
48         this.type = type;
49         this.metadata = metadata;
50     }
51 
getId()52     public long getId() {
53         return id;
54     }
55 
getUrl()56     public String getUrl() {
57         return url;
58     }
59 
getTitle()60     public String getTitle() {
61         return title;
62     }
63 
64     @Nullable
isBookmarked()65     public Boolean isBookmarked() {
66         return isBookmarked;
67     }
68 
69     @BrowserContract.TopSites.TopSiteType
getType()70     public int getType() {
71         return type;
72     }
73 
isPinned()74     public Boolean isPinned() {
75         return isPinned;
76     }
77 
78     @NonNull
79     @Override
getImageUrl()80     public String getImageUrl() {
81         final String imageUrl = metadata.getImageUrl();
82         return imageUrl != null ? imageUrl : "";
83     }
84 
85     @Override
updateBookmarked(boolean bookmarked)86     public void updateBookmarked(boolean bookmarked) {
87         this.isBookmarked = bookmarked;
88     }
89 
90     @Override
updatePinned(boolean pinned)91     public void updatePinned(boolean pinned) {
92         throw new UnsupportedOperationException(
93                 "Pinned state of a top site should be known at the time of querying the database already");
94     }
95 
96     // The TopSites cursor automatically notifies of data changes, so nothing needs to be done here.
97     @Override
onStateCommitted()98     public void onStateCommitted() {}
99 }
100