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