Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [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 | */ |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 16 | package android.service.autofill.augmented; |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 17 | |
| 18 | import android.annotation.IntDef; |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.SystemApi; |
Felipe Leme | 2cab38c | 2019-01-11 10:39:14 -0800 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 23 | import android.graphics.Rect; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 24 | import android.service.autofill.augmented.AugmentedAutofillService.AutofillProxy; |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 25 | import android.util.DebugUtils; |
| 26 | import android.view.View; |
| 27 | |
| 28 | import java.io.PrintWriter; |
| 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | |
| 32 | /** |
| 33 | * Abstraction of a "Smart Suggestion" component responsible to embed the autofill UI provided by |
| 34 | * the intelligence service. |
| 35 | * |
| 36 | * <p>The Smart Suggestion can embed the autofill UI in 3 distinct places: |
| 37 | * |
| 38 | * <ul> |
| 39 | * <li>A small area associated with suggestions (like a small strip in the top of the IME), |
| 40 | * returned by {@link #getSuggestionArea()} |
| 41 | * <li>The full area (like the full IME window), returned by {@link #getFullArea()} |
| 42 | * <li>A subset of the aforementioned areas, returned by {@link Area#getSubArea(Rect)} |
| 43 | * </ul> |
| 44 | * |
| 45 | * <p>The Smart Suggestion is represented by a {@link Area} object that contains the |
| 46 | * dimensions the smart suggestion window, so the service can use it to calculate the size of the |
| 47 | * view that will be passed to {@link FillWindow#update(Area, View, long)}. |
| 48 | * |
| 49 | * @hide |
| 50 | */ |
| 51 | @SystemApi |
Felipe Leme | 2cab38c | 2019-01-11 10:39:14 -0800 | [diff] [blame] | 52 | @TestApi |
| 53 | //TODO(b/122654591): @TestApi is needed because CtsAutoFillServiceTestCases hosts the service |
| 54 | //in the same package as the test, and that module is compiled with SDK=test_current |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 55 | public abstract class PresentationParams { |
| 56 | |
| 57 | /** |
| 58 | * Flag indicating the Smart Suggestion is hosted in the top of its container. |
| 59 | */ |
| 60 | public static final int FLAG_HINT_GRAVITY_TOP = 0x1; |
| 61 | |
| 62 | /** |
| 63 | * Flag indicating the Smart Suggestion is hosted in the bottom of its container. |
| 64 | */ |
| 65 | public static final int FLAG_HINT_GRAVITY_BOTTOM = 0x2; |
| 66 | |
| 67 | /** |
| 68 | * Flag indicating the Smart Suggestion is hosted in the left of its container. |
| 69 | */ |
| 70 | public static final int FLAG_HINT_GRAVITY_LEFT = 0x4; |
| 71 | |
| 72 | /** |
| 73 | * Flag indicating the Smart Suggestion is hosted in the right of its container. |
| 74 | */ |
| 75 | public static final int FLAG_HINT_GRAVITY_RIGHT = 0x8; |
| 76 | |
| 77 | /** |
| 78 | * Flag indicating the Smart Suggestion is hosted by the IME. |
| 79 | */ |
| 80 | public static final int FLAG_HOST_IME = 0x10; |
| 81 | |
| 82 | /** |
| 83 | * Flag indicating the Smart Suggestion is hosted by the Android System as a floating popup |
| 84 | * window. |
| 85 | */ |
| 86 | public static final int FLAG_HOST_SYSTEM = 0x20; |
| 87 | |
| 88 | /** @hide */ |
| 89 | @IntDef(flag = true, prefix = { "FLAG_" }, value = { |
| 90 | FLAG_HINT_GRAVITY_TOP, |
| 91 | FLAG_HINT_GRAVITY_BOTTOM, |
| 92 | FLAG_HINT_GRAVITY_LEFT, |
| 93 | FLAG_HINT_GRAVITY_RIGHT, |
| 94 | FLAG_HOST_IME, |
| 95 | FLAG_HOST_SYSTEM |
| 96 | }) |
| 97 | @Retention(RetentionPolicy.SOURCE) |
| 98 | @interface Flags {} |
| 99 | |
| 100 | |
| 101 | // /** @hide */ |
| 102 | PresentationParams() {} |
| 103 | |
| 104 | /** |
| 105 | * Gets the area of the suggestion strip for the given {@code metadata} |
| 106 | * |
| 107 | * @return strip dimensions, or {@code null} if the Smart Suggestion provider does not support |
| 108 | * suggestions strip. |
| 109 | */ |
| 110 | @Nullable |
| 111 | public Area getSuggestionArea() { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Gets the full area for the of the Smart Suggestion provider. |
| 117 | * |
| 118 | * @return full dimensions, or {@code null} if the Smart Suggestion provider does not support |
| 119 | * embeding the UI on its full area. |
| 120 | */ |
| 121 | @Nullable |
| 122 | public Area getFullArea() { |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Gets flags associated with the Smart Suggestion. |
| 128 | * |
| 129 | * @return any combination of {@link #FLAG_HINT_GRAVITY_TOP}, |
| 130 | * {@link #FLAG_HINT_GRAVITY_BOTTOM}, {@link #FLAG_HINT_GRAVITY_LEFT}, |
| 131 | * {@link #FLAG_HINT_GRAVITY_RIGHT}, {@link #FLAG_HOST_IME}, or |
| 132 | * {@link #FLAG_HOST_SYSTEM}, |
| 133 | */ |
| 134 | public @Flags int getFlags() { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /** @hide */ |
| 139 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 140 | final int flags = getFlags(); |
| 141 | if (flags > 0) { |
| 142 | pw.print(prefix); pw.print("flags: "); pw.println(flagsToString(flags)); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private static String flagsToString(int flags) { |
| 147 | return DebugUtils.flagsToString(PresentationParams.class, "FLAG_", flags); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Area associated with a {@link PresentationParams Smart Suggestions} provider. |
| 152 | * |
| 153 | * @hide |
Felipe Leme | 2cab38c | 2019-01-11 10:39:14 -0800 | [diff] [blame] | 154 | */ |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 155 | @SystemApi |
Felipe Leme | 2cab38c | 2019-01-11 10:39:14 -0800 | [diff] [blame] | 156 | @TestApi |
| 157 | //TODO(b/122654591): @TestApi is needed because CtsAutoFillServiceTestCases hosts the service |
| 158 | //in the same package as the test, and that module is compiled with SDK=test_current |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 159 | public abstract static class Area { |
| 160 | |
| 161 | /** @hide */ |
| 162 | public final AutofillProxy proxy; |
| 163 | |
| 164 | private final Rect mBounds; |
| 165 | |
| 166 | private Area(@NonNull AutofillProxy proxy, @NonNull Rect bounds) { |
| 167 | this.proxy = proxy; |
| 168 | mBounds = bounds; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Gets the area boundaries. |
| 173 | */ |
| 174 | @NonNull |
| 175 | public Rect getBounds() { |
| 176 | return mBounds; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Gets a subarea limited by given boundaries. |
| 181 | * |
| 182 | * @param bounds boundaries relative to this Area. |
| 183 | * |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 184 | * @return new subarea, or {@code null} if the Smart Suggestion host does not support such |
| 185 | * subaarea. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 186 | * |
| 187 | * @throws IllegalArgumentException if the {@code bounds} is not fully-contained inside this |
| 188 | * full Area. |
| 189 | * |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 190 | */ |
| 191 | @Nullable |
| 192 | public Area getSubArea(@NonNull Rect bounds) { |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 193 | // TODO(b/123100712): implement / check boundaries / throw IAE / add unit test |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 194 | return null; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public String toString() { |
| 199 | return mBounds.toString(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * System-provided poup window anchored to a view. |
| 205 | * |
| 206 | * <p>Used just for debugging purposes. |
| 207 | * |
| 208 | * @hide |
| 209 | */ |
| 210 | public static final class SystemPopupPresentationParams extends PresentationParams { |
| 211 | private final Area mSuggestionArea; |
| 212 | |
| 213 | public SystemPopupPresentationParams(@NonNull AutofillProxy proxy, @NonNull Rect rect) { |
| 214 | mSuggestionArea = new Area(proxy, rect) {}; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public Area getSuggestionArea() { |
| 219 | return mSuggestionArea; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public int getFlags() { |
| 224 | return FLAG_HOST_SYSTEM | FLAG_HINT_GRAVITY_BOTTOM; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 229 | super.dump(prefix, pw); |
| 230 | pw.print(prefix); pw.print("area: "); pw.println(mSuggestionArea); |
| 231 | } |
| 232 | } |
| 233 | } |