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