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 | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureHelper.sDebug; |
| 19 | import static android.view.contentcapture.ContentCaptureHelper.sVerbose; |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 20 | |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 25 | import android.annotation.SystemService; |
Felipe Leme | 19652c0 | 2019-02-04 13:01:29 -0800 | [diff] [blame] | 26 | import android.annotation.TestApi; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 27 | import android.annotation.UiThread; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 28 | import android.content.ComponentName; |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 29 | import android.content.ContentCaptureOptions; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 32 | import android.os.IBinder; |
Felipe Leme | 34ccedf | 2019-01-17 13:42:35 -0800 | [diff] [blame] | 33 | import android.os.Looper; |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 34 | import android.os.RemoteException; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 35 | import android.util.Log; |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 36 | import android.view.contentcapture.ContentCaptureSession.FlushReason; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 37 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 38 | import com.android.internal.annotations.GuardedBy; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 39 | import com.android.internal.util.Preconditions; |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 40 | import com.android.internal.util.SyncResultReceiver; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 41 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 42 | import java.io.PrintWriter; |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 43 | import java.lang.annotation.Retention; |
| 44 | import java.lang.annotation.RetentionPolicy; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 45 | |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 46 | /** |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 47 | * TODO(b/123577059): add javadocs / mention it can be null |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 48 | */ |
| 49 | @SystemService(Context.CONTENT_CAPTURE_MANAGER_SERVICE) |
| 50 | public final class ContentCaptureManager { |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 51 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 52 | private static final String TAG = ContentCaptureManager.class.getSimpleName(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 53 | |
Felipe Leme | bb0c2a2 | 2019-01-25 17:29:29 -0800 | [diff] [blame] | 54 | /** @hide */ |
| 55 | public static final int RESULT_CODE_TRUE = 1; |
| 56 | /** @hide */ |
| 57 | public static final int RESULT_CODE_FALSE = 2; |
| 58 | /** @hide */ |
| 59 | public static final int RESULT_CODE_NOT_SERVICE = -1; |
| 60 | |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Timeout for calls to system_server. |
| 63 | */ |
| 64 | private static final int SYNC_CALLS_TIMEOUT_MS = 5000; |
| 65 | |
Felipe Leme | 14ef461 | 2019-02-07 12:24:38 -0800 | [diff] [blame] | 66 | /** |
| 67 | * DeviceConfig property used by {@code com.android.server.SystemServer} on start to decide |
| 68 | * whether the Content Capture service should be created or not |
| 69 | * |
| 70 | * <p>By default it should *NOT* be set (or set to {@code "default"}, so the decision is based |
| 71 | * on whether the OEM provides an implementation for the service), but it can be overridden to: |
| 72 | * |
| 73 | * <ul> |
| 74 | * <li>Provide a "kill switch" so OEMs can disable it remotely in case of emergency (when |
| 75 | * it's set to {@code "false"}). |
| 76 | * <li>Enable the CTS tests to be run on AOSP builds (when it's set to {@code "true"}). |
| 77 | * </ul> |
| 78 | * |
| 79 | * @hide |
| 80 | */ |
| 81 | @TestApi |
| 82 | public static final String DEVICE_CONFIG_PROPERTY_SERVICE_EXPLICITLY_ENABLED = |
| 83 | "service_explicitly_enabled"; |
| 84 | |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Maximum number of events that are buffered before sent to the app. |
| 87 | * |
| 88 | * @hide |
| 89 | */ |
| 90 | @TestApi |
| 91 | public static final String DEVICE_CONFIG_PROPERTY_MAX_BUFFER_SIZE = "max_buffer_size"; |
| 92 | |
| 93 | /** |
| 94 | * Frequency (in ms) of buffer flushes when no events are received. |
| 95 | * |
| 96 | * @hide |
| 97 | */ |
| 98 | @TestApi |
| 99 | public static final String DEVICE_CONFIG_PROPERTY_IDLE_FLUSH_FREQUENCY = "idle_flush_frequency"; |
| 100 | |
| 101 | /** |
| 102 | * Frequency (in ms) of buffer flushes when no events are received and the last one was a |
| 103 | * text change event. |
| 104 | * |
| 105 | * @hide |
| 106 | */ |
| 107 | @TestApi |
| 108 | public static final String DEVICE_CONFIG_PROPERTY_TEXT_CHANGE_FLUSH_FREQUENCY = |
| 109 | "text_change_flush_frequency"; |
| 110 | |
| 111 | /** |
| 112 | * Size of events that are logging on {@code dump}. |
| 113 | * |
| 114 | * <p>Set it to {@code 0} or less to disable history. |
| 115 | * |
| 116 | * @hide |
| 117 | */ |
| 118 | @TestApi |
| 119 | public static final String DEVICE_CONFIG_PROPERTY_LOG_HISTORY_SIZE = "log_history_size"; |
| 120 | |
| 121 | /** |
| 122 | * Sets the logging level for {@code logcat} statements. |
| 123 | * |
| 124 | * <p>Valid values are: {@link #LOGGING_LEVEL_OFF}, {@value #LOGGING_LEVEL_DEBUG}, and |
| 125 | * {@link #LOGGING_LEVEL_VERBOSE}. |
| 126 | * |
| 127 | * @hide |
| 128 | */ |
| 129 | @TestApi |
| 130 | public static final String DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL = "logging_level"; |
| 131 | |
Felipe Leme | e764fa2 | 2019-02-21 16:45:35 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Sets how long (in ms) the service is bound while idle. |
| 134 | * |
| 135 | * <p>Use {@code 0} to keep it permanently bound. |
| 136 | * |
| 137 | * @hide |
| 138 | */ |
| 139 | public static final String DEVICE_CONFIG_PROPERTY_IDLE_UNBIND_TIMEOUT = "idle_unbind_timeout"; |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 140 | |
| 141 | /** @hide */ |
| 142 | @TestApi |
| 143 | public static final int LOGGING_LEVEL_OFF = 0; |
| 144 | |
| 145 | /** @hide */ |
| 146 | @TestApi |
| 147 | public static final int LOGGING_LEVEL_DEBUG = 1; |
| 148 | |
| 149 | /** @hide */ |
| 150 | @TestApi |
| 151 | public static final int LOGGING_LEVEL_VERBOSE = 2; |
| 152 | |
| 153 | /** @hide */ |
| 154 | @IntDef(flag = false, value = { |
| 155 | LOGGING_LEVEL_OFF, |
| 156 | LOGGING_LEVEL_DEBUG, |
| 157 | LOGGING_LEVEL_VERBOSE |
| 158 | }) |
| 159 | @Retention(RetentionPolicy.SOURCE) |
| 160 | public @interface LoggingLevel {} |
| 161 | |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 162 | |
| 163 | /** @hide */ |
| 164 | public static final int DEFAULT_MAX_BUFFER_SIZE = 100; |
| 165 | /** @hide */ |
| 166 | public static final int DEFAULT_IDLE_FLUSHING_FREQUENCY_MS = 5_000; |
| 167 | /** @hide */ |
| 168 | public static final int DEFAULT_TEXT_CHANGE_FLUSHING_FREQUENCY_MS = 1_000; |
| 169 | /** @hide */ |
| 170 | public static final int DEFAULT_LOG_HISTORY_SIZE = 10; |
| 171 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 172 | private final Object mLock = new Object(); |
| 173 | |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 174 | @NonNull |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 175 | private final Context mContext; |
| 176 | |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 177 | @NonNull |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 178 | private final IContentCaptureManager mService; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 179 | |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 180 | @NonNull |
| 181 | final ContentCaptureOptions mOptions; |
| 182 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 183 | // Flags used for starting session. |
| 184 | @GuardedBy("mLock") |
| 185 | private int mFlags; |
| 186 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 187 | // 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] | 188 | // held at the Application level |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 189 | @NonNull |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 190 | private final Handler mHandler; |
| 191 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 192 | @GuardedBy("mLock") |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 193 | private MainContentCaptureSession mMainSession; |
Felipe Leme | 4017b20 | 2018-12-10 12:13:31 -0800 | [diff] [blame] | 194 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 195 | /** @hide */ |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 196 | public ContentCaptureManager(@NonNull Context context, |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 197 | @NonNull IContentCaptureManager service, @NonNull ContentCaptureOptions options) { |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 198 | mContext = Preconditions.checkNotNull(context, "context cannot be null"); |
| 199 | mService = Preconditions.checkNotNull(service, "service cannot be null"); |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 200 | mOptions = Preconditions.checkNotNull(options, "options cannot be null"); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 201 | |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 202 | ContentCaptureHelper.setLoggingLevel(mOptions.loggingLevel); |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 203 | |
| 204 | if (sVerbose) Log.v(TAG, "Constructor for " + context.getPackageName()); |
| 205 | |
Felipe Leme | 34ccedf | 2019-01-17 13:42:35 -0800 | [diff] [blame] | 206 | // TODO(b/119220549): we might not even need a handler, as the IPCs are oneway. But if we |
| 207 | // do, then we should optimize it to run the tests after the Choreographer finishes the most |
| 208 | // important steps of the frame. |
| 209 | mHandler = Handler.createAsync(Looper.getMainLooper()); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Felipe Leme | 7a53408 | 2018-11-05 15:03:04 -0800 | [diff] [blame] | 212 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 213 | * Gets the main session associated with the context. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 214 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 215 | * <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] | 216 | * explicitly add more using |
| 217 | * {@link ContentCaptureSession#createContentCaptureSession(ContentCaptureContext)}. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 218 | * |
| 219 | * @hide |
| 220 | */ |
| 221 | @NonNull |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 222 | @UiThread |
| 223 | public MainContentCaptureSession getMainContentCaptureSession() { |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 224 | synchronized (mLock) { |
| 225 | if (mMainSession == null) { |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 226 | mMainSession = new MainContentCaptureSession(mContext, this, mHandler, mService); |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 227 | if (sVerbose) Log.v(TAG, "getMainContentCaptureSession(): created " + mMainSession); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 228 | } |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 229 | return mMainSession; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 230 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** @hide */ |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 234 | @UiThread |
Felipe Leme | b0da18f | 2019-02-22 15:10:02 -0800 | [diff] [blame] | 235 | public void onActivityCreated(@NonNull IBinder applicationToken, |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 236 | @NonNull ComponentName activityComponent, int flags) { |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 237 | synchronized (mLock) { |
| 238 | mFlags |= flags; |
| 239 | getMainContentCaptureSession().start(applicationToken, activityComponent, mFlags); |
| 240 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** @hide */ |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 244 | @UiThread |
Felipe Leme | b0da18f | 2019-02-22 15:10:02 -0800 | [diff] [blame] | 245 | public void onActivityResumed() { |
| 246 | getMainContentCaptureSession().notifySessionLifecycle(/* started= */ true); |
| 247 | } |
| 248 | |
| 249 | /** @hide */ |
| 250 | @UiThread |
| 251 | public void onActivityPaused() { |
| 252 | getMainContentCaptureSession().notifySessionLifecycle(/* started= */ false); |
| 253 | } |
| 254 | |
| 255 | /** @hide */ |
| 256 | @UiThread |
| 257 | public void onActivityDestroyed() { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 258 | getMainContentCaptureSession().destroy(); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 262 | * Flushes the content of all sessions. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 263 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 264 | * <p>Typically called by {@code Activity} when it's paused / resumed. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 265 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 266 | * @hide |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 267 | */ |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 268 | @UiThread |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 269 | public void flush(@FlushReason int reason) { |
| 270 | getMainContentCaptureSession().flush(reason); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 273 | /** |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 274 | * Returns the component name of the system service that is consuming the captured events for |
| 275 | * the current user. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 276 | */ |
| 277 | @Nullable |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 278 | public ComponentName getServiceComponentName() { |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 279 | if (!isContentCaptureEnabled()) return null; |
| 280 | |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 281 | final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 282 | try { |
Felipe Leme | effa5f4 | 2019-01-28 14:59:46 -0800 | [diff] [blame] | 283 | mService.getServiceComponentName(resultReceiver); |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 284 | return resultReceiver.getParcelableResult(); |
| 285 | } catch (RemoteException e) { |
Perumaal S | addabba | 2019-01-04 16:43:35 -0800 | [diff] [blame] | 286 | throw e.rethrowFromSystemServer(); |
| 287 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 291 | * Checks whether content capture is enabled for this activity. |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 292 | * |
| 293 | * <p>There are many reasons it could be disabled, such as: |
| 294 | * <ul> |
| 295 | * <li>App itself disabled content capture through {@link #setContentCaptureEnabled(boolean)}. |
| 296 | * <li>Service disabled content capture for this specific activity. |
| 297 | * <li>Service disabled content capture for all activities of this package. |
| 298 | * <li>Service disabled content capture globally. |
| 299 | * <li>User disabled content capture globally (through Settings). |
| 300 | * <li>OEM disabled content capture globally. |
| 301 | * <li>Transient errors. |
| 302 | * </ul> |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 303 | */ |
| 304 | public boolean isContentCaptureEnabled() { |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 305 | final MainContentCaptureSession mainSession; |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 306 | synchronized (mLock) { |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 307 | mainSession = mMainSession; |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 308 | } |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 309 | // The main session is only set when the activity starts, so we need to return true until |
| 310 | // then. |
| 311 | if (mainSession != null && mainSession.isDisabled()) return false; |
| 312 | |
| 313 | return true; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 317 | * Called by apps to explicitly enable or disable content capture. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 318 | * |
| 319 | * <p><b>Note: </b> this call is not persisted accross reboots, so apps should typically call |
| 320 | * it on {@link android.app.Activity#onCreate(android.os.Bundle, android.os.PersistableBundle)}. |
| 321 | */ |
Felipe Leme | 6b3a55c | 2018-11-13 17:14:03 -0800 | [diff] [blame] | 322 | public void setContentCaptureEnabled(boolean enabled) { |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 323 | if (sDebug) { |
Felipe Leme | bb0c2a2 | 2019-01-25 17:29:29 -0800 | [diff] [blame] | 324 | Log.d(TAG, "setContentCaptureEnabled(): setting to " + enabled + " for " + mContext); |
| 325 | } |
| 326 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 327 | synchronized (mLock) { |
| 328 | mFlags |= enabled ? 0 : ContentCaptureContext.FLAG_DISABLED_BY_APP; |
| 329 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 333 | * Gets whether Content Capture is enabled for the given user. |
| 334 | * |
| 335 | * <p>This method is typically used by the Content Capture Service settings page, so it can |
| 336 | * provide a toggle to enable / disable it. |
| 337 | * |
Felipe Leme | bb0c2a2 | 2019-01-25 17:29:29 -0800 | [diff] [blame] | 338 | * @throws SecurityException if caller is not the app that owns the Content Capture service |
| 339 | * associated with the user. |
| 340 | * |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 341 | * @hide |
| 342 | */ |
| 343 | @SystemApi |
Felipe Leme | 19652c0 | 2019-02-04 13:01:29 -0800 | [diff] [blame] | 344 | @TestApi |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 345 | public boolean isContentCaptureFeatureEnabled() { |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 346 | final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | bb0c2a2 | 2019-01-25 17:29:29 -0800 | [diff] [blame] | 347 | final int resultCode; |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 348 | try { |
| 349 | mService.isContentCaptureFeatureEnabled(resultReceiver); |
Felipe Leme | bb0c2a2 | 2019-01-25 17:29:29 -0800 | [diff] [blame] | 350 | resultCode = resultReceiver.getIntResult(); |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 351 | } catch (RemoteException e) { |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 352 | throw e.rethrowFromSystemServer(); |
| 353 | } |
Felipe Leme | bb0c2a2 | 2019-01-25 17:29:29 -0800 | [diff] [blame] | 354 | switch (resultCode) { |
| 355 | case RESULT_CODE_TRUE: |
| 356 | return true; |
| 357 | case RESULT_CODE_FALSE: |
| 358 | return false; |
| 359 | case RESULT_CODE_NOT_SERVICE: |
| 360 | throw new SecurityException("caller is not user's ContentCapture service"); |
| 361 | default: |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 362 | Log.wtf(TAG, "received invalid result: " + resultCode); |
| 363 | return false; |
Felipe Leme | bb0c2a2 | 2019-01-25 17:29:29 -0800 | [diff] [blame] | 364 | } |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 368 | * 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] | 369 | * some context. |
| 370 | * |
| 371 | * @param request object specifying what user data should be removed. |
| 372 | */ |
| 373 | public void removeUserData(@NonNull UserDataRemovalRequest request) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 374 | Preconditions.checkNotNull(request); |
| 375 | |
| 376 | try { |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame] | 377 | mService.removeUserData(request); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 378 | } catch (RemoteException e) { |
| 379 | e.rethrowFromSystemServer(); |
| 380 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 383 | /** @hide */ |
| 384 | public void dump(String prefix, PrintWriter pw) { |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 385 | pw.print(prefix); pw.println("ContentCaptureManager"); |
| 386 | final String prefix2 = prefix + " "; |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 387 | synchronized (mLock) { |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 388 | pw.print(prefix2); pw.print("isContentCaptureEnabled(): "); |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 389 | pw.println(isContentCaptureEnabled()); |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 390 | pw.print(prefix2); pw.print("Debug: "); pw.print(sDebug); |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 391 | pw.print(" Verbose: "); pw.println(sVerbose); |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 392 | pw.print(prefix2); pw.print("Context: "); pw.println(mContext); |
| 393 | pw.print(prefix2); pw.print("User: "); pw.println(mContext.getUserId()); |
| 394 | pw.print(prefix2); pw.print("Service: "); pw.println(mService); |
| 395 | pw.print(prefix2); pw.print("Flags: "); pw.println(mFlags); |
| 396 | pw.print(prefix2); pw.print("Options: "); mOptions.dumpShort(pw); pw.println(); |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 397 | if (mMainSession != null) { |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 398 | final String prefix3 = prefix2 + " "; |
| 399 | pw.print(prefix2); pw.println("Main session:"); |
| 400 | mMainSession.dump(prefix3, pw); |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 401 | } else { |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 402 | pw.print(prefix2); pw.println("No sessions"); |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 403 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 406 | } |