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
|
/*
* 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 android.service.notification;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.app.Notification;
import android.app.NotificationChannel;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.proto.ProtoOutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
/**
* ZenPolicy determines whether to allow certain notifications and their corresponding sounds to
* play when a device is in Do Not Disturb mode.
* ZenPolicy also dictates the visual effects of notifications that are intercepted when
* a device is in Do Not Disturb mode.
*/
public final class ZenPolicy implements Parcelable {
private ArrayList<Integer> mPriorityCategories;
private ArrayList<Integer> mVisualEffects;
private @PeopleType int mPriorityMessages = PEOPLE_TYPE_UNSET;
private @PeopleType int mPriorityCalls = PEOPLE_TYPE_UNSET;
/** @hide */
@IntDef(prefix = { "PRIORITY_CATEGORY_" }, value = {
PRIORITY_CATEGORY_REMINDERS,
PRIORITY_CATEGORY_EVENTS,
PRIORITY_CATEGORY_MESSAGES,
PRIORITY_CATEGORY_CALLS,
PRIORITY_CATEGORY_REPEAT_CALLERS,
PRIORITY_CATEGORY_ALARMS,
PRIORITY_CATEGORY_MEDIA,
PRIORITY_CATEGORY_SYSTEM,
})
@Retention(RetentionPolicy.SOURCE)
public @interface PriorityCategory {}
/** @hide */
public static final int PRIORITY_CATEGORY_REMINDERS = 0;
/** @hide */
public static final int PRIORITY_CATEGORY_EVENTS = 1;
/** @hide */
public static final int PRIORITY_CATEGORY_MESSAGES = 2;
/** @hide */
public static final int PRIORITY_CATEGORY_CALLS = 3;
/** @hide */
public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 4;
/** @hide */
public static final int PRIORITY_CATEGORY_ALARMS = 5;
/** @hide */
public static final int PRIORITY_CATEGORY_MEDIA = 6;
/** @hide */
public static final int PRIORITY_CATEGORY_SYSTEM = 7;
/** @hide */
@IntDef(prefix = { "VISUAL_EFFECT_" }, value = {
VISUAL_EFFECT_FULL_SCREEN_INTENT,
VISUAL_EFFECT_LIGHTS,
VISUAL_EFFECT_PEEK,
VISUAL_EFFECT_STATUS_BAR,
VISUAL_EFFECT_BADGE,
VISUAL_EFFECT_AMBIENT,
VISUAL_EFFECT_NOTIFICATION_LIST,
})
@Retention(RetentionPolicy.SOURCE)
public @interface VisualEffect {}
/** @hide */
public static final int VISUAL_EFFECT_FULL_SCREEN_INTENT = 0;
/** @hide */
public static final int VISUAL_EFFECT_LIGHTS = 1;
/** @hide */
public static final int VISUAL_EFFECT_PEEK = 2;
/** @hide */
public static final int VISUAL_EFFECT_STATUS_BAR = 3;
/** @hide */
public static final int VISUAL_EFFECT_BADGE = 4;
/** @hide */
public static final int VISUAL_EFFECT_AMBIENT = 5;
/** @hide */
public static final int VISUAL_EFFECT_NOTIFICATION_LIST = 6;
/** @hide */
@IntDef(prefix = { "PEOPLE_TYPE_" }, value = {
PEOPLE_TYPE_UNSET,
PEOPLE_TYPE_ANYONE,
PEOPLE_TYPE_CONTACTS,
PEOPLE_TYPE_STARRED,
PEOPLE_TYPE_NONE,
})
@Retention(RetentionPolicy.SOURCE)
public @interface PeopleType {}
/**
* Used to indicate no preference for the type of people that can bypass dnd for either
* calls or messages.
*/
public static final int PEOPLE_TYPE_UNSET = 0;
/**
* Used to indicate all calls or messages can bypass dnd.
*/
public static final int PEOPLE_TYPE_ANYONE = 1;
/**
* Used to indicate calls or messages from contacts can bypass dnd.
*/
public static final int PEOPLE_TYPE_CONTACTS = 2;
/**
* Used to indicate calls or messages from starred contacts can bypass dnd.
*/
public static final int PEOPLE_TYPE_STARRED = 3;
/**
* Used to indicate no calls or messages can bypass dnd.
*/
public static final int PEOPLE_TYPE_NONE = 4;
/** @hide */
@IntDef(prefix = { "STATE_" }, value = {
STATE_UNSET,
STATE_ALLOW,
STATE_DISALLOW,
})
@Retention(RetentionPolicy.SOURCE)
public @interface State {}
/**
* Indicates no preference for whether a type of sound or visual effect is or isn't allowed
* to play/show when DND is active. Will default to the current set policy.
*/
public static final int STATE_UNSET = 0;
/**
* Indicates a type of sound or visual effect is allowed to play/show when DND is active.
*/
public static final int STATE_ALLOW = 1;
/**
* Indicates a type of sound or visual effect is not allowed to play/show when DND is active.
*/
public static final int STATE_DISALLOW = 2;
/** @hide */
public ZenPolicy() {
mPriorityCategories = new ArrayList<>(Collections.nCopies(8, 0));
mVisualEffects = new ArrayList<>(Collections.nCopies(7, 0));
}
/**
* Message senders that can bypass DND.
* @return {@link #PEOPLE_TYPE_UNSET}, {@link #PEOPLE_TYPE_ANYONE},
* {@link #PEOPLE_TYPE_CONTACTS}, {@link #PEOPLE_TYPE_STARRED} or {@link #PEOPLE_TYPE_NONE}
*/
public @PeopleType int getPriorityMessageSenders() {
return mPriorityMessages;
}
/**
* Callers that can bypass DND.
* @return {@link #PEOPLE_TYPE_UNSET}, {@link #PEOPLE_TYPE_ANYONE},
* {@link #PEOPLE_TYPE_CONTACTS}, {@link #PEOPLE_TYPE_STARRED} or {@link #PEOPLE_TYPE_NONE}
*/
public @PeopleType int getPriorityCallSenders() {
return mPriorityCalls;
}
/**
* Whether this policy wants to allow notifications with category
* {@link Notification#CATEGORY_REMINDER} to play sounds and visually appear
* or to intercept them when DND is active.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategoryReminders() {
return mPriorityCategories.get(PRIORITY_CATEGORY_REMINDERS);
}
/**
* Whether this policy wants to allow notifications with category
* {@link Notification#CATEGORY_EVENT} to play sounds and visually appear
* or to intercept them when DND is active.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategoryEvents() {
return mPriorityCategories.get(PRIORITY_CATEGORY_EVENTS);
}
/**
* Whether this policy wants to allow notifications with category
* {@link Notification#CATEGORY_MESSAGE} to play sounds and visually appear
* or to intercept them when DND is active. Types of message senders that are allowed
* are specified by {@link #getPriorityMessageSenders}.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategoryMessages() {
return mPriorityCategories.get(PRIORITY_CATEGORY_MESSAGES);
}
/**
* Whether this policy wants to allow notifications with category
* {@link Notification#CATEGORY_CALL} to play sounds and visually appear
* or to intercept them when DND is active. Types of callers that are allowed
* are specified by {@link #getPriorityCallSenders()}.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategoryCalls() {
return mPriorityCategories.get(PRIORITY_CATEGORY_CALLS);
}
/**
* Whether this policy wants to allow repeat callers (notifications with category
* {@link Notification#CATEGORY_CALL} that have recently called) to play sounds and
* visually appear or to intercept them when DND is active.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategoryRepeatCallers() {
return mPriorityCategories.get(PRIORITY_CATEGORY_REPEAT_CALLERS);
}
/**
* Whether this policy wants to allow notifications with category
* {@link Notification#CATEGORY_ALARM} to play sounds and visually appear
* or to intercept them when DND is active.
* When alarms are {@link #STATE_DISALLOW disallowed}, the alarm stream will be muted when DND
* is active.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategoryAlarms() {
return mPriorityCategories.get(PRIORITY_CATEGORY_ALARMS);
}
/**
* Whether this policy wants to allow media notifications to play sounds and visually appear
* or to intercept them when DND is active.
* When media is {@link #STATE_DISALLOW disallowed}, the media stream will be muted when DND is
* active.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategoryMedia() {
return mPriorityCategories.get(PRIORITY_CATEGORY_MEDIA);
}
/**
* Whether this policy wants to allow system sounds when DND is active.
* When system is {@link #STATE_DISALLOW}, the system stream will be muted when DND is active.
* @return {@link #STATE_UNSET}, {@link #STATE_ALLOW} or {@link #STATE_DISALLOW}
*/
public @State int getPriorityCategorySystem() {
return mPriorityCategories.get(PRIORITY_CATEGORY_SYSTEM);
}
/**
* Whether this policy allows {@link Notification#fullScreenIntent full screen intents} from
* notifications intercepted by DND.
*/
public @State int getVisualEffectFullScreenIntent() {
return mVisualEffects.get(VISUAL_EFFECT_FULL_SCREEN_INTENT);
}
/**
* Whether this policy allows {@link NotificationChannel#shouldShowLights() notification
* lights} from notifications intercepted by DND.
*/
public @State int getVisualEffectLights() {
return mVisualEffects.get(VISUAL_EFFECT_LIGHTS);
}
/**
* Whether this policy allows peeking from notifications intercepted by DND.
*/
public @State int getVisualEffectPeek() {
return mVisualEffects.get(VISUAL_EFFECT_PEEK);
}
/**
* Whether this policy allows notifications intercepted by DND from appearing in the status bar
* on devices that support status bars.
*/
public @State int getVisualEffectStatusBar() {
return mVisualEffects.get(VISUAL_EFFECT_STATUS_BAR);
}
/**
* Whether this policy allows {@link NotificationChannel#canShowBadge() badges} from
* notifications intercepted by DND on devices that support badging.
*/
public @State int getVisualEffectBadge() {
return mVisualEffects.get(VISUAL_EFFECT_BADGE);
}
/**
* Whether this policy allows notifications intercepted by DND from appearing on ambient
* displays on devices that support ambient display.
*/
public @State int getVisualEffectAmbient() {
return mVisualEffects.get(VISUAL_EFFECT_AMBIENT);
}
/**
* Whether this policy allows notifications intercepted by DND from appearing in notification
* list views like the notification shade or lockscreen on devices that support those
* views.
*/
public @State int getVisualEffectNotificationList() {
return mVisualEffects.get(VISUAL_EFFECT_NOTIFICATION_LIST);
}
/**
* Whether this policy hides all visual effects
* @hide
*/
public boolean shouldHideAllVisualEffects() {
for (int i = 0; i < mVisualEffects.size(); i++) {
if (mVisualEffects.get(i) != STATE_DISALLOW) {
return false;
}
}
return true;
}
/**
* Whether this policy shows all visual effects
* @hide
*/
public boolean shouldShowAllVisualEffects() {
for (int i = 0; i < mVisualEffects.size(); i++) {
if (mVisualEffects.get(i) != STATE_ALLOW) {
return false;
}
}
return true;
}
/**
* Builder class for {@link ZenPolicy} objects.
* Provides a convenient way to set the various fields of a {@link ZenPolicy}. If a field
* is not set, it is (@link STATE_UNSET} and will not change the current set policy.
*/
public static final class Builder {
private ZenPolicy mZenPolicy;
public Builder() {
mZenPolicy = new ZenPolicy();
}
/**
* @hide
*/
public Builder(ZenPolicy policy) {
if (policy != null) {
mZenPolicy = policy.copy();
} else {
mZenPolicy = new ZenPolicy();
}
}
/**
* Builds the current ZenPolicy.
*/
public @NonNull ZenPolicy build() {
return mZenPolicy.copy();
}
/**
* Allows all notifications to bypass DND and unmutes all streams.
*/
public @NonNull Builder allowAllSounds() {
for (int i = 0; i < mZenPolicy.mPriorityCategories.size(); i++) {
mZenPolicy.mPriorityCategories.set(i, STATE_ALLOW);
}
mZenPolicy.mPriorityMessages = PEOPLE_TYPE_ANYONE;
mZenPolicy.mPriorityCalls = PEOPLE_TYPE_ANYONE;
return this;
}
/**
* Intercepts all notifications and prevents them from playing sounds
* when DND is active. Also mutes alarm, system and media streams.
* Notification channels can still play sounds only if they
* {@link NotificationChannel#canBypassDnd can bypass DND}. If no channels can bypass DND,
* the ringer stream is also muted.
*/
public @NonNull Builder disallowAllSounds() {
for (int i = 0; i < mZenPolicy.mPriorityCategories.size(); i++) {
mZenPolicy.mPriorityCategories.set(i, STATE_DISALLOW);
}
mZenPolicy.mPriorityMessages = PEOPLE_TYPE_NONE;
mZenPolicy.mPriorityCalls = PEOPLE_TYPE_NONE;
return this;
}
/**
* Allows notifications intercepted by DND to show on all surfaces when DND is active.
*/
public @NonNull Builder showAllVisualEffects() {
for (int i = 0; i < mZenPolicy.mVisualEffects.size(); i++) {
mZenPolicy.mVisualEffects.set(i, STATE_ALLOW);
}
return this;
}
/**
* Disallows notifications intercepted by DND from showing when DND is active.
*/
public @NonNull Builder hideAllVisualEffects() {
for (int i = 0; i < mZenPolicy.mVisualEffects.size(); i++) {
mZenPolicy.mVisualEffects.set(i, STATE_DISALLOW);
}
return this;
}
/**
* Unsets a priority category, neither allowing or disallowing. When applying this policy,
* unset categories will default to the current applied policy.
* @hide
*/
public @NonNull Builder unsetPriorityCategory(@PriorityCategory int category) {
mZenPolicy.mPriorityCategories.set(category, STATE_UNSET);
if (category == PRIORITY_CATEGORY_MESSAGES) {
mZenPolicy.mPriorityMessages = STATE_UNSET;
} else if (category == PRIORITY_CATEGORY_CALLS) {
mZenPolicy.mPriorityCalls = STATE_UNSET;
}
return this;
}
/**
* Unsets a visual effect, neither allowing or disallowing. When applying this policy,
* unset effects will default to the current applied policy.
* @hide
*/
public @NonNull Builder unsetVisualEffect(@VisualEffect int effect) {
mZenPolicy.mVisualEffects.set(effect, STATE_UNSET);
return this;
}
/**
* Whether to allow notifications with category {@link Notification#CATEGORY_REMINDER}
* to play sounds and visually appear or to intercept them when DND is active.
*/
public @NonNull Builder allowReminders(boolean allow) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_REMINDERS,
allow ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether to allow notifications with category {@link Notification#CATEGORY_EVENT}
* to play sounds and visually appear or to intercept them when DND is active.
*/
public @NonNull Builder allowEvents(boolean allow) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_EVENTS,
allow ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether to allow notifications with category {@link Notification#CATEGORY_MESSAGE}
* that match audienceType to play sounds and visually appear or to intercept
* them when DND is active.
* @param audienceType message senders that are allowed to bypass DND
*/
public @NonNull Builder allowMessages(@PeopleType int audienceType) {
if (audienceType == STATE_UNSET) {
return unsetPriorityCategory(PRIORITY_CATEGORY_MESSAGES);
}
if (audienceType == PEOPLE_TYPE_NONE) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_MESSAGES, STATE_DISALLOW);
} else if (audienceType == PEOPLE_TYPE_ANYONE || audienceType == PEOPLE_TYPE_CONTACTS
|| audienceType == PEOPLE_TYPE_STARRED) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_MESSAGES, STATE_ALLOW);
} else {
return this;
}
mZenPolicy.mPriorityMessages = audienceType;
return this;
}
/**
* Whether to allow notifications with category {@link Notification#CATEGORY_CALL}
* that match audienceType to play sounds and visually appear or to intercept
* them when DND is active.
* @param audienceType callers that are allowed to bypass DND
*/
public @NonNull Builder allowCalls(@PeopleType int audienceType) {
if (audienceType == STATE_UNSET) {
return unsetPriorityCategory(PRIORITY_CATEGORY_CALLS);
}
if (audienceType == PEOPLE_TYPE_NONE) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_CALLS, STATE_DISALLOW);
} else if (audienceType == PEOPLE_TYPE_ANYONE || audienceType == PEOPLE_TYPE_CONTACTS
|| audienceType == PEOPLE_TYPE_STARRED) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_CALLS, STATE_ALLOW);
} else {
return this;
}
mZenPolicy.mPriorityCalls = audienceType;
return this;
}
/**
* Whether to allow repeat callers (notifications with category
* {@link Notification#CATEGORY_CALL} that have recently called
* to play sounds and visually appear.
*/
public @NonNull Builder allowRepeatCallers(boolean allow) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_REPEAT_CALLERS,
allow ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether to allow notifications with category {@link Notification#CATEGORY_ALARM}
* to play sounds and visually appear or to intercept them when DND is active.
* Disallowing alarms will mute the alarm stream when DND is active.
*/
public @NonNull Builder allowAlarms(boolean allow) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_ALARMS,
allow ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether to allow media notifications to play sounds and visually
* appear or to intercept them when DND is active.
* Disallowing media will mute the media stream when DND is active.
*/
public @NonNull Builder allowMedia(boolean allow) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_MEDIA,
allow ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether to allow system sounds to play when DND is active.
* Disallowing system sounds will mute the system stream when DND is active.
*/
public @NonNull Builder allowSystem(boolean allow) {
mZenPolicy.mPriorityCategories.set(PRIORITY_CATEGORY_SYSTEM,
allow ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether to allow {@link PriorityCategory} sounds to play when DND is active.
* @hide
*/
public @NonNull Builder allowCategory(@PriorityCategory int category, boolean allow) {
switch (category) {
case PRIORITY_CATEGORY_ALARMS:
allowAlarms(allow);
break;
case PRIORITY_CATEGORY_MEDIA:
allowMedia(allow);
break;
case PRIORITY_CATEGORY_SYSTEM:
allowSystem(allow);
break;
case PRIORITY_CATEGORY_REMINDERS:
allowReminders(allow);
break;
case PRIORITY_CATEGORY_EVENTS:
allowEvents(allow);
break;
case PRIORITY_CATEGORY_REPEAT_CALLERS:
allowRepeatCallers(allow);
break;
}
return this;
}
/**
* Whether {@link Notification#fullScreenIntent full screen intents} that are intercepted
* by DND are shown.
*/
public @NonNull Builder showFullScreenIntent(boolean show) {
mZenPolicy.mVisualEffects.set(VISUAL_EFFECT_FULL_SCREEN_INTENT,
show ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether {@link NotificationChannel#shouldShowLights() notification lights} from
* notifications intercepted by DND are blocked.
*/
public @NonNull Builder showLights(boolean show) {
mZenPolicy.mVisualEffects.set(VISUAL_EFFECT_LIGHTS,
show ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether notifications intercepted by DND are prevented from peeking.
*/
public @NonNull Builder showPeeking(boolean show) {
mZenPolicy.mVisualEffects.set(VISUAL_EFFECT_PEEK,
show ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether notifications intercepted by DND are prevented from appearing in the status bar
* on devices that support status bars.
*/
public @NonNull Builder showStatusBarIcons(boolean show) {
mZenPolicy.mVisualEffects.set(VISUAL_EFFECT_STATUS_BAR,
show ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether {@link NotificationChannel#canShowBadge() badges} from
* notifications intercepted by DND are allowed on devices that support badging.
*/
public @NonNull Builder showBadges(boolean show) {
mZenPolicy.mVisualEffects.set(VISUAL_EFFECT_BADGE,
show ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether notification intercepted by DND are prevented from appearing on ambient displays
* on devices that support ambient display.
*/
public @NonNull Builder showInAmbientDisplay(boolean show) {
mZenPolicy.mVisualEffects.set(VISUAL_EFFECT_AMBIENT,
show ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether notification intercepted by DND are prevented from appearing in notification
* list views like the notification shade or lockscreen on devices that support those
* views.
*/
public @NonNull Builder showInNotificationList(boolean show) {
mZenPolicy.mVisualEffects.set(VISUAL_EFFECT_NOTIFICATION_LIST,
show ? STATE_ALLOW : STATE_DISALLOW);
return this;
}
/**
* Whether notifications intercepted by DND are prevented from appearing for
* {@link VisualEffect}
* @hide
*/
public @NonNull Builder showVisualEffect(@VisualEffect int effect, boolean show) {
switch (effect) {
case VISUAL_EFFECT_FULL_SCREEN_INTENT:
showFullScreenIntent(show);
break;
case VISUAL_EFFECT_LIGHTS:
showLights(show);
break;
case VISUAL_EFFECT_PEEK:
showPeeking(show);
break;
case VISUAL_EFFECT_STATUS_BAR:
showStatusBarIcons(show);
break;
case VISUAL_EFFECT_BADGE:
showBadges(show);
break;
case VISUAL_EFFECT_AMBIENT:
showInAmbientDisplay(show);
break;
case VISUAL_EFFECT_NOTIFICATION_LIST:
showInNotificationList(show);
break;
}
return this;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(mPriorityCategories);
dest.writeList(mVisualEffects);
dest.writeInt(mPriorityCalls);
dest.writeInt(mPriorityMessages);
}
public static final @android.annotation.NonNull Parcelable.Creator<ZenPolicy> CREATOR =
new Parcelable.Creator<ZenPolicy>() {
@Override
public ZenPolicy createFromParcel(Parcel source) {
ZenPolicy policy = new ZenPolicy();
policy.mPriorityCategories = source.readArrayList(Integer.class.getClassLoader());
policy.mVisualEffects = source.readArrayList(Integer.class.getClassLoader());
policy.mPriorityCalls = source.readInt();
policy.mPriorityMessages = source.readInt();
return policy;
}
@Override
public ZenPolicy[] newArray(int size) {
return new ZenPolicy[size];
}
};
@Override
public String toString() {
return new StringBuilder(ZenPolicy.class.getSimpleName())
.append('{')
.append("priorityCategories=[").append(priorityCategoriesToString())
.append("], visualEffects=[").append(visualEffectsToString())
.append("], priorityCalls=").append(peopleTypeToString(mPriorityCalls))
.append(", priorityMessages=").append(peopleTypeToString(mPriorityMessages))
.append('}')
.toString();
}
private String priorityCategoriesToString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < mPriorityCategories.size(); i++) {
if (mPriorityCategories.get(i) != STATE_UNSET) {
builder.append(indexToCategory(i))
.append("=")
.append(stateToString(mPriorityCategories.get(i)))
.append(" ");
}
}
return builder.toString();
}
private String visualEffectsToString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < mVisualEffects.size(); i++) {
if (mVisualEffects.get(i) != STATE_UNSET) {
builder.append(indexToVisualEffect(i))
.append("=")
.append(stateToString(mVisualEffects.get(i)))
.append(" ");
}
}
return builder.toString();
}
private String indexToVisualEffect(@VisualEffect int visualEffectIndex) {
switch (visualEffectIndex) {
case VISUAL_EFFECT_FULL_SCREEN_INTENT:
return "fullScreenIntent";
case VISUAL_EFFECT_LIGHTS:
return "lights";
case VISUAL_EFFECT_PEEK:
return "peek";
case VISUAL_EFFECT_STATUS_BAR:
return "statusBar";
case VISUAL_EFFECT_BADGE:
return "badge";
case VISUAL_EFFECT_AMBIENT:
return "ambient";
case VISUAL_EFFECT_NOTIFICATION_LIST:
return "notificationList";
}
return null;
}
private String indexToCategory(@PriorityCategory int categoryIndex) {
switch (categoryIndex) {
case PRIORITY_CATEGORY_REMINDERS:
return "reminders";
case PRIORITY_CATEGORY_EVENTS:
return "events";
case PRIORITY_CATEGORY_MESSAGES:
return "messages";
case PRIORITY_CATEGORY_CALLS:
return "calls";
case PRIORITY_CATEGORY_REPEAT_CALLERS:
return "repeatCallers";
case PRIORITY_CATEGORY_ALARMS:
return "alarms";
case PRIORITY_CATEGORY_MEDIA:
return "media";
case PRIORITY_CATEGORY_SYSTEM:
return "system";
}
return null;
}
private String stateToString(@State int state) {
switch (state) {
case STATE_UNSET:
return "unset";
case STATE_DISALLOW:
return "disallow";
case STATE_ALLOW:
return "allow";
}
return "invalidState{" + state + "}";
}
private String peopleTypeToString(@PeopleType int peopleType) {
switch (peopleType) {
case PEOPLE_TYPE_ANYONE:
return "anyone";
case PEOPLE_TYPE_CONTACTS:
return "contacts";
case PEOPLE_TYPE_NONE:
return "none";
case PEOPLE_TYPE_STARRED:
return "starred_contacts";
case STATE_UNSET:
return "unset";
}
return "invalidPeopleType{" + peopleType + "}";
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ZenPolicy)) return false;
if (o == this) return true;
final ZenPolicy other = (ZenPolicy) o;
return Objects.equals(other.mPriorityCategories, mPriorityCategories)
&& Objects.equals(other.mVisualEffects, mVisualEffects)
&& other.mPriorityCalls == mPriorityCalls
&& other.mPriorityMessages == mPriorityMessages;
}
@Override
public int hashCode() {
return Objects.hash(mPriorityCategories, mVisualEffects, mPriorityCalls, mPriorityMessages);
}
private @ZenPolicy.State int getZenPolicyPriorityCategoryState(@PriorityCategory int
category) {
switch (category) {
case PRIORITY_CATEGORY_REMINDERS:
return getPriorityCategoryReminders();
case PRIORITY_CATEGORY_EVENTS:
return getPriorityCategoryEvents();
case PRIORITY_CATEGORY_MESSAGES:
return getPriorityCategoryMessages();
case PRIORITY_CATEGORY_CALLS:
return getPriorityCategoryCalls();
case PRIORITY_CATEGORY_REPEAT_CALLERS:
return getPriorityCategoryRepeatCallers();
case PRIORITY_CATEGORY_ALARMS:
return getPriorityCategoryAlarms();
case PRIORITY_CATEGORY_MEDIA:
return getPriorityCategoryMedia();
case PRIORITY_CATEGORY_SYSTEM:
return getPriorityCategorySystem();
}
return -1;
}
private @ZenPolicy.State int getZenPolicyVisualEffectState(@VisualEffect int effect) {
switch (effect) {
case VISUAL_EFFECT_FULL_SCREEN_INTENT:
return getVisualEffectFullScreenIntent();
case VISUAL_EFFECT_LIGHTS:
return getVisualEffectLights();
case VISUAL_EFFECT_PEEK:
return getVisualEffectPeek();
case VISUAL_EFFECT_STATUS_BAR:
return getVisualEffectStatusBar();
case VISUAL_EFFECT_BADGE:
return getVisualEffectBadge();
case VISUAL_EFFECT_AMBIENT:
return getVisualEffectAmbient();
case VISUAL_EFFECT_NOTIFICATION_LIST:
return getVisualEffectNotificationList();
}
return -1;
}
/** @hide */
public boolean isCategoryAllowed(@PriorityCategory int category, boolean defaultVal) {
switch (getZenPolicyPriorityCategoryState(category)) {
case ZenPolicy.STATE_ALLOW:
return true;
case ZenPolicy.STATE_DISALLOW:
return false;
default:
return defaultVal;
}
}
/** @hide */
public boolean isVisualEffectAllowed(@VisualEffect int effect, boolean defaultVal) {
switch (getZenPolicyVisualEffectState(effect)) {
case ZenPolicy.STATE_ALLOW:
return true;
case ZenPolicy.STATE_DISALLOW:
return false;
default:
return defaultVal;
}
}
/**
* Applies another policy on top of this policy
* @hide
*/
public void apply(ZenPolicy policyToApply) {
if (policyToApply == null) {
return;
}
// apply priority categories
for (int category = 0; category < mPriorityCategories.size(); category++) {
if (mPriorityCategories.get(category) == STATE_DISALLOW) {
// if a priority category is already disallowed by the policy, cannot allow
continue;
}
@State int newState = policyToApply.mPriorityCategories.get(category);
if (newState != STATE_UNSET) {
mPriorityCategories.set(category, newState);
if (category == PRIORITY_CATEGORY_MESSAGES
&& mPriorityMessages < policyToApply.mPriorityMessages) {
mPriorityMessages = policyToApply.mPriorityMessages;
} else if (category == PRIORITY_CATEGORY_CALLS
&& mPriorityCalls < policyToApply.mPriorityCalls) {
mPriorityCalls = policyToApply.mPriorityCalls;
}
}
}
// apply visual effects
for (int visualEffect = 0; visualEffect < mVisualEffects.size(); visualEffect++) {
if (mVisualEffects.get(visualEffect) == STATE_DISALLOW) {
// if a visual effect is already disallowed by the policy, cannot allow
continue;
}
if (policyToApply.mVisualEffects.get(visualEffect) != STATE_UNSET) {
mVisualEffects.set(visualEffect, policyToApply.mVisualEffects.get(visualEffect));
}
}
}
/**
* @hide
*/
public void writeToProto(ProtoOutputStream proto, long fieldId) {
final long token = proto.start(fieldId);
proto.write(ZenPolicyProto.REMINDERS, getPriorityCategoryReminders());
proto.write(ZenPolicyProto.EVENTS, getPriorityCategoryEvents());
proto.write(ZenPolicyProto.MESSAGES, getPriorityCategoryMessages());
proto.write(ZenPolicyProto.CALLS, getPriorityCategoryCalls());
proto.write(ZenPolicyProto.REPEAT_CALLERS, getPriorityCategoryRepeatCallers());
proto.write(ZenPolicyProto.ALARMS, getPriorityCategoryAlarms());
proto.write(ZenPolicyProto.MEDIA, getPriorityCategoryMedia());
proto.write(ZenPolicyProto.SYSTEM, getPriorityCategorySystem());
proto.write(ZenPolicyProto.FULL_SCREEN_INTENT, getVisualEffectFullScreenIntent());
proto.write(ZenPolicyProto.LIGHTS, getVisualEffectLights());
proto.write(ZenPolicyProto.PEEK, getVisualEffectPeek());
proto.write(ZenPolicyProto.STATUS_BAR, getVisualEffectStatusBar());
proto.write(ZenPolicyProto.BADGE, getVisualEffectBadge());
proto.write(ZenPolicyProto.AMBIENT, getVisualEffectAmbient());
proto.write(ZenPolicyProto.NOTIFICATION_LIST, getVisualEffectNotificationList());
proto.write(ZenPolicyProto.PRIORITY_MESSAGES, getPriorityMessageSenders());
proto.write(ZenPolicyProto.PRIORITY_CALLS, getPriorityCallSenders());
proto.end(token);
}
/**
* Makes deep copy of this ZenPolicy.
* @hide
*/
public @NonNull ZenPolicy copy() {
final Parcel parcel = Parcel.obtain();
try {
writeToParcel(parcel, 0);
parcel.setDataPosition(0);
return CREATOR.createFromParcel(parcel);
} finally {
parcel.recycle();
}
}
}
|