1 /* 2 * Copyright 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 android.app.appsearch.observer; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 22 import java.util.Collections; 23 import java.util.Objects; 24 import java.util.Set; 25 26 /** 27 * Contains information about a schema change detected by an {@link ObserverCallback}. 28 * 29 * <p>This object will be sent when a schema type having a name matching an observer's schema 30 * filters (as determined by {@link ObserverSpec#getFilterSchemas}) has been added, updated, or 31 * removed. 32 * 33 * <p>Note that schema changes may cause documents to be migrated or removed. When this happens, 34 * individual document updates will NOT be dispatched via {@link DocumentChangeInfo}. The only 35 * notification will be of the schema type change via {@link SchemaChangeInfo}. Depending on your 36 * use case, you may need to re-query the whole schema type when this happens. 37 */ 38 public final class SchemaChangeInfo { 39 private final String mPackageName; 40 private final String mDatabaseName; 41 private final Set<String> mChangedSchemaNames; 42 43 /** 44 * Constructs a new {@link SchemaChangeInfo}. 45 * 46 * @param packageName The package name of the app which owns the schema that changed. 47 * @param databaseName The database in which the schema that changed resides. 48 * @param changedSchemaNames Names of schemas that have changed as part of this notification. 49 */ SchemaChangeInfo( @onNull String packageName, @NonNull String databaseName, @NonNull Set<String> changedSchemaNames)50 public SchemaChangeInfo( 51 @NonNull String packageName, 52 @NonNull String databaseName, 53 @NonNull Set<String> changedSchemaNames) { 54 mPackageName = Objects.requireNonNull(packageName); 55 mDatabaseName = Objects.requireNonNull(databaseName); 56 mChangedSchemaNames = 57 Collections.unmodifiableSet(Objects.requireNonNull(changedSchemaNames)); 58 } 59 60 /** Returns the package name of the app which owns the schema that changed. */ 61 @NonNull getPackageName()62 public String getPackageName() { 63 return mPackageName; 64 } 65 66 /** Returns the database in which the schema that was changed resides. */ 67 @NonNull getDatabaseName()68 public String getDatabaseName() { 69 return mDatabaseName; 70 } 71 72 /** 73 * Returns the names of schema types affected by this change notification. 74 * 75 * <p>This will never be empty. 76 */ 77 @NonNull getChangedSchemaNames()78 public Set<String> getChangedSchemaNames() { 79 return mChangedSchemaNames; 80 } 81 82 @Override equals(@ullable Object o)83 public boolean equals(@Nullable Object o) { 84 if (this == o) { 85 return true; 86 } 87 88 if (!(o instanceof SchemaChangeInfo)) { 89 return false; 90 } 91 92 SchemaChangeInfo that = (SchemaChangeInfo) o; 93 return mPackageName.equals(that.mPackageName) 94 && mDatabaseName.equals(that.mDatabaseName) 95 && mChangedSchemaNames.equals(that.mChangedSchemaNames); 96 } 97 98 @Override hashCode()99 public int hashCode() { 100 return Objects.hash(mPackageName, mDatabaseName, mChangedSchemaNames); 101 } 102 103 @NonNull 104 @Override toString()105 public String toString() { 106 return "SchemaChangeInfo{" 107 + "packageName='" 108 + mPackageName 109 + '\'' 110 + ", databaseName='" 111 + mDatabaseName 112 + '\'' 113 + ", changedSchemaNames='" 114 + mChangedSchemaNames 115 + '\'' 116 + '}'; 117 } 118 } 119