blob: a90ee93d985229e2dfc9bc7ba0f173206fcb6c12 [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;
Felipe Leme640f30a2017-03-06 15:44:06 -080028import 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
Felipe Leme640f30a2017-03-06 15:44:06 -080037/**
38 * Top-level service of the current autofill service for a given user.
39 *
40 * <p>Apps providing autofill capabilities must extend this service.
41 */
42public abstract class AutofillService extends Service {
43 private static final String TAG = "AutofillService";
44
45 /**
46 * The {@link Intent} that must be declared as handled by the service.
47 * To be supported, the service must also require the
Felipe Lemedecd8872017-04-26 17:42:38 -070048 * {@link android.Manifest.permission#BIND_AUTOFILL_SERVICE} permission so
Felipe Leme640f30a2017-03-06 15:44:06 -080049 * that other applications can not abuse it.
50 */
51 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
52 public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillService";
53
54 /**
55 * Name under which a AutoFillService component publishes information about itself.
56 * This meta-data should reference an XML resource containing a
57 * <code>&lt;{@link
Felipe Lemef78e9522017-04-04 15:07:13 -070058 * android.R.styleable#AutofillService autofill-service}&gt;</code> tag.
Felipe Leme640f30a2017-03-06 15:44:06 -080059 * This is a a sample XML file configuring an AutoFillService:
60 * <pre> &lt;autofill-service
61 * android:settingsActivity="foo.bar.SettingsActivity"
62 * . . .
63 * /&gt;</pre>
64 */
65 public static final String SERVICE_META_DATA = "android.autofill";
66
Felipe Leme640f30a2017-03-06 15:44:06 -080067 // Handler messages.
68 private static final int MSG_CONNECT = 1;
69 private static final int MSG_DISCONNECT = 2;
70 private static final int MSG_ON_FILL_REQUEST = 3;
71 private static final int MSG_ON_SAVE_REQUEST = 4;
72
73 private final IAutoFillService mInterface = new IAutoFillService.Stub() {
74 @Override
Svet Ganovf20a0372017-04-10 17:08:05 -070075 public void onConnectedStateChanged(boolean connected) {
76 if (connected) {
77 mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -080078 } else {
79 mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget();
80 }
81 }
82
83 @Override
Svet Ganov013efe12017-04-13 21:56:16 -070084 public void onFillRequest(FillRequest request, IFillCallback callback) {
Felipe Leme640f30a2017-03-06 15:44:06 -080085 ICancellationSignal transport = CancellationSignal.createTransport();
86 try {
87 callback.onCancellable(transport);
88 } catch (RemoteException e) {
89 e.rethrowFromSystemServer();
90 }
Svet Ganov013efe12017-04-13 21:56:16 -070091 mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request,
92 CancellationSignal.fromTransport(transport), callback)
Felipe Leme640f30a2017-03-06 15:44:06 -080093 .sendToTarget();
94 }
95
96 @Override
Svet Ganov013efe12017-04-13 21:56:16 -070097 public void onSaveRequest(SaveRequest request, ISaveCallback callback) {
98 mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request,
99 callback).sendToTarget();
Felipe Leme640f30a2017-03-06 15:44:06 -0800100 }
101 };
102
103 private final HandlerCaller.Callback mHandlerCallback = (msg) -> {
104 switch (msg.what) {
105 case MSG_CONNECT: {
Felipe Leme640f30a2017-03-06 15:44:06 -0800106 onConnected();
107 break;
108 } case MSG_ON_FILL_REQUEST: {
109 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700110 final FillRequest request = (FillRequest) args.arg1;
Felipe Leme640f30a2017-03-06 15:44:06 -0800111 final CancellationSignal cancellation = (CancellationSignal) args.arg2;
Svet Ganov013efe12017-04-13 21:56:16 -0700112 final IFillCallback callback = (IFillCallback) args.arg3;
113 final FillCallback fillCallback = new FillCallback(callback, request.getId());
Felipe Leme640f30a2017-03-06 15:44:06 -0800114 args.recycle();
Felipe Leme73fedac2017-05-12 09:52:07 -0700115 onFillRequest(request, cancellation, fillCallback);
Felipe Leme640f30a2017-03-06 15:44:06 -0800116 break;
117 } case MSG_ON_SAVE_REQUEST: {
118 final SomeArgs args = (SomeArgs) msg.obj;
Svet Ganov013efe12017-04-13 21:56:16 -0700119 final SaveRequest request = (SaveRequest) args.arg1;
120 final ISaveCallback callback = (ISaveCallback) args.arg2;
Felipe Leme640f30a2017-03-06 15:44:06 -0800121 final SaveCallback saveCallback = new SaveCallback(callback);
122 args.recycle();
Felipe Leme73fedac2017-05-12 09:52:07 -0700123 onSaveRequest(request, saveCallback);
Felipe Leme640f30a2017-03-06 15:44:06 -0800124 break;
125 } case MSG_DISCONNECT: {
126 onDisconnected();
Felipe Leme640f30a2017-03-06 15:44:06 -0800127 break;
128 } default: {
129 Log.w(TAG, "MyCallbacks received invalid message type: " + msg);
130 }
131 }
132 };
133
134 private HandlerCaller mHandlerCaller;
135
Felipe Leme640f30a2017-03-06 15:44:06 -0800136 /**
137 * {@inheritDoc}
138 *
139 * <strong>NOTE: </strong>if overridden, it must call {@code super.onCreate()}.
140 */
Svet Ganovecfa58a2017-05-05 19:38:45 -0700141 @CallSuper
Felipe Leme640f30a2017-03-06 15:44:06 -0800142 @Override
143 public void onCreate() {
144 super.onCreate();
145 mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true);
146 }
147
148 @Override
149 public final IBinder onBind(Intent intent) {
Felipe Leme85d1c2d2017-04-21 08:56:04 -0700150 if (SERVICE_INTERFACE.equals(intent.getAction())) {
Felipe Leme640f30a2017-03-06 15:44:06 -0800151 return mInterface.asBinder();
152 }
153 Log.w(TAG, "Tried to bind to wrong intent: " + intent);
154 return null;
155 }
156
157 /**
158 * Called when the Android system connects to service.
159 *
160 * <p>You should generally do initialization here rather than in {@link #onCreate}.
161 */
162 public void onConnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800163 }
164
165 /**
166 * Called by the Android system do decide if an {@link Activity} can be autofilled by the
167 * service.
168 *
169 * <p>Service must call one of the {@link FillCallback} methods (like
170 * {@link FillCallback#onSuccess(FillResponse)}
171 * or {@link FillCallback#onFailure(CharSequence)})
172 * to notify the result of the request.
173 *
Svet Ganov013efe12017-04-13 21:56:16 -0700174 * @param request the {@link FillRequest request} to handle.
175 * See {@link FillResponse} for examples of multiple-sections requests.
176 * @param cancellationSignal signal for observing cancellation requests. The system will use
177 * this to notify you that the fill result is no longer needed and you should stop
178 * handling this fill request in order to save resources.
179 * @param callback object used to notify the result of the request.
180 */
Felipe Leme6a778492017-04-25 09:19:49 -0700181 public abstract void onFillRequest(@NonNull FillRequest request,
182 @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700183
184 /**
Felipe Leme640f30a2017-03-06 15:44:06 -0800185 * Called when user requests service to save the fields of an {@link Activity}.
186 *
187 * <p>Service must call one of the {@link SaveCallback} methods (like
188 * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)})
189 * to notify the result of the request.
190 *
Felipe Leme26675232017-06-19 09:45:48 -0700191 * <p><b>NOTE: </b>to retrieve the actual value of the field, the service should call
192 * {@link android.app.assist.AssistStructure.ViewNode#getAutofillValue()}; if it calls
193 * {@link android.app.assist.AssistStructure.ViewNode#getText()} or other methods, there is no
194 * guarantee such method will return the most recent value of the field.
195 *
Svet Ganov013efe12017-04-13 21:56:16 -0700196 * @param request the {@link SaveRequest request} to handle.
197 * See {@link FillResponse} for examples of multiple-sections requests.
198 * @param callback object used to notify the result of the request.
199 */
Felipe Leme6a778492017-04-25 09:19:49 -0700200 public abstract void onSaveRequest(@NonNull SaveRequest request,
201 @NonNull SaveCallback callback);
Svet Ganov013efe12017-04-13 21:56:16 -0700202
203 /**
Felipe Leme640f30a2017-03-06 15:44:06 -0800204 * Called when the Android system disconnects from the service.
205 *
206 * <p> At this point this service may no longer be an active {@link AutofillService}.
207 */
208 public void onDisconnected() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800209 }
210
Felipe Lemecb4dd802017-04-21 16:25:47 -0700211 /** @hide */
Svet Ganovfc4a3002017-04-11 11:40:14 -0700212 @Deprecated
Felipe Leme640f30a2017-03-06 15:44:06 -0800213 public final void disableSelf() {
Svet Ganovf20a0372017-04-10 17:08:05 -0700214 getSystemService(AutofillManager.class).disableOwnedAutofillServices();
Felipe Leme640f30a2017-03-06 15:44:06 -0800215 }
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -0700216
217 /**
218 * Returns the {@link FillEventHistory.Event events} since the last {@link FillResponse} was
219 * returned.
220 *
221 * <p>The history is not persisted over reboots.
222 *
223 * @return The history or {@code null} if there are not events.
224 */
225 @Nullable public final FillEventHistory getFillEventHistory() {
226 AutofillManager afm = getSystemService(AutofillManager.class);
227
228 if (afm == null) {
229 return null;
230 } else {
231 return afm.getFillEventHistory();
232 }
233 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800234}