1 /* 2 * Copyright (C) 2021 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.queryable.queries; 18 19 import android.content.IntentFilter; 20 21 import com.android.queryable.Queryable; 22 23 import java.util.HashSet; 24 import java.util.Iterator; 25 import java.util.Set; 26 27 /** 28 * Implementation of {@link IntentFilterQuery}. 29 * 30 * @param <E> Type of query. 31 */ 32 public class IntentFilterQueryHelper<E extends Queryable> implements IntentFilterQuery<E> { 33 34 private final E mQuery; 35 private final SetQueryHelper<E, String, StringQuery<?>> mActionsQueryHelper; 36 private final SetQueryHelper<E, String, StringQuery<?>> mCategoriesQueryHelper; 37 IntentFilterQueryHelper()38 IntentFilterQueryHelper() { 39 mQuery = (E) this; 40 mActionsQueryHelper = new SetQueryHelper<>(mQuery); 41 mCategoriesQueryHelper = new SetQueryHelper<>(mQuery); 42 } 43 IntentFilterQueryHelper(E query)44 public IntentFilterQueryHelper(E query) { 45 mQuery = query; 46 mActionsQueryHelper = new SetQueryHelper<>(query); 47 mCategoriesQueryHelper = new SetQueryHelper<>(query); 48 } 49 50 @Override actions()51 public SetQuery<E, String, StringQuery<?>> actions() { 52 return mActionsQueryHelper; 53 } 54 55 @Override categories()56 public SetQuery<E, String, StringQuery<?>> categories() { 57 return mCategoriesQueryHelper; 58 } 59 60 @Override matches(IntentFilter value)61 public boolean matches(IntentFilter value) { 62 Set<String> actions = new HashSet<>(); 63 Set<String> categories = new HashSet<>(); 64 65 if (value.countActions() > 0) { 66 Iterator<String> actionsIterator = value.actionsIterator(); 67 68 while (actionsIterator.hasNext()) { 69 actions.add(actionsIterator.next()); 70 } 71 } 72 if (value.countCategories() > 0) { 73 Iterator<String> categoriesIterator = value.categoriesIterator(); 74 75 while (categoriesIterator.hasNext()) { 76 categories.add(categoriesIterator.next()); 77 } 78 } 79 80 81 return mActionsQueryHelper.matches(actions) 82 && mCategoriesQueryHelper.matches(categories); 83 } 84 85 @Override describeQuery(String fieldName)86 public String describeQuery(String fieldName) { 87 return Queryable.joinQueryStrings( 88 mActionsQueryHelper.describeQuery(fieldName + ".actions"), 89 mCategoriesQueryHelper.describeQuery(fieldName + ".categories") 90 ); 91 } 92 } 93