blob: 9df315b7deabaaaef9ae815d6ca2c0cd5321e3bd [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
Svet Ganovecfa58a2017-05-05 19:38:45 -070018import android.annotation.CallSuper;
Felipe Leme640f30a2017-03-06 15:44:06 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.os.RemoteException;
22import com.android.internal.os.HandlerCaller;
23import android.annotation.SdkConstant;
24import android.app.Activity;
25import android.app.Service;
26import android.app.assist.AssistStructure;
27import android.content.Intent;
28import android.os.Bundle;
29import android.os.CancellationSignal;
30import android.os.IBinder;
31import android.os.ICancellationSignal;
32import android.os.Looper;
33import android.util.Log;
Felipe Leme2ac463e2017-03-13 14:06:25 -070034import android.view.autofill.AutofillManager;
Felipe Leme640f30a2017-03-06 15:44:06 -080035
36import com.android.internal.os.SomeArgs;
37
Philip P. Moltmann121e5262017-04-25 13:33:41 -070038import java.util.ArrayList;
Svet Ganov013efe12017-04-13 21:56:16 -070039import java.util.List;
40
Felipe Leme640f30a2017-03-06 15:44:06 -080041/**
42 * Top-level service of the current autofill service for a given user.
43 *
44 * <p>Apps providing autofill capabilities must extend this service.
45 */
46public abstract class AutofillService extends Service {
47 private static final String TAG = "AutofillService";
48
49 /**
50 * The {@link Intent} that must be declared as handled by the service.
51 * To be supported, the service must also require the
Felipe Lemedecd8872017-04-26 17:42:38 -070052 * {@link android.Manifest.permission#BIND_AUTOFILL_SERVICE} permission so
Felipe Leme640f30a2017-03-06 15:44:06 -080053 * that other applications can not abuse it.
54 */
55 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
56 public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillService";
57
58 /**
59 * Name under which a AutoFillService component publishes information about itself.
60 * This meta-data should reference an XML resource containing a
61 * <code>&lt;{@link
Felipe Lemef78e9522017-04-04 15:07:13 -070062 * android.R.styleable#AutofillService autofill-service}&gt;</code> tag.
Felipe Leme640f30a2017-03-06 15:44:06 -080063 * This is a a sample XML file configuring an AutoFillService:
64 * <pre> &lt;autofill-service
65 * android:settingsActivity="foo.bar.SettingsActivity"
66 * . . .
67 * /&gt;</pre>
68 */
69 public static final String SERVICE_META_DATA = "android.autofill";
70
Felipe Leme640f30a2017-03-06 15:44:06 -080071 // Handler messages.
72 private static final int MSG_CONNECT = 1;
73 private static final int MSG_DISCONNECT = 2;
74 private static final int MSG_ON_FILL_REQUEST = 3;
75 private static final int MSG_ON_SAVE_REQUEST = 4;
76
77 private final IAutoFillService mInterface = new IAutoFillService.Stub() {
78 @Override
Svet Ganovf20a0372017-04-10 17:08:05 -070079 public void onConnectedStateChanged(boolean connected) {
80 if (connected) {
81 mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -080082 } else {
83 mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget();
84 }
85 }
86
87 @Override
Svet Ganov013efe12017-04-13 21:56:16 -070088 public void onFillRequest(FillRequest request, IFillCallback callback) {
Felipe Leme640f30a2017-03-06 15:44:06 -080089 ICancellationSignal transport = CancellationSignal.createTransport();
90 try {
91 callback.onCancellable(transport);
92 } catch (RemoteException e) {
93 e.rethrowFromSystemServer();
94 }
Svet Ganov013efe12017-04-13 21:56:16 -070095 mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request,
96 CancellationSignal.fromTransport(transport), callback)
Felipe Leme640f30a2017-03-06 15:44:06 -080097 .sendToTarget();
98 }
99
100 @Override
Svet Ganov013efe12017-04-13 21:56:16 -0700101 public void onSaveRequest(SaveRequest request, ISaveCallback callback) {
102 mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request,
103 callback).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -0800104 }
105 };
106
107 private final HandlerCaller.Callback mHandlerCallback = (msg) -> {
108 switch (msg.what) {
109 case MSG_CONNECT: {
Felipe Leme640f30a2017-03-06 15:44:06 -0800110 onConnected();
111 break;
112 } case MSG_ON_FILL_REQUEST: {
113 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700114 final FillRequest request = (FillRequest) args.arg1;
Felipe Leme640f30a2017-03-06 15:44:06 -0800115 final CancellationSignal cancellation = (CancellationSignal) args.arg2;
Svet Ganov013efe12017-04-13 21:56:16 -0700116 final IFillCallback callback = (IFillCallback) args.arg3;
117 final FillCallback fillCallback = new FillCallback(callback, request.getId());
Felipe Leme640f30a2017-03-06 15:44:06 -0800118 args.recycle();
Felipe Leme73fedac2017-05-12 09:52:07 -0700119 onFillRequest(request, cancellation, fillCallback);
Felipe Leme640f30a2017-03-06 15:44:06 -0800120 break;
121 } case MSG_ON_SAVE_REQUEST: {
122 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700123 final SaveRequest request = (SaveRequest) args.arg1;
124 final ISaveCallback callback = (ISaveCallback) args.arg2;
Felipe Leme640f30a2017-03-06 15:44:06 -0800125 final SaveCallback saveCallback = new SaveCallback(callback);
126 args.recycle();
Felipe Leme73fedac2017-05-12 09:52:07 -0700127 onSaveRequest(request, saveCallback);
Felipe Leme640f30a2017-03-06 15:44:06 -0800128 break;
129 } case MSG_DISCONNECT: {
130 onDisconnected();
Felipe Leme640f30a2017-03-06 15:44:06 -0800131 break;
132 } default: {
133 Log.w(TAG, "MyCallbacks received invalid message type: " + msg);
134 }
135 }
136 };
137
138 private HandlerCaller mHandlerCaller;
139
Felipe Leme640f30a2017-03-06 15:44:06 -0800140 /**
141 * {@inheritDoc}
142 *
143 * <strong>NOTE: </strong>if overridden, it must call {@code super.onCreate()}.
144 */
Svet Ganovecfa58a2017-05-05 19:38:45 -0700145 @CallSuper
Felipe Leme640f30a2017-03-06 15:44:06 -0800146 @Override
147 public void onCreate() {
148 super.onCreate();
149 mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true);
150 }
151
152 @Override
153 public final IBinder onBind(Intent intent) {
Felipe Leme85d1c2d2017-04-21 08:56:04 -0700154 if (SERVICE_INTERFACE.equals(intent.getAction())) {
Felipe Leme640f30a2017-03-06 15:44:06 -0800155 return mInterface.asBinder();
156 }
157 Log.w(TAG, "Tried to bind to wrong intent: " + intent);
158 return null;
159 }
160
161 /**
162 * Called when the Android system connects to service.
163 *
164 * <p>You should generally do initialization here rather than in {@link #onCreate}.
165 */
166 public void onConnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800167 }
168
169 /**
170 * Called by the Android system do decide if an {@link Activity} can be autofilled by the
171 * service.
172 *
173 * <p>Service must call one of the {@link FillCallback} methods (like
174 * {@link FillCallback#onSuccess(FillResponse)}
175 * or {@link FillCallback#onFailure(CharSequence)})
176 * to notify the result of the request.
177 *
Svet Ganov013efe12017-04-13 21:56:16 -0700178 * @param request the {@link FillRequest request} to handle.
179 * See {@link FillResponse} for examples of multiple-sections requests.
180 * @param cancellationSignal signal for observing cancellation requests. The system will use
181 * this to notify you that the fill result is no longer needed and you should stop
182 * handling this fill request in order to save resources.
183 * @param callback object used to notify the result of the request.
184 */
Felipe Leme6a778492017-04-25 09:19:49 -0700185 public abstract void onFillRequest(@NonNull FillRequest request,
186 @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700187
188 /**
Felipe Leme640f30a2017-03-06 15:44:06 -0800189 * Called when user requests service to save the fields of an {@link Activity}.
190 *
191 * <p>Service must call one of the {@link SaveCallback} methods (like
192 * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)})
193 * to notify the result of the request.
194 *
Svet Ganov013efe12017-04-13 21:56:16 -0700195 * @param request the {@link SaveRequest request} to handle.
196 * See {@link FillResponse} for examples of multiple-sections requests.
197 * @param callback object used to notify the result of the request.
198 */
Felipe Leme6a778492017-04-25 09:19:49 -0700199 public abstract void onSaveRequest(@NonNull SaveRequest request,
200 @NonNull SaveCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700201
202 /**
Felipe Leme640f30a2017-03-06 15:44:06 -0800203 * Called when the Android system disconnects from the service.
204 *
205 * <p> At this point this service may no longer be an active {@link AutofillService}.
206 */
207 public void onDisconnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800208 }
209
Felipe Lemecb4dd802017-04-21 16:25:47 -0700210 /** @hide */
Svet Ganovfc4a3002017-04-11 11:40:14 -0700211 @Deprecated
Felipe Leme640f30a2017-03-06 15:44:06 -0800212 public final void disableSelf() {
Svet Ganovf20a0372017-04-10 17:08:05 -0700213 getSystemService(AutofillManager.class).disableOwnedAutofillServices();
Felipe Leme640f30a2017-03-06 15:44:06 -0800214 }
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -0700215
216 /**
217 * Returns the {@link FillEventHistory.Event events} since the last {@link FillResponse} was
218 * returned.
219 *
220 * <p>The history is not persisted over reboots.
221 *
222 * @return The history or {@code null} if there are not events.
223 */
224 @Nullable public final FillEventHistory getFillEventHistory() {
225 AutofillManager afm = getSystemService(AutofillManager.class);
226
227 if (afm == null) {
228 return null;
229 } else {
230 return afm.getFillEventHistory();
231 }
232 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800233}