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