Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | */ |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 16 | package android.view.contentcapture; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 17 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureHelper.VERBOSE; |
| 19 | |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 20 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 21 | |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 24 | import android.annotation.SystemService; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 25 | import android.annotation.UiThread; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 28 | import android.os.Handler; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 29 | import android.os.IBinder; |
Felipe Leme | 34ccedf | 2019-01-17 13:42:35 -0800 | [diff] [blame] | 30 | import android.os.Looper; |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 32 | import android.util.Log; |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 33 | import android.view.contentcapture.ContentCaptureSession.FlushReason; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 34 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 35 | import com.android.internal.annotations.GuardedBy; |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 36 | import com.android.internal.os.IResultReceiver; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 37 | import com.android.internal.util.Preconditions; |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 38 | import com.android.internal.util.SyncResultReceiver; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 39 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 40 | import java.io.PrintWriter; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 41 | |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 42 | /* |
| 43 | * NOTE: all methods in this class should return right away, or do the real work in a handler |
| 44 | * thread. |
| 45 | * |
| 46 | * Hence, the only field that must be thread-safe is mEnabled, which is called at the beginning |
| 47 | * of every method. |
| 48 | */ |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 49 | /** |
| 50 | * TODO(b/111276913): add javadocs / implement |
| 51 | */ |
| 52 | @SystemService(Context.CONTENT_CAPTURE_MANAGER_SERVICE) |
| 53 | public final class ContentCaptureManager { |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 54 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 55 | private static final String TAG = ContentCaptureManager.class.getSimpleName(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 56 | |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Timeout for calls to system_server. |
| 59 | */ |
| 60 | private static final int SYNC_CALLS_TIMEOUT_MS = 5000; |
| 61 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 62 | private final Object mLock = new Object(); |
| 63 | |
| 64 | @GuardedBy("mLock") |
| 65 | private boolean mDisabled; |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 66 | |
| 67 | @NonNull |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 68 | private final Context mContext; |
| 69 | |
| 70 | @Nullable |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 71 | private final IContentCaptureManager mService; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 72 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 73 | // Flags used for starting session. |
| 74 | @GuardedBy("mLock") |
| 75 | private int mFlags; |
| 76 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 77 | // TODO(b/119220549): use UI Thread directly (as calls are one-way) or a shared thread / handler |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 78 | // held at the Application level |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 79 | @NonNull |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 80 | private final Handler mHandler; |
| 81 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 82 | @GuardedBy("mLock") |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 83 | private MainContentCaptureSession mMainSession; |
Felipe Leme | 4017b20 | 2018-12-10 12:13:31 -0800 | [diff] [blame] | 84 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 85 | /** @hide */ |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 86 | public ContentCaptureManager(@NonNull Context context, |
| 87 | @Nullable IContentCaptureManager service) { |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 88 | mContext = Preconditions.checkNotNull(context, "context cannot be null"); |
Felipe Leme | 34ccedf | 2019-01-17 13:42:35 -0800 | [diff] [blame] | 89 | if (VERBOSE) Log.v(TAG, "Constructor for " + context.getPackageName()); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 90 | |
Felipe Leme | 34ccedf | 2019-01-17 13:42:35 -0800 | [diff] [blame] | 91 | mService = service; |
| 92 | // TODO(b/119220549): we might not even need a handler, as the IPCs are oneway. But if we |
| 93 | // do, then we should optimize it to run the tests after the Choreographer finishes the most |
| 94 | // important steps of the frame. |
| 95 | mHandler = Handler.createAsync(Looper.getMainLooper()); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Felipe Leme | 7a53408 | 2018-11-05 15:03:04 -0800 | [diff] [blame] | 98 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 99 | * Gets the main session associated with the context. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 100 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 101 | * <p>By default there's just one (associated with the activity lifecycle), but apps could |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 102 | * explicitly add more using |
| 103 | * {@link ContentCaptureSession#createContentCaptureSession(ContentCaptureContext)}. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 104 | * |
| 105 | * @hide |
| 106 | */ |
| 107 | @NonNull |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 108 | @UiThread |
| 109 | public MainContentCaptureSession getMainContentCaptureSession() { |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 110 | synchronized (mLock) { |
| 111 | if (mMainSession == null) { |
| 112 | mMainSession = new MainContentCaptureSession(mContext, mHandler, mService, |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 113 | mDisabled); |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 114 | if (VERBOSE) { |
| 115 | Log.v(TAG, "getDefaultContentCaptureSession(): created " + mMainSession); |
| 116 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 117 | } |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 118 | return mMainSession; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 119 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** @hide */ |
| 123 | public void onActivityStarted(@NonNull IBinder applicationToken, |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 124 | @NonNull ComponentName activityComponent, int flags) { |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 125 | synchronized (mLock) { |
| 126 | mFlags |= flags; |
| 127 | getMainContentCaptureSession().start(applicationToken, activityComponent, mFlags); |
| 128 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** @hide */ |
| 132 | public void onActivityStopped() { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 133 | getMainContentCaptureSession().destroy(); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 137 | * Flushes the content of all sessions. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 138 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 139 | * <p>Typically called by {@code Activity} when it's paused / resumed. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 140 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 141 | * @hide |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 142 | */ |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 143 | public void flush(@FlushReason int reason) { |
| 144 | getMainContentCaptureSession().flush(reason); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 147 | /** |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 148 | * Returns the component name of the system service that is consuming the captured events for |
| 149 | * the current user. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 150 | */ |
| 151 | @Nullable |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 152 | public ComponentName getServiceComponentName() { |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 153 | if (!isContentCaptureEnabled()) { |
| 154 | return null; |
| 155 | } |
| 156 | // Wait for system server to return the component name. |
| 157 | final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
| 158 | mHandler.sendMessage(obtainMessage( |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame^] | 159 | ContentCaptureManager::handleGetComponentName, this, resultReceiver)); |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 160 | |
| 161 | try { |
| 162 | return resultReceiver.getParcelableResult(); |
| 163 | } catch (RemoteException e) { |
| 164 | // Unable to retrieve component name in a reasonable amount of time. |
| 165 | throw e.rethrowFromSystemServer(); |
| 166 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 170 | * Checks whether content capture is enabled for this activity. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 171 | */ |
| 172 | public boolean isContentCaptureEnabled() { |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 173 | synchronized (mLock) { |
| 174 | return mService != null && !mDisabled; |
| 175 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 179 | * Called by apps to explicitly enable or disable content capture. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 180 | * |
| 181 | * <p><b>Note: </b> this call is not persisted accross reboots, so apps should typically call |
| 182 | * it on {@link android.app.Activity#onCreate(android.os.Bundle, android.os.PersistableBundle)}. |
| 183 | */ |
Felipe Leme | 6b3a55c | 2018-11-13 17:14:03 -0800 | [diff] [blame] | 184 | public void setContentCaptureEnabled(boolean enabled) { |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 185 | synchronized (mLock) { |
| 186 | mFlags |= enabled ? 0 : ContentCaptureContext.FLAG_DISABLED_BY_APP; |
| 187 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 191 | * Called by the app to request the Content Capture service to remove user-data associated with |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 192 | * some context. |
| 193 | * |
| 194 | * @param request object specifying what user data should be removed. |
| 195 | */ |
| 196 | public void removeUserData(@NonNull UserDataRemovalRequest request) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 197 | Preconditions.checkNotNull(request); |
| 198 | |
| 199 | try { |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame^] | 200 | mService.removeUserData(request); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 201 | } catch (RemoteException e) { |
| 202 | e.rethrowFromSystemServer(); |
| 203 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 206 | /** @hide */ |
| 207 | public void dump(String prefix, PrintWriter pw) { |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 208 | synchronized (mLock) { |
| 209 | pw.print(prefix); pw.println("ContentCaptureManager"); |
| 210 | pw.print(prefix); pw.print("Disabled: "); pw.println(mDisabled); |
| 211 | pw.print(prefix); pw.print("Context: "); pw.println(mContext); |
| 212 | pw.print(prefix); pw.print("User: "); pw.println(mContext.getUserId()); |
| 213 | if (mService != null) { |
| 214 | pw.print(prefix); pw.print("Service: "); pw.println(mService); |
| 215 | } |
| 216 | pw.print(prefix); pw.print("Flags: "); pw.println(mFlags); |
| 217 | if (mMainSession != null) { |
| 218 | final String prefix2 = prefix + " "; |
| 219 | pw.print(prefix); pw.println("Main session:"); |
| 220 | mMainSession.dump(prefix2, pw); |
| 221 | } else { |
| 222 | pw.print(prefix); pw.println("No sessions"); |
| 223 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 226 | |
| 227 | |
| 228 | /** Retrieves the component name of the target content capture service through system_server. */ |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame^] | 229 | private void handleGetComponentName(@NonNull IResultReceiver resultReceiver) { |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 230 | try { |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame^] | 231 | mService.getServiceComponentName(resultReceiver); |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 232 | } catch (RemoteException e) { |
| 233 | Log.w(TAG, "Unable to retrieve service component name: " + e); |
| 234 | } |
| 235 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 236 | } |