blob: 07c91017a3a50e79481922073ce78b17345777c0 [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
Felipe Lemebe002d82019-01-23 10:22:32 -080018import static android.view.contentcapture.ContentCaptureHelper.VERBOSE;
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;
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
Perumaal Saddabba2019-01-04 16:43:35 -080054 /**
55 * Timeout for calls to system_server.
56 */
57 private static final int SYNC_CALLS_TIMEOUT_MS = 5000;
58
Adam He6079d152019-01-10 11:37:17 -080059 private final Object mLock = new Object();
60
61 @GuardedBy("mLock")
62 private boolean mDisabled;
Felipe Lemeb18e3172018-11-27 10:33:41 -080063
64 @NonNull
Felipe Lemee348dc32018-11-05 12:35:29 -080065 private final Context mContext;
66
67 @Nullable
Felipe Leme749b8892018-12-03 16:30:30 -080068 private final IContentCaptureManager mService;
Felipe Lemee348dc32018-11-05 12:35:29 -080069
Adam He6079d152019-01-10 11:37:17 -080070 // Flags used for starting session.
71 @GuardedBy("mLock")
72 private int mFlags;
73
Felipe Lemeaa5088e2018-12-10 14:53:58 -080074 // TODO(b/119220549): use UI Thread directly (as calls are one-way) or a shared thread / handler
Felipe Lemeb18e3172018-11-27 10:33:41 -080075 // held at the Application level
Felipe Lemeaa5088e2018-12-10 14:53:58 -080076 @NonNull
Felipe Leme88eae3b2018-11-07 15:11:56 -080077 private final Handler mHandler;
78
Adam He6079d152019-01-10 11:37:17 -080079 @GuardedBy("mLock")
Felipe Leme87a9dc92018-12-18 14:28:07 -080080 private MainContentCaptureSession mMainSession;
Felipe Leme4017b202018-12-10 12:13:31 -080081
Felipe Lemee348dc32018-11-05 12:35:29 -080082 /** @hide */
Felipe Leme749b8892018-12-03 16:30:30 -080083 public ContentCaptureManager(@NonNull Context context,
84 @Nullable IContentCaptureManager service) {
Felipe Leme1dfa9a02018-10-17 17:24:37 -070085 mContext = Preconditions.checkNotNull(context, "context cannot be null");
Felipe Leme34ccedf2019-01-17 13:42:35 -080086 if (VERBOSE) Log.v(TAG, "Constructor for " + context.getPackageName());
Felipe Lemee348dc32018-11-05 12:35:29 -080087
Felipe Leme34ccedf2019-01-17 13:42:35 -080088 mService = service;
89 // TODO(b/119220549): we might not even need a handler, as the IPCs are oneway. But if we
90 // do, then we should optimize it to run the tests after the Choreographer finishes the most
91 // important steps of the frame.
92 mHandler = Handler.createAsync(Looper.getMainLooper());
Felipe Leme88eae3b2018-11-07 15:11:56 -080093 }
94
Felipe Leme7a534082018-11-05 15:03:04 -080095 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -080096 * Gets the main session associated with the context.
Felipe Leme88eae3b2018-11-07 15:11:56 -080097 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -080098 * <p>By default there's just one (associated with the activity lifecycle), but apps could
Felipe Leme87a9dc92018-12-18 14:28:07 -080099 * explicitly add more using
100 * {@link ContentCaptureSession#createContentCaptureSession(ContentCaptureContext)}.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800101 *
102 * @hide
103 */
104 @NonNull
Felipe Leme87a9dc92018-12-18 14:28:07 -0800105 @UiThread
106 public MainContentCaptureSession getMainContentCaptureSession() {
Adam He6079d152019-01-10 11:37:17 -0800107 synchronized (mLock) {
108 if (mMainSession == null) {
109 mMainSession = new MainContentCaptureSession(mContext, mHandler, mService,
Felipe Leme01b87492019-01-15 13:26:52 -0800110 mDisabled);
Adam He6079d152019-01-10 11:37:17 -0800111 if (VERBOSE) {
112 Log.v(TAG, "getDefaultContentCaptureSession(): created " + mMainSession);
113 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800114 }
Adam He6079d152019-01-10 11:37:17 -0800115 return mMainSession;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800116 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800117 }
118
119 /** @hide */
120 public void onActivityStarted(@NonNull IBinder applicationToken,
Adam He328c0e32019-01-03 15:19:22 -0800121 @NonNull ComponentName activityComponent, int flags) {
Adam He6079d152019-01-10 11:37:17 -0800122 synchronized (mLock) {
123 mFlags |= flags;
124 getMainContentCaptureSession().start(applicationToken, activityComponent, mFlags);
125 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800126 }
127
128 /** @hide */
129 public void onActivityStopped() {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800130 getMainContentCaptureSession().destroy();
Felipe Leme88eae3b2018-11-07 15:11:56 -0800131 }
132
133 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800134 * Flushes the content of all sessions.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800135 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800136 * <p>Typically called by {@code Activity} when it's paused / resumed.
Felipe Leme88eae3b2018-11-07 15:11:56 -0800137 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800138 * @hide
Felipe Leme88eae3b2018-11-07 15:11:56 -0800139 */
Felipe Leme1af85ea2019-01-16 13:23:40 -0800140 public void flush(@FlushReason int reason) {
141 getMainContentCaptureSession().flush(reason);
Felipe Leme88eae3b2018-11-07 15:11:56 -0800142 }
143
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700144 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -0800145 * Returns the component name of the system service that is consuming the captured events for
146 * the current user.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700147 */
148 @Nullable
Felipe Lemeecb08be2018-11-27 15:48:47 -0800149 public ComponentName getServiceComponentName() {
Perumaal Saddabba2019-01-04 16:43:35 -0800150 if (!isContentCaptureEnabled()) {
151 return null;
152 }
153 // Wait for system server to return the component name.
154 final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS);
Felipe Lemeeffa5f42019-01-28 14:59:46 -0800155
Perumaal Saddabba2019-01-04 16:43:35 -0800156
157 try {
Felipe Lemeeffa5f42019-01-28 14:59:46 -0800158 mService.getServiceComponentName(resultReceiver);
Perumaal Saddabba2019-01-04 16:43:35 -0800159 return resultReceiver.getParcelableResult();
160 } catch (RemoteException e) {
Perumaal Saddabba2019-01-04 16:43:35 -0800161 throw e.rethrowFromSystemServer();
162 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700163 }
164
165 /**
Felipe Lemee348dc32018-11-05 12:35:29 -0800166 * Checks whether content capture is enabled for this activity.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700167 */
168 public boolean isContentCaptureEnabled() {
Adam He6079d152019-01-10 11:37:17 -0800169 synchronized (mLock) {
170 return mService != null && !mDisabled;
171 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700172 }
173
174 /**
Felipe Leme284ad1c2018-11-15 18:16:12 -0800175 * Called by apps to explicitly enable or disable content capture.
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700176 *
177 * <p><b>Note: </b> this call is not persisted accross reboots, so apps should typically call
178 * it on {@link android.app.Activity#onCreate(android.os.Bundle, android.os.PersistableBundle)}.
179 */
Felipe Leme6b3a55c2018-11-13 17:14:03 -0800180 public void setContentCaptureEnabled(boolean enabled) {
Adam He6079d152019-01-10 11:37:17 -0800181 synchronized (mLock) {
182 mFlags |= enabled ? 0 : ContentCaptureContext.FLAG_DISABLED_BY_APP;
183 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800184 }
185
186 /**
Adam He3d0409b2019-01-15 14:22:04 -0800187 * Called by the app to request the Content Capture service to remove user-data associated with
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800188 * some context.
189 *
190 * @param request object specifying what user data should be removed.
191 */
192 public void removeUserData(@NonNull UserDataRemovalRequest request) {
Adam He3d0409b2019-01-15 14:22:04 -0800193 Preconditions.checkNotNull(request);
194
195 try {
Felipe Lemef2aa0d22019-01-28 10:38:46 -0800196 mService.removeUserData(request);
Adam He3d0409b2019-01-15 14:22:04 -0800197 } catch (RemoteException e) {
198 e.rethrowFromSystemServer();
199 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700200 }
201
Felipe Lemee348dc32018-11-05 12:35:29 -0800202 /** @hide */
203 public void dump(String prefix, PrintWriter pw) {
Adam He6079d152019-01-10 11:37:17 -0800204 synchronized (mLock) {
205 pw.print(prefix); pw.println("ContentCaptureManager");
206 pw.print(prefix); pw.print("Disabled: "); pw.println(mDisabled);
207 pw.print(prefix); pw.print("Context: "); pw.println(mContext);
208 pw.print(prefix); pw.print("User: "); pw.println(mContext.getUserId());
209 if (mService != null) {
210 pw.print(prefix); pw.print("Service: "); pw.println(mService);
211 }
212 pw.print(prefix); pw.print("Flags: "); pw.println(mFlags);
213 if (mMainSession != null) {
214 final String prefix2 = prefix + " ";
215 pw.print(prefix); pw.println("Main session:");
216 mMainSession.dump(prefix2, pw);
217 } else {
218 pw.print(prefix); pw.println("No sessions");
219 }
Felipe Lemee348dc32018-11-05 12:35:29 -0800220 }
221 }
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700222}