Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [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 | */ |
| 16 | package android.view.contentcapture; |
| 17 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_FINISHED; |
| 19 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_STARTED; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 20 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_APPEARED; |
| 21 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_DISAPPEARED; |
| 22 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_TEXT_CHANGED; |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 23 | import static android.view.contentcapture.ContentCaptureHelper.DEBUG; |
| 24 | import static android.view.contentcapture.ContentCaptureHelper.VERBOSE; |
| 25 | import static android.view.contentcapture.ContentCaptureHelper.getSanitizedString; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 26 | |
| 27 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 28 | |
| 29 | import android.annotation.NonNull; |
| 30 | import android.annotation.Nullable; |
| 31 | import android.content.ComponentName; |
| 32 | import android.content.Context; |
| 33 | import android.content.pm.ParceledListSlice; |
| 34 | import android.os.Bundle; |
| 35 | import android.os.Handler; |
| 36 | import android.os.IBinder; |
| 37 | import android.os.IBinder.DeathRecipient; |
| 38 | import android.os.RemoteException; |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 39 | import android.util.LocalLog; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 40 | import android.util.Log; |
| 41 | import android.util.TimeUtils; |
| 42 | import android.view.autofill.AutofillId; |
| 43 | import android.view.contentcapture.ViewNode.ViewStructureImpl; |
| 44 | |
| 45 | import com.android.internal.os.IResultReceiver; |
| 46 | |
| 47 | import java.io.PrintWriter; |
| 48 | import java.util.ArrayList; |
| 49 | import java.util.Collections; |
| 50 | import java.util.List; |
| 51 | import java.util.concurrent.atomic.AtomicBoolean; |
| 52 | |
| 53 | /** |
| 54 | * Main session associated with a context. |
| 55 | * |
| 56 | * <p>This session is created when the activity starts and finished when it stops; clients can use |
| 57 | * it to create children activities. |
| 58 | * |
| 59 | * <p><b>NOTE: all methods in this class should return right away, or do the real work in a handler |
| 60 | * thread. Hence, the only field that must be thread-safe is {@code mEnabled}, which is called at |
| 61 | * the beginning of every method. |
| 62 | * |
| 63 | * @hide |
| 64 | */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 65 | public final class MainContentCaptureSession extends ContentCaptureSession { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 66 | |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 67 | private static final String TAG = MainContentCaptureSession.class.getSimpleName(); |
| 68 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Handler message used to flush the buffer. |
| 71 | */ |
| 72 | private static final int MSG_FLUSH = 1; |
| 73 | |
| 74 | /** |
| 75 | * Maximum number of events that are buffered before sent to the app. |
| 76 | */ |
| 77 | // TODO(b/121044064): use settings |
| 78 | private static final int MAX_BUFFER_SIZE = 100; |
| 79 | |
| 80 | /** |
| 81 | * Frequency the buffer is flushed if stale. |
| 82 | */ |
| 83 | // TODO(b/121044064): use settings |
| 84 | private static final int FLUSHING_FREQUENCY_MS = 5_000; |
| 85 | |
| 86 | /** |
| 87 | * Name of the {@link IResultReceiver} extra used to pass the binder interface to the service. |
| 88 | * @hide |
| 89 | */ |
| 90 | public static final String EXTRA_BINDER = "binder"; |
| 91 | |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 92 | // TODO(b/111276913): make sure disabled state is in sync with manager's disabled |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 93 | @NonNull |
| 94 | private final AtomicBoolean mDisabled; |
| 95 | |
| 96 | @NonNull |
| 97 | private final Context mContext; |
| 98 | |
| 99 | @NonNull |
| 100 | private final Handler mHandler; |
| 101 | |
| 102 | /** |
| 103 | * Interface to the system_server binder object - it's only used to start the session (and |
| 104 | * notify when the session is finished). |
| 105 | */ |
| 106 | @Nullable |
| 107 | private final IContentCaptureManager mSystemServerInterface; |
| 108 | |
| 109 | /** |
| 110 | * Direct interface to the service binder object - it's used to send the events, including the |
| 111 | * last ones (when the session is finished) |
| 112 | */ |
| 113 | @Nullable |
| 114 | private IContentCaptureDirectManager mDirectServiceInterface; |
| 115 | @Nullable |
| 116 | private DeathRecipient mDirectServiceVulture; |
| 117 | |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 118 | private int mState = UNKNOWN_STATE; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 119 | |
| 120 | @Nullable |
| 121 | private IBinder mApplicationToken; |
| 122 | |
| 123 | @Nullable |
| 124 | private ComponentName mComponentName; |
| 125 | |
| 126 | /** |
| 127 | * List of events held to be sent as a batch. |
| 128 | */ |
| 129 | @Nullable |
| 130 | private ArrayList<ContentCaptureEvent> mEvents; |
| 131 | |
| 132 | // Used just for debugging purposes (on dump) |
| 133 | private long mNextFlush; |
| 134 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 135 | // TODO(b/121044064): use settings to set size |
| 136 | private final LocalLog mFlushHistory = new LocalLog(10); |
| 137 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 138 | /** @hide */ |
| 139 | protected MainContentCaptureSession(@NonNull Context context, @NonNull Handler handler, |
| 140 | @Nullable IContentCaptureManager systemServerInterface, |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 141 | @NonNull boolean disabled) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 142 | mContext = context; |
| 143 | mHandler = handler; |
| 144 | mSystemServerInterface = systemServerInterface; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 145 | mDisabled = new AtomicBoolean(disabled); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 148 | @Override |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 149 | MainContentCaptureSession getMainCaptureSession() { |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | @Override |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 154 | ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) { |
| 155 | final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext); |
| 156 | notifyChildSessionStarted(mId, child.mId, clientContext); |
| 157 | return child; |
| 158 | } |
| 159 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 160 | /** |
| 161 | * Starts this session. |
| 162 | * |
| 163 | * @hide |
| 164 | */ |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 165 | void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent, |
| 166 | int flags) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 167 | if (!isContentCaptureEnabled()) return; |
| 168 | |
| 169 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 170 | Log.v(TAG, "start(): token=" + applicationToken + ", comp=" |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 171 | + ComponentName.flattenToShortString(activityComponent)); |
| 172 | } |
| 173 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 174 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this, |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 175 | applicationToken, activityComponent, flags)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 179 | void flush(@FlushReason int reason) { |
| 180 | mHandler.sendMessage( |
| 181 | obtainMessage(MainContentCaptureSession::handleForceFlush, this, reason)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | void onDestroy() { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 186 | mHandler.removeMessages(MSG_FLUSH); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 187 | mHandler.sendMessage( |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 188 | obtainMessage(MainContentCaptureSession::handleDestroySession, this)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 191 | private void handleStartSession(@NonNull IBinder token, @NonNull ComponentName componentName, |
| 192 | int flags) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 193 | if (handleHasStarted()) { |
| 194 | // TODO(b/122959591): make sure this is expected (and when), or use Log.w |
| 195 | if (DEBUG) { |
| 196 | Log.d(TAG, "ignoring handleStartSession(" + token + "/" |
| 197 | + ComponentName.flattenToShortString(componentName) + " while on state " |
| 198 | + getStateAsString(mState)); |
| 199 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | mState = STATE_WAITING_FOR_SERVER; |
| 203 | mApplicationToken = token; |
| 204 | mComponentName = componentName; |
| 205 | |
| 206 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 207 | Log.v(TAG, "handleStartSession(): token=" + token + ", act=" |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 208 | + getDebugState() + ", id=" + mId); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 209 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 210 | |
| 211 | try { |
Adam He | 6884fed | 2019-01-07 13:18:32 -0800 | [diff] [blame] | 212 | if (mSystemServerInterface == null) return; |
| 213 | |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame] | 214 | mSystemServerInterface.startSession(mApplicationToken, componentName, mId, flags, |
| 215 | new IResultReceiver.Stub() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 216 | @Override |
| 217 | public void send(int resultCode, Bundle resultData) { |
| 218 | IBinder binder = null; |
| 219 | if (resultData != null) { |
| 220 | binder = resultData.getBinder(EXTRA_BINDER); |
| 221 | if (binder == null) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 222 | Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result"); |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 223 | handleResetSession(STATE_DISABLED | STATE_INTERNAL_ERROR); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | } |
| 227 | handleSessionStarted(resultCode, binder); |
| 228 | } |
| 229 | }); |
| 230 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 231 | Log.w(TAG, "Error starting session for " + componentName.flattenToShortString() + ": " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 232 | + e); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Callback from {@code system_server} after call to |
| 238 | * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String, |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 239 | * int, IResultReceiver)}. |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 240 | * |
| 241 | * @param resultCode session state |
| 242 | * @param binder handle to {@code IContentCaptureDirectManager} |
| 243 | */ |
| 244 | private void handleSessionStarted(int resultCode, @Nullable IBinder binder) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 245 | if (binder != null) { |
| 246 | mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder); |
| 247 | mDirectServiceVulture = () -> { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 248 | Log.w(TAG, "Destroying session " + mId + " because service died"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 249 | destroy(); |
| 250 | }; |
| 251 | try { |
| 252 | binder.linkToDeath(mDirectServiceVulture, 0); |
| 253 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 254 | Log.w(TAG, "Failed to link to death on " + binder + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 257 | |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 258 | if ((resultCode & STATE_DISABLED) != 0) { |
| 259 | handleResetSession(resultCode); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 260 | } else { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 261 | mState = resultCode; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 262 | mDisabled.set(false); |
| 263 | } |
| 264 | if (VERBOSE) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 265 | Log.v(TAG, "handleSessionStarted() result: id=" + mId + " resultCode=" + resultCode |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 266 | + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get() |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 267 | + ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size())); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 272 | final int eventType = event.getType(); |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 273 | if (VERBOSE) Log.v(TAG, "handleSendEvent(" + getDebugState() + "): " + event); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 274 | if (!handleHasStarted() && eventType != ContentCaptureEvent.TYPE_SESSION_STARTED) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 275 | // TODO(b/120494182): comment when this could happen (dialogs?) |
| 276 | Log.v(TAG, "handleSendEvent(" + getDebugState() + ", " |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 277 | + ContentCaptureEvent.getTypeAsString(eventType) |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 278 | + "): session not started yet"); |
| 279 | return; |
| 280 | } |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 281 | if (mDisabled.get()) { |
| 282 | // This happens when the event was queued in the handler before the sesison was ready, |
| 283 | // then handleSessionStarted() returned and set it as disabled - we need to drop it, |
| 284 | // otherwise it will keep triggering handleScheduleFlush() |
| 285 | if (VERBOSE) Log.v(TAG, "handleSendEvent(): ignoring when disabled"); |
| 286 | return; |
| 287 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 288 | if (mEvents == null) { |
| 289 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 290 | Log.v(TAG, "handleSendEvent(): creating buffer for " + MAX_BUFFER_SIZE + " events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 291 | } |
| 292 | mEvents = new ArrayList<>(MAX_BUFFER_SIZE); |
| 293 | } |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 294 | |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 295 | // Some type of events can be merged together |
| 296 | boolean addEvent = true; |
| 297 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 298 | if (!mEvents.isEmpty() && eventType == TYPE_VIEW_TEXT_CHANGED) { |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 299 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 300 | |
| 301 | // TODO(b/121045053): check if flags match |
| 302 | if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED |
| 303 | && lastEvent.getId().equals(event.getId())) { |
| 304 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 305 | Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text=" |
| 306 | + getSanitizedString(event.getText())); |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 307 | } |
| 308 | lastEvent.setText(event.getText()); |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 309 | addEvent = false; |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 310 | } |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if (!mEvents.isEmpty() && eventType == TYPE_VIEW_DISAPPEARED) { |
| 314 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 315 | if (lastEvent.getType() == TYPE_VIEW_DISAPPEARED |
| 316 | && event.getSessionId().equals(lastEvent.getSessionId())) { |
| 317 | if (VERBOSE) { |
| 318 | Log.v(TAG, "Buffering TYPE_VIEW_DISAPPEARED events for session " |
| 319 | + lastEvent.getSessionId()); |
| 320 | } |
| 321 | lastEvent.addAutofillId(event.getId()); |
| 322 | addEvent = false; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (addEvent) { |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 327 | mEvents.add(event); |
| 328 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 329 | |
| 330 | final int numberEvents = mEvents.size(); |
| 331 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 332 | final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE; |
| 333 | |
| 334 | if (bufferEvent && !forceFlush) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 335 | handleScheduleFlush(FLUSH_REASON_IDLE_TIMEOUT, /* checkExisting= */ true); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 339 | if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 340 | // Callback from startSession hasn't been called yet - typically happens on system |
| 341 | // apps that are started before the system service |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 342 | // TODO(b/122959591): try to ignore session while system is not ready / boot |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 343 | // not complete instead. Similarly, the manager service should return right away |
| 344 | // when the user does not have a service set |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 345 | if (DEBUG) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 346 | Log.d(TAG, "Closing session for " + getDebugState() |
| 347 | + " after " + numberEvents + " delayed events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 348 | } |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 349 | handleResetSession(STATE_DISABLED | STATE_NO_RESPONSE); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 350 | // TODO(b/111276913): blacklist activity / use special flag to indicate that |
| 351 | // when it's launched again |
| 352 | return; |
| 353 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 354 | final int flushReason; |
| 355 | switch (eventType) { |
| 356 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 357 | flushReason = FLUSH_REASON_SESSION_STARTED; |
| 358 | break; |
| 359 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
| 360 | flushReason = FLUSH_REASON_SESSION_FINISHED; |
| 361 | break; |
| 362 | default: |
| 363 | flushReason = FLUSH_REASON_FULL; |
| 364 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 365 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 366 | handleForceFlush(flushReason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 369 | private boolean handleHasStarted() { |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 370 | return mState != UNKNOWN_STATE; |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 373 | private void handleScheduleFlush(@FlushReason int reason, boolean checkExisting) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 374 | if (VERBOSE) { |
| 375 | Log.v(TAG, "handleScheduleFlush(" + getDebugState(reason) |
| 376 | + ", checkExisting=" + checkExisting); |
| 377 | } |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 378 | if (!handleHasStarted()) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 379 | if (VERBOSE) Log.v(TAG, "handleScheduleFlush(): session not started yet"); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | if (mDisabled.get()) { |
| 384 | // Should not be called on this state, as handleSendEvent checks. |
| 385 | // But we rather add one if check and log than re-schedule and keep the session alive... |
| 386 | Log.e(TAG, "handleScheduleFlush(" + getDebugState(reason) + "): should not be called " |
| 387 | + "when disabled. events=" + (mEvents == null ? null : mEvents.size())); |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 388 | return; |
| 389 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 390 | if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) { |
| 391 | // "Renew" the flush message by removing the previous one |
| 392 | mHandler.removeMessages(MSG_FLUSH); |
| 393 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 394 | mNextFlush = System.currentTimeMillis() + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 395 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 396 | Log.v(TAG, "handleScheduleFlush(): scheduled to flush in " |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 397 | + FLUSHING_FREQUENCY_MS + "ms: " + TimeUtils.logTimeOfDay(mNextFlush)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 398 | } |
| 399 | mHandler.sendMessageDelayed( |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 400 | obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this, reason) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 401 | .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS); |
| 402 | } |
| 403 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 404 | private void handleFlushIfNeeded(@FlushReason int reason) { |
Felipe Leme | 3d570a4 | 2019-01-28 13:04:34 -0800 | [diff] [blame] | 405 | if (mEvents == null || mEvents.isEmpty()) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 406 | if (VERBOSE) Log.v(TAG, "Nothing to flush"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 407 | return; |
| 408 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 409 | handleForceFlush(reason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 412 | private void handleForceFlush(@FlushReason int reason) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 413 | if (mEvents == null) return; |
| 414 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 415 | if (mDisabled.get()) { |
| 416 | Log.e(TAG, "handleForceFlush(" + getDebugState(reason) + "): should not be when " |
| 417 | + "disabled"); |
| 418 | return; |
| 419 | } |
| 420 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 421 | if (mDirectServiceInterface == null) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 422 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 423 | Log.v(TAG, "handleForceFlush(" + getDebugState(reason) + "): hold your horses, " |
| 424 | + "client not ready: " + mEvents); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 425 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 426 | if (!mHandler.hasMessages(MSG_FLUSH)) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 427 | handleScheduleFlush(reason, /* checkExisting= */ false); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 428 | } |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | final int numberEvents = mEvents.size(); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 433 | final String reasonString = getflushReasonAsString(reason); |
| 434 | if (DEBUG) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 435 | Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getDebugState(reason)); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 436 | } |
| 437 | // Logs reason, size, max size, idle timeout |
| 438 | final String logRecord = "r=" + reasonString + " s=" + numberEvents |
| 439 | + " m=" + MAX_BUFFER_SIZE + " i=" + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 440 | try { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 441 | mFlushHistory.log(logRecord); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 442 | mHandler.removeMessages(MSG_FLUSH); |
| 443 | |
| 444 | final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents(); |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 445 | mDirectServiceInterface.sendEvents(events); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 446 | } catch (RemoteException e) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 447 | Log.w(TAG, "Error sending " + numberEvents + " for " + getDebugState() |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 448 | + ": " + e); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Resets the buffer and return a {@link ParceledListSlice} with the previous events. |
| 454 | */ |
| 455 | @NonNull |
| 456 | private ParceledListSlice<ContentCaptureEvent> handleClearEvents() { |
| 457 | // NOTE: we must save a reference to the current mEvents and then set it to to null, |
| 458 | // otherwise clearing it would clear it in the receiving side if the service is also local. |
| 459 | final List<ContentCaptureEvent> events = mEvents == null |
| 460 | ? Collections.emptyList() |
| 461 | : mEvents; |
| 462 | mEvents = null; |
| 463 | return new ParceledListSlice<>(events); |
| 464 | } |
| 465 | |
| 466 | private void handleDestroySession() { |
| 467 | if (DEBUG) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 468 | Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 469 | + (mEvents == null ? 0 : mEvents.size()) + " event(s) for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 470 | + getDebugState()); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | try { |
Adam He | 6884fed | 2019-01-07 13:18:32 -0800 | [diff] [blame] | 474 | if (mSystemServerInterface == null) return; |
| 475 | |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame] | 476 | mSystemServerInterface.finishSession(mId); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 477 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 478 | Log.e(TAG, "Error destroying system-service session " + mId + " for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 479 | + getDebugState() + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 483 | // TODO(b/122454205): once we support multiple sessions, we might need to move some of these |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 484 | // clearings out. |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 485 | private void handleResetSession(int newState) { |
| 486 | if (VERBOSE) { |
| 487 | Log.v(TAG, "handleResetSession(" + getActivityName() + "): from " |
| 488 | + getStateAsString(mState) + " to " + getStateAsString(newState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 489 | } |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 490 | mState = newState; |
| 491 | mDisabled.set((newState & STATE_DISABLED) != 0); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 492 | // TODO(b/122454205): must reset children (which currently is owned by superclass) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 493 | mApplicationToken = null; |
| 494 | mComponentName = null; |
| 495 | mEvents = null; |
| 496 | if (mDirectServiceInterface != null) { |
| 497 | mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 498 | } |
| 499 | mDirectServiceInterface = null; |
| 500 | mHandler.removeMessages(MSG_FLUSH); |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 505 | notifyViewAppeared(mId, node); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Override |
| 509 | void internalNotifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 510 | notifyViewDisappeared(mId, id); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | @Override |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 514 | void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) { |
| 515 | notifyViewTextChanged(mId, id, text); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | @Override |
| 519 | boolean isContentCaptureEnabled() { |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 520 | return super.isContentCaptureEnabled() && mSystemServerInterface != null |
| 521 | && !mDisabled.get(); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 524 | // TODO(b/122454205): refactor "notifyXXXX" methods below to a common "Buffer" object that is |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 525 | // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such |
| 526 | // change should also get get rid of the "internalNotifyXXXX" methods above |
| 527 | void notifyChildSessionStarted(@NonNull String parentSessionId, |
| 528 | @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) { |
| 529 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 530 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED) |
| 531 | .setParentSessionId(parentSessionId) |
| 532 | .setClientContext(clientContext), |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 533 | /* forceFlush= */ true)); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void notifyChildSessionFinished(@NonNull String parentSessionId, |
| 537 | @NonNull String childSessionId) { |
| 538 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 539 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED) |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 540 | .setParentSessionId(parentSessionId), /* forceFlush= */ true)); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) { |
| 544 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 545 | new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED) |
| 546 | .setViewNode(node.mNode), /* forceFlush= */ false)); |
| 547 | } |
| 548 | |
| 549 | void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) { |
| 550 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 551 | new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id), |
| 552 | /* forceFlush= */ false)); |
| 553 | } |
| 554 | |
| 555 | void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id, |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 556 | @Nullable CharSequence text) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 557 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 558 | new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED).setAutofillId(id) |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 559 | .setText(text), /* forceFlush= */ false)); |
| 560 | } |
| 561 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 562 | @Override |
| 563 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 564 | pw.print(prefix); pw.print("mContext: "); pw.println(mContext); |
| 565 | pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId()); |
| 566 | if (mSystemServerInterface != null) { |
| 567 | pw.print(prefix); pw.print("mSystemServerInterface: "); |
| 568 | pw.println(mSystemServerInterface); |
| 569 | } |
| 570 | if (mDirectServiceInterface != null) { |
| 571 | pw.print(prefix); pw.print("mDirectServiceInterface: "); |
| 572 | pw.println(mDirectServiceInterface); |
| 573 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 574 | pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get()); |
| 575 | pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled()); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 576 | pw.print(prefix); pw.print("state: "); pw.println(getStateAsString(mState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 577 | if (mApplicationToken != null) { |
| 578 | pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken); |
| 579 | } |
| 580 | if (mComponentName != null) { |
| 581 | pw.print(prefix); pw.print("component name: "); |
| 582 | pw.println(mComponentName.flattenToShortString()); |
| 583 | } |
| 584 | if (mEvents != null && !mEvents.isEmpty()) { |
| 585 | final int numberEvents = mEvents.size(); |
| 586 | pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents); |
| 587 | pw.print('/'); pw.println(MAX_BUFFER_SIZE); |
| 588 | if (VERBOSE && numberEvents > 0) { |
| 589 | final String prefix3 = prefix + " "; |
| 590 | for (int i = 0; i < numberEvents; i++) { |
| 591 | final ContentCaptureEvent event = mEvents.get(i); |
| 592 | pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw); |
| 593 | pw.println(); |
| 594 | } |
| 595 | } |
| 596 | pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS); |
| 597 | pw.print(prefix); pw.print("next flush: "); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 598 | TimeUtils.formatDuration(mNextFlush - System.currentTimeMillis(), pw); |
| 599 | pw.print(" ("); pw.print(TimeUtils.logTimeOfDay(mNextFlush)); pw.println(")"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 600 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 601 | pw.print(prefix); pw.println("flush history:"); |
| 602 | mFlushHistory.reverseDump(/* fd= */ null, pw, /* args= */ null); pw.println(); |
| 603 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 604 | super.dump(prefix, pw); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Gets a string that can be used to identify the activity on logging statements. |
| 609 | */ |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 610 | private String getActivityName() { |
| 611 | return mComponentName == null |
| 612 | ? "pkg:" + mContext.getPackageName() |
| 613 | : "act:" + mComponentName.flattenToShortString(); |
| 614 | } |
| 615 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 616 | @NonNull |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 617 | private String getDebugState() { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 618 | return getActivityName() + " [state=" + getStateAsString(mState) + ", disabled=" |
| 619 | + mDisabled.get() + "]"; |
| 620 | } |
| 621 | |
| 622 | @NonNull |
| 623 | private String getDebugState(@FlushReason int reason) { |
| 624 | return getDebugState() + ", reason=" + getflushReasonAsString(reason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 625 | } |
| 626 | } |