Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.service.autofill; |
| 17 | |
Svet Ganov | ecfa58a | 2017-05-05 19:38:45 -0700 | [diff] [blame] | 18 | import android.annotation.CallSuper; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.os.RemoteException; |
| 22 | import com.android.internal.os.HandlerCaller; |
| 23 | import android.annotation.SdkConstant; |
| 24 | import android.app.Activity; |
| 25 | import android.app.Service; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 26 | import android.content.Intent; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 27 | import android.os.CancellationSignal; |
| 28 | import android.os.IBinder; |
| 29 | import android.os.ICancellationSignal; |
| 30 | import android.os.Looper; |
| 31 | import android.util.Log; |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 32 | import android.view.autofill.AutofillManager; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.os.SomeArgs; |
| 35 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Top-level service of the current autofill service for a given user. |
| 38 | * |
| 39 | * <p>Apps providing autofill capabilities must extend this service. |
| 40 | */ |
| 41 | public abstract class AutofillService extends Service { |
| 42 | private static final String TAG = "AutofillService"; |
| 43 | |
| 44 | /** |
| 45 | * The {@link Intent} that must be declared as handled by the service. |
| 46 | * To be supported, the service must also require the |
Felipe Leme | decd887 | 2017-04-26 17:42:38 -0700 | [diff] [blame] | 47 | * {@link android.Manifest.permission#BIND_AUTOFILL_SERVICE} permission so |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 48 | * that other applications can not abuse it. |
| 49 | */ |
| 50 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 51 | public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillService"; |
| 52 | |
| 53 | /** |
| 54 | * Name under which a AutoFillService component publishes information about itself. |
| 55 | * This meta-data should reference an XML resource containing a |
| 56 | * <code><{@link |
Felipe Leme | f78e952 | 2017-04-04 15:07:13 -0700 | [diff] [blame] | 57 | * android.R.styleable#AutofillService autofill-service}></code> tag. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 58 | * This is a a sample XML file configuring an AutoFillService: |
| 59 | * <pre> <autofill-service |
| 60 | * android:settingsActivity="foo.bar.SettingsActivity" |
| 61 | * . . . |
| 62 | * /></pre> |
| 63 | */ |
| 64 | public static final String SERVICE_META_DATA = "android.autofill"; |
| 65 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 66 | // Handler messages. |
| 67 | private static final int MSG_CONNECT = 1; |
| 68 | private static final int MSG_DISCONNECT = 2; |
| 69 | private static final int MSG_ON_FILL_REQUEST = 3; |
| 70 | private static final int MSG_ON_SAVE_REQUEST = 4; |
| 71 | |
| 72 | private final IAutoFillService mInterface = new IAutoFillService.Stub() { |
| 73 | @Override |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 74 | public void onConnectedStateChanged(boolean connected) { |
| 75 | if (connected) { |
| 76 | mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 77 | } else { |
| 78 | mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 83 | public void onFillRequest(FillRequest request, IFillCallback callback) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 84 | ICancellationSignal transport = CancellationSignal.createTransport(); |
| 85 | try { |
| 86 | callback.onCancellable(transport); |
| 87 | } catch (RemoteException e) { |
| 88 | e.rethrowFromSystemServer(); |
| 89 | } |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 90 | mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request, |
| 91 | CancellationSignal.fromTransport(transport), callback) |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 92 | .sendToTarget(); |
| 93 | } |
| 94 | |
| 95 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 96 | public void onSaveRequest(SaveRequest request, ISaveCallback callback) { |
| 97 | mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request, |
| 98 | callback).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 99 | } |
| 100 | }; |
| 101 | |
| 102 | private final HandlerCaller.Callback mHandlerCallback = (msg) -> { |
| 103 | switch (msg.what) { |
| 104 | case MSG_CONNECT: { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 105 | onConnected(); |
| 106 | break; |
| 107 | } case MSG_ON_FILL_REQUEST: { |
| 108 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 109 | final FillRequest request = (FillRequest) args.arg1; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 110 | final CancellationSignal cancellation = (CancellationSignal) args.arg2; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 111 | final IFillCallback callback = (IFillCallback) args.arg3; |
| 112 | final FillCallback fillCallback = new FillCallback(callback, request.getId()); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 113 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 114 | onFillRequest(request, cancellation, fillCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 115 | break; |
| 116 | } case MSG_ON_SAVE_REQUEST: { |
| 117 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 118 | final SaveRequest request = (SaveRequest) args.arg1; |
| 119 | final ISaveCallback callback = (ISaveCallback) args.arg2; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 120 | final SaveCallback saveCallback = new SaveCallback(callback); |
| 121 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 122 | onSaveRequest(request, saveCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 123 | break; |
| 124 | } case MSG_DISCONNECT: { |
| 125 | onDisconnected(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 126 | break; |
| 127 | } default: { |
| 128 | Log.w(TAG, "MyCallbacks received invalid message type: " + msg); |
| 129 | } |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | private HandlerCaller mHandlerCaller; |
| 134 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 135 | /** |
| 136 | * {@inheritDoc} |
| 137 | * |
| 138 | * <strong>NOTE: </strong>if overridden, it must call {@code super.onCreate()}. |
| 139 | */ |
Svet Ganov | ecfa58a | 2017-05-05 19:38:45 -0700 | [diff] [blame] | 140 | @CallSuper |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 141 | @Override |
| 142 | public void onCreate() { |
| 143 | super.onCreate(); |
| 144 | mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public final IBinder onBind(Intent intent) { |
Felipe Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 149 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 150 | return mInterface.asBinder(); |
| 151 | } |
| 152 | Log.w(TAG, "Tried to bind to wrong intent: " + intent); |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Called when the Android system connects to service. |
| 158 | * |
| 159 | * <p>You should generally do initialization here rather than in {@link #onCreate}. |
| 160 | */ |
| 161 | public void onConnected() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Called by the Android system do decide if an {@link Activity} can be autofilled by the |
| 166 | * service. |
| 167 | * |
| 168 | * <p>Service must call one of the {@link FillCallback} methods (like |
| 169 | * {@link FillCallback#onSuccess(FillResponse)} |
| 170 | * or {@link FillCallback#onFailure(CharSequence)}) |
| 171 | * to notify the result of the request. |
| 172 | * |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 173 | * @param request the {@link FillRequest request} to handle. |
| 174 | * See {@link FillResponse} for examples of multiple-sections requests. |
| 175 | * @param cancellationSignal signal for observing cancellation requests. The system will use |
| 176 | * this to notify you that the fill result is no longer needed and you should stop |
| 177 | * handling this fill request in order to save resources. |
| 178 | * @param callback object used to notify the result of the request. |
| 179 | */ |
Felipe Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 180 | public abstract void onFillRequest(@NonNull FillRequest request, |
| 181 | @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 184 | * Called when user requests service to save the fields of an {@link Activity}. |
| 185 | * |
| 186 | * <p>Service must call one of the {@link SaveCallback} methods (like |
| 187 | * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)}) |
| 188 | * to notify the result of the request. |
| 189 | * |
Felipe Leme | 2667523 | 2017-06-19 09:45:48 -0700 | [diff] [blame] | 190 | * <p><b>NOTE: </b>to retrieve the actual value of the field, the service should call |
| 191 | * {@link android.app.assist.AssistStructure.ViewNode#getAutofillValue()}; if it calls |
| 192 | * {@link android.app.assist.AssistStructure.ViewNode#getText()} or other methods, there is no |
| 193 | * guarantee such method will return the most recent value of the field. |
| 194 | * |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 195 | * @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 Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 199 | public abstract void onSaveRequest(@NonNull SaveRequest request, |
| 200 | @NonNull SaveCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 201 | |
| 202 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 203 | * 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 Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 210 | /** |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 211 | * Gets the events that happened after the last |
| 212 | * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback)} |
| 213 | * call. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 214 | * |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 215 | * <p>This method is typically used to keep track of previous user actions to optimize further |
| 216 | * requests. For example, the service might return email addresses in alphabetical order by |
| 217 | * default, but change that order based on the address the user picked on previous requests. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 218 | * |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 219 | * <p>The history is not persisted over reboots, and it's cleared every time the service |
| 220 | * replies to a {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} by calling |
| 221 | * {@link FillCallback#onSuccess(FillResponse)} or {@link FillCallback#onFailure(CharSequence)} |
| 222 | * (if the service doesn't call any of these methods, the history will clear out after some |
| 223 | * pre-defined time). Hence, the service should call {@link #getFillEventHistory()} before |
| 224 | * finishing the {@link FillCallback}. |
| 225 | * |
| 226 | * @return The history or {@code null} if there are no events. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 227 | */ |
| 228 | @Nullable public final FillEventHistory getFillEventHistory() { |
| 229 | AutofillManager afm = getSystemService(AutofillManager.class); |
| 230 | |
| 231 | if (afm == null) { |
| 232 | return null; |
| 233 | } else { |
| 234 | return afm.getFillEventHistory(); |
| 235 | } |
| 236 | } |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 237 | } |