Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 18 | import android.annotation.IntDef; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 20 | import android.app.Activity; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 21 | import android.app.Service; |
| 22 | import android.app.assist.AssistStructure; |
| 23 | import android.content.Intent; |
| 24 | import android.os.Bundle; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 25 | import android.os.CancellationSignal; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.Looper; |
| 28 | import android.os.Message; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 29 | import android.os.RemoteException; |
| 30 | import android.service.voice.VoiceInteractionSession; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | |
| 33 | import com.android.internal.os.HandlerCaller; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 34 | import com.android.internal.os.IResultReceiver; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 35 | import com.android.internal.os.SomeArgs; |
| 36 | |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 37 | import java.lang.annotation.Retention; |
| 38 | import java.lang.annotation.RetentionPolicy; |
| 39 | |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Top-level service of the current auto-fill service for a given user. |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 42 | * |
| 43 | * <p>Apps providing auto-fill capabilities must extend this service. |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 44 | */ |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 45 | public abstract class AutoFillService extends Service { |
| 46 | |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 47 | static final String TAG = "AutoFillService"; |
| 48 | static final boolean DEBUG = true; // TODO: set to false once stable |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * The {@link Intent} that must be declared as handled by the service. |
| 52 | * To be supported, the service must also require the |
| 53 | * {@link android.Manifest.permission#BIND_AUTO_FILL} permission so |
| 54 | * that other applications can not abuse it. |
| 55 | */ |
| 56 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 57 | public static final String SERVICE_INTERFACE = "android.service.autofill.AutoFillService"; |
| 58 | |
| 59 | private static final int MSG_READY = 1; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 60 | private static final int MSG_AUTO_FILL = 2; |
| 61 | private static final int MSG_SHUTDOWN = 3; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 62 | |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 63 | private final IResultReceiver mAssistReceiver = new IResultReceiver.Stub() { |
| 64 | @Override |
| 65 | public void send(int resultCode, Bundle resultData) throws RemoteException { |
| 66 | final AssistStructure structure = resultData |
| 67 | .getParcelable(VoiceInteractionSession.KEY_STRUCTURE); |
| 68 | |
| 69 | final IBinder binder = resultData |
| 70 | .getBinder(VoiceInteractionSession.KEY_AUTO_FILL_CALLBACK); |
| 71 | |
| 72 | mHandlerCaller |
| 73 | .obtainMessageOO(MSG_AUTO_FILL, structure, binder).sendToTarget(); |
| 74 | } |
| 75 | |
| 76 | }; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 77 | |
| 78 | private final IAutoFillService mInterface = new IAutoFillService.Stub() { |
| 79 | @Override |
| 80 | public void ready() { |
| 81 | mHandlerCaller.sendMessage(mHandlerCaller.obtainMessage(MSG_READY)); |
| 82 | } |
| 83 | |
| 84 | @Override |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 85 | public IResultReceiver getAssistReceiver() { |
| 86 | return mAssistReceiver; |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void shutdown() { |
| 91 | mHandlerCaller.sendMessage(mHandlerCaller.obtainMessage(MSG_SHUTDOWN)); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | private final HandlerCaller.Callback mHandlerCallback = new HandlerCaller.Callback() { |
| 96 | |
| 97 | @Override |
| 98 | public void executeMessage(Message msg) { |
| 99 | switch (msg.what) { |
| 100 | case MSG_READY: { |
| 101 | onReady(); |
| 102 | break; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 103 | } case MSG_AUTO_FILL: { |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 104 | final SomeArgs args = (SomeArgs) msg.obj; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 105 | final AssistStructure structure = (AssistStructure) args.arg1; |
| 106 | final IBinder binder = (IBinder) args.arg2; |
| 107 | autoFillActivity(structure, binder); |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 108 | break; |
| 109 | } case MSG_SHUTDOWN: { |
| 110 | onShutdown(); |
| 111 | break; |
| 112 | } default: { |
| 113 | Log.w(TAG, "MyCallbacks received invalid message type: " + msg); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | private HandlerCaller mHandlerCaller; |
| 120 | |
| 121 | @Override |
| 122 | public void onCreate() { |
| 123 | super.onCreate(); |
| 124 | |
| 125 | mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public final IBinder onBind(Intent intent) { |
| 130 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
| 131 | return mInterface.asBinder(); |
| 132 | } |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 133 | Log.w(TAG, "Tried to bind to wrong intent: " + intent); |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 134 | return null; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Called during service initialization to tell you when the system is ready |
| 139 | * to receive interaction from it. |
| 140 | * |
| 141 | * <p>You should generally do initialization here rather than in {@link #onCreate}. |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 142 | */ |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 143 | // TODO: rename to onConnect() / update javadoc |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 144 | public void onReady() { |
| 145 | if (DEBUG) Log.d(TAG, "onReady()"); |
| 146 | } |
| 147 | |
| 148 | /** |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 149 | * Handles an auto-fill request. |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 150 | * |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 151 | * @param structure {@link Activity}'s view structure . |
| 152 | * @param cancellationSignal signal for observing cancel requests. |
| 153 | * @param callback object used to fulllfill the request. |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 154 | */ |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 155 | public abstract void onFillRequest(AssistStructure structure, |
| 156 | CancellationSignal cancellationSignal, FillCallback callback); |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 157 | |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 158 | private void autoFillActivity(AssistStructure structure, IBinder binder) { |
| 159 | final FillCallback callback = new FillCallback(binder); |
| 160 | // TODO: hook up the cancelationSignal |
| 161 | onFillRequest(structure, new CancellationSignal(), callback); |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Called during service de-initialization to tell you when the system is shutting the |
| 166 | * service down. |
| 167 | * |
| 168 | * <p> At this point this service may no longer be an active {@link AutoFillService}. |
| 169 | */ |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 170 | // TODO: rename to onDisconnected() / update javadoc |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 171 | public void onShutdown() { |
| 172 | if (DEBUG) Log.d(TAG, "onShutdown()"); |
| 173 | } |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame^] | 174 | |
Felipe Leme | 5381aa4 | 2016-10-13 09:02:32 -0700 | [diff] [blame] | 175 | } |