21
21
import android .app .Fragment ;
22
22
import android .app .TimePickerDialog ;
23
23
import android .app .admin .DevicePolicyManager ;
24
+ import android .app .admin .FreezePeriod ;
24
25
import android .app .admin .SystemUpdatePolicy ;
25
26
import android .content .Context ;
26
27
import android .os .Build ;
43
44
import com .afwsamples .testdpc .R ;
44
45
45
46
import java .time .LocalDate ;
47
+ import java .time .MonthDay ;
46
48
import java .time .format .DateTimeFormatter ;
47
49
import java .util .ArrayList ;
48
50
import java .util .List ;
@@ -62,18 +64,20 @@ public class SystemUpdatePolicyFragment extends Fragment implements View.OnClick
62
64
63
65
@ RequiresApi (api = Build .VERSION_CODES .O )
64
66
static class Period {
65
- LocalDate mStart ;
66
- LocalDate mEnd ;
67
+ MonthDay mStart ;
68
+ MonthDay mEnd ;
67
69
68
- public Period (Integer start , Integer end ) {
69
- int currentYear = LocalDate .now ().getYear ();
70
- mStart = LocalDate .ofYearDay (2001 , start ).withYear (currentYear );
71
- mEnd = LocalDate .ofYearDay (2001 , end ).withYear (end >= start ? currentYear : currentYear + 1 );
70
+ public Period () {
72
71
}
73
72
74
- public Period (LocalDate startDate , LocalDate endDate ) {
75
- mStart = startDate ;
76
- mEnd = endDate ;
73
+ public Period (MonthDay start , MonthDay end ) {
74
+ mStart = start ;
75
+ mEnd = end ;
76
+ }
77
+
78
+ public void set (LocalDate startDate , LocalDate endDate ) {
79
+ mStart = MonthDay .of (startDate .getMonth (), startDate .getDayOfMonth ());
80
+ mEnd = MonthDay .of (endDate .getMonth (), endDate .getDayOfMonth ());
77
81
}
78
82
79
83
@ Override
@@ -82,9 +86,17 @@ public String toString() {
82
86
return mStart .format (formatter ) + " - " + mEnd .format (formatter );
83
87
}
84
88
85
- public Pair <Integer , Integer > toIntegers () {
86
- return new Pair <>(mStart .withYear (2001 ).getDayOfYear (),
87
- mEnd .withYear (2001 ).getDayOfYear ());
89
+ public LocalDate getStartDate () {
90
+ return mStart .atYear (LocalDate .now ().getYear ());
91
+ }
92
+
93
+ public LocalDate getEndDate () {
94
+ return mEnd .atYear (LocalDate .now ().getYear ());
95
+ }
96
+
97
+ @ TargetApi (28 )
98
+ public FreezePeriod toFreezePeriod () {
99
+ return new FreezePeriod (mStart , mEnd );
88
100
}
89
101
}
90
102
@@ -131,10 +143,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
131
143
textView .setOnClickListener (view -> {
132
144
final Period period = (Period ) view .getTag ();
133
145
promptToSetFreezePeriod ((LocalDate startDate , LocalDate endDate ) -> {
134
- period .mStart = startDate ;
135
- period .mEnd = endDate ;
146
+ period .set (startDate , endDate );
136
147
mFreezePeriodAdapter .notifyDataSetChanged ();
137
- }, period .mStart , period .mEnd );
148
+ }, period .getStartDate () , period .getEndDate () );
138
149
});
139
150
View deleteButton = convertView .findViewById (R .id .delete_period );
140
151
deleteButton .setTag (mData .get (position ));
@@ -210,7 +221,8 @@ public void onClick(View v) {
210
221
break ;
211
222
case R .id .system_update_policy_btn_add_period :
212
223
promptToSetFreezePeriod ((LocalDate startDate , LocalDate endDate ) -> {
213
- Period period = new Period (startDate , endDate );
224
+ Period period = new Period ();
225
+ period .set (startDate , endDate );
214
226
mFreezePeriods .add (period );
215
227
mFreezePeriodAdapter .notifyDataSetChanged ();
216
228
}, LocalDate .now (), LocalDate .now ());
@@ -270,9 +282,9 @@ private boolean setSystemUpdatePolicy() {
270
282
newPolicy = null ;
271
283
}
272
284
if (BuildCompat .isAtLeastP () && newPolicy != null && mFreezePeriods .size () != 0 ) {
273
- List <Pair < Integer , Integer > > periods = new ArrayList <>(mFreezePeriods .size ());
285
+ List <FreezePeriod > periods = new ArrayList <>(mFreezePeriods .size ());
274
286
for (Period p : mFreezePeriods ) {
275
- periods .add (p .toIntegers ());
287
+ periods .add (p .toFreezePeriod ());
276
288
}
277
289
try {
278
290
newPolicy .setFreezePeriods (periods );
@@ -332,10 +344,10 @@ private void reloadSystemUpdatePolicy() {
332
344
break ;
333
345
}
334
346
if (BuildCompat .isAtLeastP ()) {
335
- List <Pair < Integer , Integer > > freezePeriods = policy .getFreezePeriods ();
347
+ List <FreezePeriod > freezePeriods = policy .getFreezePeriods ();
336
348
mFreezePeriods .clear ();
337
- for (Pair < Integer , Integer > period : freezePeriods ) {
338
- Period p = new Period (period .first , period .second );
349
+ for (FreezePeriod period : freezePeriods ) {
350
+ Period p = new Period (period .getStart () , period .getEnd () );
339
351
mFreezePeriods .add (p );
340
352
}
341
353
mFreezePeriodAdapter .notifyDataSetChanged ();
0 commit comments