1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.search2;
18 
19 import android.graphics.drawable.Drawable;
20 
21 import java.util.List;
22 import java.util.Objects;
23 
24 /**
25  * Data class as an interface for all Search Results.
26  */
27 public class SearchResult implements Comparable<SearchResult> {
28 
29     /**
30      * Defines the lowest rank for a search result to be considered as ranked. Results with ranks
31      * higher than this have no guarantee for sorting order.
32      */
33     public static final int BOTTOM_RANK = 10;
34 
35     /**
36      * Defines the highest rank for a search result. Used for special search results only.
37      */
38     public static final int TOP_RANK = 0;
39 
40     /**
41      * The title of the result and main text displayed.
42      * Intent Results: Displays as the primary
43      */
44     public final CharSequence title;
45 
46     /**
47      * Summary / subtitle text
48      * Intent Results: Displays the text underneath the title
49      */
50     final public CharSequence summary;
51 
52     /**
53      * An ordered list of the information hierarchy.
54      * Intent Results: Displayed a hierarchy of selections to reach the setting from the home screen
55      */
56     public final List<String> breadcrumbs;
57 
58     /**
59      * A suggestion for the ranking of the result.
60      * Based on Settings Rank:
61      * 1 is a near perfect match
62      * 9 is the weakest match
63      * TODO subject to change
64      */
65     public final int rank;
66 
67     /**
68      * Identifier for the recycler view adapter.
69      */
70     @ResultPayload.PayloadType
71     public final int viewType;
72 
73     /**
74      * Metadata for the specific result types.
75      */
76     public final ResultPayload payload;
77 
78     /**
79      * Result's icon.
80      */
81     public final Drawable icon;
82 
83     /**
84      * Stable id for this object.
85      */
86     public final long stableId;
87 
SearchResult(Builder builder)88     protected SearchResult(Builder builder) {
89         title = builder.mTitle;
90         summary = builder.mSummary;
91         breadcrumbs = builder.mBreadcrumbs;
92         rank = builder.mRank;
93         icon = builder.mIcon;
94         payload = builder.mResultPayload;
95         viewType = payload.getType();
96         stableId = Objects.hash(title, summary, breadcrumbs, rank, viewType);
97     }
98 
99     @Override
compareTo(SearchResult searchResult)100     public int compareTo(SearchResult searchResult) {
101         if (searchResult == null) {
102             return -1;
103         }
104         return this.rank - searchResult.rank;
105     }
106 
107     @Override
equals(Object obj)108     public boolean equals(Object obj) {
109         if (this == obj) {
110             return true;
111         }
112         if (!(obj instanceof SearchResult)) {
113             return false;
114         }
115         return this.stableId == ((SearchResult) obj).stableId;
116     }
117 
118     @Override
hashCode()119     public int hashCode() {
120         return (int) stableId;
121     }
122 
123     public static class Builder {
124         protected CharSequence mTitle;
125         protected CharSequence mSummary;
126         protected List<String> mBreadcrumbs;
127         protected int mRank = 42;
128         protected ResultPayload mResultPayload;
129         protected Drawable mIcon;
130 
addTitle(CharSequence title)131         public Builder addTitle(CharSequence title) {
132             mTitle = title;
133             return this;
134         }
135 
addSummary(CharSequence summary)136         public Builder addSummary(CharSequence summary) {
137             mSummary = summary;
138             return this;
139         }
140 
addBreadcrumbs(List<String> breadcrumbs)141         public Builder addBreadcrumbs(List<String> breadcrumbs) {
142             mBreadcrumbs = breadcrumbs;
143             return this;
144         }
145 
addRank(int rank)146         public Builder addRank(int rank) {
147             if (rank >= 0 && rank <= 9) {
148                 mRank = rank;
149             }
150             return this;
151         }
152 
addIcon(Drawable icon)153         public Builder addIcon(Drawable icon) {
154             mIcon = icon;
155             return this;
156         }
157 
addPayload(ResultPayload payload)158         public Builder addPayload(ResultPayload payload) {
159             mResultPayload = payload;
160             return this;
161         }
162 
build()163         public SearchResult build() {
164             // Check that all of the mandatory fields are set.
165             if (mTitle == null) {
166                 throw new IllegalArgumentException("SearchResult missing title argument");
167             } else if (mResultPayload == null) {
168                 throw new IllegalArgumentException("SearchResult missing Payload argument");
169             }
170             return new SearchResult(this);
171         }
172     }
173 }