1 /* 2 * Copyright (C) 2009 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.globalsearch; 18 19 import java.util.Collections; 20 import java.util.List; 21 22 /** 23 * Holds the data returned by a suggestion source for a single query. 24 */ 25 public class SuggestionResult { 26 27 /** 28 * The {@link #getResultCode} for when the source succesfully returned results. 29 */ 30 public static final int RESULT_OK = 29; 31 32 /** 33 * The {@link #getResultCode} for when the source encountered an error while producign the 34 * results. 35 */ 36 public static final int RESULT_ERROR = 30; 37 38 /** 39 * The {@link #getResultCode} for when the source was canceled, either due to timeout, or from 40 * the user typing another key before it had a chance to return its results. 41 */ 42 public static final int RESULT_CANCELED = 31; 43 44 private final SuggestionSource mSource; 45 private final List<SuggestionData> mSuggestions; 46 private final int mCount; 47 private final int mQueryLimit; 48 private final int mResultCode; 49 createErrorResult(SuggestionSource source)50 public static SuggestionResult createErrorResult(SuggestionSource source) { 51 return new SuggestionResult(source, RESULT_ERROR); 52 } 53 createCancelled(SuggestionSource source)54 public static SuggestionResult createCancelled(SuggestionSource source) { 55 return new SuggestionResult(source, RESULT_CANCELED); 56 } 57 SuggestionResult(SuggestionSource source, int resultCode)58 private SuggestionResult(SuggestionSource source, int resultCode) { 59 mSource = source; 60 mSuggestions = Collections.emptyList(); 61 mCount = 0; 62 mQueryLimit = 0; 63 mResultCode = resultCode; 64 } 65 66 /** 67 * @param source The source that the suggestions come from. 68 * @param suggestions The suggestions. 69 * @param count The total number of suggestions, which may be greater than the suggestions 70 * returned if that was capped for some reason. 71 * @param queryLimit The number of results that the source was asked for. If {@code count} 72 * is greater than or equal to {@code queryLimit}, {@code count} is only 73 * a lower bound, not an exact number. 74 */ SuggestionResult(SuggestionSource source, List<SuggestionData> suggestions, int count, int queryLimit)75 public SuggestionResult(SuggestionSource source, List<SuggestionData> suggestions, int count, 76 int queryLimit) { 77 mSource = source; 78 mSuggestions = suggestions; 79 mCount = count; 80 mQueryLimit = queryLimit; 81 mResultCode = RESULT_OK; 82 } 83 84 /** 85 * @param source The source that the suggestions come from. 86 * @param suggestions The suggestions. 87 */ SuggestionResult(SuggestionSource source, List<SuggestionData> suggestions)88 public SuggestionResult(SuggestionSource source, List<SuggestionData> suggestions) { 89 this(source, suggestions, suggestions.size(), suggestions.size()); 90 } 91 92 /** 93 * Can be used when there are no results. 94 * 95 * @param source The source that the suggestions come from. 96 */ SuggestionResult(SuggestionSource source)97 public SuggestionResult(SuggestionSource source) { 98 this(source, Collections.<SuggestionData>emptyList(), 0, 0); 99 } 100 getSource()101 public SuggestionSource getSource() { 102 return mSource; 103 } 104 getSuggestions()105 public List<SuggestionData> getSuggestions() { 106 return mSuggestions; 107 } 108 getCount()109 public int getCount() { 110 return mCount; 111 } 112 getQueryLimit()113 public int getQueryLimit() { 114 return mQueryLimit; 115 } 116 117 /** 118 * @return one of {@link #RESULT_OK}, {@link #RESULT_CANCELED} or {@link #RESULT_ERROR}. 119 */ getResultCode()120 public int getResultCode() { 121 return mResultCode; 122 } 123 } 124