1 /* 2 * Copyright 2018 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 androidx.work; 18 19 import android.net.Uri; 20 import android.support.annotation.NonNull; 21 22 import java.util.HashSet; 23 import java.util.Iterator; 24 import java.util.Set; 25 26 /** 27 * Stores a set of {@link Trigger}s 28 */ 29 public final class ContentUriTriggers implements Iterable<ContentUriTriggers.Trigger> { 30 private final Set<Trigger> mTriggers = new HashSet<>(); 31 32 /** 33 * Add a Content {@link Uri} to observe 34 * @param uri {@link Uri} to observe 35 * @param triggerForDescendants {@code true} if any changes in descendants cause this 36 * {@link WorkRequest} to run 37 */ add(Uri uri, boolean triggerForDescendants)38 public void add(Uri uri, boolean triggerForDescendants) { 39 Trigger trigger = new Trigger(uri, triggerForDescendants); 40 mTriggers.add(trigger); 41 } 42 43 @NonNull 44 @Override iterator()45 public Iterator<Trigger> iterator() { 46 return mTriggers.iterator(); 47 } 48 49 /** 50 * @return number of {@link Trigger} objects 51 */ size()52 public int size() { 53 return mTriggers.size(); 54 } 55 56 @Override equals(Object o)57 public boolean equals(Object o) { 58 if (this == o) return true; 59 if (o == null || getClass() != o.getClass()) return false; 60 61 ContentUriTriggers that = (ContentUriTriggers) o; 62 63 return mTriggers.equals(that.mTriggers); 64 } 65 66 @Override hashCode()67 public int hashCode() { 68 return mTriggers.hashCode(); 69 } 70 71 /** 72 * Defines a content {@link Uri} trigger for a {@link WorkRequest} 73 */ 74 75 public static final class Trigger { 76 @NonNull 77 private final Uri mUri; 78 private final boolean mTriggerForDescendants; 79 Trigger(@onNull Uri uri, boolean triggerForDescendants)80 public Trigger(@NonNull Uri uri, 81 boolean triggerForDescendants) { 82 mUri = uri; 83 mTriggerForDescendants = triggerForDescendants; 84 } 85 86 @NonNull getUri()87 public Uri getUri() { 88 return mUri; 89 } 90 91 /** 92 * @return {@code true} if trigger applies to descendants of {@link Uri} also 93 */ shouldTriggerForDescendants()94 public boolean shouldTriggerForDescendants() { 95 return mTriggerForDescendants; 96 } 97 98 @Override equals(Object o)99 public boolean equals(Object o) { 100 if (this == o) return true; 101 if (o == null || getClass() != o.getClass()) return false; 102 103 Trigger trigger = (Trigger) o; 104 105 return mTriggerForDescendants == trigger.mTriggerForDescendants 106 && mUri.equals(trigger.mUri); 107 } 108 109 @Override hashCode()110 public int hashCode() { 111 int result = mUri.hashCode(); 112 result = 31 * result + (mTriggerForDescendants ? 1 : 0); 113 return result; 114 } 115 } 116 } 117