blob: 81b2e01dfbc75d2c482786dea310380e2b8dc7dd [file] [log] [blame]
Felipe Leme1dfa9a02018-10-17 17:24:37 -07001/*
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 Leme749b8892018-12-03 16:30:30 -080016package android.view.contentcapture;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070017
Perumaal Saddabba2019-01-04 16:43:35 -080018import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
19
Felipe Leme1dfa9a02018-10-17 17:24:37 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Felipe Lemee348dc32018-11-05 12:35:29 -080022import android.annotation.SystemService;
Felipe Leme87a9dc92018-12-18 14:28:07 -080023import android.annotation.UiThread;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070024import android.content.ComponentName;
25import android.content.Context;
Felipe Leme88eae3b2018-11-07 15:11:56 -080026import android.os.Handler;
27import android.os.HandlerThread;
Felipe Lemee348dc32018-11-05 12:35:29 -080028import android.os.IBinder;
Perumaal Saddabba2019-01-04 16:43:35 -080029import android.os.RemoteException;
Felipe Lemee348dc32018-11-05 12:35:29 -080030import android.util.Log;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070031
Adam He6079d152019-01-10 11:37:17 -080032import com.android.internal.annotations.GuardedBy;
Perumaal Saddabba2019-01-04 16:43:35 -080033import com.android.internal.os.IResultReceiver;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070034import com.android.internal.util.Preconditions;
Perumaal Saddabba2019-01-04 16:43:35 -080035import com.android.internal.util.SyncResultReceiver;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070036
Felipe Lemee348dc32018-11-05 12:35:29 -080037import java.io.PrintWriter;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070038
Felipe Lemeb18e3172018-11-27 10:33:41 -080039/*
40 * NOTE: all methods in this class should return right away, or do the real work in a handler
41 * thread.
42 *
43 * Hence, the only field that must be thread-safe is mEnabled, which is called at the beginning
44 * of every method.
45 */
Felipe Lemeecb08be2018-11-27 15:48:47 -080046/**
47 * TODO(b/111276913): add javadocs / implement
48 */
49@SystemService(Context.CONTENT_CAPTURE_MANAGER_SERVICE)
50public final class ContentCaptureManager {
Felipe Leme1dfa9a02018-10-17 17:24:37 -070051
Felipe Leme749b8892018-12-03 16:30:30 -080052 private static final String TAG = ContentCaptureManager.class.getSimpleName();
Felipe Lemee348dc32018-11-05 12:35:29 -080053
Felipe Leme88eae3b2018-11-07 15:11:56 -080054 private static final String BG_THREAD_NAME = "intel_svc_streamer_thread";
55
Perumaal Saddabba2019-01-04 16:43:35 -080056 /**
57 * Timeout for calls to system_server.
58 */
59 private static final int SYNC_CALLS_TIMEOUT_MS = 5000;
60
Felipe Lemeaa5088e2018-12-10 14:53:58 -080061 // TODO(b/121044306): define a way to dynamically set them(for example, using settings?)
62 static final boolean VERBOSE = false;
63 static final boolean DEBUG = true; // STOPSHIP if not set to false
Felipe Leme4017b202018-12-10 12:13:31 -080064
Adam He6079d152019-01-10 11:37:17 -080065 private final Object mLock = new Object();
66
67 @GuardedBy("mLock")
68 private boolean mDisabled;
Felipe Lemeb18e3172018-11-27 10:33:41 -080069
70 @NonNull
Felipe Lemee348dc32018-11-05 12:35:29 -080071 private final Context mContext;
72
73 @Nullable
Felipe Leme749b8892018-12-03 16:30:30 -080074 private final IContentCaptureManager mService;
Felipe Lemee348dc32018-11-05 12:35:29 -080075
Adam He6079d152019-01-10 11:37:17 -080076 // Flags used for starting session.
77 @GuardedBy("mLock")
78 private int mFlags;
79
Felipe Lemeaa5088e2018-12-10 14:53:58 -080080 // TODO(b/119220549): use UI Thread directly (as calls are one-way) or a shared thread / handler
Felipe Lemeb18e3172018-11-27 10:33:41 -080081 // held at the Application level
Felipe Lemeaa5088e2018-12-10 14:53:58 -080082 @NonNull
Felipe Leme88eae3b2018-11-07 15:11:56 -080083 private final Handler mHandler;
84
Adam He6079d152019-01-10 11:37:17 -080085 @GuardedBy("mLock")
Felipe Leme87a9dc92018-12-18 14:28:07 -080086 private MainContentCaptureSession mMainSession;
Felipe Leme4017b202018-12-10 12:13:31 -080087
Felipe Lemee348dc32018-11-05 12:35:29 -080088 /** @hide */
Felipe Leme749b8892018-12-03 16:30:30 -080089 public ContentCaptureManager(@NonNull Context context,
90 @Nullable IContentCaptureManager service) {
Felipe Leme1dfa9a02018-10-17 17:24:37 -070091 mContext = Preconditions.checkNotNull(context, "context cannot be null");
Felipe Lemeb18e3172018-11-27 10:33:41 -080092 if (VERBOSE) {
93 Log.v(TAG, "Constructor for " + context.getPackageName());
94 }
Felipe Lemee348dc32018-11-05 12:35:29 -080095 mService = service;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080096 // TODO(b/119220549): use an existing bg thread instead...
Felipe Leme88eae3b2018-11-07 15:11:56 -080097 final HandlerThread bgThread = new HandlerThread(BG_THREAD_NAME);
98 bgThread.start();
99 mHandler = Handler.createAsync(bgThread.getLooper());
Felipe Lemee348dc32018-11-05 12:35:29 -0800100 }
101
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800102 @NonNull
103 private static Handler newHandler() {
104 // TODO(b/119220549): use an existing bg thread instead...
105 // TODO(b/119220549): use UI Thread directly (as calls are one-way) or an existing bgThread
106 // or a shared thread / handler held at the Application level
107 final HandlerThread bgThread = new HandlerThread(BG_THREAD_NAME);
108 bgThread.start();
109 return Handler.createAsync(bgThread.getLooper());
Felipe Leme88eae3b2018-11-07 15:11:56 -0800110 }
111
Felipe Leme7a534082018-11-05 15:03:04 -0800112 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800113 * Gets the main session associated with the context.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800114 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800115 * <p>By default there's just one (associated with the activity lifecycle), but apps could
Felipe Leme87a9dc92018-12-18 14:28:07 -0800116 * explicitly add more using
117 * {@link ContentCaptureSession#createContentCaptureSession(ContentCaptureContext)}.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800118 *
119 * @hide
120 */
121 @NonNull
Felipe Leme87a9dc92018-12-18 14:28:07 -0800122 @UiThread
123 public MainContentCaptureSession getMainContentCaptureSession() {
Adam He6079d152019-01-10 11:37:17 -0800124 synchronized (mLock) {
125 if (mMainSession == null) {
126 mMainSession = new MainContentCaptureSession(mContext, mHandler, mService,
Felipe Leme01b87492019-01-15 13:26:52 -0800127 mDisabled);
Adam He6079d152019-01-10 11:37:17 -0800128 if (VERBOSE) {
129 Log.v(TAG, "getDefaultContentCaptureSession(): created " + mMainSession);
130 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800131 }
Adam He6079d152019-01-10 11:37:17 -0800132 return mMainSession;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800133 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800134 }
135
136 /** @hide */
137 public void onActivityStarted(@NonNull IBinder applicationToken,
Adam He328c0e32019-01-03 15:19:22 -0800138 @NonNull ComponentName activityComponent, int flags) {
Adam He6079d152019-01-10 11:37:17 -0800139 synchronized (mLock) {
140 mFlags |= flags;
141 getMainContentCaptureSession().start(applicationToken, activityComponent, mFlags);
142 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800143 }
144
145 /** @hide */
146 public void onActivityStopped() {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800147 getMainContentCaptureSession().destroy();
Felipe Leme88eae3b2018-11-07 15:11:56 -0800148 }
149
150 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800151 * Flushes the content of all sessions.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800152 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800153 * <p>Typically called by {@code Activity} when it's paused / resumed.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800154 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800155 * @hide
Felipe Leme88eae3b2018-11-07 15:11:56 -0800156 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800157 public void flush() {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800158 getMainContentCaptureSession().flush();
Felipe Leme88eae3b2018-11-07 15:11:56 -0800159 }
160
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700161 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -0800162 * Returns the component name of the system service that is consuming the captured events for
163 * the current user.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700164 */
165 @Nullable
Felipe Lemeecb08be2018-11-27 15:48:47 -0800166 public ComponentName getServiceComponentName() {
Perumaal Saddabba2019-01-04 16:43:35 -0800167 if (!isContentCaptureEnabled()) {
168 return null;
169 }
170 // Wait for system server to return the component name.
171 final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS);
172 mHandler.sendMessage(obtainMessage(
173 ContentCaptureManager::handleReceiverServiceComponentName,
174 this, mContext.getUserId(), resultReceiver));
175
176 try {
177 return resultReceiver.getParcelableResult();
178 } catch (RemoteException e) {
179 // Unable to retrieve component name in a reasonable amount of time.
180 throw e.rethrowFromSystemServer();
181 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700182 }
183
184 /**
Felipe Lemee348dc32018-11-05 12:35:29 -0800185 * Checks whether content capture is enabled for this activity.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700186 */
187 public boolean isContentCaptureEnabled() {
Adam He6079d152019-01-10 11:37:17 -0800188 synchronized (mLock) {
189 return mService != null && !mDisabled;
190 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700191 }
192
193 /**
Felipe Leme284ad1c2018-11-15 18:16:12 -0800194 * Called by apps to explicitly enable or disable content capture.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700195 *
196 * <p><b>Note: </b> this call is not persisted accross reboots, so apps should typically call
197 * it on {@link android.app.Activity#onCreate(android.os.Bundle, android.os.PersistableBundle)}.
198 */
Felipe Leme6b3a55c2018-11-13 17:14:03 -0800199 public void setContentCaptureEnabled(boolean enabled) {
Adam He6079d152019-01-10 11:37:17 -0800200 synchronized (mLock) {
201 mFlags |= enabled ? 0 : ContentCaptureContext.FLAG_DISABLED_BY_APP;
202 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800203 }
204
205 /**
206 * Called by the ap to request the Content Capture service to remove user-data associated with
207 * some context.
208 *
209 * @param request object specifying what user data should be removed.
210 */
211 public void removeUserData(@NonNull UserDataRemovalRequest request) {
Felipe Lemee348dc32018-11-05 12:35:29 -0800212 //TODO(b/111276913): implement
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700213 }
214
Felipe Lemee348dc32018-11-05 12:35:29 -0800215 /** @hide */
216 public void dump(String prefix, PrintWriter pw) {
Adam He6079d152019-01-10 11:37:17 -0800217 synchronized (mLock) {
218 pw.print(prefix); pw.println("ContentCaptureManager");
219 pw.print(prefix); pw.print("Disabled: "); pw.println(mDisabled);
220 pw.print(prefix); pw.print("Context: "); pw.println(mContext);
221 pw.print(prefix); pw.print("User: "); pw.println(mContext.getUserId());
222 if (mService != null) {
223 pw.print(prefix); pw.print("Service: "); pw.println(mService);
224 }
225 pw.print(prefix); pw.print("Flags: "); pw.println(mFlags);
226 if (mMainSession != null) {
227 final String prefix2 = prefix + " ";
228 pw.print(prefix); pw.println("Main session:");
229 mMainSession.dump(prefix2, pw);
230 } else {
231 pw.print(prefix); pw.println("No sessions");
232 }
Felipe Lemee348dc32018-11-05 12:35:29 -0800233 }
234 }
Perumaal Saddabba2019-01-04 16:43:35 -0800235
236
237 /** Retrieves the component name of the target content capture service through system_server. */
238 private void handleReceiverServiceComponentName(int userId, IResultReceiver resultReceiver) {
239 try {
240 mService.getReceiverServiceComponentName(userId, resultReceiver);
241 } catch (RemoteException e) {
242 Log.w(TAG, "Unable to retrieve service component name: " + e);
243 }
244 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700245}