Skip to content

Commit 7d94be9

Browse files
committed
Encapsulate xxxPreferentialNetworkServiceEnabled into gateway.
These methods are only available on Android S, so they need to be accessed using reflection until such SDK is available. Test: gradlew assembleDebug Fixes: 186451999 Change-Id: I03d478b50c304ed8d98baaff633d426bb8ac96b6
1 parent e1df254 commit 7d94be9

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGateway.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,17 @@ void setUsbDataSignalingEnabled(boolean enabled, @NonNull Consumer<Void> onSucce
283283
*/
284284
void setUsbDataSignalingEnabled(boolean enabled);
285285

286+
/**
287+
* See {@link android.app.admin.DevicePolicyManager#setPreferentialNetworkServiceEnabled(ComponentName, boolean)}.
288+
*/
289+
void setPreferentialNetworkServiceEnabled(boolean enabled,
290+
@NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
291+
292+
/**
293+
* See {@link android.app.admin.DevicePolicyManager#isPreferentialNetworkServiceEnabled(ComponentName)}.
294+
*/
295+
boolean isPreferentialNetworkServiceEnabled();
296+
286297
/**
287298
* See {@link android.app.admin.DevicePolicyManager#setPackagesSuspended(ComponentName, String[], boolean)}.
288299
*/

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGatewayImpl.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,17 @@ public boolean setPermittedInputMethods(List<String> packageNames, Consumer<Void
472472

473473
@Override
474474
public void setUsbDataSignalingEnabled(boolean enabled, @NonNull Consumer<Void> onSuccess,
475-
@NonNull Consumer<Exception> onError) {
475+
@NonNull Consumer<Exception> onError) {
476476
Log.d(TAG, "setUsbDataSignalingEnabled(" + enabled + ")");
477+
Util.requireAndroidS();
477478

479+
String method = "setUsbDataSignalingEnabled";
478480
try {
479-
ReflectionUtil.invoke(mDevicePolicyManager, "setUsbDataSignalingEnabled",
480-
new Class[]{Boolean.TYPE}, enabled);
481+
// TODO(b/179160578): use proper method when available on SDK
482+
ReflectionUtil.invoke(mDevicePolicyManager, method, new Class[]{Boolean.TYPE}, enabled);
481483
onSuccess.accept(null);
482484
} catch (Exception e) {
485+
Log.wtf(TAG, "Error calling " + method + "()", e);
483486
onError.accept(e);
484487
}
485488
}
@@ -492,6 +495,37 @@ public void setUsbDataSignalingEnabled(boolean enabled) {
492495
(e) -> onErrorLog(e, message));
493496
}
494497

498+
@Override
499+
public void setPreferentialNetworkServiceEnabled(boolean enabled, Consumer<Void> onSuccess,
500+
Consumer<Exception> onError) {
501+
Log.d(TAG, "setPreferentialNetworkServiceEnabled(" + enabled + ")");
502+
Util.requireAndroidS();
503+
504+
String method = "setPreferentialNetworkServiceEnabled";
505+
try {
506+
// TODO(b/179160578): use proper method when available on SDK
507+
ReflectionUtil.invoke(mDevicePolicyManager, method, new Class[]{Boolean.TYPE}, enabled);
508+
onSuccess.accept(null);
509+
} catch (Exception e) {
510+
Log.wtf(TAG, "Error calling " + method + "()", e);
511+
onError.accept(e);
512+
}
513+
}
514+
515+
@Override
516+
public boolean isPreferentialNetworkServiceEnabled() {
517+
Util.requireAndroidS();
518+
519+
String method = "isPreferentialNetworkServiceEnabled";
520+
try {
521+
// TODO(b/179160578): use proper method when available on SDK
522+
return (Boolean) ReflectionUtil.invoke(mUserManager, method);
523+
} catch (ReflectionIsTemporaryException e) {
524+
Log.wtf(TAG, "Error calling " + method + "()", e);
525+
return false;
526+
}
527+
}
528+
495529
@Override
496530
public void removeActiveAdmin(Consumer<Void> onSuccess, Consumer<Exception> onError) {
497531
Log.d(TAG, "removeActiveAdmin()");

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
613613
mPreferentialNetworkServiceSwitchPreference.setOnPreferenceChangeListener(this);
614614
if (mIsProfileOwner) {
615615
mPreferentialNetworkServiceSwitchPreference.setChecked(
616-
isPreferentialNetworkServiceEnabled());
616+
mDevicePolicyManagerGateway.isPreferentialNetworkServiceEnabled());
617617
}
618618
findPreference(LOCK_SCREEN_POLICY_KEY).setOnPreferenceClickListener(this);
619619
findPreference(PASSWORD_CONSTRAINTS_KEY).setOnPreferenceClickListener(this);
@@ -845,27 +845,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
845845
reloadPersonalAppsSuspendedUi();
846846
}
847847

848-
private boolean isPreferentialNetworkServiceEnabled() {
849-
try {
850-
//TODO: Call directly when the S SDK is available.
851-
return (Boolean) ReflectionUtil.invoke(mDevicePolicyManager,
852-
"isPreferentialNetworkServiceEnabled");
853-
} catch (ReflectionIsTemporaryException e) {
854-
Log.e(TAG, "Error invoking isPreferentialNetworkServiceEnabled", e);
855-
}
856-
return false;
857-
}
858-
859-
private void setPreferentialNetworkServiceEnabled(Boolean isEnabled) {
860-
try {
861-
//TODO: Call directly when the S SDK is available.
862-
ReflectionUtil.invoke(mDevicePolicyManager, "setPreferentialNetworkServiceEnabled",
863-
new Class[]{Boolean.TYPE}, isEnabled);
864-
} catch (ReflectionIsTemporaryException e) {
865-
Log.e(TAG, "Error invoking setPreferentialNetworkServiceEnabled", e);
866-
}
867-
}
868-
869848
@Override
870849
public void dump(String prefix, PrintWriter pw, String[] args) {
871850
// TODO(b/173541467): needs to compile against @SystemAPI SDK to get it
@@ -1590,8 +1569,11 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
15901569
reloadMuteAudioUi();
15911570
return true;
15921571
case SET_GET_PREFERENTIAL_NETWORK_SERVICE_STATUS:
1593-
setPreferentialNetworkServiceEnabled((Boolean) newValue);
1594-
showToast(Boolean.toString(isPreferentialNetworkServiceEnabled()));
1572+
mDevicePolicyManagerGateway.setPreferentialNetworkServiceEnabled((Boolean) newValue,
1573+
(v) -> onSuccessShowToastWithHardcodedMessage(
1574+
"setPreferentialNetworkServiceEnabled(%b)",
1575+
mDevicePolicyManagerGateway.isPreferentialNetworkServiceEnabled()),
1576+
(e) -> onErrorLog("setPreferentialNetworkServiceEnabled", e));
15951577
return true;
15961578
case STAY_ON_WHILE_PLUGGED_IN:
15971579
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
@@ -4416,6 +4398,13 @@ private void onSuccessShowToast(String method, int msgId, Object...args) {
44164398
showToast(msgId, args);
44174399
}
44184400

4401+
/**
4402+
* Used for messages not backed by resources.
4403+
*/
4404+
private void onSuccessShowToastWithHardcodedMessage(String format, Object...args) {
4405+
showToast(String.format(format, args));
4406+
}
4407+
44194408
private void onErrorShowToast(String method, int msgId, Object... args) {
44204409
Log.e(TAG, method + "() failed");
44214410
showToast(msgId, args);

0 commit comments

Comments
 (0)