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 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 20 | import android.annotation.SystemService; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame^] | 21 | import android.annotation.UiThread; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.HandlerThread; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 26 | import android.os.IBinder; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 28 | |
| 29 | import com.android.internal.util.Preconditions; |
| 30 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 31 | import java.io.PrintWriter; |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 32 | import java.util.concurrent.atomic.AtomicBoolean; |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 33 | |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 34 | /* |
| 35 | * NOTE: all methods in this class should return right away, or do the real work in a handler |
| 36 | * thread. |
| 37 | * |
| 38 | * Hence, the only field that must be thread-safe is mEnabled, which is called at the beginning |
| 39 | * of every method. |
| 40 | */ |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 41 | /** |
| 42 | * TODO(b/111276913): add javadocs / implement |
| 43 | */ |
| 44 | @SystemService(Context.CONTENT_CAPTURE_MANAGER_SERVICE) |
| 45 | public final class ContentCaptureManager { |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 46 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 47 | private static final String TAG = ContentCaptureManager.class.getSimpleName(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 48 | |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 49 | private static final String BG_THREAD_NAME = "intel_svc_streamer_thread"; |
| 50 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 51 | // TODO(b/121044306): define a way to dynamically set them(for example, using settings?) |
| 52 | static final boolean VERBOSE = false; |
| 53 | static final boolean DEBUG = true; // STOPSHIP if not set to false |
Felipe Leme | 4017b20 | 2018-12-10 12:13:31 -0800 | [diff] [blame] | 54 | |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 55 | @NonNull |
| 56 | private final AtomicBoolean mDisabled = new AtomicBoolean(); |
| 57 | |
| 58 | @NonNull |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 59 | private final Context mContext; |
| 60 | |
| 61 | @Nullable |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 62 | private final IContentCaptureManager mService; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 63 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 64 | // 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] | 65 | // held at the Application level |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 66 | @NonNull |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 67 | private final Handler mHandler; |
| 68 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame^] | 69 | private MainContentCaptureSession mMainSession; |
Felipe Leme | 4017b20 | 2018-12-10 12:13:31 -0800 | [diff] [blame] | 70 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 71 | /** @hide */ |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 72 | public ContentCaptureManager(@NonNull Context context, |
| 73 | @Nullable IContentCaptureManager service) { |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 74 | mContext = Preconditions.checkNotNull(context, "context cannot be null"); |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 75 | if (VERBOSE) { |
| 76 | Log.v(TAG, "Constructor for " + context.getPackageName()); |
| 77 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 78 | mService = service; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 79 | // TODO(b/119220549): use an existing bg thread instead... |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 80 | final HandlerThread bgThread = new HandlerThread(BG_THREAD_NAME); |
| 81 | bgThread.start(); |
| 82 | mHandler = Handler.createAsync(bgThread.getLooper()); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 85 | @NonNull |
| 86 | private static Handler newHandler() { |
| 87 | // TODO(b/119220549): use an existing bg thread instead... |
| 88 | // TODO(b/119220549): use UI Thread directly (as calls are one-way) or an existing bgThread |
| 89 | // or a shared thread / handler held at the Application level |
| 90 | final HandlerThread bgThread = new HandlerThread(BG_THREAD_NAME); |
| 91 | bgThread.start(); |
| 92 | return Handler.createAsync(bgThread.getLooper()); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Felipe Leme | 7a53408 | 2018-11-05 15:03:04 -0800 | [diff] [blame] | 95 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 96 | * Gets the main session associated with the context. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 97 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 98 | * <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^] | 99 | * explicitly add more using |
| 100 | * {@link ContentCaptureSession#createContentCaptureSession(ContentCaptureContext)}. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 101 | * |
| 102 | * @hide |
| 103 | */ |
| 104 | @NonNull |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame^] | 105 | @UiThread |
| 106 | public MainContentCaptureSession getMainContentCaptureSession() { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 107 | if (mMainSession == null) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame^] | 108 | mMainSession = new MainContentCaptureSession(mContext, mHandler, mService, |
| 109 | mDisabled); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 110 | if (VERBOSE) { |
| 111 | Log.v(TAG, "getDefaultContentCaptureSession(): created " + mMainSession); |
| 112 | } |
| 113 | } |
| 114 | return mMainSession; |
| 115 | } |
| 116 | |
| 117 | /** @hide */ |
| 118 | public void onActivityStarted(@NonNull IBinder applicationToken, |
| 119 | @NonNull ComponentName activityComponent) { |
| 120 | // TODO(b/121033016): must start all sessions |
| 121 | getMainContentCaptureSession().start(applicationToken, activityComponent); |
| 122 | } |
| 123 | |
| 124 | /** @hide */ |
| 125 | public void onActivityStopped() { |
| 126 | // TODO(b/121033016): must finish all sessions |
| 127 | getMainContentCaptureSession().destroy(); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 131 | * Flushes the content of all sessions. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 132 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 133 | * <p>Typically called by {@code Activity} when it's paused / resumed. |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 134 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 135 | * @hide |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 136 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 137 | public void flush() { |
| 138 | // TODO(b/121033016): must flush all sessions |
| 139 | getMainContentCaptureSession().flush(); |
Felipe Leme | 88eae3b | 2018-11-07 15:11:56 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 142 | /** |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 143 | * Returns the component name of the system service that is consuming the captured events for |
| 144 | * the current user. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 145 | */ |
| 146 | @Nullable |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 147 | public ComponentName getServiceComponentName() { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 148 | //TODO(b/121047489): implement |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 149 | return null; |
| 150 | } |
| 151 | |
| 152 | /** |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 153 | * Checks whether content capture is enabled for this activity. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 154 | */ |
| 155 | public boolean isContentCaptureEnabled() { |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 156 | return mService != null && !mDisabled.get(); |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 160 | * Called by apps to explicitly enable or disable content capture. |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 161 | * |
| 162 | * <p><b>Note: </b> this call is not persisted accross reboots, so apps should typically call |
| 163 | * it on {@link android.app.Activity#onCreate(android.os.Bundle, android.os.PersistableBundle)}. |
| 164 | */ |
Felipe Leme | 6b3a55c | 2018-11-13 17:14:03 -0800 | [diff] [blame] | 165 | public void setContentCaptureEnabled(boolean enabled) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 166 | //TODO(b/111276913): implement (need to finish / disable all sessions) |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Called by the ap to request the Content Capture service to remove user-data associated with |
| 171 | * some context. |
| 172 | * |
| 173 | * @param request object specifying what user data should be removed. |
| 174 | */ |
| 175 | public void removeUserData(@NonNull UserDataRemovalRequest request) { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 176 | //TODO(b/111276913): implement |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 179 | /** @hide */ |
| 180 | public void dump(String prefix, PrintWriter pw) { |
Felipe Leme | b718e03 | 2018-12-10 09:44:23 -0800 | [diff] [blame] | 181 | pw.print(prefix); pw.println("ContentCaptureManager"); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 182 | |
| 183 | pw.print(prefix); pw.print("Disabled: "); pw.println(mDisabled.get()); |
| 184 | pw.print(prefix); pw.print("Context: "); pw.println(mContext); |
| 185 | pw.print(prefix); pw.print("User: "); pw.println(mContext.getUserId()); |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 186 | if (mService != null) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 187 | pw.print(prefix); pw.print("Service: "); pw.println(mService); |
Felipe Leme | b18e317 | 2018-11-27 10:33:41 -0800 | [diff] [blame] | 188 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 189 | if (mMainSession != null) { |
| 190 | final String prefix2 = prefix + " "; |
| 191 | pw.print(prefix); pw.println("Main session:"); |
| 192 | mMainSession.dump(prefix2, pw); |
| 193 | } else { |
| 194 | pw.print(prefix); pw.println("No sessions"); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
Felipe Leme | 1dfa9a0 | 2018-10-17 17:24:37 -0700 | [diff] [blame] | 197 | } |