blob: cb47c104ed9ffe0de490e8cf3a585cbb8fc3c23d [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
Svet Ganov013efe12017-04-13 21:56:16 -070037import java.util.List;
38
Felipe Leme640f30a2017-03-06 15:44:06 -080039/**
40 * Top-level service of the current autofill service for a given user.
41 *
42 * <p>Apps providing autofill capabilities must extend this service.
43 */
44public abstract class AutofillService extends Service {
45 private static final String TAG = "AutofillService";
46
47 /**
48 * The {@link Intent} that must be declared as handled by the service.
49 * To be supported, the service must also require the
Felipe Leme640f30a2017-03-06 15:44:06 -080050 * {@link android.Manifest.permission#BIND_AUTOFILL} permission so
51 * that other applications can not abuse it.
52 */
53 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
54 public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillService";
55
56 /**
57 * Name under which a AutoFillService component publishes information about itself.
58 * This meta-data should reference an XML resource containing a
59 * <code>&lt;{@link
Felipe Lemef78e9522017-04-04 15:07:13 -070060 * android.R.styleable#AutofillService autofill-service}&gt;</code> tag.
Felipe Leme640f30a2017-03-06 15:44:06 -080061 * This is a a sample XML file configuring an AutoFillService:
62 * <pre> &lt;autofill-service
63 * android:settingsActivity="foo.bar.SettingsActivity"
64 * . . .
65 * /&gt;</pre>
66 */
67 public static final String SERVICE_META_DATA = "android.autofill";
68
Felipe Leme640f30a2017-03-06 15:44:06 -080069 // Handler messages.
70 private static final int MSG_CONNECT = 1;
71 private static final int MSG_DISCONNECT = 2;
72 private static final int MSG_ON_FILL_REQUEST = 3;
73 private static final int MSG_ON_SAVE_REQUEST = 4;
74
75 private final IAutoFillService mInterface = new IAutoFillService.Stub() {
76 @Override
Svet Ganovf20a0372017-04-10 17:08:05 -070077 public void onConnectedStateChanged(boolean connected) {
78 if (connected) {
79 mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -080080 } else {
81 mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget();
82 }
83 }
84
85 @Override
Svet Ganov013efe12017-04-13 21:56:16 -070086 public void onFillRequest(FillRequest request, IFillCallback callback) {
Felipe Leme640f30a2017-03-06 15:44:06 -080087 ICancellationSignal transport = CancellationSignal.createTransport();
88 try {
89 callback.onCancellable(transport);
90 } catch (RemoteException e) {
91 e.rethrowFromSystemServer();
92 }
Svet Ganov013efe12017-04-13 21:56:16 -070093 mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request,
94 CancellationSignal.fromTransport(transport), callback)
Felipe Leme640f30a2017-03-06 15:44:06 -080095 .sendToTarget();
96 }
97
98 @Override
Svet Ganov013efe12017-04-13 21:56:16 -070099 public void onSaveRequest(SaveRequest request, ISaveCallback callback) {
100 mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request,
101 callback).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -0800102 }
103 };
104
105 private final HandlerCaller.Callback mHandlerCallback = (msg) -> {
106 switch (msg.what) {
107 case MSG_CONNECT: {
Felipe Leme640f30a2017-03-06 15:44:06 -0800108 onConnected();
109 break;
110 } case MSG_ON_FILL_REQUEST: {
111 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700112 final FillRequest request = (FillRequest) args.arg1;
Felipe Leme640f30a2017-03-06 15:44:06 -0800113 final CancellationSignal cancellation = (CancellationSignal) args.arg2;
Svet Ganov013efe12017-04-13 21:56:16 -0700114 final IFillCallback callback = (IFillCallback) args.arg3;
115 final FillCallback fillCallback = new FillCallback(callback, request.getId());
Felipe Leme640f30a2017-03-06 15:44:06 -0800116 args.recycle();
Felipe Leme6a778492017-04-25 09:19:49 -0700117 // TODO(b/37563972): temporary try-catch hack to support old method
118 try {
119 onFillRequest(request, cancellation, fillCallback);
120 } catch (AbstractMethodError e) {
121 onFillRequest(request.getStructure(), request.getClientState(),
122 request.getFlags(), cancellation, fillCallback);
123 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800124 break;
125 } case MSG_ON_SAVE_REQUEST: {
126 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700127 final SaveRequest request = (SaveRequest) args.arg1;
128 final ISaveCallback callback = (ISaveCallback) args.arg2;
Felipe Leme640f30a2017-03-06 15:44:06 -0800129 final SaveCallback saveCallback = new SaveCallback(callback);
130 args.recycle();
Felipe Leme6a778492017-04-25 09:19:49 -0700131 // TODO(b/37563972): temporary try-catch hack to support old method
132 try {
133 onSaveRequest(request, saveCallback);
134 } catch (AbstractMethodError e) {
135 final List<FillContext> contexts = request.getFillContexts();
136 onSaveRequest(contexts.get(contexts.size() - 1).getStructure(),
137 request.getClientState(), saveCallback);
138 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800139 break;
140 } case MSG_DISCONNECT: {
141 onDisconnected();
Felipe Leme640f30a2017-03-06 15:44:06 -0800142 break;
143 } default: {
144 Log.w(TAG, "MyCallbacks received invalid message type: " + msg);
145 }
146 }
147 };
148
149 private HandlerCaller mHandlerCaller;
150
Felipe Leme640f30a2017-03-06 15:44:06 -0800151 /**
152 * {@inheritDoc}
153 *
154 * <strong>NOTE: </strong>if overridden, it must call {@code super.onCreate()}.
155 */
156 @Override
157 public void onCreate() {
158 super.onCreate();
159 mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true);
160 }
161
162 @Override
163 public final IBinder onBind(Intent intent) {
Felipe Leme85d1c2d2017-04-21 08:56:04 -0700164 if (SERVICE_INTERFACE.equals(intent.getAction())) {
Felipe Leme640f30a2017-03-06 15:44:06 -0800165 return mInterface.asBinder();
166 }
167 Log.w(TAG, "Tried to bind to wrong intent: " + intent);
168 return null;
169 }
170
171 /**
172 * Called when the Android system connects to service.
173 *
174 * <p>You should generally do initialization here rather than in {@link #onCreate}.
175 */
176 public void onConnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800177 }
178
179 /**
180 * Called by the Android system do decide if an {@link Activity} can be autofilled by the
181 * service.
182 *
183 * <p>Service must call one of the {@link FillCallback} methods (like
184 * {@link FillCallback#onSuccess(FillResponse)}
185 * or {@link FillCallback#onFailure(CharSequence)})
186 * to notify the result of the request.
187 *
Svet Ganov013efe12017-04-13 21:56:16 -0700188 * @param request the {@link FillRequest request} to handle.
189 * See {@link FillResponse} for examples of multiple-sections requests.
190 * @param cancellationSignal signal for observing cancellation requests. The system will use
191 * this to notify you that the fill result is no longer needed and you should stop
192 * handling this fill request in order to save resources.
193 * @param callback object used to notify the result of the request.
194 */
Felipe Leme6a778492017-04-25 09:19:49 -0700195 public abstract void onFillRequest(@NonNull FillRequest request,
196 @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700197
198 /**
199 * Called by the Android system do decide if an {@link Activity} can be autofilled by the
200 * service.
201 *
202 * <p>Service must call one of the {@link FillCallback} methods (like
203 * {@link FillCallback#onSuccess(FillResponse)}
204 * or {@link FillCallback#onFailure(CharSequence)})
205 * to notify the result of the request.
206 *
Felipe Leme640f30a2017-03-06 15:44:06 -0800207 * @param structure {@link Activity}'s view structure.
Felipe Leme52d5d3d2017-04-04 13:10:58 -0700208 * @param data bundle containing data passed by the service in a last call to
209 * {@link FillResponse.Builder#setExtras(Bundle)}, if any. This bundle allows your
210 * service to keep state between fill and save requests as well as when filling different
211 * sections of the UI as the system will try to aggressively unbind from the service to
212 * conserve resources.
213 * See {@link FillResponse} for examples of multiple-sections requests.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700214 * @param flags either {@code 0} or {@link AutofillManager#FLAG_MANUAL_REQUEST}.
Felipe Leme640f30a2017-03-06 15:44:06 -0800215 * @param cancellationSignal signal for observing cancellation requests. The system will use
216 * this to notify you that the fill result is no longer needed and you should stop
217 * handling this fill request in order to save resources.
218 * @param callback object used to notify the result of the request.
Felipe Lemecb4dd802017-04-21 16:25:47 -0700219 *
220 * @hide
Felipe Leme640f30a2017-03-06 15:44:06 -0800221 */
Svet Ganov013efe12017-04-13 21:56:16 -0700222 @Deprecated
Felipe Lemea86aee62017-04-04 18:27:49 -0700223 public abstract void onFillRequest(@NonNull AssistStructure structure, @Nullable Bundle data,
224 int flags, @NonNull CancellationSignal cancellationSignal,
225 @NonNull FillCallback callback);
Felipe Leme640f30a2017-03-06 15:44:06 -0800226
227 /**
228 * Called when user requests service to save the fields of an {@link Activity}.
229 *
230 * <p>Service must call one of the {@link SaveCallback} methods (like
231 * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)})
232 * to notify the result of the request.
233 *
Svet Ganov013efe12017-04-13 21:56:16 -0700234 * @param request the {@link SaveRequest request} to handle.
235 * See {@link FillResponse} for examples of multiple-sections requests.
236 * @param callback object used to notify the result of the request.
237 */
Felipe Leme6a778492017-04-25 09:19:49 -0700238 public abstract void onSaveRequest(@NonNull SaveRequest request,
239 @NonNull SaveCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700240
241 /**
242 * Called when user requests service to save the fields of an {@link Activity}.
243 *
244 * <p>Service must call one of the {@link SaveCallback} methods (like
245 * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)})
246 * to notify the result of the request.
247 *
Felipe Leme640f30a2017-03-06 15:44:06 -0800248 * @param structure {@link Activity}'s view structure.
Felipe Leme52d5d3d2017-04-04 13:10:58 -0700249 * @param data bundle containing data passed by the service in a last call to
250 * {@link FillResponse.Builder#setExtras(Bundle)}, if any. This bundle allows your
251 * service to keep state between fill and save requests as well as when filling different
252 * sections of the UI as the system will try to aggressively unbind from the service to
253 * conserve resources.
254 * See {@link FillResponse} for examples of multiple-sections requests.
Felipe Leme640f30a2017-03-06 15:44:06 -0800255 * @param callback object used to notify the result of the request.
Felipe Lemecb4dd802017-04-21 16:25:47 -0700256 *
257 * @hide
Felipe Leme640f30a2017-03-06 15:44:06 -0800258 */
Svet Ganov013efe12017-04-13 21:56:16 -0700259 @Deprecated
Felipe Leme640f30a2017-03-06 15:44:06 -0800260 public abstract void onSaveRequest(@NonNull AssistStructure structure, @Nullable Bundle data,
261 @NonNull SaveCallback callback);
262
263 /**
264 * Called when the Android system disconnects from the service.
265 *
266 * <p> At this point this service may no longer be an active {@link AutofillService}.
267 */
268 public void onDisconnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800269 }
270
Felipe Lemecb4dd802017-04-21 16:25:47 -0700271 /** @hide */
Svet Ganovfc4a3002017-04-11 11:40:14 -0700272 @Deprecated
Felipe Leme640f30a2017-03-06 15:44:06 -0800273 public final void disableSelf() {
Svet Ganovf20a0372017-04-10 17:08:05 -0700274 getSystemService(AutofillManager.class).disableOwnedAutofillServices();
Felipe Leme640f30a2017-03-06 15:44:06 -0800275 }
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -0700276
277 /**
278 * Returns the {@link FillEventHistory.Event events} since the last {@link FillResponse} was
279 * returned.
280 *
281 * <p>The history is not persisted over reboots.
282 *
283 * @return The history or {@code null} if there are not events.
284 */
285 @Nullable public final FillEventHistory getFillEventHistory() {
286 AutofillManager afm = getSystemService(AutofillManager.class);
287
288 if (afm == null) {
289 return null;
290 } else {
291 return afm.getFillEventHistory();
292 }
293 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800294}