Skip to content

Commit c01c2be

Browse files
author
Rubin Xu
committed
Use revised OTA API
SystemUpdatePolicy API for freeze period was revised according to API reviews, update usages in TestDPC here as well. Bug: 74976911 Test: builds Change-Id: I086baf5a84835a7c179bb421070cdd55ca0a21a7
1 parent dab1264 commit c01c2be

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/systemupdatepolicy/SystemUpdatePolicyFragment.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.app.Fragment;
2222
import android.app.TimePickerDialog;
2323
import android.app.admin.DevicePolicyManager;
24+
import android.app.admin.FreezePeriod;
2425
import android.app.admin.SystemUpdatePolicy;
2526
import android.content.Context;
2627
import android.os.Build;
@@ -43,6 +44,7 @@
4344
import com.afwsamples.testdpc.R;
4445

4546
import java.time.LocalDate;
47+
import java.time.MonthDay;
4648
import java.time.format.DateTimeFormatter;
4749
import java.util.ArrayList;
4850
import java.util.List;
@@ -62,18 +64,20 @@ public class SystemUpdatePolicyFragment extends Fragment implements View.OnClick
6264

6365
@RequiresApi(api = Build.VERSION_CODES.O)
6466
static class Period {
65-
LocalDate mStart;
66-
LocalDate mEnd;
67+
MonthDay mStart;
68+
MonthDay mEnd;
6769

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() {
7271
}
7372

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());
7781
}
7882

7983
@Override
@@ -82,9 +86,17 @@ public String toString() {
8286
return mStart.format(formatter) + " - " + mEnd.format(formatter);
8387
}
8488

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);
88100
}
89101
}
90102

@@ -131,10 +143,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
131143
textView.setOnClickListener(view -> {
132144
final Period period = (Period) view.getTag();
133145
promptToSetFreezePeriod((LocalDate startDate, LocalDate endDate) -> {
134-
period.mStart = startDate;
135-
period.mEnd = endDate;
146+
period.set(startDate, endDate);
136147
mFreezePeriodAdapter.notifyDataSetChanged();
137-
}, period.mStart, period.mEnd);
148+
}, period.getStartDate(), period.getEndDate());
138149
});
139150
View deleteButton = convertView.findViewById(R.id.delete_period);
140151
deleteButton.setTag(mData.get(position));
@@ -210,7 +221,8 @@ public void onClick(View v) {
210221
break;
211222
case R.id.system_update_policy_btn_add_period:
212223
promptToSetFreezePeriod((LocalDate startDate, LocalDate endDate) -> {
213-
Period period = new Period(startDate, endDate);
224+
Period period = new Period();
225+
period.set(startDate, endDate);
214226
mFreezePeriods.add(period);
215227
mFreezePeriodAdapter.notifyDataSetChanged();
216228
}, LocalDate.now(), LocalDate.now());
@@ -270,9 +282,9 @@ private boolean setSystemUpdatePolicy() {
270282
newPolicy = null;
271283
}
272284
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());
274286
for (Period p : mFreezePeriods) {
275-
periods.add(p.toIntegers());
287+
periods.add(p.toFreezePeriod());
276288
}
277289
try {
278290
newPolicy.setFreezePeriods(periods);
@@ -332,10 +344,10 @@ private void reloadSystemUpdatePolicy() {
332344
break;
333345
}
334346
if (BuildCompat.isAtLeastP()) {
335-
List<Pair<Integer, Integer>> freezePeriods = policy.getFreezePeriods();
347+
List<FreezePeriod> freezePeriods = policy.getFreezePeriods();
336348
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());
339351
mFreezePeriods.add(p);
340352
}
341353
mFreezePeriodAdapter.notifyDataSetChanged();

0 commit comments

Comments
 (0)