blob: db54f08d815ca95739e3ee2265743783228a95a9 [file] [log] [blame]
Eugene Susla6ed45d82017-01-22 13:52:51 -08001/*
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
17package android.companion;
18
Eugene Susla6ed45d82017-01-22 13:52:51 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
Mathew Inwood70e89d52018-08-09 15:27:52 +010021import android.annotation.UnsupportedAppUsage;
Eugene Susla6ed45d82017-01-22 13:52:51 -080022import android.os.Parcel;
23import android.os.Parcelable;
24import android.provider.OneTimeUseBuilder;
25
Eugene Susla36e866b2017-02-23 18:24:39 -080026import com.android.internal.util.ArrayUtils;
Eugene Susla6a7006a2017-03-13 12:57:58 -070027import com.android.internal.util.CollectionUtils;
Eugene Susla36e866b2017-02-23 18:24:39 -080028
29import java.util.ArrayList;
30import java.util.List;
Eugene Suslaa38fbf62017-03-14 10:26:10 -070031import java.util.Objects;
Eugene Susla6ed45d82017-01-22 13:52:51 -080032
33/**
34 * A request for the user to select a companion device to associate with.
35 *
Eugene Susla36e866b2017-02-23 18:24:39 -080036 * You can optionally set {@link Builder#addDeviceFilter filters} for which devices to show to the
Eugene Susla6ed45d82017-01-22 13:52:51 -080037 * 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 Susla6ed45d82017-01-22 13:52:51 -080044 */
Eugene Susla36e866b2017-02-23 18:24:39 -080045public final class AssociationRequest implements Parcelable {
Eugene Susla6ed45d82017-01-22 13:52:51 -080046
47 private final boolean mSingleDevice;
Eugene Susla36e866b2017-02-23 18:24:39 -080048 private final List<DeviceFilter<?>> mDeviceFilters;
Eugene Susla6ed45d82017-01-22 13:52:51 -080049
Eugene Susla36e866b2017-02-23 18:24:39 -080050 private AssociationRequest(
51 boolean singleDevice, @Nullable List<DeviceFilter<?>> deviceFilters) {
Eugene Susla6ed45d82017-01-22 13:52:51 -080052 this.mSingleDevice = singleDevice;
Eugene Susla6a7006a2017-03-13 12:57:58 -070053 this.mDeviceFilters = CollectionUtils.emptyIfNull(deviceFilters);
Eugene Susla6ed45d82017-01-22 13:52:51 -080054 }
55
56 private AssociationRequest(Parcel in) {
57 this(
58 in.readByte() != 0,
Eugene Susla36e866b2017-02-23 18:24:39 -080059 in.readParcelableList(new ArrayList<>(), AssociationRequest.class.getClassLoader()));
Eugene Susla6ed45d82017-01-22 13:52:51 -080060 }
61
62 /** @hide */
Mathew Inwood70e89d52018-08-09 15:27:52 +010063 @UnsupportedAppUsage
Eugene Susla6ed45d82017-01-22 13:52:51 -080064 public boolean isSingleDevice() {
65 return mSingleDevice;
66 }
67
68 /** @hide */
Eugene Susla36e866b2017-02-23 18:24:39 -080069 @NonNull
Mathew Inwood70e89d52018-08-09 15:27:52 +010070 @UnsupportedAppUsage
Eugene Susla36e866b2017-02-23 18:24:39 -080071 public List<DeviceFilter<?>> getDeviceFilters() {
72 return mDeviceFilters;
Eugene Susla6ed45d82017-01-22 13:52:51 -080073 }
74
75 @Override
Eugene Suslaa38fbf62017-03-14 10:26:10 -070076 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 Suslaa7717e32017-04-17 19:13:31 -070090 public String toString() {
91 return "AssociationRequest{" +
92 "mSingleDevice=" + mSingleDevice +
93 ", mDeviceFilters=" + mDeviceFilters +
94 '}';
95 }
96
97 @Override
Eugene Susla6ed45d82017-01-22 13:52:51 -080098 public void writeToParcel(Parcel dest, int flags) {
99 dest.writeByte((byte) (mSingleDevice ? 1 : 0));
Eugene Susla36e866b2017-02-23 18:24:39 -0800100 dest.writeParcelableList(mDeviceFilters, flags);
Eugene Susla6ed45d82017-01-22 13:52:51 -0800101 }
102
103 @Override
104 public int describeContents() {
105 return 0;
106 }
107
108 public static final Creator<AssociationRequest> CREATOR = new Creator<AssociationRequest>() {
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 Susla6ed45d82017-01-22 13:52:51 -0800122 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800123 public static final class Builder extends OneTimeUseBuilder<AssociationRequest> {
Eugene Susla6ed45d82017-01-22 13:52:51 -0800124 private boolean mSingleDevice = false;
Eugene Susla36e866b2017-02-23 18:24:39 -0800125 @Nullable private ArrayList<DeviceFilter<?>> mDeviceFilters = null;
Eugene Susla6ed45d82017-01-22 13:52:51 -0800126
Eugene Susla36e866b2017-02-23 18:24:39 -0800127 public Builder() {}
Eugene Susla6ed45d82017-01-22 13:52:51 -0800128
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 Susla36e866b2017-02-23 18:24:39 -0800134 public Builder setSingleDevice(boolean singleDevice) {
Eugene Susla6ed45d82017-01-22 13:52:51 -0800135 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 Susla36e866b2017-02-23 18:24:39 -0800145 public Builder addDeviceFilter(@Nullable DeviceFilter<?> deviceFilter) {
Eugene Susla6ed45d82017-01-22 13:52:51 -0800146 checkNotUsed();
Eugene Susla36e866b2017-02-23 18:24:39 -0800147 if (deviceFilter != null) {
148 mDeviceFilters = ArrayUtils.add(mDeviceFilters, deviceFilter);
149 }
Eugene Susla6ed45d82017-01-22 13:52:51 -0800150 return this;
151 }
152
153 /** @inheritDoc */
154 @NonNull
155 @Override
Eugene Susla36e866b2017-02-23 18:24:39 -0800156 public AssociationRequest build() {
Eugene Susla6ed45d82017-01-22 13:52:51 -0800157 markUsed();
Eugene Susla36e866b2017-02-23 18:24:39 -0800158 return new AssociationRequest(mSingleDevice, mDeviceFilters);
Eugene Susla6ed45d82017-01-22 13:52:51 -0800159 }
160 }
161}