summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java
blob: 57c828c20d38fdfad4a0b7afc338f7ff38c6046d (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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
/*
 * Copyright (C) 2018 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.permissioncontroller.permission.service;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission_group.LOCATION;
import static android.app.AppOpsManager.OPSTR_FINE_LOCATION;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.PendingIntent.FLAG_IMMUTABLE;
import static android.app.PendingIntent.FLAG_ONE_SHOT;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.app.job.JobScheduler.RESULT_SUCCESS;
import static android.content.Context.MODE_PRIVATE;
import static android.content.Intent.ACTION_MANAGE_APP_PERMISSION;
import static android.content.Intent.ACTION_SAFETY_CENTER;
import static android.content.Intent.EXTRA_PACKAGE_NAME;
import static android.content.Intent.EXTRA_PERMISSION_GROUP_NAME;
import static android.content.Intent.EXTRA_UID;
import static android.content.Intent.EXTRA_USER;
import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.Intent.FLAG_RECEIVER_FOREGROUND;
import static android.content.pm.PackageManager.GET_PERMISSIONS;
import static android.graphics.Bitmap.Config.ARGB_8888;
import static android.graphics.Bitmap.createBitmap;
import static android.os.UserHandle.getUserHandleForUid;
import static android.os.UserHandle.myUserId;
import static android.provider.Settings.Secure.LOCATION_ACCESS_CHECK_DELAY_MILLIS;
import static android.provider.Settings.Secure.LOCATION_ACCESS_CHECK_INTERVAL_MILLIS;
import static android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ID;
import static android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ISSUE_ID;
import static android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_USER_HANDLE;

import static com.android.permissioncontroller.Constants.EXTRA_SESSION_ID;
import static com.android.permissioncontroller.Constants.INVALID_SESSION_ID;
import static com.android.permissioncontroller.Constants.KEY_LAST_LOCATION_ACCESS_NOTIFICATION_SHOWN;
import static com.android.permissioncontroller.Constants.KEY_LOCATION_ACCESS_CHECK_ENABLED_TIME;
import static com.android.permissioncontroller.Constants.LOCATION_ACCESS_CHECK_ALREADY_NOTIFIED_FILE;
import static com.android.permissioncontroller.Constants.LOCATION_ACCESS_CHECK_JOB_ID;
import static com.android.permissioncontroller.Constants.LOCATION_ACCESS_CHECK_NOTIFICATION_ID;
import static com.android.permissioncontroller.Constants.PERIODIC_LOCATION_ACCESS_CHECK_JOB_ID;
import static com.android.permissioncontroller.Constants.PERMISSION_REMINDER_CHANNEL_ID;
import static com.android.permissioncontroller.Constants.PREFERENCES_FILE;
import static com.android.permissioncontroller.PermissionControllerStatsLog.LOCATION_ACCESS_CHECK_NOTIFICATION_ACTION;
import static com.android.permissioncontroller.PermissionControllerStatsLog.LOCATION_ACCESS_CHECK_NOTIFICATION_ACTION__RESULT__NOTIFICATION_DECLINED;
import static com.android.permissioncontroller.PermissionControllerStatsLog.LOCATION_ACCESS_CHECK_NOTIFICATION_ACTION__RESULT__NOTIFICATION_PRESENTED;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION__ACTION__CARD_DISMISSED;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION__ACTION__CLICKED_CTA1;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION__PRIVACY_SOURCE__BG_LOCATION;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_NOTIFICATION_INTERACTION;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_NOTIFICATION_INTERACTION__ACTION__DISMISSED;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_NOTIFICATION_INTERACTION__ACTION__NOTIFICATION_SHOWN;
import static com.android.permissioncontroller.PermissionControllerStatsLog.PRIVACY_SIGNAL_NOTIFICATION_INTERACTION__PRIVACY_SOURCE__BG_LOCATION;
import static com.android.permissioncontroller.permission.utils.Utils.OS_PKG;
import static com.android.permissioncontroller.permission.utils.Utils.getParcelableExtraSafe;
import static com.android.permissioncontroller.permission.utils.Utils.getParentUserContext;
import static com.android.permissioncontroller.permission.utils.Utils.getStringExtraSafe;
import static com.android.permissioncontroller.permission.utils.Utils.getSystemServiceSafe;

import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.DAYS;

import android.app.AppOpsManager;
import android.app.AppOpsManager.OpEntry;
import android.app.AppOpsManager.PackageOps;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.location.LocationManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.DeviceConfig;
import android.provider.Settings;
import android.safetycenter.SafetyCenterManager;
import android.safetycenter.SafetyEvent;
import android.safetycenter.SafetySourceData;
import android.safetycenter.SafetySourceIssue;
import android.safetycenter.SafetySourceIssue.Action;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;

import androidx.annotation.ChecksSdkIntAtLeast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.WorkerThread;
import androidx.core.util.Preconditions;

import com.android.modules.utils.build.SdkLevel;
import com.android.permissioncontroller.PermissionControllerStatsLog;
import com.android.permissioncontroller.R;
import com.android.permissioncontroller.permission.model.AppPermissionGroup;
import com.android.permissioncontroller.permission.utils.KotlinUtils;
import com.android.permissioncontroller.permission.utils.Utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.stream.Collectors;

/**
 * Show notification that double-guesses the user if she/he really wants to grant fine background
 * location access to an app.
 *
 * <p>A notification is scheduled after the background permission access is granted via
 * {@link #checkLocationAccessSoon()} or periodically.
 *
 * <p>We rate limit the number of notification we show and only ever show one notification at a
 * time. Further we only shown notifications if the app has actually accessed the fine location
 * in the background.
 *
 * <p>As there are many cases why a notification should not been shown, we always schedule a
 * {@link #addLocationNotificationIfNeeded check} which then might add a notification.
 */
public class LocationAccessCheck {
    private static final String LOG_TAG = LocationAccessCheck.class.getSimpleName();
    private static final boolean DEBUG = false;
    private static final long DEFAULT_RENOTIFY_DURATION_MILLIS = DAYS.toMillis(90);
    private static final String ISSUE_ID_PREFIX = "bg_location_";
    private static final String ISSUE_TYPE_ID = "bg_location_privacy_issue";
    private static final String REVOKE_LOCATION_ACCESS_ID_PREFIX = "revoke_location_access_";
    private static final String VIEW_LOCATION_ACCESS_ID = "view_location_access";
    public static final String BG_LOCATION_SOURCE_ID = "AndroidBackgroundLocation";

    /**
     * Device config property for delay in milliseconds
     * between granting a permission and the follow up check
     **/
    public static final String PROPERTY_LOCATION_ACCESS_CHECK_DELAY_MILLIS =
            "location_access_check_delay_millis";

    /**
     * Device config property for delay in milliseconds
     * between periodic checks for background location access
     **/
    public static final String PROPERTY_LOCATION_ACCESS_PERIODIC_INTERVAL_MILLIS =
            "location_access_check_periodic_interval_millis";

    /**
     * Device config property for flag that determines whether location check for safety center
     * is enabled.
     */
    public static final String PROPERTY_BG_LOCATION_CHECK_ENABLED = "bg_location_check_is_enabled";

    /**
     * Lock required for all methods called {@code ...Locked}
     */
    private static final Object sLock = new Object();

    private final Random mRandom = new Random();

    private final @NonNull Context mContext;
    private final @NonNull JobScheduler mJobScheduler;
    private final @NonNull ContentResolver mContentResolver;
    private final @NonNull AppOpsManager mAppOpsManager;
    private final @NonNull PackageManager mPackageManager;
    private final @NonNull UserManager mUserManager;
    private final @NonNull SharedPreferences mSharedPrefs;

    /**
     * If the current long running operation should be canceled
     */
    private final @Nullable BooleanSupplier mShouldCancel;

    /**
     * Get time in between two periodic checks.
     *
     * <p>Default: 1 day
     *
     * @return The time in between check in milliseconds
     */
    private long getPeriodicCheckIntervalMillis() {
        return SdkLevel.isAtLeastT() ? DeviceConfig.getLong(DeviceConfig.NAMESPACE_PRIVACY,
                PROPERTY_LOCATION_ACCESS_PERIODIC_INTERVAL_MILLIS, DAYS.toMillis(1))
                : Settings.Secure.getLong(mContentResolver,
                        LOCATION_ACCESS_CHECK_INTERVAL_MILLIS, DAYS.toMillis(1));
    }

    /**
     * Flexibility of the periodic check.
     *
     * <p>10% of {@link #getPeriodicCheckIntervalMillis()}
     *
     * @return The flexibility of the periodic check in milliseconds
     */
    private long getFlexForPeriodicCheckMillis() {
        return getPeriodicCheckIntervalMillis() / 10;
    }

    /**
     * Get the delay in between granting a permission and the follow up check.
     *
     * <p>Default: 1 day
     *
     * @return The delay in milliseconds
     */
    private long getDelayMillis() {
        return SdkLevel.isAtLeastT() ? DeviceConfig.getLong(DeviceConfig.NAMESPACE_PRIVACY,
                PROPERTY_LOCATION_ACCESS_CHECK_DELAY_MILLIS, DAYS.toMillis(1))
                : Settings.Secure.getLong(mContentResolver, LOCATION_ACCESS_CHECK_DELAY_MILLIS,
                        DAYS.toMillis(1));
    }

    /**
     * Minimum time in between showing two notifications.
     *
     * <p>This is just small enough so that the periodic check can always show a notification.
     *
     * @return The minimum time in milliseconds
     */
    private long getInBetweenNotificationsMillis() {
        return getPeriodicCheckIntervalMillis() - (long) (getFlexForPeriodicCheckMillis() * 2.1);
    }

    /**
     * Load the list of {@link UserPackage packages} we already shown a notification for.
     *
     * @return The list of packages we already shown a notification for.
     */
    private @NonNull ArraySet<UserPackage> loadAlreadyNotifiedPackagesLocked() {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(
            mContext.openFileInput(LOCATION_ACCESS_CHECK_ALREADY_NOTIFIED_FILE)))) {
            ArraySet<UserPackage> packages = new ArraySet<>();

            /*
             * The format of the file is <package> <serial of user> <dismissed in safety center>,
             * Since notification timestamp was added later it is possible that it might be
             * missing during the first check. We need to handle that.
             *
             * e.g.
             * com.one.package 5630633845 true
             * com.two.package 5630633853 false
             * com.three.package 5630633853 false
             */
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                String[] lineComponents = line.split(" ");
                String pkg = lineComponents[0];
                UserHandle user = mUserManager.getUserForSerialNumber(
                        Long.valueOf(lineComponents[1]));
                boolean dismissedInSafetyCenter = lineComponents.length == 3
                        ? Boolean.valueOf(lineComponents[2]) : false;
                if (user != null) {
                    UserPackage userPkg = new UserPackage(mContext, pkg, user,
                            dismissedInSafetyCenter);
                    packages.add(userPkg);
                } else {
                    Log.i(LOG_TAG, "Not restoring state \"" + line + "\" as user is unknown");
                }
            }
            return packages;
        } catch (FileNotFoundException ignored) {
            return new ArraySet<>();
        } catch (Exception e) {
            Log.w(LOG_TAG, "Could not read " + LOCATION_ACCESS_CHECK_ALREADY_NOTIFIED_FILE, e);
            return new ArraySet<>();
        }
    }

    /**
     * Persist the list of {@link UserPackage packages} we have already shown a notification for.
     *
     * @param packages The list of packages we already shown a notification for.
     */
    private void persistAlreadyNotifiedPackagesLocked(@NonNull ArraySet<UserPackage> packages) {
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                mContext.openFileOutput(LOCATION_ACCESS_CHECK_ALREADY_NOTIFIED_FILE,
                        MODE_PRIVATE)))) {
            /*
             * The format of the file is <package> <serial of user> <dismissed in safety center>,
             * e.g.
             * com.one.package 5630633845 true
             * com.two.package 5630633853 false
             * com.three.package 5630633853 false
             */
            int numPkgs = packages.size();
            for (int i = 0; i < numPkgs; i++) {
                UserPackage userPkg = packages.valueAt(i);
                writer.append(userPkg.pkg);
                writer.append(' ');
                writer.append(
                        Long.valueOf(mUserManager.getSerialNumberForUser(userPkg.user)).toString());
                writer.append(' ');
                writer.append(Boolean.toString(userPkg.dismissedInSafetyCenter));
                writer.newLine();
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Could not write " + LOCATION_ACCESS_CHECK_ALREADY_NOTIFIED_FILE, e);
        }
    }

    /**
     * Remember that we showed a notification for a {@link UserPackage}
     *
     * @param pkg                     The package we notified for
     * @param user                    The user we notified for
     * @param dismissedInSafetyCenter Whether this warning was dismissed by the user in safety
     *                                center
     */
    private void markAsNotified(@NonNull String pkg, @NonNull UserHandle user,
            boolean dismissedInSafetyCenter) {
        synchronized (sLock) {
            ArraySet<UserPackage> alreadyNotifiedPackages = loadAlreadyNotifiedPackagesLocked();
            UserPackage userPackage = new UserPackage(mContext, pkg, user, dismissedInSafetyCenter);
            // Remove stale persisted info
            alreadyNotifiedPackages.remove(userPackage);
            // Persist new info about the package
            alreadyNotifiedPackages.add(userPackage);
            persistAlreadyNotifiedPackagesLocked(alreadyNotifiedPackages);
        }
    }

    /**
     * Create the channel the location access notifications should be posted to.
     *
     * @param user The user to create the channel for
     */
    private void createPermissionReminderChannel(@NonNull UserHandle user) {
        NotificationManager notificationManager = getSystemServiceSafe(mContext,
                NotificationManager.class, user);

        NotificationChannel permissionReminderChannel = new NotificationChannel(
                PERMISSION_REMINDER_CHANNEL_ID, mContext.getString(R.string.permission_reminders),
                IMPORTANCE_LOW);
        notificationManager.createNotificationChannel(permissionReminderChannel);
    }

    /**
     * If {@link #mShouldCancel} throw an {@link InterruptedException}.
     */
    private void throwInterruptedExceptionIfTaskIsCanceled() throws InterruptedException {
        if (mShouldCancel != null && mShouldCancel.getAsBoolean()) {
            throw new InterruptedException();
        }
    }

    /**
     * Create a new {@link LocationAccessCheck} object.
     *
     * @param context      Used to resolve managers
     * @param shouldCancel If supplied, can be used to interrupt long running operations
     */
    public LocationAccessCheck(@NonNull Context context, @Nullable BooleanSupplier shouldCancel) {
        mContext = getParentUserContext(context);
        mJobScheduler = getSystemServiceSafe(mContext, JobScheduler.class);
        mAppOpsManager = getSystemServiceSafe(mContext, AppOpsManager.class);
        mPackageManager = mContext.getPackageManager();
        mUserManager = getSystemServiceSafe(mContext, UserManager.class);
        mSharedPrefs = mContext.getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE);
        mContentResolver = mContext.getContentResolver();
        mShouldCancel = shouldCancel;
    }

    /**
     * Check if a location access notification should be shown and then add it.
     *
     * <p>Always run async inside a
     * {@link LocationAccessCheckJobService.AddLocationNotificationIfNeededTask}.
     */
    @WorkerThread
    private void addLocationNotificationIfNeeded(@NonNull JobParameters params,
            @NonNull LocationAccessCheckJobService service) {
        if (!checkLocationAccessCheckEnabledAndUpdateEnabledTime()) {
            Log.v(LOG_TAG, "LocationAccessCheck feature is not enabled.");
            service.jobFinished(params, false);
            return;
        }

        synchronized (sLock) {
            try {
                if (currentTimeMillis() - mSharedPrefs.getLong(
                        KEY_LAST_LOCATION_ACCESS_NOTIFICATION_SHOWN, 0)
                        < getInBetweenNotificationsMillis()) {
                    Log.v(LOG_TAG, "location notification interval is not enough.");
                    service.jobFinished(params, false);
                    return;
                }

                if (getCurrentlyShownNotificationLocked() != null) {
                    Log.v(LOG_TAG, "already location notification exist.");
                    service.jobFinished(params, false);
                    return;
                }

                addLocationNotificationIfNeeded(mAppOpsManager.getPackagesForOps(
                        new String[]{OPSTR_FINE_LOCATION}));
                service.jobFinished(params, false);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Could not check for location access", e);
                service.jobFinished(params, true);
            } finally {
                synchronized (sLock) {
                    service.mAddLocationNotificationIfNeededTask = null;
                    Log.v(LOG_TAG, "LocationAccessCheck privacy job marked complete.");
                }
            }
        }
    }

    private void addLocationNotificationIfNeeded(@NonNull List<PackageOps> ops)
            throws InterruptedException {
        synchronized (sLock) {
            List<UserPackage> packages = getLocationUsersLocked(ops);
            ArraySet<UserPackage> alreadyNotifiedPackages = loadAlreadyNotifiedPackagesLocked();
            if (DEBUG) {
                Log.v(LOG_TAG, "location packages: " + packages);
                Log.v(LOG_TAG, "already notified packages: " + alreadyNotifiedPackages);
            }
            throwInterruptedExceptionIfTaskIsCanceled();
            // Send these issues to safety center
            if (isSafetyCenterBgLocationReminderEnabled()) {
                SafetyEvent safetyEvent = new SafetyEvent.Builder(
                        SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED).build();
                sendToSafetyCenter(packages, safetyEvent, alreadyNotifiedPackages, null);
            }
            filterAlreadyNotifiedPackagesLocked(packages, alreadyNotifiedPackages);

            // Get a random package and resolve package info
            PackageInfo pkgInfo = null;
            while (pkgInfo == null) {
                throwInterruptedExceptionIfTaskIsCanceled();

                if (packages.isEmpty()) {
                    if (DEBUG) {
                        Log.v(LOG_TAG, "No package found to send a notification");
                    }
                    return;
                }

                UserPackage packageToNotifyFor = null;

                // Prefer to show notification for location controller extra package
                int numPkgs = packages.size();
                for (int i = 0; i < numPkgs; i++) {
                    UserPackage pkg = packages.get(i);

                    LocationManager locationManager = getSystemServiceSafe(mContext,
                            LocationManager.class, pkg.user);
                    if (locationManager.isExtraLocationControllerPackageEnabled() && pkg.pkg.equals(
                            locationManager.getExtraLocationControllerPackage())) {
                        packageToNotifyFor = pkg;
                        break;
                    }
                }

                if (packageToNotifyFor == null) {
                    packageToNotifyFor = packages.get(mRandom.nextInt(packages.size()));
                }

                try {
                    pkgInfo = packageToNotifyFor.getPackageInfo();
                } catch (PackageManager.NameNotFoundException e) {
                    packages.remove(packageToNotifyFor);
                }
            }
            createPermissionReminderChannel(getUserHandleForUid(pkgInfo.applicationInfo.uid));
            createNotificationForLocationUser(pkgInfo);
        }
    }

    /**
     * Get the {@link UserPackage packages} which accessed the location
     *
     * <p>This also ignores all packages that are excepted from the notification.
     *
     * @return The packages we might need to show a notification for
     * @throws InterruptedException If {@link #mShouldCancel}
     */
    private @NonNull List<UserPackage> getLocationUsersLocked(
            @NonNull List<PackageOps> allOps) throws InterruptedException {
        List<UserPackage> pkgsWithLocationAccess = new ArrayList<>();
        List<UserHandle> profiles = mUserManager.getUserProfiles();

        LocationManager lm = mContext.getSystemService(LocationManager.class);

        int numPkgs = allOps.size();
        for (int pkgNum = 0; pkgNum < numPkgs; pkgNum++) {
            PackageOps packageOps = allOps.get(pkgNum);

            String pkg = packageOps.getPackageName();
            if (pkg.equals(OS_PKG) || lm.isProviderPackage(pkg)) {
                continue;
            }

            UserHandle user = getUserHandleForUid(packageOps.getUid());
            // Do not handle apps that belong to a different profile user group
            if (!profiles.contains(user)) {
                continue;
            }

            UserPackage userPkg = new UserPackage(mContext, pkg, user, false);
            AppPermissionGroup bgLocationGroup = userPkg.getBackgroundLocationGroup();
            // Do not show notification that do not request the background permission anymore
            if (bgLocationGroup == null) {
                continue;
            }

            // Do not show notification that do not currently have the background permission
            // granted
            if (!bgLocationGroup.areRuntimePermissionsGranted()) {
                continue;
            }

            // Do not show notification for permissions that are not user sensitive
            if (!bgLocationGroup.isUserSensitive()) {
                continue;
            }

            // Never show notification for pregranted permissions as warning the user via the
            // notification and then warning the user again when revoking the permission is
            // confusing
            if (userPkg.getLocationGroup().hasGrantedByDefaultPermission()
                    && bgLocationGroup.hasGrantedByDefaultPermission()) {
                continue;
            }

            int numOps = packageOps.getOps().size();
            for (int opNum = 0; opNum < numOps; opNum++) {
                OpEntry entry = packageOps.getOps().get(opNum);

                // To protect against OEM apps that accidentally blame app ops on other packages
                // since they can hold the privileged UPDATE_APP_OPS_STATS permission for location
                // access in the background we trust only the OS and the location providers. Note
                // that this mitigation only handles usage of AppOpsManager#noteProxyOp and not
                // direct usage of AppOpsManager#noteOp, i.e. handles bad blaming and not bad
                // attribution.
                String proxyPackageName = entry.getProxyPackageName();
                if (proxyPackageName != null && !proxyPackageName.equals(OS_PKG)
                        && !lm.isProviderPackage(proxyPackageName)) {
                    continue;
                }

                // We show only bg accesses since the location access check feature was enabled
                // to handle cases where the feature is remotely toggled since we don't want to
                // notify for accesses before the feature was turned on.
                long featureEnabledTime = getLocationAccessCheckEnabledTime();
                if (featureEnabledTime >= 0 && entry.getLastAccessBackgroundTime(
                        AppOpsManager.OP_FLAGS_ALL_TRUSTED) >= featureEnabledTime) {
                    pkgsWithLocationAccess.add(userPkg);
                    break;
                }
            }
        }
        return pkgsWithLocationAccess;
    }

    private void filterAlreadyNotifiedPackagesLocked(
            @NonNull List<UserPackage> pkgsWithLocationAccess,
            @NonNull ArraySet<UserPackage> alreadyNotifiedPkgs) throws InterruptedException {
        resetAlreadyNotifiedPackagesWithoutPermissionLocked(alreadyNotifiedPkgs);
        pkgsWithLocationAccess.removeAll(alreadyNotifiedPkgs);
    }

    /**
     * Checks whether the location access check feature is enabled and updates the
     * time when the feature was first enabled. If the feature is enabled and no
     * enabled time persisted we persist the current time as the enabled time. If
     * the feature is disabled and an enabled time is persisted we delete the
     * persisted time.
     *
     * @return Whether the location access feature is enabled.
     */
    private boolean checkLocationAccessCheckEnabledAndUpdateEnabledTime() {
        final long enabledTime = getLocationAccessCheckEnabledTime();
        if (Utils.isLocationAccessCheckEnabled()) {
            if (enabledTime <= 0) {
                mSharedPrefs.edit().putLong(KEY_LOCATION_ACCESS_CHECK_ENABLED_TIME,
                        currentTimeMillis()).commit();
            }
            return true;
        } else {
            if (enabledTime > 0) {
                mSharedPrefs.edit().remove(KEY_LOCATION_ACCESS_CHECK_ENABLED_TIME)
                        .commit();
            }
            return false;
        }
    }

    /**
     * @return The time the location access check was enabled, or 0 if not enabled.
     */
    private long getLocationAccessCheckEnabledTime() {
        return mSharedPrefs.getLong(KEY_LOCATION_ACCESS_CHECK_ENABLED_TIME, 0);
    }

    /**
     * Create a notification reminding the user that a package used the location. From this
     * notification the user can directly go to the screen that allows to change the permission.
     *
     * @param pkg The {@link PackageInfo} for the package to to be changed
     */
    private void createNotificationForLocationUser(@NonNull PackageInfo pkg) {
        CharSequence pkgLabel = mPackageManager.getApplicationLabel(pkg.applicationInfo);

        boolean safetyCenterBgLocationReminderEnabled = isSafetyCenterBgLocationReminderEnabled();

        String pkgName = pkg.packageName;
        int uid = pkg.applicationInfo.uid;
        UserHandle user = getUserHandleForUid(uid);

        NotificationManager notificationManager = getSystemServiceSafe(mContext,
                NotificationManager.class, user);

        long sessionId = INVALID_SESSION_ID;
        while (sessionId == INVALID_SESSION_ID) {
            sessionId = new Random().nextLong();
        }

        CharSequence appName = Utils.getSettingsLabelForNotifications(mPackageManager);

        CharSequence notificationTitle =
                safetyCenterBgLocationReminderEnabled ? mContext.getString(
                        R.string.safety_center_background_location_access_notification_title
                ) : mContext.getString(
                        R.string.background_location_access_reminder_notification_title,
                        pkgLabel);

        CharSequence notificationContent = safetyCenterBgLocationReminderEnabled
                ? mContext.getString(
                R.string.safety_center_background_location_access_reminder_notification_content,
                pkgLabel) : mContext.getString(
                R.string.background_location_access_reminder_notification_content);

        CharSequence appLabel = appName;
        Icon smallIcon;
        int color = mContext.getColor(android.R.color.system_notification_accent_color);
        if (safetyCenterBgLocationReminderEnabled) {
            KotlinUtils.NotificationResources notifRes =
                    KotlinUtils.INSTANCE.getSafetyCenterNotificationResources(mContext);
            appLabel = notifRes.getAppLabel();
            smallIcon = notifRes.getSmallIcon();
            color = notifRes.getColor();
        } else {
            smallIcon = Icon.createWithResource(mContext, R.drawable.ic_pin_drop);
        }

        Notification.Builder b = (new Notification.Builder(mContext,
                PERMISSION_REMINDER_CHANNEL_ID))
                .setLocalOnly(true)
                .setContentTitle(notificationTitle)
                .setContentText(notificationContent)
                .setStyle(new Notification.BigTextStyle().bigText(notificationContent))
                .setSmallIcon(smallIcon)
                .setColor(color)
                .setDeleteIntent(createNotificationDismissIntent(pkgName, sessionId, uid))
                .setContentIntent(createNotificationClickIntent(pkgName, user, sessionId, uid))
                .setAutoCancel(true);

        if (!safetyCenterBgLocationReminderEnabled) {
            Drawable pkgIcon = mPackageManager.getApplicationIcon(pkg.applicationInfo);
            Bitmap pkgIconBmp = createBitmap(pkgIcon.getIntrinsicWidth(),
                    pkgIcon.getIntrinsicHeight(),
                    ARGB_8888);
            Canvas canvas = new Canvas(pkgIconBmp);
            pkgIcon.setBounds(0, 0, pkgIcon.getIntrinsicWidth(), pkgIcon.getIntrinsicHeight());
            pkgIcon.draw(canvas);
            b.setLargeIcon(pkgIconBmp);
        }

        if (!TextUtils.isEmpty(appLabel)) {
            Bundle extras = new Bundle();
            String appNameSubstitute = appLabel.toString();
            extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, appNameSubstitute);
            b.addExtras(extras);
        }

        notificationManager.notify(pkgName, LOCATION_ACCESS_CHECK_NOTIFICATION_ID, b.build());
        markAsNotified(pkgName, user, false);

        if (DEBUG) Log.i(LOG_TAG, "Notified " + pkgName);

        Log.v(LOG_TAG, "Location access check notification shown with sessionId=" + sessionId + ""
                + " uid=" + pkg.applicationInfo.uid + " pkgName=" + pkgName);
        if (safetyCenterBgLocationReminderEnabled) {
            PermissionControllerStatsLog.write(
                    PRIVACY_SIGNAL_NOTIFICATION_INTERACTION,
                    PRIVACY_SIGNAL_NOTIFICATION_INTERACTION__PRIVACY_SOURCE__BG_LOCATION,
                    uid,
                    PRIVACY_SIGNAL_NOTIFICATION_INTERACTION__ACTION__NOTIFICATION_SHOWN,
                    sessionId);
        } else {
            PermissionControllerStatsLog.write(LOCATION_ACCESS_CHECK_NOTIFICATION_ACTION, sessionId,
                    pkg.applicationInfo.uid, pkgName,
                    LOCATION_ACCESS_CHECK_NOTIFICATION_ACTION__RESULT__NOTIFICATION_PRESENTED);
        }

        mSharedPrefs.edit().putLong(KEY_LAST_LOCATION_ACCESS_NOTIFICATION_SHOWN,
                currentTimeMillis()).apply();
    }

    /**
     * Get currently shown notification. We only ever show one notification per profile group.
     *
     * @return The notification or {@code null} if no notification is currently shown
     */
    private @Nullable StatusBarNotification getCurrentlyShownNotificationLocked() {
        List<UserHandle> profiles = mUserManager.getUserProfiles();

        int numProfiles = profiles.size();
        for (int profileNum = 0; profileNum < numProfiles; profileNum++) {
            NotificationManager notificationManager;
            try {
                notificationManager = getSystemServiceSafe(mContext, NotificationManager.class,
                        profiles.get(profileNum));
            } catch (IllegalStateException e) {
                continue;
            }

            StatusBarNotification[] notifications = notificationManager.getActiveNotifications();

            int numNotifications = notifications.length;
            for (int notificationNum = 0; notificationNum < numNotifications; notificationNum++) {
                StatusBarNotification notification = notifications[notificationNum];
                if (notification.getId() == LOCATION_ACCESS_CHECK_NOTIFICATION_ID
                        && notification.getUser() != null && notification.getTag() != null) {
                    return notification;
                }
            }
        }
        return null;
    }

    /**
     * Go through the list of packages we already shown a notification for and remove those that do
     * not request fine background location access.
     *
     * @param alreadyNotifiedPkgs The packages we already shown a notification for. This parameter
     *                            is modified inside of this method.
     * @throws InterruptedException If {@link #mShouldCancel}
     */
    private void resetAlreadyNotifiedPackagesWithoutPermissionLocked(
            @NonNull ArraySet<UserPackage> alreadyNotifiedPkgs) throws InterruptedException {
        ArrayList<UserPackage> packagesToRemove = new ArrayList<>();

        for (UserPackage userPkg : alreadyNotifiedPkgs) {
            throwInterruptedExceptionIfTaskIsCanceled();
            AppPermissionGroup bgLocationGroup = userPkg.getBackgroundLocationGroup();
            if (bgLocationGroup == null || !bgLocationGroup.areRuntimePermissionsGranted()) {
                packagesToRemove.add(userPkg);
            }
        }

        if (!packagesToRemove.isEmpty()) {
            alreadyNotifiedPkgs.removeAll(packagesToRemove);
            persistAlreadyNotifiedPackagesLocked(alreadyNotifiedPkgs);
            throwInterruptedExceptionIfTaskIsCanceled();
        }
    }

    /**
     * Remove all persisted state for a package.
     *
     * @param pkg  name of package
     * @param user user the package belongs to
     */
    private void forgetAboutPackage(@NonNull String pkg, @NonNull UserHandle user) {
        synchronized (sLock) {
            StatusBarNotification notification = getCurrentlyShownNotificationLocked();
            if (notification != null && notification.getUser().equals(user)
                    && notification.getTag().equals(pkg)) {
                getSystemServiceSafe(mContext, NotificationManager.class, user).cancel(
                        pkg, LOCATION_ACCESS_CHECK_NOTIFICATION_ID);
            }

            ArraySet<UserPackage> packages = loadAlreadyNotifiedPackagesLocked();
            packages.remove(new UserPackage(mContext, pkg, user, false));
            persistAlreadyNotifiedPackagesLocked(packages);
        }
    }

    /**
     * After a small delay schedule a check if we should show a notification.
     *
     * <p>This is called when location access is granted to an app. In this case it is likely that
     * the app will access the location soon. If this happens the notification will appear only a
     * little after the user granted the location.
     */
    public void checkLocationAccessSoon() {
        JobInfo.Builder b = (new JobInfo.Builder(LOCATION_ACCESS_CHECK_JOB_ID,
                new ComponentName(mContext, LocationAccessCheckJobService.class)))
                .setMinimumLatency(getDelayMillis());

        int scheduleResult = mJobScheduler.schedule(b.build());
        if (scheduleResult != RESULT_SUCCESS) {
            Log.e(LOG_TAG, "Could not schedule location access check " + scheduleResult);
        }
    }

    /**
     * Cancel the background access warning notification for an app if the permission has been
     * revoked for the app and forget persisted information about the app
     */
    public void cancelBackgroundAccessWarningNotification(String packageName, UserHandle user,
            Boolean forgetAboutPackage) {
        // Cancel the current notification if background
        // location access for the package is revoked
        StatusBarNotification notification = getCurrentlyShownNotificationLocked();
        if (notification != null && notification.getUser().equals(user)
                && notification.getTag().equals(packageName)) {
            getSystemServiceSafe(mContext, NotificationManager.class, user).cancel(
                    packageName, LOCATION_ACCESS_CHECK_NOTIFICATION_ID);
        }

        if (isSafetyCenterBgLocationReminderEnabled()) {
            rescanAndPushSafetyCenterData(new SafetyEvent.Builder(
                    SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED)
                    .build(), user);
        }

        if (forgetAboutPackage) {
            forgetAboutPackage(packageName, user);
        }
    }

    /**
     * Cancel the background access warning notification if currently being shown
     */
    public void cancelBackgroundAccessWarningNotification() {
        StatusBarNotification notification = getCurrentlyShownNotificationLocked();
        if (notification != null) {
            getSystemServiceSafe(mContext, NotificationManager.class,
                    notification.getUser()).cancel(
                    notification.getTag(), LOCATION_ACCESS_CHECK_NOTIFICATION_ID);
        }
    }

    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU)
    private boolean isSafetyCenterBgLocationReminderEnabled() {
        if (!SdkLevel.isAtLeastT()) {
            return false;
        }

        return DeviceConfig.getBoolean(
                DeviceConfig.NAMESPACE_PRIVACY,
                PROPERTY_BG_LOCATION_CHECK_ENABLED, true)
                && getSystemServiceSafe(mContext,
                SafetyCenterManager.class).isSafetyCenterEnabled();
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private void sendToSafetyCenter(List<UserPackage> userPackages, SafetyEvent safetyEvent,
            @Nullable ArraySet<UserPackage> alreadyNotifiedPackages, @Nullable UserHandle user) {
        try {
            Set<UserPackage> alreadyDismissedPackages =
                    getAlreadyDismissedPackages(alreadyNotifiedPackages);

            // Filter out packages already dismissed by the user in safety center
            List<UserPackage> filteredPackages = userPackages.stream().filter(
                    pkg -> !alreadyDismissedPackages.contains(pkg)).collect(
                    Collectors.toList());

            Map<UserHandle, List<UserPackage>> userHandleToUserPackagesMap =
                    splitUserPackageByUserHandle(filteredPackages);

            if (user == null) {
                // Get all the user profiles
                List<UserHandle> userProfiles = mUserManager.getUserProfiles();
                for (UserHandle userProfile : userProfiles) {
                    sendUserDataToSafetyCenter(userHandleToUserPackagesMap.getOrDefault(userProfile,
                            new ArrayList<>()), safetyEvent, userProfile);
                }
            } else {
                sendUserDataToSafetyCenter(userHandleToUserPackagesMap.getOrDefault(user,
                        new ArrayList<>()), safetyEvent, user);
            }

        } catch (Exception e) {
            Log.e(LOG_TAG, "Could not send to safety center", e);
        }
    }

    private Set<UserPackage> getAlreadyDismissedPackages(
            @Nullable ArraySet<UserPackage> alreadyNotifiedPackages) {
        if (alreadyNotifiedPackages == null) {
            alreadyNotifiedPackages = loadAlreadyNotifiedPackagesLocked();
        }
        return alreadyNotifiedPackages.stream().filter(
                pkg -> pkg.dismissedInSafetyCenter).collect(
                Collectors.toSet());
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private Map<UserHandle, List<UserPackage>> splitUserPackageByUserHandle(
            List<UserPackage> userPackages) {
        Map<UserHandle, List<UserPackage>> userHandleToUserPackagesMap = new ArrayMap<>();
        for (UserPackage userPackage : userPackages) {
            if (userHandleToUserPackagesMap.get(userPackage.user) == null) {
                userHandleToUserPackagesMap.put(userPackage.user, new ArrayList<>());
            }
            userHandleToUserPackagesMap.get(userPackage.user).add(userPackage);
        }
        return userHandleToUserPackagesMap;
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private void sendUserDataToSafetyCenter(List<UserPackage> userPackages,
            SafetyEvent safetyEvent, @Nullable UserHandle user) {
        SafetySourceData.Builder safetySourceDataBuilder = new SafetySourceData.Builder();
        Context userContext = null;
        for (UserPackage userPkg : userPackages) {
            if (userContext == null) {
                userContext = userPkg.mContext;
            }
            SafetySourceIssue sourceIssue = createSafetySourceIssue(userPkg);
            if (sourceIssue != null) {
                safetySourceDataBuilder.addIssue(sourceIssue);
            }
        }
        if (userContext == null && user != null) {
            userContext = mContext.createContextAsUser(user, 0);
        }
        if (userContext != null) {
            getSystemServiceSafe(userContext, SafetyCenterManager.class).setSafetySourceData(
                    BG_LOCATION_SOURCE_ID,
                    safetySourceDataBuilder.build(),
                    safetyEvent
            );
        }
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private SafetySourceIssue createSafetySourceIssue(UserPackage userPackage) {
        PackageInfo pkgInfo = null;
        try {
            pkgInfo = userPackage.getPackageInfo();
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG_TAG, "Could not get package info for " + userPackage, e);
            return null;
        }

        long sessionId = INVALID_SESSION_ID;
        while (sessionId == INVALID_SESSION_ID) {
            sessionId = new Random().nextLong();
        }

        int uid = pkgInfo.applicationInfo.uid;

        Intent primaryActionIntent = new Intent(mContext, SafetyCenterPrimaryActionHandler.class);
        primaryActionIntent.putExtra(EXTRA_PACKAGE_NAME, userPackage.pkg);
        primaryActionIntent.putExtra(EXTRA_USER, userPackage.user);
        primaryActionIntent.putExtra(EXTRA_UID, uid);
        primaryActionIntent.putExtra(EXTRA_SESSION_ID, sessionId);
        primaryActionIntent.setFlags(FLAG_RECEIVER_FOREGROUND);
        primaryActionIntent.setIdentifier(userPackage.pkg + userPackage.user);

        PendingIntent revokeIntent = PendingIntent.getBroadcast(mContext, 0,
                primaryActionIntent,
                FLAG_ONE_SHOT | FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);

        Action revokeAction = new Action.Builder(createLocationRevokeActionId(userPackage.pkg,
                userPackage.user),
                mContext.getString(R.string.permission_access_only_foreground),
                revokeIntent).setWillResolve(true).setSuccessMessage(mContext.getString(
                R.string.safety_center_background_location_access_revoked)).build();

        Intent secondaryActionIntent = new Intent(Intent.ACTION_REVIEW_PERMISSION_HISTORY);
        secondaryActionIntent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, LOCATION);

        PendingIntent locationUsageIntent = PendingIntent.getActivity(mContext, 0,
                secondaryActionIntent,
                FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);

        Action viewLocationUsageAction = new Action.Builder(VIEW_LOCATION_ACCESS_ID,
                mContext.getString(R.string.safety_center_view_recent_location_access),
                locationUsageIntent).build();

        String pkgName = userPackage.pkg;
        String id = createSafetySourceIssueId(pkgName, userPackage.user);

        CharSequence pkgLabel = mPackageManager.getApplicationLabel(pkgInfo.applicationInfo);

        return new SafetySourceIssue.Builder(
                id,
                mContext.getString(
                        R.string.safety_center_background_location_access_reminder_title),
                mContext.getString(
                        R.string.safety_center_background_location_access_reminder_summary),
                SafetySourceData.SEVERITY_LEVEL_INFORMATION,
                ISSUE_TYPE_ID)
                .setSubtitle(pkgLabel)
                .addAction(revokeAction)
                .addAction(viewLocationUsageAction)
                .setOnDismissPendingIntent(
                        createWarningCardDismissalIntent(pkgName, sessionId, uid))
                .setIssueCategory(SafetySourceIssue.ISSUE_CATEGORY_DEVICE)
                .build();
    }

    private PendingIntent createNotificationDismissIntent(String pkgName, long sessionId, int uid) {
        Intent dismissIntent = new Intent(mContext, NotificationDeleteHandler.class);
        dismissIntent.putExtra(EXTRA_PACKAGE_NAME, pkgName);
        dismissIntent.putExtra(EXTRA_SESSION_ID, sessionId);
        dismissIntent.putExtra(EXTRA_UID, uid);
        UserHandle user = getUserHandleForUid(uid);
        dismissIntent.putExtra(EXTRA_USER, user);
        dismissIntent.setIdentifier(pkgName + user);
        dismissIntent.setFlags(FLAG_RECEIVER_FOREGROUND);
        return PendingIntent.getBroadcast(mContext, 0, dismissIntent,
                FLAG_ONE_SHOT | FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
    }

    private PendingIntent createNotificationClickIntent(String pkg, UserHandle user,
            long sessionId, int uid) {
        Intent clickIntent = null;
        if (isSafetyCenterBgLocationReminderEnabled()) {
            clickIntent = new Intent(ACTION_SAFETY_CENTER);
            clickIntent.putExtra(EXTRA_SAFETY_SOURCE_ID, BG_LOCATION_SOURCE_ID);
            clickIntent.putExtra(
                    EXTRA_SAFETY_SOURCE_ISSUE_ID, createSafetySourceIssueId(pkg, user));
            clickIntent.putExtra(EXTRA_SAFETY_SOURCE_USER_HANDLE, user);
        } else {
            clickIntent = new Intent(ACTION_MANAGE_APP_PERMISSION);
            clickIntent.putExtra(EXTRA_PERMISSION_GROUP_NAME, LOCATION);
        }
        clickIntent.putExtra(EXTRA_PACKAGE_NAME, pkg);
        clickIntent.putExtra(EXTRA_USER, user);
        clickIntent.putExtra(EXTRA_SESSION_ID, sessionId);
        clickIntent.putExtra(EXTRA_UID, uid);
        clickIntent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK);
        return PendingIntent.getActivity(mContext, 0, clickIntent,
                FLAG_ONE_SHOT | FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
    }

    private PendingIntent createWarningCardDismissalIntent(String pkgName, long sessionId,
            int uid) {
        Intent dismissIntent = new Intent(mContext, WarningCardDismissalHandler.class);
        dismissIntent.putExtra(EXTRA_PACKAGE_NAME, pkgName);
        dismissIntent.putExtra(EXTRA_SESSION_ID, sessionId);
        dismissIntent.putExtra(EXTRA_UID, uid);
        UserHandle user = getUserHandleForUid(uid);
        dismissIntent.putExtra(EXTRA_USER, user);
        dismissIntent.setIdentifier(pkgName + user);
        dismissIntent.setFlags(FLAG_RECEIVER_FOREGROUND);
        return PendingIntent.getBroadcast(mContext, 0, dismissIntent,
                FLAG_ONE_SHOT | FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
    }

    /**
     * Check if the current user is the profile parent.
     *
     * @return {@code true} if the current user is the profile parent.
     */
    private boolean isRunningInParentProfile() {
        UserHandle user = UserHandle.of(myUserId());
        UserHandle parent = mUserManager.getProfileParent(user);

        return parent == null || user.equals(parent);
    }

    /**
     * Query for packages having background location access and push to safety center
     *
     * @param safetyEvent Safety event for which data is being pushed
     * @param user Optional, if supplied only send safety center data for that user
     */
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    public void rescanAndPushSafetyCenterData(SafetyEvent safetyEvent, @Nullable UserHandle user) {
        if (!isSafetyCenterBgLocationReminderEnabled()) {
            return;
        }
        try {
            List<UserPackage> packages = getLocationUsersLocked(mAppOpsManager.getPackagesForOps(
                    new String[]{OPSTR_FINE_LOCATION}));
            sendToSafetyCenter(packages, safetyEvent, null, user);
        } catch (InterruptedException e) {
            Log.e(LOG_TAG, "Couldn't get ops for location");
        }
    }

    /**
     * On boot set up a periodic job that starts checks.
     */
    public static class SetupPeriodicBackgroundLocationAccessCheck extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            LocationAccessCheck locationAccessCheck = new LocationAccessCheck(context, null);
            JobScheduler jobScheduler = getSystemServiceSafe(context, JobScheduler.class);

            if (!locationAccessCheck.isRunningInParentProfile()) {
                // Profile parent handles child profiles too.
                return;
            }

            // Init LocationAccessCheckEnabledTime if needed
            locationAccessCheck.checkLocationAccessCheckEnabledAndUpdateEnabledTime();

            if (jobScheduler.getPendingJob(PERIODIC_LOCATION_ACCESS_CHECK_JOB_ID) == null) {
                JobInfo.Builder b = (new JobInfo.Builder(PERIODIC_LOCATION_ACCESS_CHECK_JOB_ID,
                        new ComponentName(context, LocationAccessCheckJobService.class)))
                        .setPeriodic(locationAccessCheck.getPeriodicCheckIntervalMillis(),
                                locationAccessCheck.getFlexForPeriodicCheckMillis());

                int scheduleResult = jobScheduler.schedule(b.build());
                if (scheduleResult != RESULT_SUCCESS) {
                    Log.e(LOG_TAG, "Could not schedule periodic location access check "
                            + scheduleResult);
                }
            }
        }
    }

    /**
     * Checks if a new notification should be shown.
     */
    public static class LocationAccessCheckJobService extends JobService {
        private LocationAccessCheck mLocationAccessCheck;

        /**
         * If we currently check if we should show a notification, the task executing the check
         */
        // @GuardedBy("sLock")
        private @Nullable AddLocationNotificationIfNeededTask mAddLocationNotificationIfNeededTask;

        @Override
        public void onCreate() {
            Log.v(LOG_TAG, "LocationAccessCheck privacy job is created");
            super.onCreate();
            mLocationAccessCheck = new LocationAccessCheck(this, () -> {
                synchronized (sLock) {
                    AddLocationNotificationIfNeededTask task = mAddLocationNotificationIfNeededTask;

                    return task != null && task.isCancelled();
                }
            });
        }

        /**
         * Starts an asynchronous check if a location access notification should be shown.
         *
         * @param params Not used other than for interacting with job scheduling
         * @return {@code false} iff another check if already running
         */
        @Override
        public boolean onStartJob(JobParameters params) {
            Log.v(LOG_TAG, "LocationAccessCheck privacy job is started");
            synchronized (LocationAccessCheck.sLock) {
                if (mAddLocationNotificationIfNeededTask != null) {
                    Log.v(LOG_TAG, "LocationAccessCheck old job not completed yet.");
                    return false;
                }

                mAddLocationNotificationIfNeededTask =
                        new AddLocationNotificationIfNeededTask();

                mAddLocationNotificationIfNeededTask.execute(params, this);
            }

            return true;
        }

        /**
         * Abort the check if still running.
         *
         * @param params ignored
         * @return false
         */
        @Override
        public boolean onStopJob(JobParameters params) {
            Log.v(LOG_TAG, "LocationAccessCheck privacy source onStopJob called.");
            AddLocationNotificationIfNeededTask task;
            synchronized (sLock) {
                if (mAddLocationNotificationIfNeededTask == null) {
                    return false;
                } else {
                    task = mAddLocationNotificationIfNeededTask;
                }
            }

            task.cancel(false);

            try {
                // Wait for task to finish
                task.get();
            } catch (Exception e) {
                Log.e(LOG_TAG, "While waiting for " + task + " to finish", e);
            }

            return false;
        }

        /**
         * A {@link AsyncTask task} that runs the check in the background.
         */
        private class AddLocationNotificationIfNeededTask extends
                AsyncTask<Object, Void, Void> {
            @Override
            protected final Void doInBackground(Object... in) {
                JobParameters params = (JobParameters) in[0];
                LocationAccessCheckJobService service = (LocationAccessCheckJobService) in[1];
                mLocationAccessCheck.addLocationNotificationIfNeeded(params, service);
                return null;
            }
        }
    }

    /**
     * Handle the case where the notification is swiped away without further interaction.
     */
    public static class NotificationDeleteHandler extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String pkg = getStringExtraSafe(intent, EXTRA_PACKAGE_NAME);
            UserHandle user = getParcelableExtraSafe(intent, EXTRA_USER);
            long sessionId = intent.getLongExtra(EXTRA_SESSION_ID, INVALID_SESSION_ID);
            int uid = intent.getIntExtra(EXTRA_UID, -1);

            Log.v(LOG_TAG,
                    "Location access check notification declined with sessionId=" + sessionId + ""
                            + " uid=" + uid + " pkgName=" + pkg);
            LocationAccessCheck locationAccessCheck = new LocationAccessCheck(context, null);

            if (locationAccessCheck.isSafetyCenterBgLocationReminderEnabled()) {
                PermissionControllerStatsLog.write(
                        PRIVACY_SIGNAL_NOTIFICATION_INTERACTION,
                        PRIVACY_SIGNAL_NOTIFICATION_INTERACTION__PRIVACY_SOURCE__BG_LOCATION,
                        uid,
                        PRIVACY_SIGNAL_NOTIFICATION_INTERACTION__ACTION__DISMISSED,
                        sessionId
                );
            } else {
                PermissionControllerStatsLog.write(LOCATION_ACCESS_CHECK_NOTIFICATION_ACTION,
                        sessionId,
                        uid, pkg,
                        LOCATION_ACCESS_CHECK_NOTIFICATION_ACTION__RESULT__NOTIFICATION_DECLINED);
            }
            locationAccessCheck.markAsNotified(pkg, user, false);
        }
    }

    /**
     * Broadcast receiver to handle the primary action from a safety center warning card
     */
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    public static class SafetyCenterPrimaryActionHandler extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String packageName = getStringExtraSafe(intent, EXTRA_PACKAGE_NAME);
            UserHandle user = getParcelableExtraSafe(intent, EXTRA_USER);
            int uid = intent.getIntExtra(EXTRA_UID, -1);
            long sessionId = intent.getLongExtra(EXTRA_SESSION_ID, INVALID_SESSION_ID);
            // Revoke bg location permission and notify safety center
            KotlinUtils.INSTANCE.revokeBackgroundRuntimePermissions(context, packageName, LOCATION,
                    user, () -> {
                        new LocationAccessCheck(context, null).rescanAndPushSafetyCenterData(
                                new SafetyEvent.Builder(
                                        SafetyEvent.SAFETY_EVENT_TYPE_RESOLVING_ACTION_SUCCEEDED)
                                        .setSafetySourceIssueId(
                                                createSafetySourceIssueId(packageName, user))
                                        .setSafetySourceIssueActionId(
                                                createLocationRevokeActionId(packageName, user))
                                        .build(), user);
                    });
            PermissionControllerStatsLog.write(
                    PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION,
                    PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION__PRIVACY_SOURCE__BG_LOCATION,
                    uid,
                    PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION__ACTION__CLICKED_CTA1,
                    sessionId
            );

        }
    }

    private static String createSafetySourceIssueId(String packageName, UserHandle user) {
        return ISSUE_ID_PREFIX + packageName + user;
    }

    private static String createLocationRevokeActionId(String packageName, UserHandle user) {
        return REVOKE_LOCATION_ACCESS_ID_PREFIX + packageName + user;
    }

    /**
     * Handle the case where the warning card is dismissed by the user in Safety center
     */
    public static class WarningCardDismissalHandler extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String pkg = getStringExtraSafe(intent, EXTRA_PACKAGE_NAME);
            UserHandle user = getParcelableExtraSafe(intent, EXTRA_USER);
            long sessionId = intent.getLongExtra(EXTRA_SESSION_ID, INVALID_SESSION_ID);
            int uid = intent.getIntExtra(EXTRA_UID, -1);
            Log.v(LOG_TAG,
                    "Location access check warning card dismissed with sessionId=" + sessionId + ""
                            + " uid=" + uid + " pkgName=" + pkg);
            PermissionControllerStatsLog.write(
                    PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION,
                    PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION__PRIVACY_SOURCE__BG_LOCATION,
                    uid,
                    PRIVACY_SIGNAL_ISSUE_CARD_INTERACTION__ACTION__CARD_DISMISSED,
                    sessionId
            );

            LocationAccessCheck locationAccessCheck = new LocationAccessCheck(context, null);
            locationAccessCheck.markAsNotified(pkg, user, true);
            locationAccessCheck.cancelBackgroundAccessWarningNotification(pkg, user, false);
        }
    }

    /**
     * If a package gets removed or the data of the package gets cleared, forget that we showed a
     * notification for it.
     */
    public static class PackageResetHandler extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!(Objects.equals(action, Intent.ACTION_PACKAGE_DATA_CLEARED)
                    || Objects.equals(action, Intent.ACTION_PACKAGE_FULLY_REMOVED))) {
                return;
            }

            Uri data = Preconditions.checkNotNull(intent.getData());
            UserHandle user = getUserHandleForUid(intent.getIntExtra(EXTRA_UID, 0));
            if (DEBUG) Log.i(LOG_TAG, "Reset " + data.getSchemeSpecificPart());
            LocationAccessCheck locationAccessCheck = new LocationAccessCheck(context, null);
            String packageName =  data.getSchemeSpecificPart();
            locationAccessCheck.forgetAboutPackage(packageName, user);
            if (locationAccessCheck.isSafetyCenterBgLocationReminderEnabled()) {
                locationAccessCheck.rescanAndPushSafetyCenterData(
                        new SafetyEvent.Builder(SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED)
                                .build(), user);
            }
        }
    }

    /**
     * A immutable class containing a package name and a {@link UserHandle}.
     */
    private static final class UserPackage {
        private final @NonNull Context mContext;

        public final @NonNull String pkg;
        public final @NonNull UserHandle user;
        public final boolean dismissedInSafetyCenter;

        /**
         * Create a new {@link UserPackage}
         *
         * @param context               A context to be used by methods of this object
         * @param pkg                   The name of the package
         * @param user                  The user the package belongs to
         * @param dismissedInSafetyCenter Optional boolean recording if the safety center
         *                                       warning was dismissed by the user
         */
        UserPackage(@NonNull Context context, @NonNull String pkg, @NonNull UserHandle user,
                boolean dismissedInSafetyCenter) {
            try {
                mContext = context.createPackageContextAsUser(context.getPackageName(), 0, user);
            } catch (PackageManager.NameNotFoundException e) {
                throw new IllegalStateException(e);
            }

            this.pkg = pkg;
            this.user = user;
            this.dismissedInSafetyCenter = dismissedInSafetyCenter;
        }

        /**
         * Get {@link PackageInfo} for this user package.
         *
         * @return The package info
         * @throws PackageManager.NameNotFoundException if package/user does not exist
         */
        @NonNull
        PackageInfo getPackageInfo() throws PackageManager.NameNotFoundException {
            return mContext.getPackageManager().getPackageInfo(pkg, GET_PERMISSIONS);
        }

        /**
         * Get the {@link AppPermissionGroup} for
         * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and this user package.
         *
         * @return The app permission group or {@code null} if the app does not request location
         */
        @Nullable
        AppPermissionGroup getLocationGroup() {
            try {
                return AppPermissionGroup.create(mContext, getPackageInfo(), ACCESS_FINE_LOCATION,
                        false);
            } catch (PackageManager.NameNotFoundException e) {
                return null;
            }
        }

        /**
         * Get the {@link AppPermissionGroup} for the background location of
         * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and this user package.
         *
         * @return The app permission group or {@code null} if the app does not request background
         * location
         */
        @Nullable
        AppPermissionGroup getBackgroundLocationGroup() {
            AppPermissionGroup locationGroup = getLocationGroup();
            if (locationGroup == null) {
                return null;
            }

            return locationGroup.getBackgroundPermissions();
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof UserPackage)) {
                return false;
            }

            UserPackage userPackage = (UserPackage) o;
            return pkg.equals(userPackage.pkg) && user.equals(userPackage.user);
        }

        @Override
        public int hashCode() {
            return Objects.hash(pkg, user);
        }

        @Override
        public String toString() {
            return "UserPackage { "
                    + "pkg = " + pkg + ", "
                    + "UserHandle = " + user.toString() + ", "
                    + "dismissedInSafetyCenter = " + dismissedInSafetyCenter + " }";
        }
    }
}