Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.settingslib.suggestions; |
| 18 | |
| 19 | import android.content.Intent; |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 20 | import android.util.ArrayMap; |
| 21 | import android.util.ArraySet; |
| 22 | |
| 23 | import com.android.settingslib.drawer.Tile; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | public class SuggestionList { |
| 31 | // Category -> list of suggestion map |
| 32 | private final Map<SuggestionCategory, List<Tile>> mSuggestions; |
| 33 | |
| 34 | // A flatten list of all suggestions. |
| 35 | private List<Tile> mSuggestionList; |
| 36 | |
| 37 | public SuggestionList() { |
| 38 | mSuggestions = new ArrayMap<>(); |
| 39 | } |
| 40 | |
| 41 | public void addSuggestions(SuggestionCategory category, List<Tile> suggestions) { |
| 42 | mSuggestions.put(category, suggestions); |
| 43 | } |
| 44 | |
| 45 | public List<Tile> getSuggestions() { |
| 46 | if (mSuggestionList != null) { |
| 47 | return mSuggestionList; |
| 48 | } |
| 49 | mSuggestionList = new ArrayList<>(); |
| 50 | for (List<Tile> suggestions : mSuggestions.values()) { |
| 51 | mSuggestionList.addAll(suggestions); |
| 52 | } |
| 53 | dedupeSuggestions(mSuggestionList); |
| 54 | return mSuggestionList; |
| 55 | } |
| 56 | |
| 57 | public boolean isExclusiveSuggestionCategory() { |
| 58 | if (mSuggestions.size() != 1) { |
| 59 | // If there is no category, or more than 1 category, it's not exclusive by definition. |
| 60 | return false; |
| 61 | } |
| 62 | for (SuggestionCategory category : mSuggestions.keySet()) { |
| 63 | if (category.exclusive) { |
| 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Filter suggestions list so they are all unique. |
| 72 | */ |
| 73 | private void dedupeSuggestions(List<Tile> suggestions) { |
| 74 | final Set<String> intents = new ArraySet<>(); |
| 75 | for (int i = suggestions.size() - 1; i >= 0; i--) { |
| 76 | final Tile suggestion = suggestions.get(i); |
| 77 | final String intentUri = suggestion.intent.toUri(Intent.URI_INTENT_SCHEME); |
| 78 | if (intents.contains(intentUri)) { |
| 79 | suggestions.remove(i); |
| 80 | } else { |
| 81 | intents.add(intentUri); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |