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