blob: 46a4f182fac429993a6a5b810d46a2fd23b9c2d6 [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;
Felipe Lemee348dc32018-11-05 12:35:29 -080027import android.os.IBinder;
Felipe Leme34ccedf2019-01-17 13:42:35 -080028import android.os.Looper;
Perumaal Saddabba2019-01-04 16:43:35 -080029import android.os.RemoteException;
Felipe Lemee348dc32018-11-05 12:35:29 -080030import android.util.Log;
Felipe Leme1af85ea2019-01-16 13:23:40 -080031import android.view.contentcapture.ContentCaptureSession.FlushReason;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070032
Adam He6079d152019-01-10 11:37:17 -080033import com.android.internal.annotations.GuardedBy;
Perumaal Saddabba2019-01-04 16:43:35 -080034import com.android.internal.os.IResultReceiver;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070035import com.android.internal.util.Preconditions;
Perumaal Saddabba2019-01-04 16:43:35 -080036import com.android.internal.util.SyncResultReceiver;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070037
Felipe Lemee348dc32018-11-05 12:35:29 -080038import java.io.PrintWriter;
Felipe Leme1dfa9a02018-10-17 17:24:37 -070039
Felipe Lemeb18e3172018-11-27 10:33:41 -080040/*
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 Lemeecb08be2018-11-27 15:48:47 -080047/**
48 * TODO(b/111276913): add javadocs / implement
49 */
50@SystemService(Context.CONTENT_CAPTURE_MANAGER_SERVICE)
51public final class ContentCaptureManager {
Felipe Leme1dfa9a02018-10-17 17:24:37 -070052
Felipe Leme749b8892018-12-03 16:30:30 -080053 private static final String TAG = ContentCaptureManager.class.getSimpleName();
Felipe Lemee348dc32018-11-05 12:35:29 -080054
Perumaal Saddabba2019-01-04 16:43:35 -080055 /**
56 * Timeout for calls to system_server.
57 */
58 private static final int SYNC_CALLS_TIMEOUT_MS = 5000;
59
Felipe Lemeaa5088e2018-12-10 14:53:58 -080060 // TODO(b/121044306): define a way to dynamically set them(for example, using settings?)
61 static final boolean VERBOSE = false;
62 static final boolean DEBUG = true; // STOPSHIP if not set to false
Felipe Leme4017b202018-12-10 12:13:31 -080063
Adam He6079d152019-01-10 11:37:17 -080064 private final Object mLock = new Object();
65
66 @GuardedBy("mLock")
67 private boolean mDisabled;
Felipe Lemeb18e3172018-11-27 10:33:41 -080068
69 @NonNull
Felipe Lemee348dc32018-11-05 12:35:29 -080070 private final Context mContext;
71
72 @Nullable
Felipe Leme749b8892018-12-03 16:30:30 -080073 private final IContentCaptureManager mService;
Felipe Lemee348dc32018-11-05 12:35:29 -080074
Adam He6079d152019-01-10 11:37:17 -080075 // Flags used for starting session.
76 @GuardedBy("mLock")
77 private int mFlags;
78
Felipe Lemeaa5088e2018-12-10 14:53:58 -080079 // TODO(b/119220549): use UI Thread directly (as calls are one-way) or a shared thread / handler
Felipe Lemeb18e3172018-11-27 10:33:41 -080080 // held at the Application level
Felipe Lemeaa5088e2018-12-10 14:53:58 -080081 @NonNull
Felipe Leme88eae3b2018-11-07 15:11:56 -080082 private final Handler mHandler;
83
Adam He6079d152019-01-10 11:37:17 -080084 @GuardedBy("mLock")
Felipe Leme87a9dc92018-12-18 14:28:07 -080085 private MainContentCaptureSession mMainSession;
Felipe Leme4017b202018-12-10 12:13:31 -080086
Felipe Lemee348dc32018-11-05 12:35:29 -080087 /** @hide */
Felipe Leme749b8892018-12-03 16:30:30 -080088 public ContentCaptureManager(@NonNull Context context,
89 @Nullable IContentCaptureManager service) {
Felipe Leme1dfa9a02018-10-17 17:24:37 -070090 mContext = Preconditions.checkNotNull(context, "context cannot be null");
Felipe Leme34ccedf2019-01-17 13:42:35 -080091 if (VERBOSE) Log.v(TAG, "Constructor for " + context.getPackageName());
Felipe Lemee348dc32018-11-05 12:35:29 -080092
Felipe Leme34ccedf2019-01-17 13:42:35 -080093 mService = service;
94 // TODO(b/119220549): we might not even need a handler, as the IPCs are oneway. But if we
95 // do, then we should optimize it to run the tests after the Choreographer finishes the most
96 // important steps of the frame.
97 mHandler = Handler.createAsync(Looper.getMainLooper());
Felipe Leme88eae3b2018-11-07 15:11:56 -080098 }
99
Felipe Leme7a534082018-11-05 15:03:04 -0800100 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800101 * Gets the main session associated with the context.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800102 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800103 * <p>By default there's just one (associated with the activity lifecycle), but apps could
Felipe Leme87a9dc92018-12-18 14:28:07 -0800104 * explicitly add more using
105 * {@link ContentCaptureSession#createContentCaptureSession(ContentCaptureContext)}.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800106 *
107 * @hide
108 */
109 @NonNull
Felipe Leme87a9dc92018-12-18 14:28:07 -0800110 @UiThread
111 public MainContentCaptureSession getMainContentCaptureSession() {
Adam He6079d152019-01-10 11:37:17 -0800112 synchronized (mLock) {
113 if (mMainSession == null) {
114 mMainSession = new MainContentCaptureSession(mContext, mHandler, mService,
Felipe Leme01b87492019-01-15 13:26:52 -0800115 mDisabled);
Adam He6079d152019-01-10 11:37:17 -0800116 if (VERBOSE) {
117 Log.v(TAG, "getDefaultContentCaptureSession(): created " + mMainSession);
118 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800119 }
Adam He6079d152019-01-10 11:37:17 -0800120 return mMainSession;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800121 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800122 }
123
124 /** @hide */
125 public void onActivityStarted(@NonNull IBinder applicationToken,
Adam He328c0e32019-01-03 15:19:22 -0800126 @NonNull ComponentName activityComponent, int flags) {
Adam He6079d152019-01-10 11:37:17 -0800127 synchronized (mLock) {
128 mFlags |= flags;
129 getMainContentCaptureSession().start(applicationToken, activityComponent, mFlags);
130 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800131 }
132
133 /** @hide */
134 public void onActivityStopped() {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800135 getMainContentCaptureSession().destroy();
Felipe Leme88eae3b2018-11-07 15:11:56 -0800136 }
137
138 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800139 * Flushes the content of all sessions.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800140 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800141 * <p>Typically called by {@code Activity} when it's paused / resumed.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800142 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800143 * @hide
Felipe Leme88eae3b2018-11-07 15:11:56 -0800144 */
Felipe Leme1af85ea2019-01-16 13:23:40 -0800145 public void flush(@FlushReason int reason) {
146 getMainContentCaptureSession().flush(reason);
Felipe Leme88eae3b2018-11-07 15:11:56 -0800147 }
148
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700149 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -0800150 * Returns the component name of the system service that is consuming the captured events for
151 * the current user.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700152 */
153 @Nullable
Felipe Lemeecb08be2018-11-27 15:48:47 -0800154 public ComponentName getServiceComponentName() {
Perumaal Saddabba2019-01-04 16:43:35 -0800155 if (!isContentCaptureEnabled()) {
156 return null;
157 }
158 // Wait for system server to return the component name.
159 final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS);
160 mHandler.sendMessage(obtainMessage(
161 ContentCaptureManager::handleReceiverServiceComponentName,
162 this, mContext.getUserId(), resultReceiver));
163
164 try {
165 return resultReceiver.getParcelableResult();
166 } catch (RemoteException e) {
167 // Unable to retrieve component name in a reasonable amount of time.
168 throw e.rethrowFromSystemServer();
169 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700170 }
171
172 /**
Felipe Lemee348dc32018-11-05 12:35:29 -0800173 * Checks whether content capture is enabled for this activity.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700174 */
175 public boolean isContentCaptureEnabled() {
Adam He6079d152019-01-10 11:37:17 -0800176 synchronized (mLock) {
177 return mService != null && !mDisabled;
178 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700179 }
180
181 /**
Felipe Leme284ad1c2018-11-15 18:16:12 -0800182 * Called by apps to explicitly enable or disable content capture.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700183 *
184 * <p><b>Note: </b> this call is not persisted accross reboots, so apps should typically call
185 * it on {@link android.app.Activity#onCreate(android.os.Bundle, android.os.PersistableBundle)}.
186 */
Felipe Leme6b3a55c2018-11-13 17:14:03 -0800187 public void setContentCaptureEnabled(boolean enabled) {
Adam He6079d152019-01-10 11:37:17 -0800188 synchronized (mLock) {
189 mFlags |= enabled ? 0 : ContentCaptureContext.FLAG_DISABLED_BY_APP;
190 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800191 }
192
193 /**
Adam He3d0409b2019-01-15 14:22:04 -0800194 * Called by the app to request the Content Capture service to remove user-data associated with
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800195 * some context.
196 *
197 * @param request object specifying what user data should be removed.
198 */
199 public void removeUserData(@NonNull UserDataRemovalRequest request) {
Adam He3d0409b2019-01-15 14:22:04 -0800200 Preconditions.checkNotNull(request);
201
202 try {
203 mService.removeUserData(mContext.getUserId(), request);
204 } catch (RemoteException e) {
205 e.rethrowFromSystemServer();
206 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700207 }
208
Felipe Lemee348dc32018-11-05 12:35:29 -0800209 /** @hide */
210 public void dump(String prefix, PrintWriter pw) {
Adam He6079d152019-01-10 11:37:17 -0800211 synchronized (mLock) {
212 pw.print(prefix); pw.println("ContentCaptureManager");
213 pw.print(prefix); pw.print("Disabled: "); pw.println(mDisabled);
214 pw.print(prefix); pw.print("Context: "); pw.println(mContext);
215 pw.print(prefix); pw.print("User: "); pw.println(mContext.getUserId());
216 if (mService != null) {
217 pw.print(prefix); pw.print("Service: "); pw.println(mService);
218 }
219 pw.print(prefix); pw.print("Flags: "); pw.println(mFlags);
220 if (mMainSession != null) {
221 final String prefix2 = prefix + " ";
222 pw.print(prefix); pw.println("Main session:");
223 mMainSession.dump(prefix2, pw);
224 } else {
225 pw.print(prefix); pw.println("No sessions");
226 }
Felipe Lemee348dc32018-11-05 12:35:29 -0800227 }
228 }
Perumaal Saddabba2019-01-04 16:43:35 -0800229
230
231 /** Retrieves the component name of the target content capture service through system_server. */
232 private void handleReceiverServiceComponentName(int userId, IResultReceiver resultReceiver) {
233 try {
234 mService.getReceiverServiceComponentName(userId, resultReceiver);
235 } catch (RemoteException e) {
236 Log.w(TAG, "Unable to retrieve service component name: " + e);
237 }
238 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700239}