summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/permission/ui/handheld/PermissionPreference.java
blob: 13a7b997f2d96fd5d4bd3c3f6866f0311f3c3709 (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
/*
 * 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.packageinstaller.permission.ui.handheld;

import static com.android.packageinstaller.permission.utils.Utils.DEFAULT_MAX_LABEL_SIZE_PX;
import static com.android.packageinstaller.permission.utils.Utils.getRequestMessage;

import static java.lang.annotation.RetentionPolicy.SOURCE;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.UserHandle;
import android.text.BidiFormatter;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Switch;

import androidx.annotation.IntDef;
import androidx.annotation.LayoutRes;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceViewHolder;

import com.android.packageinstaller.permission.model.AppPermissionGroup;
import com.android.packageinstaller.permission.model.Permission;
import com.android.packageinstaller.permission.utils.LocationUtils;
import com.android.packageinstaller.permission.utils.Utils;
import com.android.permissioncontroller.R;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

import java.lang.annotation.Retention;
import java.util.List;

/**
 * A preference for representing a permission group requested by an app.
 */
class PermissionPreference extends MultiTargetSwitchPreference {
    @Retention(SOURCE)
    @IntDef(value = {CHANGE_FOREGROUND, CHANGE_BACKGROUND}, flag = true)
    @interface ChangeTarget {}
    static final int CHANGE_FOREGROUND = 1;
    static final int CHANGE_BACKGROUND = 2;
    static final int CHANGE_BOTH = CHANGE_FOREGROUND | CHANGE_BACKGROUND;

    private final AppPermissionGroup mGroup;
    private final PreferenceFragmentCompat mFragment;
    private final PermissionPreferenceChangeListener mCallBacks;
    private final @LayoutRes int mOriginalWidgetLayoutRes;
    private final int mIconSize;

    /** Callbacks for the permission to the fragment showing a list of permissions */
    interface PermissionPreferenceChangeListener {
        /**
         * Checks if the user has to confirm a revocation of a permission granted by default.
         *
         * @return {@code true} iff the user has to confirm it
         */
        boolean shouldConfirmDefaultPermissionRevoke();

        /**
         * Notify the listener that the user confirmed that she/he wants to revoke permissions that
         * were granted by default.
         */
        void hasConfirmDefaultPermissionRevoke();

        /**
         * Notify the listener that this preference has changed.
         *
         * @param key The key uniquely identifying this preference
         */
        void onPreferenceChanged(String key);
    }

    /**
     * Callbacks from dialogs to the fragment. These callbacks are supposed to directly cycle back
     * to the permission tha created the dialog.
     */
    interface PermissionPreferenceOwnerFragment {
        /**
         * The {@link DefaultDenyDialog} can only interact with the fragment, not the preference
         * that created it. Hence this call goes to the fragment, which then finds the preference an
         * calls {@link #onDenyAnyWay(int)}.
         *
         * @param key Key uniquely identifying the preference that created the default deny dialog
         * @param changeTarget Whether background or foreground permissions should be changed
         *
         * @see #showDefaultDenyDialog(int)
         */
        void onDenyAnyWay(String key, @ChangeTarget int changeTarget);

        /**
         * The {@link BackgroundAccessChooser} can only interact with the fragment, not the
         * preference that created it. Hence this call goes to the fragment, which then finds the
         * preference an calls {@link #onBackgroundAccessChosen(int)}}.
         *
         * @param key Key uniquely identifying the preference that created the background access
         *            chooser
         * @param chosenItem The index of the item selected by the user.
         *
         * @see #showBackgroundChooserDialog()
         */
        void onBackgroundAccessChosen(String key, int chosenItem);
    }

    PermissionPreference(PreferenceFragmentCompat fragment, AppPermissionGroup group,
            PermissionPreferenceChangeListener callbacks, int iconSize) {
        super(fragment.getPreferenceManager().getContext());

        mFragment = fragment;
        mGroup = group;
        mCallBacks = callbacks;
        mOriginalWidgetLayoutRes = getWidgetLayoutResource();
        mIconSize = iconSize;

        setPersistent(false);
        updateUi();
    }

    /**
     * Are any permissions of this group fixed by the system, i.e. not changeable by the user.
     *
     * @return {@code true} iff any permission is fixed
     */
    private boolean isSystemFixed() {
        return mGroup.isSystemFixed();
    }

    /**
     * Is any foreground permissions of this group fixed by the policy, i.e. not changeable by the
     * user.
     *
     * @return {@code true} iff any foreground permission is fixed
     */
    private boolean isForegroundPolicyFixed() {
        return mGroup.isPolicyFixed();
    }

    /**
     * Is any background permissions of this group fixed by the policy, i.e. not changeable by the
     * user.
     *
     * @return {@code true} iff any background permission is fixed
     */
    private boolean isBackgroundPolicyFixed() {
        return mGroup.getBackgroundPermissions() != null
                && mGroup.getBackgroundPermissions().isPolicyFixed();
    }

    /**
     * Are there permissions fixed, so that the user cannot change the preference at all?
     *
     * @return {@code true} iff the permissions of this group are fixed
     */
    private boolean isPolicyFullyFixed() {
        return isForegroundPolicyFixed() && (mGroup.getBackgroundPermissions() == null
                || isBackgroundPolicyFixed());
    }

    /**
     * Is the foreground part of this group disabled. If the foreground is disabled, there is no
     * need to possible grant background access.
     *
     * @return {@code true} iff the permissions of this group are fixed
     */
    private boolean isForegroundDisabledByPolicy() {
        return isForegroundPolicyFixed() && !mGroup.areRuntimePermissionsGranted();
    }

    /**
     * Get the app that acts as admin for this profile.
     *
     * @return The admin or {@code null} if there is no admin.
     */
    private EnforcedAdmin getAdmin() {
        return RestrictedLockUtils.getProfileOrDeviceOwner(getContext(), mGroup.getUser());
    }

    /**
     * Update the preference after the state might have changed.
     */
    void updateUi() {
        boolean arePermissionsIndividuallyControlled =
                Utils.areGroupPermissionsIndividuallyControlled(getContext(), mGroup.getName());
        EnforcedAdmin admin = getAdmin();

        // Reset ui state
        setEnabled(true);
        setWidgetLayoutResource(mOriginalWidgetLayoutRes);
        setOnPreferenceClickListener(null);
        setSwitchOnClickListener(null);
        setSummary(null);

        setChecked(mGroup.areRuntimePermissionsGranted());

        if (isSystemFixed() || isPolicyFullyFixed() || isForegroundDisabledByPolicy()) {
            if (admin != null) {
                setWidgetLayoutResource(R.layout.restricted_icon);

                setOnPreferenceClickListener((v) -> {
                    RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), admin);
                    return true;
                });
            } else {
                setEnabled(false);
            }

            updateSummaryForFixedByPolicyPermissionGroup();
        } else if (arePermissionsIndividuallyControlled) {
            setOnPreferenceClickListener((pref) -> {
                showAllPermissions(mGroup.getName());
                return false;
            });

            setSwitchOnClickListener(v -> {
                Switch switchView = (Switch) v;
                requestChange(switchView.isChecked(), CHANGE_BOTH);

                // Update UI as the switch widget might be in wrong state
                updateUi();
            });

            updateSummaryForIndividuallyControlledPermissionGroup();
        } else {
            if (mGroup.hasPermissionWithBackgroundMode()) {
                if (mGroup.getBackgroundPermissions() == null) {
                    // The group has background permissions but the app did not request any. I.e.
                    // The app can only switch between 'never" and "only in foreground".
                    setOnPreferenceChangeListener((pref, newValue) ->
                            requestChange((Boolean) newValue, CHANGE_FOREGROUND));

                    updateSummaryForPermissionGroupWithBackgroundPermission();
                } else {
                    if (isBackgroundPolicyFixed()) {
                        setOnPreferenceChangeListener((pref, newValue) ->
                                requestChange((Boolean) newValue, CHANGE_FOREGROUND));

                        updateSummaryForFixedByPolicyPermissionGroup();
                    } else if (isForegroundPolicyFixed()) {
                        setOnPreferenceChangeListener((pref, newValue) ->
                                requestChange((Boolean) newValue, CHANGE_BACKGROUND));

                        updateSummaryForFixedByPolicyPermissionGroup();
                    } else {
                        updateSummaryForPermissionGroupWithBackgroundPermission();

                        setOnPreferenceClickListener((pref) -> {
                            showBackgroundChooserDialog();
                            return true;
                        });

                        setSwitchOnClickListener(v -> {
                            Switch switchView = (Switch) v;

                            if (switchView.isChecked()) {
                                showBackgroundChooserDialog();
                            } else {
                                requestChange(false, CHANGE_BOTH);
                            }

                            // Update UI as the switch widget might be in wrong state
                            updateUi();
                        });
                    }
                }
            } else {
                setOnPreferenceChangeListener((pref, newValue) ->
                        requestChange((Boolean) newValue, CHANGE_BOTH));
            }
        }
    }

    /**
     * Update the summary in the case the permission group has individually controlled permissions.
     */
    private void updateSummaryForIndividuallyControlledPermissionGroup() {
        int revokedCount = 0;
        List<Permission> permissions = mGroup.getPermissions();
        final int permissionCount = permissions.size();
        for (int i = 0; i < permissionCount; i++) {
            Permission permission = permissions.get(i);
            if (!permission.isGrantedIncludingAppOp()) {
                revokedCount++;
            }
        }

        final int resId;
        if (revokedCount == 0) {
            resId = R.string.permission_revoked_none;
        } else if (revokedCount == permissionCount) {
            resId = R.string.permission_revoked_all;
        } else {
            resId = R.string.permission_revoked_count;
        }

        String summary = getContext().getString(resId, revokedCount);
        setSummary(summary);
    }

    /**
     * Update the summary of a permission group that has background permission.
     *
     * <p>This does not apply to permission groups that are fixed by policy</p>
     */
    private void updateSummaryForPermissionGroupWithBackgroundPermission() {
        AppPermissionGroup backgroundGroup = mGroup.getBackgroundPermissions();

        if (mGroup.areRuntimePermissionsGranted()) {
            if (backgroundGroup == null) {
                setSummary(R.string.permission_access_only_foreground);
            } else {
                if (backgroundGroup.areRuntimePermissionsGranted()) {
                    setSummary(R.string.permission_access_always);
                } else {
                    setSummary(R.string.permission_access_only_foreground);
                }
            }
        } else {
            setSummary(R.string.permission_access_never);
        }
    }

    /**
     * Update the summary of a permission group that is at least partially fixed by policy.
     */
    private void updateSummaryForFixedByPolicyPermissionGroup() {
        EnforcedAdmin admin = getAdmin();
        AppPermissionGroup backgroundGroup = mGroup.getBackgroundPermissions();

        boolean hasAdmin = admin != null;

        if (isSystemFixed()) {
            // Permission is fully controlled by the system and cannot be switched

            setSummary(R.string.permission_summary_enabled_system_fixed);
        } else if (isForegroundDisabledByPolicy()) {
            // Permission is fully controlled by policy and cannot be switched

            if (hasAdmin) {
                setSummary(R.string.disabled_by_admin);
            } else {
                // Disabled state will be displayed by switch, so no need to add text for that
                setSummary(R.string.permission_summary_enforced_by_policy);
            }
        } else if (isPolicyFullyFixed()) {
            // Permission is fully controlled by policy and cannot be switched

            if (backgroundGroup == null) {
                if (hasAdmin) {
                    setSummary(R.string.enabled_by_admin);
                } else {
                    // Enabled state will be displayed by switch, so no need to add text for
                    // that
                    setSummary(R.string.permission_summary_enforced_by_policy);
                }
            } else {
                if (backgroundGroup.areRuntimePermissionsGranted()) {
                    if (hasAdmin) {
                        setSummary(R.string.enabled_by_admin);
                    } else {
                        // Enabled state will be displayed by switch, so no need to add text for
                        // that
                        setSummary(R.string.permission_summary_enforced_by_policy);
                    }
                } else {
                    if (hasAdmin) {
                        setSummary(
                                R.string.permission_summary_enabled_by_admin_foreground_only);
                    } else {
                        setSummary(
                                R.string.permission_summary_enabled_by_policy_foreground_only);
                    }
                }
            }
        } else {
            // Part of the permission group can still be switched

            if (isBackgroundPolicyFixed()) {
                if (backgroundGroup.areRuntimePermissionsGranted()) {
                    if (hasAdmin) {
                        setSummary(R.string.permission_summary_enabled_by_admin_background_only);
                    } else {
                        setSummary(R.string.permission_summary_enabled_by_policy_background_only);
                    }
                } else {
                    if (hasAdmin) {
                        setSummary(R.string.permission_summary_disabled_by_admin_background_only);
                    } else {
                        setSummary(R.string.permission_summary_disabled_by_policy_background_only);
                    }
                }
            } else if (isForegroundPolicyFixed()) {
                if (hasAdmin) {
                    setSummary(R.string.permission_summary_enabled_by_admin_foreground_only);
                } else {
                    setSummary(R.string.permission_summary_enabled_by_policy_foreground_only);
                }
            }
        }
    }

    /**
     * Show all individual permissions in this group in a new fragment.
     */
    private void showAllPermissions(String filterGroup) {
        Fragment frag = AllAppPermissionsFragment.newInstance(mGroup.getApp().packageName,
                filterGroup, UserHandle.getUserHandleForUid(mGroup.getApp().applicationInfo.uid));
        mFragment.getFragmentManager().beginTransaction()
                .replace(android.R.id.content, frag)
                .addToBackStack("AllPerms")
                .commit();
    }

    /**
     * Get the label of the app the permission group belongs to. (App permission groups are all
     * permissions of a group an app has requested.)
     *
     * @return The label of the app
     */
    private String getAppLabel() {
        return BidiFormatter.getInstance().unicodeWrap(
                mGroup.getApp().applicationInfo.loadSafeLabel(getContext().getPackageManager(),
                        DEFAULT_MAX_LABEL_SIZE_PX,
                        TextUtils.SAFE_STRING_FLAG_TRIM
                                | TextUtils.SAFE_STRING_FLAG_FIRST_LINE)
                        .toString());
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        if (mIconSize > 0) {
            ImageView icon = ((ImageView) holder.findViewById(android.R.id.icon));

            icon.setMaxWidth(mIconSize);
            icon.setMaxHeight(mIconSize);
        }

        super.onBindViewHolder(holder);
    }

    /**
     * Request to grant/revoke permissions group.
     *
     * <p>Does <u>not</u> handle:
     * <ul>
     * <li>Individually granted permissions</li>
     * <li>Permission groups with background permissions</li>
     * </ul>
     * <p><u>Does</u> handle:
     * <ul>
     * <li>Default grant permissions</li>
     * </ul>
     *
     * @param requestGrant If this group should be granted
     * @param changeTarget Which permission group (foreground/background/both) should be changed
     * @return If the request was processed.
     */
    private boolean requestChange(boolean requestGrant, @ChangeTarget int changeTarget) {
        if (LocationUtils.isLocationGroupAndProvider(getContext(), mGroup.getName(),
                mGroup.getApp().packageName)) {
            LocationUtils.showLocationDialog(getContext(), getAppLabel());
            return false;
        }
        if (requestGrant) {
            mCallBacks.onPreferenceChanged(getKey());

            if ((changeTarget & CHANGE_FOREGROUND) != 0) {
                mGroup.grantRuntimePermissions(false);
            }
            if ((changeTarget & CHANGE_BACKGROUND) != 0) {
                if (mGroup.getBackgroundPermissions() != null) {
                    mGroup.getBackgroundPermissions().grantRuntimePermissions(false);
                }
            }
        } else {
            boolean requestToRevokeGrantedByDefault = false;
            if ((changeTarget & CHANGE_FOREGROUND) != 0) {
                requestToRevokeGrantedByDefault = mGroup.hasGrantedByDefaultPermission();
            }
            if ((changeTarget & CHANGE_BACKGROUND) != 0) {
                if (mGroup.getBackgroundPermissions() != null) {
                    requestToRevokeGrantedByDefault |=
                            mGroup.getBackgroundPermissions().hasGrantedByDefaultPermission();
                }
            }

            if ((requestToRevokeGrantedByDefault || !mGroup.doesSupportRuntimePermissions())
                    && mCallBacks.shouldConfirmDefaultPermissionRevoke()) {
                showDefaultDenyDialog(changeTarget);
                return false;
            } else {
                mCallBacks.onPreferenceChanged(getKey());

                if ((changeTarget & CHANGE_FOREGROUND) != 0) {
                    mGroup.revokeRuntimePermissions(false);
                }
                if ((changeTarget & CHANGE_BACKGROUND) != 0) {
                    if (mGroup.getBackgroundPermissions() != null) {
                        mGroup.getBackgroundPermissions().revokeRuntimePermissions(false);
                    }
                }
            }
        }

        updateUi();

        return true;
    }

    /**
     * Show a dialog that warns the user that she/he is about to revoke permissions that were
     * granted by default.
     *
     * <p>The order of operation to revoke a permission granted by default is:
     * <ol>
     *     <li>{@code showDefaultDenyDialog}</li>
     *     <li>{@link DefaultDenyDialog#onCreateDialog}</li>
     *     <li>{@link PermissionPreferenceOwnerFragment#onDenyAnyWay}</li>
     *     <li>{@link PermissionPreference#onDenyAnyWay}</li>
     * </ol>
     *
     * @param changeTarget Whether background or foreground should be changed
     */
    private void showDefaultDenyDialog(@ChangeTarget int changeTarget) {
        if (!mFragment.isResumed()) {
            return;
        }

        Bundle args = new Bundle();

        boolean showGrantedByDefaultWarning = false;
        if ((changeTarget & CHANGE_FOREGROUND) != 0) {
            showGrantedByDefaultWarning = mGroup.hasGrantedByDefaultPermission();
        }
        if ((changeTarget & CHANGE_BACKGROUND) != 0) {
            if (mGroup.getBackgroundPermissions() != null) {
                showGrantedByDefaultWarning |=
                        mGroup.getBackgroundPermissions().hasGrantedByDefaultPermission();
            }
        }

        args.putInt(DefaultDenyDialog.MSG, showGrantedByDefaultWarning ? R.string.system_warning
                : R.string.old_sdk_deny_warning);
        args.putString(DefaultDenyDialog.KEY, getKey());
        args.putInt(DefaultDenyDialog.CHANGE_TARGET, changeTarget);

        DefaultDenyDialog deaultDenyDialog = new DefaultDenyDialog();
        deaultDenyDialog.setArguments(args);
        deaultDenyDialog.show(mFragment.getChildFragmentManager().beginTransaction(),
                "denyDefault");
    }

    /**
     * Show a dialog that asks the user if foreground/background/none access should be enabled.
     *
     * <p>The order of operation to grant foreground/background/none access is:
     * <ol>
     *     <li>{@code showBackgroundChooserDialog}</li>
     *     <li>{@link BackgroundAccessChooser#onCreateDialog}</li>
     *     <li>{@link PermissionPreferenceOwnerFragment#onBackgroundAccessChosen}</li>
     *     <li>{@link PermissionPreference#onBackgroundAccessChosen}</li>
     * </ol>
     */
    private void showBackgroundChooserDialog() {
        if (!mFragment.isResumed()) {
            return;
        }

        if (LocationUtils.isLocationGroupAndProvider(getContext(), mGroup.getName(),
                mGroup.getApp().packageName)) {
            LocationUtils.showLocationDialog(getContext(), getAppLabel());
            return;
        }

        Bundle args = new Bundle();
        args.putCharSequence(BackgroundAccessChooser.TITLE,
                getRequestMessage(getAppLabel(), mGroup, getContext(), mGroup.getRequest()));
        args.putString(BackgroundAccessChooser.KEY, getKey());


        if (mGroup.areRuntimePermissionsGranted()) {
            if (mGroup.getBackgroundPermissions().areRuntimePermissionsGranted()) {
                args.putInt(BackgroundAccessChooser.SELECTION,
                        BackgroundAccessChooser.ALWAYS_OPTION);
            } else {
                args.putInt(BackgroundAccessChooser.SELECTION,
                        BackgroundAccessChooser.FOREGROUND_ONLY_OPTION);
            }
        } else {
            args.putInt(BackgroundAccessChooser.SELECTION, BackgroundAccessChooser.NEVER_OPTION);
        }

        BackgroundAccessChooser chooserDialog = new BackgroundAccessChooser();
        chooserDialog.setArguments(args);
        chooserDialog.show(mFragment.getChildFragmentManager().beginTransaction(),
                "backgroundChooser");
    }

    /**
     * Once we user has confirmed that he/she wants to revoke a permission that was granted by
     * default, actually revoke the permissions.
     *
     * @see #showDefaultDenyDialog(int)
     */
    void onDenyAnyWay(@ChangeTarget int changeTarget) {
        mCallBacks.onPreferenceChanged(getKey());

        boolean hasDefaultPermissions = false;
        if ((changeTarget & CHANGE_FOREGROUND) != 0) {
            mGroup.revokeRuntimePermissions(false);
            hasDefaultPermissions = mGroup.hasGrantedByDefaultPermission();
        }
        if ((changeTarget & CHANGE_BACKGROUND) != 0) {
            if (mGroup.getBackgroundPermissions() != null) {
                mGroup.getBackgroundPermissions().revokeRuntimePermissions(false);
                hasDefaultPermissions |=
                        mGroup.getBackgroundPermissions().hasGrantedByDefaultPermission();
            }
        }

        if (hasDefaultPermissions || !mGroup.doesSupportRuntimePermissions()) {
            mCallBacks.hasConfirmDefaultPermissionRevoke();
        }
        updateUi();
    }

    /**
     * Process the return from a {@link BackgroundAccessChooser} dialog.
     *
     * <p>These dialog are started when the user want to grant a permission group that has
     * background permissions.
     *
     * @param choosenItem The item that the user chose
     */
    void onBackgroundAccessChosen(int choosenItem) {
        AppPermissionGroup backgroundGroup = mGroup.getBackgroundPermissions();

        switch (choosenItem) {
            case BackgroundAccessChooser.ALWAYS_OPTION:
                requestChange(true, CHANGE_BOTH);
                break;
            case BackgroundAccessChooser.FOREGROUND_ONLY_OPTION:
                if (backgroundGroup.areRuntimePermissionsGranted()) {
                    requestChange(false, CHANGE_BACKGROUND);
                }
                requestChange(true, CHANGE_FOREGROUND);
                break;
            case BackgroundAccessChooser.NEVER_OPTION:
                if (mGroup.areRuntimePermissionsGranted()
                        || mGroup.getBackgroundPermissions().areRuntimePermissionsGranted()) {
                    requestChange(false, CHANGE_BOTH);
                }
                break;
        }
    }

    /**
     * A dialog warning the user that she/he is about to deny a permission that was granted by
     * default.
     *
     * @see #showDefaultDenyDialog(int)
     */
    public static class DefaultDenyDialog extends DialogFragment {
        private static final String MSG = DefaultDenyDialog.class.getName() + ".arg.msg";
        private static final String CHANGE_TARGET = DefaultDenyDialog.class.getName()
                + ".arg.changeTarget";
        private static final String KEY = DefaultDenyDialog.class.getName() + ".arg.key";

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder b = new AlertDialog.Builder(getContext())
                    .setMessage(getArguments().getInt(MSG))
                    .setNegativeButton(R.string.cancel, null)
                    .setPositiveButton(R.string.grant_dialog_button_deny_anyway,
                            (DialogInterface dialog, int which) -> (
                                    (PermissionPreferenceOwnerFragment) getParentFragment())
                                    .onDenyAnyWay(getArguments().getString(KEY),
                                            getArguments().getInt(CHANGE_TARGET)));

            return b.create();
        }
    }

    /**
     * If a permission group has background permission this chooser is used to let the user
     * choose how the permission group should be granted.
     *
     * @see #showBackgroundChooserDialog()
     */
    public static class BackgroundAccessChooser extends DialogFragment {
        private static final String TITLE = BackgroundAccessChooser.class.getName() + ".arg.title";
        private static final String KEY = BackgroundAccessChooser.class.getName() + ".arg.key";
        private static final String SELECTION = BackgroundAccessChooser.class.getName()
                + ".arg.selection";

        // Needs to match the entries in R.array.background_access_chooser_dialog_choices
        static final int ALWAYS_OPTION = 0;
        static final int FOREGROUND_ONLY_OPTION = 1;
        static final int NEVER_OPTION = 2;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
                    .setTitle(getArguments().getCharSequence(TITLE))
                    .setSingleChoiceItems(R.array.background_access_chooser_dialog_choices,
                            getArguments().getInt(SELECTION),
                            (dialog, which) -> {
                                dismissAllowingStateLoss();
                                ((PermissionPreferenceOwnerFragment) getParentFragment())
                                        .onBackgroundAccessChosen(getArguments().getString(KEY),
                                                which);
                            }
                    );

            return b.create();
        }
    }
}