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