| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.autofill; |
| 18 |
|
| 19 |
import static android.view.autofill.Helper.sDebug; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.util.Pair; |
| 26 |
import android.widget.RemoteViews; |
| 27 |
|
| 28 |
import com.android.internal.util.Preconditions; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.Objects; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public final class BatchUpdates implements Parcelable { |
| 49 |
|
| 50 |
private final ArrayList<Pair<Integer, InternalTransformation>> mTransformations; |
| 51 |
private final RemoteViews mUpdates; |
| 52 |
|
| 53 |
private BatchUpdates(Builder builder) { |
| 54 |
mTransformations = builder.mTransformations; |
| 55 |
mUpdates = builder.mUpdates; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
@Nullable |
| 60 |
public ArrayList<Pair<Integer, InternalTransformation>> getTransformations() { |
| 61 |
return mTransformations; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
@Nullable |
| 66 |
public RemoteViews getUpdates() { |
| 67 |
return mUpdates; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static class Builder { |
| 74 |
private RemoteViews mUpdates; |
| 75 |
|
| 76 |
private boolean mDestroyed; |
| 77 |
private ArrayList<Pair<Integer, InternalTransformation>> mTransformations; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public Builder updateTemplate(@NonNull RemoteViews updates) { |
| 97 |
throwIfDestroyed(); |
| 98 |
mUpdates = Objects.requireNonNull(updates); |
| 99 |
return this; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public Builder transformChild(int id, @NonNull Transformation transformation) { |
| 119 |
throwIfDestroyed(); |
| 120 |
Preconditions.checkArgument((transformation instanceof InternalTransformation), |
| 121 |
"not provided by Android System: %s", transformation); |
| 122 |
if (mTransformations == null) { |
| 123 |
mTransformations = new ArrayList<>(); |
| 124 |
} |
| 125 |
mTransformations.add(new Pair<>(id, (InternalTransformation) transformation)); |
| 126 |
return this; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public BatchUpdates build() { |
| 137 |
throwIfDestroyed(); |
| 138 |
Preconditions.checkState(mUpdates != null || mTransformations != null, |
| 139 |
"must call either updateTemplate() or transformChild() at least once"); |
| 140 |
mDestroyed = true; |
| 141 |
return new BatchUpdates(this); |
| 142 |
} |
| 143 |
|
| 144 |
private void throwIfDestroyed() { |
| 145 |
if (mDestroyed) { |
| 146 |
throw new IllegalStateException("Already called #build()"); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
@Override |
| 155 |
public String toString() { |
| 156 |
if (!sDebug) return super.toString(); |
| 157 |
|
| 158 |
return new StringBuilder("BatchUpdates: [") |
| 159 |
.append(", transformations=") |
| 160 |
.append(mTransformations == null ? "N/A" : mTransformations.size()) |
| 161 |
.append(", updates=").append(mUpdates) |
| 162 |
.append("]").toString(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@Override |
| 169 |
public int describeContents() { |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void writeToParcel(Parcel dest, int flags) { |
| 175 |
if (mTransformations == null) { |
| 176 |
dest.writeIntArray(null); |
| 177 |
} else { |
| 178 |
final int size = mTransformations.size(); |
| 179 |
final int[] ids = new int[size]; |
| 180 |
final InternalTransformation[] values = new InternalTransformation[size]; |
| 181 |
for (int i = 0; i < size; i++) { |
| 182 |
final Pair<Integer, InternalTransformation> pair = mTransformations.get(i); |
| 183 |
ids[i] = pair.first; |
| 184 |
values[i] = pair.second; |
| 185 |
} |
| 186 |
dest.writeIntArray(ids); |
| 187 |
dest.writeParcelableArray(values, flags); |
| 188 |
} |
| 189 |
dest.writeParcelable(mUpdates, flags); |
| 190 |
} |
| 191 |
public static final @android.annotation.NonNull Parcelable.Creator<BatchUpdates> CREATOR = |
| 192 |
new Parcelable.Creator<BatchUpdates>() { |
| 193 |
@Override |
| 194 |
public BatchUpdates createFromParcel(Parcel parcel) { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
final Builder builder = new Builder(); |
| 199 |
final int[] ids = parcel.createIntArray(); |
| 200 |
if (ids != null) { |
| 201 |
final InternalTransformation[] values = |
| 202 |
parcel.readParcelableArray(null, InternalTransformation.class); |
| 203 |
final int size = ids.length; |
| 204 |
for (int i = 0; i < size; i++) { |
| 205 |
builder.transformChild(ids[i], values[i]); |
| 206 |
} |
| 207 |
} |
| 208 |
final RemoteViews updates = parcel.readParcelable(null, android.widget.RemoteViews.class); |
| 209 |
if (updates != null) { |
| 210 |
builder.updateTemplate(updates); |
| 211 |
} |
| 212 |
return builder.build(); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public BatchUpdates[] newArray(int size) { |
| 217 |
return new BatchUpdates[size]; |
| 218 |
} |
| 219 |
}; |
| 220 |
} |