Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.companion; |
| 18 | |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Mathew Inwood | 70e89d5 | 2018-08-09 15:27:52 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
| 24 | import android.provider.OneTimeUseBuilder; |
| 25 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 26 | import com.android.internal.util.ArrayUtils; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 27 | import com.android.internal.util.CollectionUtils; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 31 | import java.util.Objects; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * A request for the user to select a companion device to associate with. |
| 35 | * |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 36 | * You can optionally set {@link Builder#addDeviceFilter filters} for which devices to show to the |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 37 | * user to select from. |
| 38 | * The exact type and fields of the filter you can set depend on the |
| 39 | * medium type. See {@link Builder}'s static factory methods for specific protocols that are |
| 40 | * supported. |
| 41 | * |
| 42 | * You can also set {@link Builder#setSingleDevice single device} to request a popup with single |
| 43 | * device to be shown instead of a list to choose from |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 44 | */ |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 45 | public final class AssociationRequest implements Parcelable { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 46 | |
| 47 | private final boolean mSingleDevice; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 48 | private final List<DeviceFilter<?>> mDeviceFilters; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 49 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 50 | private AssociationRequest( |
| 51 | boolean singleDevice, @Nullable List<DeviceFilter<?>> deviceFilters) { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 52 | this.mSingleDevice = singleDevice; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 53 | this.mDeviceFilters = CollectionUtils.emptyIfNull(deviceFilters); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private AssociationRequest(Parcel in) { |
| 57 | this( |
| 58 | in.readByte() != 0, |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 59 | in.readParcelableList(new ArrayList<>(), AssociationRequest.class.getClassLoader())); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** @hide */ |
Mathew Inwood | 70e89d5 | 2018-08-09 15:27:52 +0100 | [diff] [blame] | 63 | @UnsupportedAppUsage |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 64 | public boolean isSingleDevice() { |
| 65 | return mSingleDevice; |
| 66 | } |
| 67 | |
| 68 | /** @hide */ |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 69 | @NonNull |
Mathew Inwood | 70e89d5 | 2018-08-09 15:27:52 +0100 | [diff] [blame] | 70 | @UnsupportedAppUsage |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 71 | public List<DeviceFilter<?>> getDeviceFilters() { |
| 72 | return mDeviceFilters; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 76 | public boolean equals(Object o) { |
| 77 | if (this == o) return true; |
| 78 | if (o == null || getClass() != o.getClass()) return false; |
| 79 | AssociationRequest that = (AssociationRequest) o; |
| 80 | return mSingleDevice == that.mSingleDevice && |
| 81 | Objects.equals(mDeviceFilters, that.mDeviceFilters); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return Objects.hash(mSingleDevice, mDeviceFilters); |
| 87 | } |
| 88 | |
| 89 | @Override |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 90 | public String toString() { |
| 91 | return "AssociationRequest{" + |
| 92 | "mSingleDevice=" + mSingleDevice + |
| 93 | ", mDeviceFilters=" + mDeviceFilters + |
| 94 | '}'; |
| 95 | } |
| 96 | |
| 97 | @Override |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 98 | public void writeToParcel(Parcel dest, int flags) { |
| 99 | dest.writeByte((byte) (mSingleDevice ? 1 : 0)); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 100 | dest.writeParcelableList(mDeviceFilters, flags); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int describeContents() { |
| 105 | return 0; |
| 106 | } |
| 107 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 108 | public static final @android.annotation.NonNull Creator<AssociationRequest> CREATOR = new Creator<AssociationRequest>() { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 109 | @Override |
| 110 | public AssociationRequest createFromParcel(Parcel in) { |
| 111 | return new AssociationRequest(in); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public AssociationRequest[] newArray(int size) { |
| 116 | return new AssociationRequest[size]; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * A builder for {@link AssociationRequest} |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 122 | */ |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 123 | public static final class Builder extends OneTimeUseBuilder<AssociationRequest> { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 124 | private boolean mSingleDevice = false; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 125 | @Nullable private ArrayList<DeviceFilter<?>> mDeviceFilters = null; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 126 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 127 | public Builder() {} |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * @param singleDevice if true, scanning for a device will stop as soon as at least one |
| 131 | * fitting device is found |
| 132 | */ |
| 133 | @NonNull |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 134 | public Builder setSingleDevice(boolean singleDevice) { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 135 | checkNotUsed(); |
| 136 | this.mSingleDevice = singleDevice; |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param deviceFilter if set, only devices matching the given filter will be shown to the |
| 142 | * user |
| 143 | */ |
| 144 | @NonNull |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 145 | public Builder addDeviceFilter(@Nullable DeviceFilter<?> deviceFilter) { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 146 | checkNotUsed(); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 147 | if (deviceFilter != null) { |
| 148 | mDeviceFilters = ArrayUtils.add(mDeviceFilters, deviceFilter); |
| 149 | } |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 150 | return this; |
| 151 | } |
| 152 | |
| 153 | /** @inheritDoc */ |
| 154 | @NonNull |
| 155 | @Override |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 156 | public AssociationRequest build() { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 157 | markUsed(); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 158 | return new AssociationRequest(mSingleDevice, mDeviceFilters); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |