| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.om; |
| 18 |
|
| 19 |
import static com.android.internal.util.Preconditions.checkNotNull; |
| 20 |
|
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.content.Context; |
| 25 |
import android.os.Bundle; |
| 26 |
import android.os.Parcel; |
| 27 |
import android.os.Parcelable; |
| 28 |
import android.os.UserHandle; |
| 29 |
|
| 30 |
import java.lang.annotation.Retention; |
| 31 |
import java.lang.annotation.RetentionPolicy; |
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Iterator; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Locale; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class OverlayManagerTransaction |
| 52 |
implements Iterable<OverlayManagerTransaction.Request>, Parcelable { |
| 53 |
|
| 54 |
|
| 55 |
private final List<Request> mRequests; |
| 56 |
|
| 57 |
OverlayManagerTransaction(@NonNull final List<Request> requests) { |
| 58 |
checkNotNull(requests); |
| 59 |
if (requests.contains(null)) { |
| 60 |
throw new IllegalArgumentException("null request"); |
| 61 |
} |
| 62 |
mRequests = requests; |
| 63 |
} |
| 64 |
|
| 65 |
private OverlayManagerTransaction(@NonNull final Parcel source) { |
| 66 |
final int size = source.readInt(); |
| 67 |
mRequests = new ArrayList<>(size); |
| 68 |
for (int i = 0; i < size; i++) { |
| 69 |
final int request = source.readInt(); |
| 70 |
final OverlayIdentifier overlay = source.readParcelable(null, android.content.om.OverlayIdentifier.class); |
| 71 |
final int userId = source.readInt(); |
| 72 |
final Bundle extras = source.readBundle(null); |
| 73 |
mRequests.add(new Request(request, overlay, userId, extras)); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public Iterator<Request> iterator() { |
| 79 |
return mRequests.iterator(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public String toString() { |
| 84 |
return String.format("OverlayManagerTransaction { mRequests = %s }", mRequests); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static class Request { |
| 94 |
@IntDef(prefix = "TYPE_", value = { |
| 95 |
TYPE_SET_ENABLED, |
| 96 |
TYPE_SET_DISABLED, |
| 97 |
}) |
| 98 |
@Retention(RetentionPolicy.SOURCE) |
| 99 |
@interface RequestType {} |
| 100 |
|
| 101 |
public static final int TYPE_SET_ENABLED = 0; |
| 102 |
public static final int TYPE_SET_DISABLED = 1; |
| 103 |
public static final int TYPE_REGISTER_FABRICATED = 2; |
| 104 |
public static final int TYPE_UNREGISTER_FABRICATED = 3; |
| 105 |
|
| 106 |
public static final String BUNDLE_FABRICATED_OVERLAY = "fabricated_overlay"; |
| 107 |
|
| 108 |
@RequestType |
| 109 |
public final int type; |
| 110 |
@NonNull |
| 111 |
public final OverlayIdentifier overlay; |
| 112 |
public final int userId; |
| 113 |
@Nullable |
| 114 |
public final Bundle extras; |
| 115 |
|
| 116 |
public Request(@RequestType final int type, @NonNull final OverlayIdentifier overlay, |
| 117 |
final int userId) { |
| 118 |
this(type, overlay, userId, null ); |
| 119 |
} |
| 120 |
|
| 121 |
public Request(@RequestType final int type, @NonNull final OverlayIdentifier overlay, |
| 122 |
final int userId, @Nullable Bundle extras) { |
| 123 |
this.type = type; |
| 124 |
this.overlay = overlay; |
| 125 |
this.userId = userId; |
| 126 |
this.extras = extras; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public String toString() { |
| 131 |
return String.format(Locale.US, "Request{type=0x%02x (%s), overlay=%s, userId=%d}", |
| 132 |
type, typeToString(), overlay, userId); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public String typeToString() { |
| 142 |
switch (type) { |
| 143 |
case TYPE_SET_ENABLED: return "TYPE_SET_ENABLED"; |
| 144 |
case TYPE_SET_DISABLED: return "TYPE_SET_DISABLED"; |
| 145 |
case TYPE_REGISTER_FABRICATED: return "TYPE_REGISTER_FABRICATED"; |
| 146 |
case TYPE_UNREGISTER_FABRICATED: return "TYPE_UNREGISTER_FABRICATED"; |
| 147 |
default: return String.format("TYPE_UNKNOWN (0x%02x)", type); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public static class Builder { |
| 158 |
private final List<Request> mRequests = new ArrayList<>(); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public Builder setEnabled(@NonNull OverlayIdentifier overlay, boolean enable) { |
| 181 |
return setEnabled(overlay, enable, UserHandle.myUserId()); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public Builder setEnabled(@NonNull OverlayIdentifier overlay, boolean enable, int userId) { |
| 188 |
checkNotNull(overlay); |
| 189 |
@Request.RequestType final int type = |
| 190 |
enable ? Request.TYPE_SET_ENABLED : Request.TYPE_SET_DISABLED; |
| 191 |
mRequests.add(new Request(type, overlay, userId)); |
| 192 |
return this; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public Builder registerFabricatedOverlay(@NonNull FabricatedOverlay overlay) { |
| 209 |
final Bundle extras = new Bundle(); |
| 210 |
extras.putParcelable(Request.BUNDLE_FABRICATED_OVERLAY, overlay.mOverlay); |
| 211 |
mRequests.add(new Request(Request.TYPE_REGISTER_FABRICATED, overlay.getIdentifier(), |
| 212 |
UserHandle.USER_ALL, extras)); |
| 213 |
return this; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public Builder unregisterFabricatedOverlay(@NonNull OverlayIdentifier overlay) { |
| 224 |
mRequests.add(new Request(Request.TYPE_UNREGISTER_FABRICATED, overlay, |
| 225 |
UserHandle.USER_ALL)); |
| 226 |
return this; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public OverlayManagerTransaction build() { |
| 237 |
return new OverlayManagerTransaction(mRequests); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public int describeContents() { |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public void writeToParcel(Parcel dest, int flags) { |
| 248 |
final int size = mRequests.size(); |
| 249 |
dest.writeInt(size); |
| 250 |
for (int i = 0; i < size; i++) { |
| 251 |
final Request req = mRequests.get(i); |
| 252 |
dest.writeInt(req.type); |
| 253 |
dest.writeParcelable(req.overlay, flags); |
| 254 |
dest.writeInt(req.userId); |
| 255 |
dest.writeBundle(req.extras); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
public static final Parcelable.Creator<OverlayManagerTransaction> CREATOR = |
| 260 |
new Parcelable.Creator<OverlayManagerTransaction>() { |
| 261 |
|
| 262 |
@Override |
| 263 |
public OverlayManagerTransaction createFromParcel(Parcel source) { |
| 264 |
return new OverlayManagerTransaction(source); |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public OverlayManagerTransaction[] newArray(int size) { |
| 269 |
return new OverlayManagerTransaction[size]; |
| 270 |
} |
| 271 |
}; |
| 272 |
} |