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