1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.alarm; 18 19 import static android.app.AlarmManager.FLAG_ALLOW_WHILE_IDLE; 20 import static android.app.AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED; 21 22 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_CLOCK; 23 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_NONWAKEUP_EXACT; 24 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_NONWAKEUP_EXACT_ALLOW_WHILE_IDLE; 25 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_NONWAKEUP_INEXACT; 26 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_NONWAKEUP_INEXACT_ALLOW_WHILE_IDLE; 27 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_WAKEUP_EXACT; 28 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_WAKEUP_EXACT_ALLOW_WHILE_IDLE; 29 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_WAKEUP_INEXACT; 30 import static com.android.server.tare.AlarmManagerEconomicPolicy.ACTION_ALARM_WAKEUP_INEXACT_ALLOW_WHILE_IDLE; 31 32 import android.annotation.NonNull; 33 34 import com.android.server.tare.EconomyManagerInternal; 35 import com.android.server.tare.EconomyManagerInternal.ActionBill; 36 37 import java.util.List; 38 39 /** 40 * Container to maintain alarm TARE {@link ActionBill ActionBills} and their related methods. 41 */ 42 final class TareBill { 43 /** 44 * Bill to use for AlarmClocks. 45 */ 46 static final ActionBill ALARM_CLOCK = new ActionBill(List.of( 47 new EconomyManagerInternal.AnticipatedAction(ACTION_ALARM_CLOCK, 1, 0))); 48 /** 49 * Bills to use for various alarm types. 50 */ 51 static final ActionBill NONWAKEUP_INEXACT_ALARM = new ActionBill(List.of( 52 new EconomyManagerInternal.AnticipatedAction(ACTION_ALARM_NONWAKEUP_INEXACT, 1, 0))); 53 static final ActionBill NONWAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM = new ActionBill(List.of( 54 new EconomyManagerInternal.AnticipatedAction( 55 ACTION_ALARM_NONWAKEUP_INEXACT_ALLOW_WHILE_IDLE, 1, 0))); 56 static final ActionBill NONWAKEUP_EXACT_ALARM = new ActionBill(List.of( 57 new EconomyManagerInternal.AnticipatedAction(ACTION_ALARM_NONWAKEUP_EXACT, 1, 0))); 58 static final ActionBill NONWAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM = new ActionBill(List.of( 59 new EconomyManagerInternal.AnticipatedAction( 60 ACTION_ALARM_NONWAKEUP_EXACT_ALLOW_WHILE_IDLE, 1, 0))); 61 static final ActionBill WAKEUP_INEXACT_ALARM = new ActionBill(List.of( 62 new EconomyManagerInternal.AnticipatedAction(ACTION_ALARM_WAKEUP_INEXACT, 1, 0))); 63 static final ActionBill WAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM = new ActionBill(List.of( 64 new EconomyManagerInternal.AnticipatedAction( 65 ACTION_ALARM_WAKEUP_INEXACT_ALLOW_WHILE_IDLE, 1, 0))); 66 static final ActionBill WAKEUP_EXACT_ALARM = new ActionBill(List.of( 67 new EconomyManagerInternal.AnticipatedAction(ACTION_ALARM_WAKEUP_EXACT, 1, 0))); 68 static final ActionBill WAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM = new ActionBill(List.of( 69 new EconomyManagerInternal.AnticipatedAction( 70 ACTION_ALARM_WAKEUP_EXACT_ALLOW_WHILE_IDLE, 1, 0))); 71 72 @NonNull getAppropriateBill(@onNull Alarm alarm)73 static ActionBill getAppropriateBill(@NonNull Alarm alarm) { 74 if (alarm.alarmClock != null) { 75 return ALARM_CLOCK; 76 } 77 78 final boolean allowWhileIdle = 79 (alarm.flags & (FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED | FLAG_ALLOW_WHILE_IDLE)) != 0; 80 final boolean isExact = alarm.windowLength == 0; 81 82 if (alarm.wakeup) { 83 if (isExact) { 84 if (allowWhileIdle) { 85 return WAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM; 86 } 87 return WAKEUP_EXACT_ALARM; 88 } 89 // Inexact 90 if (allowWhileIdle) { 91 return WAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM; 92 } 93 return WAKEUP_INEXACT_ALARM; 94 } 95 96 // Nonwakeup 97 if (isExact) { 98 if (allowWhileIdle) { 99 return NONWAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM; 100 } 101 return NONWAKEUP_EXACT_ALARM; 102 103 } 104 if (allowWhileIdle) { 105 return NONWAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM; 106 } 107 return NONWAKEUP_INEXACT_ALARM; 108 } 109 110 @NonNull getName(@onNull ActionBill bill)111 static String getName(@NonNull ActionBill bill) { 112 if (bill.equals(ALARM_CLOCK)) { 113 return "ALARM_CLOCK_BILL"; 114 } 115 if (bill.equals(NONWAKEUP_INEXACT_ALARM)) { 116 return "NONWAKEUP_INEXACT_ALARM_BILL"; 117 } 118 if (bill.equals(NONWAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM)) { 119 return "NONWAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM_BILL"; 120 } 121 if (bill.equals(NONWAKEUP_EXACT_ALARM)) { 122 return "NONWAKEUP_EXACT_ALARM_BILL"; 123 } 124 if (bill.equals(NONWAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM)) { 125 return "NONWAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM_BILL"; 126 } 127 if (bill.equals(WAKEUP_INEXACT_ALARM)) { 128 return "WAKEUP_INEXACT_ALARM_BILL"; 129 } 130 if (bill.equals(WAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM)) { 131 return "WAKEUP_INEXACT_ALLOW_WHILE_IDLE_ALARM_BILL"; 132 } 133 if (bill.equals(WAKEUP_EXACT_ALARM)) { 134 return "WAKEUP_EXACT_ALARM_BILL"; 135 } 136 if (bill.equals(WAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM)) { 137 return "WAKEUP_EXACT_ALLOW_WHILE_IDLE_ALARM_BILL"; 138 } 139 return "UNKNOWN_BILL (" + bill.toString() + ")"; 140 } 141 TareBill()142 private TareBill() { 143 } 144 } 145