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 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import com.android.queryable.Queryable; 24 25 import java.util.HashSet; 26 import java.util.Iterator; 27 import java.util.Objects; 28 import java.util.Set; 29 30 /** 31 * Implementation of {@link IntentFilterQuery}. 32 * 33 * @param <E> Type of query. 34 */ 35 public class IntentFilterQueryHelper<E extends Queryable> implements IntentFilterQuery<E> { 36 37 private final transient E mQuery; 38 private final SetQueryHelper<E, String, StringQuery<?>> mActionsQueryHelper; 39 private final SetQueryHelper<E, String, StringQuery<?>> mCategoriesQueryHelper; 40 IntentFilterQueryHelper()41 IntentFilterQueryHelper() { 42 mQuery = (E) this; 43 mActionsQueryHelper = new SetQueryHelper<>(mQuery); 44 mCategoriesQueryHelper = new SetQueryHelper<>(mQuery); 45 } 46 IntentFilterQueryHelper(E query)47 public IntentFilterQueryHelper(E query) { 48 mQuery = query; 49 mActionsQueryHelper = new SetQueryHelper<>(query); 50 mCategoriesQueryHelper = new SetQueryHelper<>(query); 51 } 52 IntentFilterQueryHelper(Parcel in)53 private IntentFilterQueryHelper(Parcel in) { 54 mQuery = null; 55 mActionsQueryHelper = in.readParcelable(IntentFilterQueryHelper.class.getClassLoader()); 56 mCategoriesQueryHelper = in.readParcelable(IntentFilterQueryHelper.class.getClassLoader()); 57 } 58 59 @Override actions()60 public SetQuery<E, String, StringQuery<?>> actions() { 61 return mActionsQueryHelper; 62 } 63 64 @Override categories()65 public SetQuery<E, String, StringQuery<?>> categories() { 66 return mCategoriesQueryHelper; 67 } 68 69 @Override matches(IntentFilter value)70 public boolean matches(IntentFilter value) { 71 Set<String> actions = new HashSet<>(); 72 Set<String> categories = new HashSet<>(); 73 74 if (value.countActions() > 0) { 75 Iterator<String> actionsIterator = value.actionsIterator(); 76 77 while (actionsIterator.hasNext()) { 78 actions.add(actionsIterator.next()); 79 } 80 } 81 if (value.countCategories() > 0) { 82 Iterator<String> categoriesIterator = value.categoriesIterator(); 83 84 while (categoriesIterator.hasNext()) { 85 categories.add(categoriesIterator.next()); 86 } 87 } 88 89 90 return mActionsQueryHelper.matches(actions) 91 && mCategoriesQueryHelper.matches(categories); 92 } 93 94 @Override describeQuery(String fieldName)95 public String describeQuery(String fieldName) { 96 return Queryable.joinQueryStrings( 97 mActionsQueryHelper.describeQuery(fieldName + ".actions"), 98 mCategoriesQueryHelper.describeQuery(fieldName + ".categories") 99 ); 100 } 101 102 @Override describeContents()103 public int describeContents() { 104 return 0; 105 } 106 107 @Override writeToParcel(Parcel out, int flags)108 public void writeToParcel(Parcel out, int flags) { 109 out.writeParcelable(mActionsQueryHelper, flags); 110 out.writeParcelable(mCategoriesQueryHelper, flags); 111 } 112 113 public static final Parcelable.Creator<IntentFilterQueryHelper> CREATOR = 114 new Parcelable.Creator<IntentFilterQueryHelper>() { 115 public IntentFilterQueryHelper createFromParcel(Parcel in) { 116 return new IntentFilterQueryHelper(in); 117 } 118 119 public IntentFilterQueryHelper[] newArray(int size) { 120 return new IntentFilterQueryHelper[size]; 121 } 122 }; 123 124 @Override equals(Object o)125 public boolean equals(Object o) { 126 if (this == o) return true; 127 if (!(o instanceof IntentFilterQueryHelper)) return false; 128 IntentFilterQueryHelper<?> that = (IntentFilterQueryHelper<?>) o; 129 return Objects.equals(mActionsQueryHelper, that.mActionsQueryHelper) 130 && Objects.equals(mCategoriesQueryHelper, that.mCategoriesQueryHelper); 131 } 132 133 @Override hashCode()134 public int hashCode() { 135 return Objects.hash(mActionsQueryHelper, mCategoriesQueryHelper); 136 } 137 } 138