| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.view.contentcapture; |
| 17 |
|
| 18 |
import static android.view.contentcapture.ContentCaptureManager.NO_SESSION_ID; |
| 19 |
|
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.SystemApi; |
| 24 |
import android.app.TaskInfo; |
| 25 |
import android.app.assist.ActivityId; |
| 26 |
import android.content.ComponentName; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.LocusId; |
| 29 |
import android.os.Bundle; |
| 30 |
import android.os.IBinder; |
| 31 |
import android.os.Parcel; |
| 32 |
import android.os.Parcelable; |
| 33 |
import android.view.Display; |
| 34 |
import android.view.View; |
| 35 |
|
| 36 |
import com.android.internal.util.Preconditions; |
| 37 |
|
| 38 |
import java.io.PrintWriter; |
| 39 |
import java.lang.annotation.Retention; |
| 40 |
import java.lang.annotation.RetentionPolicy; |
| 41 |
import java.util.Objects; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public final class ContentCaptureContext implements Parcelable { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@SystemApi |
| 64 |
public static final int FLAG_DISABLED_BY_APP = 0x1; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
@SystemApi |
| 74 |
public static final int FLAG_DISABLED_BY_FLAG_SECURE = 0x2; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@SystemApi |
| 83 |
public static final int FLAG_RECONNECTED = 0x4; |
| 84 |
|
| 85 |
|
| 86 |
@IntDef(flag = true, prefix = { "FLAG_" }, value = { |
| 87 |
FLAG_DISABLED_BY_APP, |
| 88 |
FLAG_DISABLED_BY_FLAG_SECURE, |
| 89 |
FLAG_RECONNECTED |
| 90 |
}) |
| 91 |
@Retention(RetentionPolicy.SOURCE) |
| 92 |
@interface ContextCreationFlags{} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private final boolean mHasClientContext; |
| 99 |
|
| 100 |
|
| 101 |
private final @Nullable Bundle mExtras; |
| 102 |
private final @Nullable LocusId mId; |
| 103 |
|
| 104 |
|
| 105 |
private final @Nullable ComponentName mComponentName; |
| 106 |
private final int mFlags; |
| 107 |
private final int mDisplayId; |
| 108 |
private final ActivityId mActivityId; |
| 109 |
private final IBinder mWindowToken; |
| 110 |
|
| 111 |
|
| 112 |
private int mParentSessionId = NO_SESSION_ID; |
| 113 |
|
| 114 |
|
| 115 |
public ContentCaptureContext(@Nullable ContentCaptureContext clientContext, |
| 116 |
@NonNull ActivityId activityId, @NonNull ComponentName componentName, int displayId, |
| 117 |
IBinder windowToken, int flags) { |
| 118 |
if (clientContext != null) { |
| 119 |
mHasClientContext = true; |
| 120 |
mExtras = clientContext.mExtras; |
| 121 |
mId = clientContext.mId; |
| 122 |
} else { |
| 123 |
mHasClientContext = false; |
| 124 |
mExtras = null; |
| 125 |
mId = null; |
| 126 |
} |
| 127 |
mComponentName = Objects.requireNonNull(componentName); |
| 128 |
mFlags = flags; |
| 129 |
mDisplayId = displayId; |
| 130 |
mActivityId = activityId; |
| 131 |
mWindowToken = windowToken; |
| 132 |
} |
| 133 |
|
| 134 |
private ContentCaptureContext(@NonNull Builder builder) { |
| 135 |
mHasClientContext = true; |
| 136 |
mExtras = builder.mExtras; |
| 137 |
mId = builder.mId; |
| 138 |
|
| 139 |
mComponentName = null; |
| 140 |
mFlags = 0; |
| 141 |
mDisplayId = Display.INVALID_DISPLAY; |
| 142 |
mActivityId = null; |
| 143 |
mWindowToken = null; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
public ContentCaptureContext(@Nullable ContentCaptureContext original, int extraFlags) { |
| 148 |
mHasClientContext = original.mHasClientContext; |
| 149 |
mExtras = original.mExtras; |
| 150 |
mId = original.mId; |
| 151 |
mComponentName = original.mComponentName; |
| 152 |
mFlags = original.mFlags | extraFlags; |
| 153 |
mDisplayId = original.mDisplayId; |
| 154 |
mActivityId = original.mActivityId; |
| 155 |
mWindowToken = original.mWindowToken; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
@Nullable |
| 164 |
public Bundle getExtras() { |
| 165 |
return mExtras; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
@Nullable |
| 172 |
public LocusId getLocusId() { |
| 173 |
return mId; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
@SystemApi |
| 182 |
public int getTaskId() { |
| 183 |
return mHasClientContext ? 0 : mActivityId.getTaskId(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@SystemApi |
| 192 |
public @Nullable ComponentName getActivityComponent() { |
| 193 |
return mComponentName; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@SystemApi |
| 203 |
@Nullable |
| 204 |
public ActivityId getActivityId() { |
| 205 |
return mHasClientContext ? null : mActivityId; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
@SystemApi |
| 216 |
public @Nullable ContentCaptureSessionId getParentSessionId() { |
| 217 |
return mParentSessionId == NO_SESSION_ID ? null |
| 218 |
: new ContentCaptureSessionId(mParentSessionId); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
public void setParentSessionId(int parentSessionId) { |
| 223 |
mParentSessionId = parentSessionId; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
@SystemApi |
| 233 |
public int getDisplayId() { |
| 234 |
return mDisplayId; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
@SystemApi |
| 246 |
@Nullable |
| 247 |
public IBinder getWindowToken() { |
| 248 |
return mWindowToken; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
@SystemApi |
| 260 |
public @ContextCreationFlags int getFlags() { |
| 261 |
return mFlags; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
@NonNull |
| 268 |
public static ContentCaptureContext forLocusId(@NonNull String id) { |
| 269 |
return new Builder(new LocusId(id)).build(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public static final class Builder { |
| 276 |
private Bundle mExtras; |
| 277 |
private final LocusId mId; |
| 278 |
private boolean mDestroyed; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public Builder(@NonNull LocusId id) { |
| 298 |
mId = Objects.requireNonNull(id); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
@NonNull |
| 312 |
public Builder setExtras(@NonNull Bundle extras) { |
| 313 |
mExtras = Objects.requireNonNull(extras); |
| 314 |
throwIfDestroyed(); |
| 315 |
return this; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
@NonNull |
| 326 |
public ContentCaptureContext build() { |
| 327 |
throwIfDestroyed(); |
| 328 |
mDestroyed = true; |
| 329 |
return new ContentCaptureContext(this); |
| 330 |
} |
| 331 |
|
| 332 |
private void throwIfDestroyed() { |
| 333 |
Preconditions.checkState(!mDestroyed, "Already called #build()"); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public void dump(PrintWriter pw) { |
| 342 |
if (mComponentName != null) { |
| 343 |
pw.print("activity="); pw.print(mComponentName.flattenToShortString()); |
| 344 |
} |
| 345 |
if (mId != null) { |
| 346 |
pw.print(", id="); mId.dump(pw); |
| 347 |
} |
| 348 |
pw.print(", activityId="); pw.print(mActivityId); |
| 349 |
pw.print(", displayId="); pw.print(mDisplayId); |
| 350 |
pw.print(", windowToken="); pw.print(mWindowToken); |
| 351 |
if (mParentSessionId != NO_SESSION_ID) { |
| 352 |
pw.print(", parentId="); pw.print(mParentSessionId); |
| 353 |
} |
| 354 |
if (mFlags > 0) { |
| 355 |
pw.print(", flags="); pw.print(mFlags); |
| 356 |
} |
| 357 |
if (mExtras != null) { |
| 358 |
|
| 359 |
pw.print(", hasExtras"); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
private boolean fromServer() { |
| 364 |
return mComponentName != null; |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public String toString() { |
| 369 |
final StringBuilder builder = new StringBuilder("Context["); |
| 370 |
|
| 371 |
if (fromServer()) { |
| 372 |
builder.append("act=").append(ComponentName.flattenToShortString(mComponentName)) |
| 373 |
.append(", activityId=").append(mActivityId) |
| 374 |
.append(", displayId=").append(mDisplayId) |
| 375 |
.append(", windowToken=").append(mWindowToken) |
| 376 |
.append(", flags=").append(mFlags); |
| 377 |
} else { |
| 378 |
builder.append("id=").append(mId); |
| 379 |
if (mExtras != null) { |
| 380 |
|
| 381 |
builder.append(", hasExtras"); |
| 382 |
} |
| 383 |
} |
| 384 |
if (mParentSessionId != NO_SESSION_ID) { |
| 385 |
builder.append(", parentId=").append(mParentSessionId); |
| 386 |
} |
| 387 |
return builder.append(']').toString(); |
| 388 |
} |
| 389 |
|
| 390 |
@Override |
| 391 |
public int describeContents() { |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
@Override |
| 396 |
public void writeToParcel(Parcel parcel, int flags) { |
| 397 |
parcel.writeInt(mHasClientContext ? 1 : 0); |
| 398 |
if (mHasClientContext) { |
| 399 |
parcel.writeParcelable(mId, flags); |
| 400 |
parcel.writeBundle(mExtras); |
| 401 |
} |
| 402 |
parcel.writeParcelable(mComponentName, flags); |
| 403 |
if (fromServer()) { |
| 404 |
parcel.writeInt(mDisplayId); |
| 405 |
parcel.writeStrongBinder(mWindowToken); |
| 406 |
parcel.writeInt(mFlags); |
| 407 |
mActivityId.writeToParcel(parcel, flags); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
public static final @android.annotation.NonNull Parcelable.Creator<ContentCaptureContext> CREATOR = |
| 412 |
new Parcelable.Creator<ContentCaptureContext>() { |
| 413 |
|
| 414 |
@Override |
| 415 |
@NonNull |
| 416 |
public ContentCaptureContext createFromParcel(Parcel parcel) { |
| 417 |
final boolean hasClientContext = parcel.readInt() == 1; |
| 418 |
|
| 419 |
final ContentCaptureContext clientContext; |
| 420 |
if (hasClientContext) { |
| 421 |
|
| 422 |
final LocusId id = parcel.readParcelable(null, android.content.LocusId.class); |
| 423 |
final Bundle extras = parcel.readBundle(); |
| 424 |
final Builder builder = new Builder(id); |
| 425 |
if (extras != null) builder.setExtras(extras); |
| 426 |
clientContext = new ContentCaptureContext(builder); |
| 427 |
} else { |
| 428 |
clientContext = null; |
| 429 |
} |
| 430 |
final ComponentName componentName = parcel.readParcelable(null, android.content.ComponentName.class); |
| 431 |
if (componentName == null) { |
| 432 |
|
| 433 |
return clientContext; |
| 434 |
} else { |
| 435 |
final int displayId = parcel.readInt(); |
| 436 |
final IBinder windowToken = parcel.readStrongBinder(); |
| 437 |
final int flags = parcel.readInt(); |
| 438 |
final ActivityId activityId = new ActivityId(parcel); |
| 439 |
|
| 440 |
return new ContentCaptureContext(clientContext, activityId, componentName, |
| 441 |
displayId, windowToken, flags); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
@Override |
| 446 |
@NonNull |
| 447 |
public ContentCaptureContext[] newArray(int size) { |
| 448 |
return new ContentCaptureContext[size]; |
| 449 |
} |
| 450 |
}; |
| 451 |
} |