1 /*
2  * Copyright (C) 2022 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 android.adservices.ondevicepersonalization;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.NonNull;
21 
22 import com.android.adservices.ondevicepersonalization.flags.Flags;
23 import com.android.ondevicepersonalization.internal.util.AnnotationValidations;
24 import com.android.ondevicepersonalization.internal.util.DataClass;
25 
26 import java.time.Duration;
27 
28 /** Training interval settings required for federated computation jobs. */
29 @FlaggedApi(Flags.FLAG_ON_DEVICE_PERSONALIZATION_APIS_ENABLED)
30 @DataClass(genBuilder = true, genHiddenConstDefs = true, genEqualsHashCode = true)
31 public final class TrainingInterval {
32     /** The scheduling mode for a one-off task. */
33     public static final int SCHEDULING_MODE_ONE_TIME = 1;
34 
35     /** The scheduling mode for a task that will be rescheduled after each run. */
36     public static final int SCHEDULING_MODE_RECURRENT = 2;
37 
38     /**
39      * The scheduling mode for this task, either {@link #SCHEDULING_MODE_ONE_TIME} or {@link
40      * #SCHEDULING_MODE_RECURRENT}. The default scheduling mode is {@link #SCHEDULING_MODE_ONE_TIME}
41      * if unspecified.
42      */
43     @SchedulingMode private int mSchedulingMode = SCHEDULING_MODE_ONE_TIME;
44 
45     /**
46      * Sets the minimum time interval between two training runs.
47      *
48      * <p>This field will only be used when the scheduling mode is {@link
49      * #SCHEDULING_MODE_RECURRENT}. The value has be greater than zero.
50      *
51      * <p>Please also note this value is advisory, which does not guarantee the job will be run
52      * immediately after the interval expired. Federated compute will still enforce a minimum
53      * required interval and training constraints to ensure system health. The current training
54      * constraints are device on unmetered network, idle and battery not low.
55      */
56     @NonNull private Duration mMinimumInterval = Duration.ZERO;
57 
58     // Code below generated by codegen v1.0.23.
59     //
60     // DO NOT MODIFY!
61     // CHECKSTYLE:OFF Generated code
62     //
63     // To regenerate run:
64     // $ codegen
65     // $ANDROID_BUILD_TOP/packages/modules/OnDevicePersonalization/framework/java/android/adservices/ondevicepersonalization/TrainingInterval.java
66     //
67     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
68     //   Settings > Editor > Code Style > Formatter Control
69     // @formatter:off
70 
71     /** @hide */
72     @android.annotation.IntDef(
73             prefix = "SCHEDULING_MODE_",
74             value = {SCHEDULING_MODE_ONE_TIME, SCHEDULING_MODE_RECURRENT})
75     @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE)
76     @DataClass.Generated.Member
77     public @interface SchedulingMode {}
78 
79     /** @hide */
80     @DataClass.Generated.Member
schedulingModeToString(@chedulingMode int value)81     public static String schedulingModeToString(@SchedulingMode int value) {
82         switch (value) {
83             case SCHEDULING_MODE_ONE_TIME:
84                 return "SCHEDULING_MODE_ONE_TIME";
85             case SCHEDULING_MODE_RECURRENT:
86                 return "SCHEDULING_MODE_RECURRENT";
87             default:
88                 return Integer.toHexString(value);
89         }
90     }
91 
92     @DataClass.Generated.Member
TrainingInterval( @chedulingMode int schedulingMode, @NonNull Duration minimumInterval)93     /* package-private */ TrainingInterval(
94             @SchedulingMode int schedulingMode, @NonNull Duration minimumInterval) {
95         this.mSchedulingMode = schedulingMode;
96 
97         if (!(mSchedulingMode == SCHEDULING_MODE_ONE_TIME)
98                 && !(mSchedulingMode == SCHEDULING_MODE_RECURRENT)) {
99             throw new java.lang.IllegalArgumentException(
100                     "schedulingMode was "
101                             + mSchedulingMode
102                             + " but must be one of: "
103                             + "SCHEDULING_MODE_ONE_TIME("
104                             + SCHEDULING_MODE_ONE_TIME
105                             + "), "
106                             + "SCHEDULING_MODE_RECURRENT("
107                             + SCHEDULING_MODE_RECURRENT
108                             + ")");
109         }
110 
111         this.mMinimumInterval = minimumInterval;
112         AnnotationValidations.validate(NonNull.class, null, mMinimumInterval);
113 
114         // onConstructed(); // You can define this method to get a callback
115     }
116 
117     /**
118      * The scheduling mode for this task, either {@link #SCHEDULING_MODE_ONE_TIME} or {@link
119      * #SCHEDULING_MODE_RECURRENT}. The default scheduling mode is {@link #SCHEDULING_MODE_ONE_TIME}
120      * if unspecified.
121      */
122     @DataClass.Generated.Member
getSchedulingMode()123     public @SchedulingMode int getSchedulingMode() {
124         return mSchedulingMode;
125     }
126 
127     /**
128      * Sets the minimum time interval between two training runs.
129      *
130      * <p>This field will only be used when the scheduling mode is {@link
131      * #SCHEDULING_MODE_RECURRENT}. Only positive values are accepted, zero or negative values will
132      * result in IllegalArgumentException.
133      *
134      * <p>Please also note this value is advisory, which does not guarantee the job will be run
135      * immediately after the interval expired. Federated compute will still enforce a minimum
136      * required interval and training constraints to ensure system health. The current training
137      * constraints are device on unmetered network, idle and battery not low.
138      */
139     @DataClass.Generated.Member
getMinimumInterval()140     public @NonNull Duration getMinimumInterval() {
141         return mMinimumInterval;
142     }
143 
144     @Override
145     @DataClass.Generated.Member
equals(@ndroid.annotation.Nullable Object o)146     public boolean equals(@android.annotation.Nullable Object o) {
147         // You can override field equality logic by defining either of the methods like:
148         // boolean fieldNameEquals(TrainingInterval other) { ... }
149         // boolean fieldNameEquals(FieldType otherValue) { ... }
150 
151         if (this == o) return true;
152         if (o == null || getClass() != o.getClass()) return false;
153         @SuppressWarnings("unchecked")
154         TrainingInterval that = (TrainingInterval) o;
155         //noinspection PointlessBooleanExpression
156         return true
157                 && mSchedulingMode == that.mSchedulingMode
158                 && java.util.Objects.equals(mMinimumInterval, that.mMinimumInterval);
159     }
160 
161     @Override
162     @DataClass.Generated.Member
hashCode()163     public int hashCode() {
164         // You can override field hashCode logic by defining methods like:
165         // int fieldNameHashCode() { ... }
166 
167         int _hash = 1;
168         _hash = 31 * _hash + mSchedulingMode;
169         _hash = 31 * _hash + java.util.Objects.hashCode(mMinimumInterval);
170         return _hash;
171     }
172 
173     /** A builder for {@link TrainingInterval} */
174     @SuppressWarnings("WeakerAccess")
175     @DataClass.Generated.Member
176     public static final class Builder {
177 
178         private @SchedulingMode int mSchedulingMode;
179         private @NonNull Duration mMinimumInterval;
180 
181         private long mBuilderFieldsSet = 0L;
182 
Builder()183         public Builder() {}
184 
185         /**
186          * The scheduling mode for this task, either {@link #SCHEDULING_MODE_ONE_TIME} or {@link
187          * #SCHEDULING_MODE_RECURRENT}. The default scheduling mode is {@link
188          * #SCHEDULING_MODE_ONE_TIME} if unspecified.
189          */
190         @DataClass.Generated.Member
setSchedulingMode(@chedulingMode int value)191         public @NonNull Builder setSchedulingMode(@SchedulingMode int value) {
192             checkNotUsed();
193             mBuilderFieldsSet |= 0x1;
194             mSchedulingMode = value;
195             return this;
196         }
197 
198         /**
199          * Sets the minimum time interval between two training runs.
200          *
201          * <p>This field will only be used when the scheduling mode is {@link
202          * #SCHEDULING_MODE_RECURRENT}. Only positive values are accepted, zero or negative values
203          * will result in IllegalArgumentException.
204          *
205          * <p>Please also note this value is advisory, which does not guarantee the job will be run
206          * immediately after the interval expired. Federated compute will still enforce a minimum
207          * required interval and training constraints to ensure system health. The current training
208          * constraints are device on unmetered network, idle and battery not low.
209          */
210         @DataClass.Generated.Member
setMinimumInterval(@onNull Duration value)211         public @NonNull Builder setMinimumInterval(@NonNull Duration value) {
212             checkNotUsed();
213             mBuilderFieldsSet |= 0x2;
214             mMinimumInterval = value;
215             return this;
216         }
217 
218         /** Builds the instance. This builder should not be touched after calling this! */
build()219         public @NonNull TrainingInterval build() {
220             checkNotUsed();
221             mBuilderFieldsSet |= 0x4; // Mark builder used
222 
223             if ((mBuilderFieldsSet & 0x1) == 0) {
224                 mSchedulingMode = SCHEDULING_MODE_ONE_TIME;
225             }
226             if ((mBuilderFieldsSet & 0x2) == 0) {
227                 mMinimumInterval = Duration.ZERO;
228             }
229             TrainingInterval o = new TrainingInterval(mSchedulingMode, mMinimumInterval);
230             return o;
231         }
232 
checkNotUsed()233         private void checkNotUsed() {
234             if ((mBuilderFieldsSet & 0x4) != 0) {
235                 throw new IllegalStateException(
236                         "This Builder should not be reused. Use a new Builder instance instead");
237             }
238         }
239     }
240 
241     @DataClass.Generated(
242             time = 1697653739724L,
243             codegenVersion = "1.0.23",
244             sourceFile =
245                     "packages/modules/OnDevicePersonalization/framework/java/android/adservices/ondevicepersonalization/TrainingInterval.java",
246             inputSignatures =
247                     "public static final  int SCHEDULING_MODE_ONE_TIME\npublic static final  int SCHEDULING_MODE_RECURRENT\nprivate @android.adservices.ondevicepersonalization.TrainingInterval.SchedulingMode int mSchedulingMode\nprivate @android.annotation.NonNull java.time.Duration mMinimumInterval\nclass TrainingInterval extends java.lang.Object implements []\n@com.android.ondevicepersonalization.internal.util.DataClass(genBuilder=true, genHiddenConstDefs=true, genEqualsHashCode=true)")
248     @Deprecated
__metadata()249     private void __metadata() {}
250 
251     // @formatter:on
252     // End of generated code
253 
254 }
255