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