Skip to content

Commit 3eed62f

Browse files
committed
Cleanup more ReflectionUtils usage
BUG: 73451839 Change-Id: Iaed984a3b70f1bcc76b085966a70fb6de8e826f9
1 parent 4367b12 commit 3eed62f

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

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

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import android.widget.ListView;
3939
import android.widget.RadioGroup;
4040
import android.widget.Toast;
41+
4142
import com.afwsamples.testdpc.DeviceAdminReceiver;
4243
import com.afwsamples.testdpc.R;
43-
import com.afwsamples.testdpc.common.ReflectionUtil;
44-
import java.lang.reflect.InvocationTargetException;
44+
4545
import java.time.LocalDate;
4646
import java.time.format.DateTimeFormatter;
4747
import java.util.ArrayList;
@@ -251,7 +251,7 @@ private void promptToSetFreezePeriod(FreezePeriodPickResult callback, final Loca
251251
});
252252
}
253253

254-
@TargetApi(Build.VERSION_CODES.P)
254+
@TargetApi(28)
255255
private boolean setSystemUpdatePolicy() {
256256
SystemUpdatePolicy newPolicy;
257257
switch (mSystemUpdatePolicySelection.getCheckedRadioButtonId()) {
@@ -269,33 +269,21 @@ private boolean setSystemUpdatePolicy() {
269269
default:
270270
newPolicy = null;
271271
}
272-
try {
273-
if (BuildCompat.isAtLeastP() && newPolicy != null && mFreezePeriods.size() != 0) {
274-
List<Pair<Integer, Integer>> periods = new ArrayList<>(mFreezePeriods.size());
275-
for (Period p : mFreezePeriods) {
276-
periods.add(p.toIntegers());
277-
}
278-
ReflectionUtil.invoke(newPolicy, "setFreezePeriods",
279-
new Class[]{List.class}, periods);
272+
if (BuildCompat.isAtLeastP() && newPolicy != null && mFreezePeriods.size() != 0) {
273+
List<Pair<Integer, Integer>> periods = new ArrayList<>(mFreezePeriods.size());
274+
for (Period p : mFreezePeriods) {
275+
periods.add(p.toIntegers());
280276
}
281-
mDpm.setSystemUpdatePolicy(DeviceAdminReceiver.getComponentName(getActivity()),
282-
newPolicy);
283-
Toast.makeText(getContext(), "Policy set successfully", Toast.LENGTH_SHORT).show();
284-
return true;
285-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
286-
Throwable cause = e.getCause();
287-
if (cause instanceof InvocationTargetException) {
288-
if (cause.getCause().getClass().getSimpleName().equals(
289-
"ValidationFailedException")) {
290-
cause = cause.getCause();
291-
}
277+
try {
278+
newPolicy.setFreezePeriods(periods);
279+
mDpm.setSystemUpdatePolicy(DeviceAdminReceiver.getComponentName(getActivity()),
280+
newPolicy);
281+
Toast.makeText(getContext(), "Policy set successfully", Toast.LENGTH_LONG).show();
282+
return true;
283+
} catch (IllegalArgumentException e) {
284+
Toast.makeText(getContext(), "Failed to set system update policy: " + e.getMessage(),
285+
Toast.LENGTH_LONG).show();
292286
}
293-
cause.printStackTrace();
294-
Toast.makeText(getContext(), "Fail to set policy: " + cause.getMessage(),
295-
Toast.LENGTH_LONG).show();
296-
} catch (IllegalArgumentException e) {
297-
Toast.makeText(getContext(), "setSystemUpdatePolicy fails: " + e.getMessage(),
298-
Toast.LENGTH_LONG).show();
299287
}
300288
return false;
301289
}
@@ -309,7 +297,7 @@ private void updateMaintenanceWindowDisplay() {
309297
mSetMaintenanceWindowEnd.setText(formatMinutes(mMaintenanceEnd));
310298
}
311299

312-
@TargetApi(Build.VERSION_CODES.P)
300+
@TargetApi(28)
313301
private void reloadSystemUpdatePolicy() {
314302
SystemUpdatePolicy policy = mDpm.getSystemUpdatePolicy();
315303
String policyDescription = "Unknown";
@@ -344,18 +332,13 @@ private void reloadSystemUpdatePolicy() {
344332
break;
345333
}
346334
if (BuildCompat.isAtLeastP()) {
347-
try {
348-
List<Pair<Integer, Integer>> freezePeriods = (List<Pair<Integer, Integer>>)
349-
ReflectionUtil.invoke(policy, "getFreezePeriods");
350-
mFreezePeriods.clear();
351-
for (Pair<Integer, Integer> period : freezePeriods) {
352-
Period p = new Period(period.first, period.second);
353-
mFreezePeriods.add(p);
354-
}
355-
mFreezePeriodAdapter.notifyDataSetChanged();
356-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
357-
e.printStackTrace();
335+
List<Pair<Integer, Integer>> freezePeriods = policy.getFreezePeriods();
336+
mFreezePeriods.clear();
337+
for (Pair<Integer, Integer> period : freezePeriods) {
338+
Period p = new Period(period.first, period.second);
339+
mFreezePeriods.add(p);
358340
}
341+
mFreezePeriodAdapter.notifyDataSetChanged();
359342
mFreezePeriodPanel.setVisibility(View.VISIBLE);
360343
}
361344
}

0 commit comments

Comments
 (0)