1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.ambientcontext;
import android.annotation.NonNull;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.app.ambientcontext.AmbientContextEvent;
import android.os.Parcelable;
import com.android.internal.util.AnnotationValidations;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Represents a {@code AmbientContextEvent} detection result reported by the detection service.
*
* @hide
*/
@SystemApi
public final class AmbientContextDetectionResult implements Parcelable {
/**
* The bundle key for this class of object, used in {@code RemoteCallback#sendResult}.
*
* @hide
*/
public static final String RESULT_RESPONSE_BUNDLE_KEY =
"android.app.ambientcontext.AmbientContextDetectionResultBundleKey";
@NonNull private final List<AmbientContextEvent> mEvents;
@NonNull private final String mPackageName;
AmbientContextDetectionResult(
@NonNull List<AmbientContextEvent> events,
@NonNull String packageName) {
this.mEvents = events;
AnnotationValidations.validate(NonNull.class, null, mEvents);
this.mPackageName = packageName;
AnnotationValidations.validate(NonNull.class, null, mPackageName);
}
/**
* A list of detected event.
*/
@SuppressLint("ConcreteCollection")
public @NonNull List<AmbientContextEvent> getEvents() {
return mEvents;
}
/**
* The package to deliver the response to.
*/
public @NonNull String getPackageName() {
return mPackageName;
}
@Override
public String toString() {
return "AmbientContextEventResponse { "
+ "events = " + mEvents + ", " + "packageName = " + mPackageName + " }";
}
@Override
public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
byte flg = 0;
dest.writeByte(flg);
dest.writeParcelableList(mEvents, flags);
dest.writeString(mPackageName);
}
@Override
public int describeContents() {
return 0;
}
/** @hide */
@SuppressWarnings({"unchecked", "RedundantCast"})
AmbientContextDetectionResult(@NonNull android.os.Parcel in) {
byte flg = in.readByte();
ArrayList<AmbientContextEvent> events = new ArrayList<>();
in.readParcelableList(events, AmbientContextEvent.class.getClassLoader(),
AmbientContextEvent.class);
String packageName = in.readString();
this.mEvents = events;
AnnotationValidations.validate(
NonNull.class, null, mEvents);
this.mPackageName = packageName;
AnnotationValidations.validate(
NonNull.class, null, mPackageName);
}
public static final @NonNull Creator<AmbientContextDetectionResult> CREATOR =
new Creator<AmbientContextDetectionResult>() {
@Override
public AmbientContextDetectionResult[] newArray(int size) {
return new AmbientContextDetectionResult[size];
}
@Override
public AmbientContextDetectionResult createFromParcel(@NonNull android.os.Parcel in) {
return new AmbientContextDetectionResult(in);
}
};
/**
* A builder for {@link AmbientContextDetectionResult}
*/
@SuppressWarnings("WeakerAccess")
public static final class Builder {
private @NonNull ArrayList<AmbientContextEvent> mEvents;
private @NonNull String mPackageName;
private long mBuilderFieldsSet = 0L;
public Builder(@NonNull String packageName) {
Objects.requireNonNull(packageName);
mPackageName = packageName;
}
/**
* Adds an event to the builder.
*/
public @NonNull Builder addEvent(@NonNull AmbientContextEvent value) {
checkNotUsed();
if (mEvents == null) {
mBuilderFieldsSet |= 0x1;
mEvents = new ArrayList<>();
}
mEvents.add(value);
return this;
}
/**
* Adds a list of events to the builder.
*/
public @NonNull Builder addEvents(@NonNull List<AmbientContextEvent> values) {
checkNotUsed();
if (mEvents == null) {
mBuilderFieldsSet |= 0x1;
mEvents = new ArrayList<>();
}
mEvents.addAll(values);
return this;
}
/**
* Clears all events from the builder.
*/
public @NonNull Builder clearEvents() {
checkNotUsed();
if (mEvents != null) {
mEvents.clear();
}
return this;
}
/** Builds the instance. This builder should not be touched after calling this! */
public @NonNull AmbientContextDetectionResult build() {
checkNotUsed();
mBuilderFieldsSet |= 0x2; // Mark builder used
if ((mBuilderFieldsSet & 0x1) == 0) {
mEvents = new ArrayList<>();
}
AmbientContextDetectionResult o = new AmbientContextDetectionResult(
mEvents,
mPackageName);
return o;
}
private void checkNotUsed() {
if ((mBuilderFieldsSet & 0x2) != 0) {
throw new IllegalStateException(
"This Builder should not be reused. Use a new Builder instance instead");
}
}
}
}
|