summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/appop/AttributedOp.java
blob: dcc36bcf6149cbde873006c1b10192867a2aa513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.appop;

import static android.app.AppOpsManager.MODE_ALLOWED;
import static android.app.AppOpsManager.OnOpStartedListener.START_TYPE_RESUMED;
import static android.app.AppOpsManager.makeKey;

import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AppOpsManager;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.ArrayMap;
import android.util.LongSparseArray;
import android.util.Pools;
import android.util.Slog;

import com.android.internal.util.function.pooled.PooledLambda;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

final class AttributedOp {
    private final @NonNull AppOpsService mAppOpsService;
    public final @Nullable String tag;
    public final @NonNull AppOpsService.Op parent;

    /**
     * Last successful accesses (noteOp + finished startOp) for each uidState/opFlag combination
     *
     * <p>Key is {@link AppOpsManager#makeKey}
     */
    // TODO(b/248108338)
    // @GuardedBy("mAppOpsService")
    private @Nullable LongSparseArray<AppOpsManager.NoteOpEvent> mAccessEvents;

    /**
     * Last rejected accesses for each uidState/opFlag combination
     *
     * <p>Key is {@link AppOpsManager#makeKey}
     */
    // TODO(b/248108338)
    // @GuardedBy("mAppOpsService")
    private @Nullable LongSparseArray<AppOpsManager.NoteOpEvent> mRejectEvents;

    /**
     * Currently in progress startOp events
     *
     * <p>Key is clientId
     */
    // TODO(b/248108338)
    // @GuardedBy("mAppOpsService")
    @Nullable ArrayMap<IBinder, InProgressStartOpEvent> mInProgressEvents;

    /**
     * Currently paused startOp events
     *
     * <p>Key is clientId
     */
    // TODO(b/248108338)
    // @GuardedBy("mAppOpsService")
    @Nullable ArrayMap<IBinder, InProgressStartOpEvent> mPausedInProgressEvents;

    AttributedOp(@NonNull AppOpsService appOpsService, @Nullable String tag,
            @NonNull AppOpsService.Op parent) {
        mAppOpsService = appOpsService;
        this.tag = tag;
        this.parent = parent;
    }

    /**
     * Update state when noteOp was rejected or startOp->finishOp event finished
     *
     * @param proxyUid            The uid of the proxy
     * @param proxyPackageName    The package name of the proxy
     * @param proxyAttributionTag the attributionTag in the proxies package
     * @param uidState            UID state of the app noteOp/startOp was called for
     * @param flags               OpFlags of the call
     */
    public void accessed(int proxyUid, @Nullable String proxyPackageName,
            @Nullable String proxyAttributionTag, @AppOpsManager.UidState int uidState,
            @AppOpsManager.OpFlags int flags) {
        long accessTime = System.currentTimeMillis();
        accessed(accessTime, -1, proxyUid, proxyPackageName,
                proxyAttributionTag, uidState, flags);

        mAppOpsService.mHistoricalRegistry.incrementOpAccessedCount(parent.op, parent.uid,
                parent.packageName, tag, uidState, flags, accessTime,
                AppOpsManager.ATTRIBUTION_FLAGS_NONE, AppOpsManager.ATTRIBUTION_CHAIN_ID_NONE);
    }

    /**
     * Add an access that was previously collected.
     *
     * @param noteTime            The time of the event
     * @param duration            The duration of the event
     * @param proxyUid            The uid of the proxy
     * @param proxyPackageName    The package name of the proxy
     * @param proxyAttributionTag the attributionTag in the proxies package
     * @param uidState            UID state of the app noteOp/startOp was called for
     * @param flags               OpFlags of the call
     */
    @SuppressWarnings("GuardedBy") // Lock is held on mAppOpsService
    public void accessed(long noteTime, long duration, int proxyUid,
            @Nullable String proxyPackageName, @Nullable String proxyAttributionTag,
            @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags) {
        long key = makeKey(uidState, flags);

        if (mAccessEvents == null) {
            mAccessEvents = new LongSparseArray<>(1);
        }

        AppOpsManager.OpEventProxyInfo proxyInfo = null;
        if (proxyUid != Process.INVALID_UID) {
            proxyInfo = mAppOpsService.mOpEventProxyInfoPool.acquire(proxyUid, proxyPackageName,
                    proxyAttributionTag);
        }

        AppOpsManager.NoteOpEvent existingEvent = mAccessEvents.get(key);
        if (existingEvent != null) {
            existingEvent.reinit(noteTime, duration, proxyInfo,
                    mAppOpsService.mOpEventProxyInfoPool);
        } else {
            mAccessEvents.put(key, new AppOpsManager.NoteOpEvent(noteTime, duration, proxyInfo));
        }
    }

    /**
     * Update state when noteOp/startOp was rejected.
     *
     * @param uidState UID state of the app noteOp is called for
     * @param flags    OpFlags of the call
     */
    public void rejected(@AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags) {
        rejected(System.currentTimeMillis(), uidState, flags);

        mAppOpsService.mHistoricalRegistry.incrementOpRejected(parent.op, parent.uid,
                parent.packageName, tag, uidState, flags);
    }

    /**
     * Add an rejection that was previously collected
     *
     * @param noteTime The time of the event
     * @param uidState UID state of the app noteOp/startOp was called for
     * @param flags    OpFlags of the call
     */
    @SuppressWarnings("GuardedBy") // Lock is held on mAppOpsService
    public void rejected(long noteTime, @AppOpsManager.UidState int uidState,
            @AppOpsManager.OpFlags int flags) {
        long key = makeKey(uidState, flags);

        if (mRejectEvents == null) {
            mRejectEvents = new LongSparseArray<>(1);
        }

        // We do not collect proxy information for rejections yet
        AppOpsManager.NoteOpEvent existingEvent = mRejectEvents.get(key);
        if (existingEvent != null) {
            existingEvent.reinit(noteTime, -1, null, mAppOpsService.mOpEventProxyInfoPool);
        } else {
            mRejectEvents.put(key, new AppOpsManager.NoteOpEvent(noteTime, -1, null));
        }
    }

    /**
     * Update state when start was called
     *
     * @param clientId            Id of the startOp caller
     * @param proxyUid            The UID of the proxy app
     * @param proxyPackageName    The package name of the proxy app
     * @param proxyAttributionTag The attribution tag of the proxy app
     * @param uidState            UID state of the app startOp is called for
     * @param flags               The proxy flags
     * @param attributionFlags    The attribution flags associated with this operation.
     * @param attributionChainId  The if of the attribution chain this operations is a part of.
     */
    public void started(@NonNull IBinder clientId, int proxyUid,
            @Nullable String proxyPackageName, @Nullable String proxyAttributionTag,
            @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags,
            @AppOpsManager.AttributionFlags
                    int attributionFlags, int attributionChainId) throws RemoteException {
        started(clientId, proxyUid, proxyPackageName, proxyAttributionTag,
                uidState, flags, /*triggerCallbackIfNeeded*/ true, attributionFlags,
                attributionChainId);
    }

    private void started(@NonNull IBinder clientId, int proxyUid,
            @Nullable String proxyPackageName, @Nullable String proxyAttributionTag,
            @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags,
            boolean triggerCallbackIfNeeded, @AppOpsManager.AttributionFlags int attributionFlags,
            int attributionChainId) throws RemoteException {
        startedOrPaused(clientId, proxyUid, proxyPackageName,
                proxyAttributionTag, uidState, flags, triggerCallbackIfNeeded,
                /*triggerCallbackIfNeeded*/ true, attributionFlags, attributionChainId);
    }

    @SuppressWarnings("GuardedBy") // Lock is held on mAppOpsService
    private void startedOrPaused(@NonNull IBinder clientId, int proxyUid,
            @Nullable String proxyPackageName, @Nullable String proxyAttributionTag,
            @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags,
            boolean triggerCallbackIfNeeded, boolean isStarted, @AppOpsManager.AttributionFlags
            int attributionFlags, int attributionChainId) throws RemoteException {
        if (triggerCallbackIfNeeded && !parent.isRunning() && isStarted) {
            mAppOpsService.scheduleOpActiveChangedIfNeededLocked(parent.op, parent.uid,
                    parent.packageName, tag, true, attributionFlags, attributionChainId);
        }

        if (isStarted && mInProgressEvents == null) {
            mInProgressEvents = new ArrayMap<>(1);
        } else if (!isStarted && mPausedInProgressEvents == null) {
            mPausedInProgressEvents = new ArrayMap<>(1);
        }
        ArrayMap<IBinder, InProgressStartOpEvent> events = isStarted
                ? mInProgressEvents : mPausedInProgressEvents;

        long startTime = System.currentTimeMillis();
        InProgressStartOpEvent event = events.get(clientId);
        if (event == null) {
            event = mAppOpsService.mInProgressStartOpEventPool.acquire(startTime,
                    SystemClock.elapsedRealtime(), clientId, tag,
                    PooledLambda.obtainRunnable(AppOpsService::onClientDeath, this, clientId),
                    proxyUid, proxyPackageName, proxyAttributionTag, uidState, flags,
                    attributionFlags, attributionChainId);
            events.put(clientId, event);
        } else {
            if (uidState != event.getUidState()) {
                onUidStateChanged(uidState);
            }
        }

        event.mNumUnfinishedStarts++;

        if (isStarted) {
            mAppOpsService.mHistoricalRegistry.incrementOpAccessedCount(parent.op, parent.uid,
                    parent.packageName, tag, uidState, flags, startTime, attributionFlags,
                    attributionChainId);
        }
    }

    /**
     * Update state when finishOp was called. Will finish started ops, and delete paused ops.
     *
     * @param clientId Id of the finishOp caller
     */
    public void finished(@NonNull IBinder clientId) {
        finished(clientId, true);
    }

    private void finished(@NonNull IBinder clientId, boolean triggerCallbackIfNeeded) {
        finishOrPause(clientId, triggerCallbackIfNeeded, false);
    }

    /**
     * Update state when paused or finished is called. If pausing, it records the op as
     * stopping in the HistoricalRegistry, but does not delete it.
     */
    @SuppressWarnings("GuardedBy") // Lock is held on mAppOpsService
    private void finishOrPause(@NonNull IBinder clientId, boolean triggerCallbackIfNeeded,
            boolean isPausing) {
        int indexOfToken = isRunning() ? mInProgressEvents.indexOfKey(clientId) : -1;
        if (indexOfToken < 0) {
            finishPossiblyPaused(clientId, isPausing);
            return;
        }

        InProgressStartOpEvent event = mInProgressEvents.valueAt(indexOfToken);
        if (!isPausing) {
            event.mNumUnfinishedStarts--;
        }
        // If we are pausing, create a NoteOpEvent, but don't change the InProgress event
        if (event.mNumUnfinishedStarts == 0 || isPausing) {
            if (!isPausing) {
                event.finish();
                mInProgressEvents.removeAt(indexOfToken);
            }

            if (mAccessEvents == null) {
                mAccessEvents = new LongSparseArray<>(1);
            }

            AppOpsManager.OpEventProxyInfo proxyCopy = event.getProxy() != null
                    ? new AppOpsManager.OpEventProxyInfo(event.getProxy()) : null;

            long accessDurationMillis =
                    SystemClock.elapsedRealtime() - event.getStartElapsedTime();
            AppOpsManager.NoteOpEvent finishedEvent = new AppOpsManager.NoteOpEvent(
                    event.getStartTime(),
                    accessDurationMillis, proxyCopy);
            mAccessEvents.put(makeKey(event.getUidState(), event.getFlags()),
                    finishedEvent);

            mAppOpsService.mHistoricalRegistry.increaseOpAccessDuration(parent.op, parent.uid,
                    parent.packageName, tag, event.getUidState(),
                    event.getFlags(), finishedEvent.getNoteTime(), finishedEvent.getDuration(),
                    event.getAttributionFlags(), event.getAttributionChainId());

            if (!isPausing) {
                mAppOpsService.mInProgressStartOpEventPool.release(event);
                if (mInProgressEvents.isEmpty()) {
                    mInProgressEvents = null;

                    // TODO ntmyren: Also callback for single attribution tag activity changes
                    if (triggerCallbackIfNeeded && !parent.isRunning()) {
                        mAppOpsService.scheduleOpActiveChangedIfNeededLocked(parent.op,
                                parent.uid, parent.packageName, tag, false,
                                event.getAttributionFlags(), event.getAttributionChainId());
                    }
                }
            }
        }
    }

    // Finish or pause (no-op) an already paused op
    @SuppressWarnings("GuardedBy") // Lock is held on mAppOpsService
    private void finishPossiblyPaused(@NonNull IBinder clientId, boolean isPausing) {
        if (!isPaused()) {
            Slog.wtf(AppOpsService.TAG, "No ops running or paused");
            return;
        }

        int indexOfToken = mPausedInProgressEvents.indexOfKey(clientId);
        if (indexOfToken < 0) {
            Slog.wtf(AppOpsService.TAG, "No op running or paused for the client");
            return;
        } else if (isPausing) {
            // already paused
            return;
        }

        // no need to record a paused event finishing.
        InProgressStartOpEvent event = mPausedInProgressEvents.valueAt(indexOfToken);
        event.mNumUnfinishedStarts--;
        if (event.mNumUnfinishedStarts == 0) {
            mPausedInProgressEvents.removeAt(indexOfToken);
            mAppOpsService.mInProgressStartOpEventPool.release(event);
            if (mPausedInProgressEvents.isEmpty()) {
                mPausedInProgressEvents = null;
            }
        }
    }

    /**
     * Create an event that will be started, if the op is unpaused.
     */
    public void createPaused(@NonNull IBinder clientId, int proxyUid,
            @Nullable String proxyPackageName, @Nullable String proxyAttributionTag,
            @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags,
            @AppOpsManager.AttributionFlags
                    int attributionFlags, int attributionChainId) throws RemoteException {
        startedOrPaused(clientId, proxyUid, proxyPackageName, proxyAttributionTag,
                uidState, flags, true, false, attributionFlags, attributionChainId);
    }

    /**
     * Pause all currently started ops. This will create a HistoricalRegistry
     */
    public void pause() {
        if (!isRunning()) {
            return;
        }

        if (mPausedInProgressEvents == null) {
            mPausedInProgressEvents = new ArrayMap<>(1);
        }

        for (int i = 0; i < mInProgressEvents.size(); i++) {
            InProgressStartOpEvent event = mInProgressEvents.valueAt(i);
            mPausedInProgressEvents.put(event.getClientId(), event);
            finishOrPause(event.getClientId(), true, true);

            mAppOpsService.scheduleOpActiveChangedIfNeededLocked(parent.op, parent.uid,
                    parent.packageName, tag, false,
                    event.getAttributionFlags(), event.getAttributionChainId());
        }
        mInProgressEvents = null;
    }

    /**
     * Unpause all currently paused ops. This will reinitialize their start and duration
     * times, but keep all other values the same
     */
    public void resume() {
        if (!isPaused()) {
            return;
        }

        if (mInProgressEvents == null) {
            mInProgressEvents = new ArrayMap<>(mPausedInProgressEvents.size());
        }
        boolean shouldSendActive = !mPausedInProgressEvents.isEmpty()
                && mInProgressEvents.isEmpty();

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < mPausedInProgressEvents.size(); i++) {
            InProgressStartOpEvent event = mPausedInProgressEvents.valueAt(i);
            mInProgressEvents.put(event.getClientId(), event);
            event.setStartElapsedTime(SystemClock.elapsedRealtime());
            event.setStartTime(startTime);
            mAppOpsService.mHistoricalRegistry.incrementOpAccessedCount(parent.op, parent.uid,
                    parent.packageName, tag, event.getUidState(), event.getFlags(), startTime,
                    event.getAttributionFlags(), event.getAttributionChainId());
            if (shouldSendActive) {
                mAppOpsService.scheduleOpActiveChangedIfNeededLocked(parent.op, parent.uid,
                        parent.packageName, tag, true, event.getAttributionFlags(),
                        event.getAttributionChainId());
            }
            // Note: this always sends MODE_ALLOWED, even if the mode is FOREGROUND
            // TODO ntmyren: figure out how to get the real mode.
            mAppOpsService.scheduleOpStartedIfNeededLocked(parent.op, parent.uid,
                    parent.packageName, tag, event.getFlags(), MODE_ALLOWED, START_TYPE_RESUMED,
                    event.getAttributionFlags(), event.getAttributionChainId());
        }
        mPausedInProgressEvents = null;
    }

    /**
     * Called in the case the client dies without calling finish first
     *
     * @param clientId The client that died
     */
    void onClientDeath(@NonNull IBinder clientId) {
        synchronized (mAppOpsService) {
            if (!isPaused() && !isRunning()) {
                return;
            }

            ArrayMap<IBinder, InProgressStartOpEvent> events = isPaused()
                    ? mPausedInProgressEvents : mInProgressEvents;
            InProgressStartOpEvent deadEvent = events.get(clientId);
            if (deadEvent != null) {
                deadEvent.mNumUnfinishedStarts = 1;
            }

            finished(clientId);
        }
    }

    /**
     * Notify that the state of the uid changed
     *
     * @param newState The new state
     */
    public void onUidStateChanged(@AppOpsManager.UidState int newState) {
        if (!isPaused() && !isRunning()) {
            return;
        }

        boolean isRunning = isRunning();
        ArrayMap<IBinder, InProgressStartOpEvent> events =
                isRunning ? mInProgressEvents : mPausedInProgressEvents;

        int numInProgressEvents = events.size();
        List<IBinder> binders = new ArrayList<>(events.keySet());
        for (int i = 0; i < numInProgressEvents; i++) {
            InProgressStartOpEvent event = events.get(binders.get(i));

            if (event != null && event.getUidState() != newState) {
                try {
                    // Remove all but one unfinished start count and then call finished() to
                    // remove start event object
                    int numPreviousUnfinishedStarts = event.mNumUnfinishedStarts;
                    event.mNumUnfinishedStarts = 1;
                    AppOpsManager.OpEventProxyInfo proxy = event.getProxy();

                    finished(event.getClientId(), false);

                    // Call started() to add a new start event object and then add the
                    // previously removed unfinished start counts back
                    if (proxy != null) {
                        startedOrPaused(event.getClientId(), proxy.getUid(),
                                proxy.getPackageName(), proxy.getAttributionTag(), newState,
                                event.getFlags(), false, isRunning,
                                event.getAttributionFlags(), event.getAttributionChainId());
                    } else {
                        startedOrPaused(event.getClientId(), Process.INVALID_UID, null, null,
                                newState, event.getFlags(), false, isRunning,
                                event.getAttributionFlags(), event.getAttributionChainId());
                    }

                    events = isRunning ? mInProgressEvents : mPausedInProgressEvents;
                    InProgressStartOpEvent newEvent = events.get(binders.get(i));
                    if (newEvent != null) {
                        newEvent.mNumUnfinishedStarts += numPreviousUnfinishedStarts - 1;
                    }
                } catch (RemoteException e) {
                    if (AppOpsService.DEBUG) {
                        Slog.e(AppOpsService.TAG,
                                "Cannot switch to new uidState " + newState);
                    }
                }
            }
        }
    }

    /**
     * Combine {@code a} and {@code b} and return the result. The result might be {@code a}
     * or {@code b}. If there is an event for the same key in both the later event is retained.
     */
    private @Nullable LongSparseArray<AppOpsManager.NoteOpEvent> add(
            @Nullable LongSparseArray<AppOpsManager.NoteOpEvent> a,
            @Nullable LongSparseArray<AppOpsManager.NoteOpEvent> b) {
        if (a == null) {
            return b;
        }

        if (b == null) {
            return a;
        }

        int numEventsToAdd = b.size();
        for (int i = 0; i < numEventsToAdd; i++) {
            long keyOfEventToAdd = b.keyAt(i);
            AppOpsManager.NoteOpEvent bEvent = b.valueAt(i);
            AppOpsManager.NoteOpEvent aEvent = a.get(keyOfEventToAdd);

            if (aEvent == null || bEvent.getNoteTime() > aEvent.getNoteTime()) {
                a.put(keyOfEventToAdd, bEvent);
            }
        }

        return a;
    }

    /**
     * Add all data from the {@code opToAdd} to this op.
     *
     * <p>If there is an event for the same key in both the later event is retained.
     * <p>{@code opToAdd} should not be used after this method is called.
     *
     * @param opToAdd The op to add
     */
    @SuppressWarnings("GuardedBy") // Lock is held on mAppOpsService
    public void add(@NonNull AttributedOp opToAdd) {
        if (opToAdd.isRunning() || opToAdd.isPaused()) {
            ArrayMap<IBinder, InProgressStartOpEvent> ignoredEvents =
                    opToAdd.isRunning()
                            ? opToAdd.mInProgressEvents : opToAdd.mPausedInProgressEvents;
            Slog.w(AppOpsService.TAG, "Ignoring " + ignoredEvents.size() + " app-ops, running: "
                    + opToAdd.isRunning());

            int numInProgressEvents = ignoredEvents.size();
            for (int i = 0; i < numInProgressEvents; i++) {
                InProgressStartOpEvent event = ignoredEvents.valueAt(i);

                event.finish();
                mAppOpsService.mInProgressStartOpEventPool.release(event);
            }
        }

        mAccessEvents = add(mAccessEvents, opToAdd.mAccessEvents);
        mRejectEvents = add(mRejectEvents, opToAdd.mRejectEvents);
    }

    public boolean isRunning() {
        return mInProgressEvents != null && !mInProgressEvents.isEmpty();
    }

    public boolean isPaused() {
        return mPausedInProgressEvents != null && !mPausedInProgressEvents.isEmpty();
    }

    boolean hasAnyTime() {
        return (mAccessEvents != null && mAccessEvents.size() > 0)
                || (mRejectEvents != null && mRejectEvents.size() > 0);
    }

    /**
     * Clone a {@link LongSparseArray} and clone all values.
     */
    private @Nullable LongSparseArray<AppOpsManager.NoteOpEvent> deepClone(
            @Nullable LongSparseArray<AppOpsManager.NoteOpEvent> original) {
        if (original == null) {
            return original;
        }

        int size = original.size();
        LongSparseArray<AppOpsManager.NoteOpEvent> clone = new LongSparseArray<>(size);
        for (int i = 0; i < size; i++) {
            clone.put(original.keyAt(i), new AppOpsManager.NoteOpEvent(original.valueAt(i)));
        }

        return clone;
    }

    @NonNull AppOpsManager.AttributedOpEntry createAttributedOpEntryLocked() {
        LongSparseArray<AppOpsManager.NoteOpEvent> accessEvents = deepClone(mAccessEvents);

        // Add in progress events as access events
        if (isRunning()) {
            long now = SystemClock.elapsedRealtime();
            int numInProgressEvents = mInProgressEvents.size();

            if (accessEvents == null) {
                accessEvents = new LongSparseArray<>(numInProgressEvents);
            }

            for (int i = 0; i < numInProgressEvents; i++) {
                InProgressStartOpEvent event = mInProgressEvents.valueAt(i);

                accessEvents.append(makeKey(event.getUidState(), event.getFlags()),
                        new AppOpsManager.NoteOpEvent(event.getStartTime(),
                                now - event.getStartElapsedTime(),
                                event.getProxy()));
            }
        }

        LongSparseArray<AppOpsManager.NoteOpEvent> rejectEvents = deepClone(mRejectEvents);

        return new AppOpsManager.AttributedOpEntry(parent.op, isRunning(), accessEvents,
                rejectEvents);
    }

    /** A in progress startOp->finishOp event */
    static final class InProgressStartOpEvent implements IBinder.DeathRecipient {
        /** Wall clock time of startOp event (not monotonic) */
        private long mStartTime;

        /** Elapsed time since boot of startOp event */
        private long mStartElapsedTime;

        /** Id of the client that started the event */
        private @NonNull IBinder mClientId;

        /** The attribution tag for this operation */
        private @Nullable String mAttributionTag;

        /** To call when client dies */
        private @NonNull Runnable mOnDeath;

        /** uidstate used when calling startOp */
        private @AppOpsManager.UidState int mUidState;

        /** Proxy information of the startOp event */
        private @Nullable AppOpsManager.OpEventProxyInfo mProxy;

        /** Proxy flag information */
        private @AppOpsManager.OpFlags int mFlags;

        /** How many times the op was started but not finished yet */
        int mNumUnfinishedStarts;

        /** The attribution flags related to this event */
        private @AppOpsManager.AttributionFlags int mAttributionFlags;

        /** The id of the attribution chain this even is a part of */
        private int mAttributionChainId;

        /**
         * Create a new {@link InProgressStartOpEvent}.
         *
         * @param startTime          The time {@link #startOperation} was called
         * @param startElapsedTime   The elapsed time when {@link #startOperation} was called
         * @param clientId           The client id of the caller of {@link #startOperation}
         * @param attributionTag     The attribution tag for the operation.
         * @param onDeath            The code to execute on client death
         * @param uidState           The uidstate of the app {@link #startOperation} was called for
         * @param attributionFlags   the attribution flags for this operation.
         * @param attributionChainId the unique id of the attribution chain this op is a part of.
         * @param proxy              The proxy information, if {@link #startProxyOperation} was
         *                           called
         * @param flags              The trusted/nontrusted/self flags.
         * @throws RemoteException If the client is dying
         */
        InProgressStartOpEvent(long startTime, long startElapsedTime,
                @NonNull IBinder clientId, @Nullable String attributionTag,
                @NonNull Runnable onDeath, @AppOpsManager.UidState int uidState,
                @Nullable AppOpsManager.OpEventProxyInfo proxy, @AppOpsManager.OpFlags int flags,
                @AppOpsManager.AttributionFlags int attributionFlags, int attributionChainId)
                throws RemoteException {
            mStartTime = startTime;
            mStartElapsedTime = startElapsedTime;
            mClientId = clientId;
            mAttributionTag = attributionTag;
            mOnDeath = onDeath;
            mUidState = uidState;
            mProxy = proxy;
            mFlags = flags;
            mAttributionFlags = attributionFlags;
            mAttributionChainId = attributionChainId;

            clientId.linkToDeath(this, 0);
        }

        /** Clean up event */
        public void finish() {
            try {
                mClientId.unlinkToDeath(this, 0);
            } catch (NoSuchElementException e) {
                // Either not linked, or already unlinked. Either way, nothing to do.
            }
        }

        @Override
        public void binderDied() {
            mOnDeath.run();
        }

        /**
         * Reinit existing object with new state.
         *
         * @param startTime          The time {@link #startOperation} was called
         * @param startElapsedTime   The elapsed time when {@link #startOperation} was called
         * @param clientId           The client id of the caller of {@link #startOperation}
         * @param attributionTag     The attribution tag for this operation.
         * @param onDeath            The code to execute on client death
         * @param uidState           The uidstate of the app {@link #startOperation} was called for
         * @param flags              The flags relating to the proxy
         * @param proxy              The proxy information, if {@link #startProxyOperation}
         *                           was called
         * @param attributionFlags   the attribution flags for this operation.
         * @param attributionChainId the unique id of the attribution chain this op is a part of.
         * @param proxyPool          The pool to release
         *                           previous {@link AppOpsManager.OpEventProxyInfo} to
         * @throws RemoteException If the client is dying
         */
        public void reinit(long startTime, long startElapsedTime, @NonNull IBinder clientId,
                @Nullable String attributionTag, @NonNull Runnable onDeath,
                @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags,
                @Nullable AppOpsManager.OpEventProxyInfo proxy,
                @AppOpsManager.AttributionFlags int attributionFlags,
                int attributionChainId,
                @NonNull Pools.Pool<AppOpsManager.OpEventProxyInfo> proxyPool
        ) throws RemoteException {
            mStartTime = startTime;
            mStartElapsedTime = startElapsedTime;
            mClientId = clientId;
            mAttributionTag = attributionTag;
            mOnDeath = onDeath;
            mUidState = uidState;
            mFlags = flags;

            if (mProxy != null) {
                proxyPool.release(mProxy);
            }
            mProxy = proxy;
            mAttributionFlags = attributionFlags;
            mAttributionChainId = attributionChainId;

            clientId.linkToDeath(this, 0);
        }

        /** @return Wall clock time of startOp event */
        public long getStartTime() {
            return mStartTime;
        }

        /** @return Elapsed time since boot of startOp event */
        public long getStartElapsedTime() {
            return mStartElapsedTime;
        }

        /** @return Id of the client that started the event */
        public @NonNull IBinder getClientId() {
            return mClientId;
        }

        /** @return uidstate used when calling startOp */
        public @AppOpsManager.UidState int getUidState() {
            return mUidState;
        }

        /** @return proxy tag for the access */
        public @Nullable AppOpsManager.OpEventProxyInfo getProxy() {
            return mProxy;
        }

        /** @return flags used for the access */
        public @AppOpsManager.OpFlags int getFlags() {
            return mFlags;
        }

        /** @return attributoin flags used for the access */
        public @AppOpsManager.AttributionFlags int getAttributionFlags() {
            return mAttributionFlags;
        }

        /** @return attribution chain id for the access */
        public int getAttributionChainId() {
            return mAttributionChainId;
        }

        public void setStartTime(long startTime) {
            mStartTime = startTime;
        }

        public void setStartElapsedTime(long startElapsedTime) {
            mStartElapsedTime = startElapsedTime;
        }
    }

    /**
     * An unsynchronized pool of {@link InProgressStartOpEvent} objects.
     */
    static class InProgressStartOpEventPool extends Pools.SimplePool<InProgressStartOpEvent> {
        private OpEventProxyInfoPool mOpEventProxyInfoPool;

        InProgressStartOpEventPool(OpEventProxyInfoPool opEventProxyInfoPool,
                int maxUnusedPooledObjects) {
            super(maxUnusedPooledObjects);
            this.mOpEventProxyInfoPool = opEventProxyInfoPool;
        }

        InProgressStartOpEvent acquire(long startTime, long elapsedTime, @NonNull IBinder clientId,
                @Nullable String attributionTag, @NonNull Runnable onDeath, int proxyUid,
                @Nullable String proxyPackageName, @Nullable String proxyAttributionTag,
                @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags,
                @AppOpsManager.AttributionFlags
                        int attributionFlags, int attributionChainId) throws RemoteException {

            InProgressStartOpEvent recycled = acquire();

            AppOpsManager.OpEventProxyInfo proxyInfo = null;
            if (proxyUid != Process.INVALID_UID) {
                proxyInfo = mOpEventProxyInfoPool.acquire(proxyUid, proxyPackageName,
                        proxyAttributionTag);
            }

            if (recycled != null) {
                recycled.reinit(startTime, elapsedTime, clientId, attributionTag, onDeath,
                        uidState, flags, proxyInfo, attributionFlags, attributionChainId,
                        mOpEventProxyInfoPool);
                return recycled;
            }

            return new InProgressStartOpEvent(startTime, elapsedTime, clientId, attributionTag,
                    onDeath, uidState, proxyInfo, flags, attributionFlags, attributionChainId);
        }
    }

    /**
     * An unsynchronized pool of {@link AppOpsManager.OpEventProxyInfo} objects.
     */
    static class OpEventProxyInfoPool extends Pools.SimplePool<AppOpsManager.OpEventProxyInfo> {
        OpEventProxyInfoPool(int maxUnusedPooledObjects) {
            super(maxUnusedPooledObjects);
        }

        AppOpsManager.OpEventProxyInfo acquire(@IntRange(from = 0) int uid,
                @Nullable String packageName,
                @Nullable String attributionTag) {
            AppOpsManager.OpEventProxyInfo recycled = acquire();
            if (recycled != null) {
                recycled.reinit(uid, packageName, attributionTag);
                return recycled;
            }

            return new AppOpsManager.OpEventProxyInfo(uid, packageName, attributionTag);
        }
    }
}