1 /* 2 * Copyright (C) 2010 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.os; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.compat.annotation.UnsupportedAppUsage; 23 24 /** 25 * @hide 26 */ 27 @SystemApi 28 @android.ravenwood.annotation.RavenwoodKeepWholeClass 29 public final class RemoteCallback implements Parcelable { 30 31 public interface OnResultListener { onResult(@ullable Bundle result)32 void onResult(@Nullable Bundle result); 33 } 34 35 private final OnResultListener mListener; 36 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) 37 private final Handler mHandler; 38 private final IRemoteCallback mCallback; 39 RemoteCallback(OnResultListener listener)40 public RemoteCallback(OnResultListener listener) { 41 this(listener, null); 42 } 43 RemoteCallback(@onNull OnResultListener listener, @Nullable Handler handler)44 public RemoteCallback(@NonNull OnResultListener listener, @Nullable Handler handler) { 45 if (listener == null) { 46 throw new NullPointerException("listener cannot be null"); 47 } 48 mListener = listener; 49 mHandler = handler; 50 mCallback = new IRemoteCallback.Stub() { 51 @Override 52 public void sendResult(Bundle data) { 53 RemoteCallback.this.sendResult(data); 54 } 55 }; 56 } 57 RemoteCallback(Parcel parcel)58 RemoteCallback(Parcel parcel) { 59 mListener = null; 60 mHandler = null; 61 mCallback = IRemoteCallback.Stub.asInterface( 62 parcel.readStrongBinder()); 63 } 64 sendResult(@ullable final Bundle result)65 public void sendResult(@Nullable final Bundle result) { 66 // Do local dispatch 67 if (mListener != null) { 68 if (mHandler != null) { 69 mHandler.post(new Runnable() { 70 @Override 71 public void run() { 72 mListener.onResult(result); 73 } 74 }); 75 } else { 76 mListener.onResult(result); 77 } 78 // Do remote dispatch 79 } else { 80 try { 81 mCallback.sendResult(result); 82 } catch (RemoteException e) { 83 /* ignore */ 84 } 85 } 86 } 87 88 /** @hide */ getInterface()89 public IRemoteCallback getInterface() { 90 return mCallback; 91 } 92 93 @Override describeContents()94 public int describeContents() { 95 return 0; 96 } 97 98 @Override writeToParcel(Parcel parcel, int flags)99 public void writeToParcel(Parcel parcel, int flags) { 100 parcel.writeStrongBinder(mCallback.asBinder()); 101 } 102 103 public static final @android.annotation.NonNull Parcelable.Creator<RemoteCallback> CREATOR 104 = new Parcelable.Creator<RemoteCallback>() { 105 public RemoteCallback createFromParcel(Parcel parcel) { 106 return new RemoteCallback(parcel); 107 } 108 109 public RemoteCallback[] newArray(int size) { 110 return new RemoteCallback[size]; 111 } 112 }; 113 } 114