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.server.appsearch.observer; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.appsearch.aidl.IAppSearchObserverProxy; 22 import android.app.appsearch.observer.DocumentChangeInfo; 23 import android.app.appsearch.observer.ObserverCallback; 24 import android.app.appsearch.observer.SchemaChangeInfo; 25 import android.os.IBinder; 26 import android.os.RemoteException; 27 import android.util.Log; 28 29 import java.util.ArrayList; 30 import java.util.Objects; 31 32 /** 33 * A wrapper that adapts {@link android.app.appsearch.aidl.IAppSearchObserverProxy} to the 34 * {@link android.app.appsearch.observer.ObserverCallback} interface. 35 * 36 * <p>When using this class, you must register for {@link android.os.IBinder#linkToDeath} 37 * notifications on the stub you provide to the constructor, to unregister this class from 38 * {@link com.android.server.appsearch.external.localstorage.AppSearchImpl} when binder dies. 39 * 40 * @hide 41 */ 42 public class AppSearchObserverProxy implements ObserverCallback { 43 private static final String TAG = "AppSearchObserverProxy"; 44 45 private final IAppSearchObserverProxy mStub; 46 AppSearchObserverProxy(@onNull IAppSearchObserverProxy stub)47 public AppSearchObserverProxy(@NonNull IAppSearchObserverProxy stub) { 48 mStub = Objects.requireNonNull(stub); 49 } 50 51 @Override onSchemaChanged(@onNull SchemaChangeInfo changeInfo)52 public void onSchemaChanged(@NonNull SchemaChangeInfo changeInfo) { 53 try { 54 mStub.onSchemaChanged( 55 changeInfo.getPackageName(), 56 changeInfo.getDatabaseName(), 57 new ArrayList<>(changeInfo.getChangedSchemaNames())); 58 } catch (RemoteException e) { 59 onRemoteException(e); 60 } 61 } 62 63 @Override onDocumentChanged(@onNull DocumentChangeInfo changeInfo)64 public void onDocumentChanged(@NonNull DocumentChangeInfo changeInfo) { 65 try { 66 mStub.onDocumentChanged( 67 changeInfo.getPackageName(), 68 changeInfo.getDatabaseName(), 69 changeInfo.getNamespace(), 70 changeInfo.getSchemaName(), 71 new ArrayList<>(changeInfo.getChangedDocumentIds())); 72 } catch (RemoteException e) { 73 onRemoteException(e); 74 } 75 } 76 onRemoteException(@onNull RemoteException e)77 private void onRemoteException(@NonNull RemoteException e) { 78 // The originating app has disconnected. The user of this class must watch for binder 79 // disconnections and unregister us, so we don't have to take any special action. 80 Log.w(TAG, "AppSearchObserver failed to fire; stub disconnected", e); 81 } 82 83 @Override equals(@ullable Object o)84 public boolean equals(@Nullable Object o) { 85 if (this == o) return true; 86 if (!(o instanceof AppSearchObserverProxy)) return false; 87 AppSearchObserverProxy that = (AppSearchObserverProxy) o; 88 return Objects.equals(mStub.asBinder(), that.mStub.asBinder()); 89 } 90 91 @Override hashCode()92 public int hashCode() { 93 return Objects.hashCode(mStub.asBinder()); 94 } 95 } 96