Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package android.service.intelligence; |
| 17 | |
| 18 | import android.annotation.IntDef; |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.SystemApi; |
| 21 | import android.app.TaskInfo; |
| 22 | import android.content.ComponentName; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
| 27 | |
| 28 | import java.io.PrintWriter; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | |
| 32 | // TODO(b/111276913): add javadocs / implement Parcelable / implement equals/hashcode/toString |
| 33 | /** @hide */ |
| 34 | @SystemApi |
| 35 | public final class InteractionContext implements Parcelable { |
| 36 | |
| 37 | /** |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 38 | * Flag used to indicate that the app explicitly disabled content capture for the activity |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 39 | * (using |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 40 | * {@link android.view.intelligence.ContentCaptureManager#setContentCaptureEnabled()}), |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 41 | * in which case the service will just receive activity-level events. |
| 42 | */ |
| 43 | public static final int FLAG_DISABLED_BY_APP = 0x1; |
| 44 | |
| 45 | /** |
| 46 | * Flag used to indicate that the activity's window is tagged with |
| 47 | * {@link android.view.Display#FLAG_SECURE}, in which case the service will just receive |
| 48 | * activity-level events. |
| 49 | */ |
| 50 | public static final int FLAG_DISABLED_BY_FLAG_SECURE = 0x2; |
| 51 | |
| 52 | /** @hide */ |
| 53 | @IntDef(flag = true, prefix = { "FLAG_" }, value = { |
| 54 | FLAG_DISABLED_BY_APP, |
| 55 | FLAG_DISABLED_BY_FLAG_SECURE |
| 56 | }) |
| 57 | @Retention(RetentionPolicy.SOURCE) |
| 58 | @interface ContextCreationFlags{} |
| 59 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 60 | // TODO(b/111276913): create new object for taskId + componentName / reuse on other places |
| 61 | private final @NonNull ComponentName mComponentName; |
| 62 | private final int mTaskId; |
| 63 | private final int mDisplayId; |
| 64 | private final int mFlags; |
| 65 | |
| 66 | |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 67 | /** @hide */ |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 68 | public InteractionContext(@NonNull ComponentName componentName, int taskId, int displayId, |
| 69 | int flags) { |
| 70 | mComponentName = Preconditions.checkNotNull(componentName); |
| 71 | mTaskId = taskId; |
| 72 | mDisplayId = displayId; |
| 73 | mFlags = flags; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the id of the {@link TaskInfo task} associated with this context. |
| 78 | */ |
| 79 | public int getTaskId() { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 80 | return mTaskId; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets the activity associated with this context. |
| 85 | */ |
| 86 | public @NonNull ComponentName getActivityComponent() { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 87 | return mComponentName; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets the ID of the display associated with this context, as defined by |
| 92 | * {G android.hardware.display.DisplayManager#getDisplay(int) DisplayManager.getDisplay()}. |
| 93 | */ |
| 94 | public int getDisplayId() { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 95 | return mDisplayId; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets the flags associated with this context. |
| 100 | * |
| 101 | * @return any combination of {@link #FLAG_DISABLED_BY_FLAG_SECURE} and |
| 102 | * {@link #FLAG_DISABLED_BY_APP}. |
| 103 | */ |
| 104 | public @ContextCreationFlags int getFlags() { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 105 | return mFlags; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @hide |
| 110 | */ |
| 111 | // TODO(b/111276913): dump to proto as well |
| 112 | public void dump(PrintWriter pw) { |
| 113 | pw.print("comp="); pw.print(mComponentName.flattenToShortString()); |
| 114 | pw.print(", taskId="); pw.print(mTaskId); |
| 115 | pw.print(", displayId="); pw.print(mDisplayId); |
| 116 | if (mFlags > 0) { |
| 117 | pw.print(", flags="); pw.print(mFlags); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public String toString() { |
| 123 | return "Context[act=" + mComponentName.flattenToShortString() + ", taskId=" + mTaskId |
| 124 | + ", displayId=" + mDisplayId + ", flags=" + mFlags + "]"; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int describeContents() { |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void writeToParcel(Parcel parcel, int flags) { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 134 | parcel.writeParcelable(mComponentName, flags); |
| 135 | parcel.writeInt(mTaskId); |
| 136 | parcel.writeInt(mDisplayId); |
| 137 | parcel.writeInt(mFlags); |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public static final Parcelable.Creator<InteractionContext> CREATOR = |
| 141 | new Parcelable.Creator<InteractionContext>() { |
| 142 | |
| 143 | @Override |
| 144 | public InteractionContext createFromParcel(Parcel parcel) { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 145 | final ComponentName componentName = parcel.readParcelable(null); |
| 146 | final int taskId = parcel.readInt(); |
| 147 | final int displayId = parcel.readInt(); |
| 148 | final int flags = parcel.readInt(); |
| 149 | return new InteractionContext(componentName, taskId, displayId, flags); |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public InteractionContext[] newArray(int size) { |
| 154 | return new InteractionContext[size]; |
| 155 | } |
| 156 | }; |
| 157 | } |