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; |
| 26 | import android.app.assist.AssistStructure; |
| 27 | import android.content.Intent; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 28 | import android.os.CancellationSignal; |
| 29 | import android.os.IBinder; |
| 30 | import android.os.ICancellationSignal; |
| 31 | import android.os.Looper; |
| 32 | import android.util.Log; |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 33 | import android.view.autofill.AutofillManager; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 34 | |
| 35 | import com.android.internal.os.SomeArgs; |
| 36 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 37 | /** |
| 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 | */ |
| 42 | public 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 Leme | decd887 | 2017-04-26 17:42:38 -0700 | [diff] [blame] | 48 | * {@link android.Manifest.permission#BIND_AUTOFILL_SERVICE} permission so |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 49 | * 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><{@link |
Felipe Leme | f78e952 | 2017-04-04 15:07:13 -0700 | [diff] [blame] | 58 | * android.R.styleable#AutofillService autofill-service}></code> tag. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 59 | * This is a a sample XML file configuring an AutoFillService: |
| 60 | * <pre> <autofill-service |
| 61 | * android:settingsActivity="foo.bar.SettingsActivity" |
| 62 | * . . . |
| 63 | * /></pre> |
| 64 | */ |
| 65 | public static final String SERVICE_META_DATA = "android.autofill"; |
| 66 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 67 | // 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 Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 75 | public void onConnectedStateChanged(boolean connected) { |
| 76 | if (connected) { |
| 77 | mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 78 | } else { |
| 79 | mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 84 | public void onFillRequest(FillRequest request, IFillCallback callback) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 85 | ICancellationSignal transport = CancellationSignal.createTransport(); |
| 86 | try { |
| 87 | callback.onCancellable(transport); |
| 88 | } catch (RemoteException e) { |
| 89 | e.rethrowFromSystemServer(); |
| 90 | } |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 91 | mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request, |
| 92 | CancellationSignal.fromTransport(transport), callback) |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 93 | .sendToTarget(); |
| 94 | } |
| 95 | |
| 96 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 97 | public void onSaveRequest(SaveRequest request, ISaveCallback callback) { |
| 98 | mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request, |
| 99 | callback).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 100 | } |
| 101 | }; |
| 102 | |
| 103 | private final HandlerCaller.Callback mHandlerCallback = (msg) -> { |
| 104 | switch (msg.what) { |
| 105 | case MSG_CONNECT: { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 106 | onConnected(); |
| 107 | break; |
| 108 | } case MSG_ON_FILL_REQUEST: { |
| 109 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 110 | final FillRequest request = (FillRequest) args.arg1; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 111 | final CancellationSignal cancellation = (CancellationSignal) args.arg2; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 112 | final IFillCallback callback = (IFillCallback) args.arg3; |
| 113 | final FillCallback fillCallback = new FillCallback(callback, request.getId()); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 114 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 115 | onFillRequest(request, cancellation, fillCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 116 | break; |
| 117 | } case MSG_ON_SAVE_REQUEST: { |
| 118 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 119 | final SaveRequest request = (SaveRequest) args.arg1; |
| 120 | final ISaveCallback callback = (ISaveCallback) args.arg2; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 121 | final SaveCallback saveCallback = new SaveCallback(callback); |
| 122 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 123 | onSaveRequest(request, saveCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 124 | break; |
| 125 | } case MSG_DISCONNECT: { |
| 126 | onDisconnected(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 127 | break; |
| 128 | } default: { |
| 129 | Log.w(TAG, "MyCallbacks received invalid message type: " + msg); |
| 130 | } |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | private HandlerCaller mHandlerCaller; |
| 135 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 136 | /** |
| 137 | * {@inheritDoc} |
| 138 | * |
| 139 | * <strong>NOTE: </strong>if overridden, it must call {@code super.onCreate()}. |
| 140 | */ |
Svet Ganov | ecfa58a | 2017-05-05 19:38:45 -0700 | [diff] [blame] | 141 | @CallSuper |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 142 | @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 Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 150 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 151 | 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 Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 163 | } |
| 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 Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 174 | * @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 Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 181 | public abstract void onFillRequest(@NonNull FillRequest request, |
| 182 | @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 183 | |
| 184 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 185 | * 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 Leme | 2667523 | 2017-06-19 09:45:48 -0700 | [diff] [blame] | 191 | * <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 Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 196 | * @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 Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 200 | public abstract void onSaveRequest(@NonNull SaveRequest request, |
| 201 | @NonNull SaveCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 204 | * 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 Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Felipe Leme | cb4dd80 | 2017-04-21 16:25:47 -0700 | [diff] [blame] | 211 | /** @hide */ |
Svet Ganov | fc4a300 | 2017-04-11 11:40:14 -0700 | [diff] [blame] | 212 | @Deprecated |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 213 | public final void disableSelf() { |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 214 | getSystemService(AutofillManager.class).disableOwnedAutofillServices(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 215 | } |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 216 | |
| 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 Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 234 | } |