blob: 88d17ef32c1393e1ec5eab374ccb313d8c3331db [file] [log] [blame]
Felipe Leme640f30a2017-03-06 15:44:06 -08001/*
2 * Copyright (C) 2017 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.autofill;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.os.RemoteException;
21import com.android.internal.os.HandlerCaller;
22import android.annotation.SdkConstant;
23import android.app.Activity;
24import android.app.Service;
25import android.app.assist.AssistStructure;
26import android.content.Intent;
27import android.os.Bundle;
28import android.os.CancellationSignal;
29import android.os.IBinder;
30import android.os.ICancellationSignal;
31import android.os.Looper;
32import android.util.Log;
Felipe Leme2ac463e2017-03-13 14:06:25 -070033import android.view.autofill.AutofillManager;
Felipe Leme640f30a2017-03-06 15:44:06 -080034
35import com.android.internal.os.SomeArgs;
36
Philip P. Moltmann121e5262017-04-25 13:33:41 -070037import java.util.ArrayList;
Svet Ganov013efe12017-04-13 21:56:16 -070038import java.util.List;
39
Felipe Leme640f30a2017-03-06 15:44:06 -080040/**
41 * Top-level service of the current autofill service for a given user.
42 *
43 * <p>Apps providing autofill capabilities must extend this service.
44 */
45public abstract class AutofillService extends Service {
46 private static final String TAG = "AutofillService";
47
48 /**
49 * The {@link Intent} that must be declared as handled by the service.
50 * To be supported, the service must also require the
Felipe Lemedecd8872017-04-26 17:42:38 -070051 * {@link android.Manifest.permission#BIND_AUTOFILL_SERVICE} permission so
Felipe Leme640f30a2017-03-06 15:44:06 -080052 * that other applications can not abuse it.
53 */
54 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
55 public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillService";
56
57 /**
58 * Name under which a AutoFillService component publishes information about itself.
59 * This meta-data should reference an XML resource containing a
60 * <code>&lt;{@link
Felipe Lemef78e9522017-04-04 15:07:13 -070061 * android.R.styleable#AutofillService autofill-service}&gt;</code> tag.
Felipe Leme640f30a2017-03-06 15:44:06 -080062 * This is a a sample XML file configuring an AutoFillService:
63 * <pre> &lt;autofill-service
64 * android:settingsActivity="foo.bar.SettingsActivity"
65 * . . .
66 * /&gt;</pre>
67 */
68 public static final String SERVICE_META_DATA = "android.autofill";
69
Felipe Leme640f30a2017-03-06 15:44:06 -080070 // Handler messages.
71 private static final int MSG_CONNECT = 1;
72 private static final int MSG_DISCONNECT = 2;
73 private static final int MSG_ON_FILL_REQUEST = 3;
74 private static final int MSG_ON_SAVE_REQUEST = 4;
75
76 private final IAutoFillService mInterface = new IAutoFillService.Stub() {
77 @Override
Svet Ganovf20a0372017-04-10 17:08:05 -070078 public void onConnectedStateChanged(boolean connected) {
79 if (connected) {
80 mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -080081 } else {
82 mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget();
83 }
84 }
85
86 @Override
Svet Ganov013efe12017-04-13 21:56:16 -070087 public void onFillRequest(FillRequest request, IFillCallback callback) {
Felipe Leme640f30a2017-03-06 15:44:06 -080088 ICancellationSignal transport = CancellationSignal.createTransport();
89 try {
90 callback.onCancellable(transport);
91 } catch (RemoteException e) {
92 e.rethrowFromSystemServer();
93 }
Svet Ganov013efe12017-04-13 21:56:16 -070094 mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request,
95 CancellationSignal.fromTransport(transport), callback)
Felipe Leme640f30a2017-03-06 15:44:06 -080096 .sendToTarget();
97 }
98
99 @Override
Svet Ganov013efe12017-04-13 21:56:16 -0700100 public void onSaveRequest(SaveRequest request, ISaveCallback callback) {
101 mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request,
102 callback).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -0800103 }
104 };
105
106 private final HandlerCaller.Callback mHandlerCallback = (msg) -> {
107 switch (msg.what) {
108 case MSG_CONNECT: {
Felipe Leme640f30a2017-03-06 15:44:06 -0800109 onConnected();
110 break;
111 } case MSG_ON_FILL_REQUEST: {
112 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700113 final FillRequest request = (FillRequest) args.arg1;
Felipe Leme640f30a2017-03-06 15:44:06 -0800114 final CancellationSignal cancellation = (CancellationSignal) args.arg2;
Svet Ganov013efe12017-04-13 21:56:16 -0700115 final IFillCallback callback = (IFillCallback) args.arg3;
116 final FillCallback fillCallback = new FillCallback(callback, request.getId());
Felipe Leme640f30a2017-03-06 15:44:06 -0800117 args.recycle();
Felipe Leme6a778492017-04-25 09:19:49 -0700118 // TODO(b/37563972): temporary try-catch hack to support old method
119 try {
120 onFillRequest(request, cancellation, fillCallback);
121 } catch (AbstractMethodError e) {
Jeff Sharkey000ce802017-04-29 13:13:27 -0600122 final List<FillContext> contexts = request.getFillContexts();
Philip P. Moltmann121e5262017-04-25 13:33:41 -0700123 onFillRequest(contexts.get(contexts.size() - 1).getStructure(),
124 request.getClientState(), request.getFlags(), cancellation,
125 fillCallback);
Felipe Leme6a778492017-04-25 09:19:49 -0700126 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800127 break;
128 } case MSG_ON_SAVE_REQUEST: {
129 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700130 final SaveRequest request = (SaveRequest) args.arg1;
131 final ISaveCallback callback = (ISaveCallback) args.arg2;
Felipe Leme640f30a2017-03-06 15:44:06 -0800132 final SaveCallback saveCallback = new SaveCallback(callback);
133 args.recycle();
Felipe Leme6a778492017-04-25 09:19:49 -0700134 // TODO(b/37563972): temporary try-catch hack to support old method
135 try {
136 onSaveRequest(request, saveCallback);
137 } catch (AbstractMethodError e) {
138 final List<FillContext> contexts = request.getFillContexts();
139 onSaveRequest(contexts.get(contexts.size() - 1).getStructure(),
140 request.getClientState(), saveCallback);
141 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800142 break;
143 } case MSG_DISCONNECT: {
144 onDisconnected();
Felipe Leme640f30a2017-03-06 15:44:06 -0800145 break;
146 } default: {
147 Log.w(TAG, "MyCallbacks received invalid message type: " + msg);
148 }
149 }
150 };
151
152 private HandlerCaller mHandlerCaller;
153
Felipe Leme640f30a2017-03-06 15:44:06 -0800154 /**
155 * {@inheritDoc}
156 *
157 * <strong>NOTE: </strong>if overridden, it must call {@code super.onCreate()}.
158 */
159 @Override
160 public void onCreate() {
161 super.onCreate();
162 mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true);
163 }
164
165 @Override
166 public final IBinder onBind(Intent intent) {
Felipe Leme85d1c2d2017-04-21 08:56:04 -0700167 if (SERVICE_INTERFACE.equals(intent.getAction())) {
Felipe Leme640f30a2017-03-06 15:44:06 -0800168 return mInterface.asBinder();
169 }
170 Log.w(TAG, "Tried to bind to wrong intent: " + intent);
171 return null;
172 }
173
174 /**
175 * Called when the Android system connects to service.
176 *
177 * <p>You should generally do initialization here rather than in {@link #onCreate}.
178 */
179 public void onConnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800180 }
181
182 /**
183 * Called by the Android system do decide if an {@link Activity} can be autofilled by the
184 * service.
185 *
186 * <p>Service must call one of the {@link FillCallback} methods (like
187 * {@link FillCallback#onSuccess(FillResponse)}
188 * or {@link FillCallback#onFailure(CharSequence)})
189 * to notify the result of the request.
190 *
Svet Ganov013efe12017-04-13 21:56:16 -0700191 * @param request the {@link FillRequest request} to handle.
192 * See {@link FillResponse} for examples of multiple-sections requests.
193 * @param cancellationSignal signal for observing cancellation requests. The system will use
194 * this to notify you that the fill result is no longer needed and you should stop
195 * handling this fill request in order to save resources.
196 * @param callback object used to notify the result of the request.
197 */
Felipe Leme6a778492017-04-25 09:19:49 -0700198 public abstract void onFillRequest(@NonNull FillRequest request,
199 @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700200
201 /**
202 * Called by the Android system do decide if an {@link Activity} can be autofilled by the
203 * service.
204 *
205 * <p>Service must call one of the {@link FillCallback} methods (like
206 * {@link FillCallback#onSuccess(FillResponse)}
207 * or {@link FillCallback#onFailure(CharSequence)})
208 * to notify the result of the request.
209 *
Felipe Leme640f30a2017-03-06 15:44:06 -0800210 * @param structure {@link Activity}'s view structure.
Felipe Leme52d5d3d2017-04-04 13:10:58 -0700211 * @param data bundle containing data passed by the service in a last call to
212 * {@link FillResponse.Builder#setExtras(Bundle)}, if any. This bundle allows your
213 * service to keep state between fill and save requests as well as when filling different
214 * sections of the UI as the system will try to aggressively unbind from the service to
215 * conserve resources.
216 * See {@link FillResponse} for examples of multiple-sections requests.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700217 * @param flags either {@code 0} or {@link AutofillManager#FLAG_MANUAL_REQUEST}.
Felipe Leme640f30a2017-03-06 15:44:06 -0800218 * @param cancellationSignal signal for observing cancellation requests. The system will use
219 * this to notify you that the fill result is no longer needed and you should stop
220 * handling this fill request in order to save resources.
221 * @param callback object used to notify the result of the request.
Felipe Lemecb4dd802017-04-21 16:25:47 -0700222 *
223 * @hide
Felipe Leme640f30a2017-03-06 15:44:06 -0800224 */
Svet Ganov013efe12017-04-13 21:56:16 -0700225 @Deprecated
Svetoslav Ganovdc6cccb2017-04-24 18:11:27 -0700226 public void onFillRequest(@NonNull AssistStructure structure, @Nullable Bundle data,
Felipe Lemea86aee62017-04-04 18:27:49 -0700227 int flags, @NonNull CancellationSignal cancellationSignal,
Svetoslav Ganovdc6cccb2017-04-24 18:11:27 -0700228 @NonNull FillCallback callback) {
229
230 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800231
232 /**
233 * Called when user requests service to save the fields of an {@link Activity}.
234 *
235 * <p>Service must call one of the {@link SaveCallback} methods (like
236 * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)})
237 * to notify the result of the request.
238 *
Svet Ganov013efe12017-04-13 21:56:16 -0700239 * @param request the {@link SaveRequest request} to handle.
240 * See {@link FillResponse} for examples of multiple-sections requests.
241 * @param callback object used to notify the result of the request.
242 */
Felipe Leme6a778492017-04-25 09:19:49 -0700243 public abstract void onSaveRequest(@NonNull SaveRequest request,
244 @NonNull SaveCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700245
246 /**
247 * Called when user requests service to save the fields of an {@link Activity}.
248 *
249 * <p>Service must call one of the {@link SaveCallback} methods (like
250 * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)})
251 * to notify the result of the request.
252 *
Felipe Leme640f30a2017-03-06 15:44:06 -0800253 * @param structure {@link Activity}'s view structure.
Felipe Leme52d5d3d2017-04-04 13:10:58 -0700254 * @param data bundle containing data passed by the service in a last call to
255 * {@link FillResponse.Builder#setExtras(Bundle)}, if any. This bundle allows your
256 * service to keep state between fill and save requests as well as when filling different
257 * sections of the UI as the system will try to aggressively unbind from the service to
258 * conserve resources.
259 * See {@link FillResponse} for examples of multiple-sections requests.
Felipe Leme640f30a2017-03-06 15:44:06 -0800260 * @param callback object used to notify the result of the request.
Felipe Lemecb4dd802017-04-21 16:25:47 -0700261 *
262 * @hide
Felipe Leme640f30a2017-03-06 15:44:06 -0800263 */
Svet Ganov013efe12017-04-13 21:56:16 -0700264 @Deprecated
Svetoslav Ganovdc6cccb2017-04-24 18:11:27 -0700265 public void onSaveRequest(@NonNull AssistStructure structure, @Nullable Bundle data,
266 @NonNull SaveCallback callback) {
267
268 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800269
270 /**
271 * Called when the Android system disconnects from the service.
272 *
273 * <p> At this point this service may no longer be an active {@link AutofillService}.
274 */
275 public void onDisconnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800276 }
277
Felipe Lemecb4dd802017-04-21 16:25:47 -0700278 /** @hide */
Svet Ganovfc4a3002017-04-11 11:40:14 -0700279 @Deprecated
Felipe Leme640f30a2017-03-06 15:44:06 -0800280 public final void disableSelf() {
Svet Ganovf20a0372017-04-10 17:08:05 -0700281 getSystemService(AutofillManager.class).disableOwnedAutofillServices();
Felipe Leme640f30a2017-03-06 15:44:06 -0800282 }
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -0700283
284 /**
285 * Returns the {@link FillEventHistory.Event events} since the last {@link FillResponse} was
286 * returned.
287 *
288 * <p>The history is not persisted over reboots.
289 *
290 * @return The history or {@code null} if there are not events.
291 */
292 @Nullable public final FillEventHistory getFillEventHistory() {
293 AutofillManager afm = getSystemService(AutofillManager.class);
294
295 if (afm == null) {
296 return null;
297 } else {
298 return afm.getFillEventHistory();
299 }
300 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800301}