Skip to content

Commit e49a0d2

Browse files
author
Alex Kershaw
committed
Add managed system updates to TestDPC.
Bug: 116511569 Test: manual Change-Id: I2888aeb57e2a393649d219ac23a8935a0baf852c
1 parent a2eb738 commit e49a0d2

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2828
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2929
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
30+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
31+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
3032

3133
<application
3234
android:allowBackup="true"

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.app.Fragment;
3434
import android.app.FragmentManager;
3535
import android.app.admin.DevicePolicyManager;
36+
import android.app.admin.DevicePolicyManager.InstallUpdateCallback;
3637
import android.app.admin.SystemUpdateInfo;
3738
import android.content.ComponentName;
3839
import android.content.ContentResolver;
@@ -80,7 +81,6 @@
8081
import android.widget.RadioButton;
8182
import android.widget.RadioGroup;
8283
import android.widget.Toast;
83-
8484
import com.afwsamples.testdpc.AddAccountActivity;
8585
import com.afwsamples.testdpc.BuildConfig;
8686
import com.afwsamples.testdpc.CrossProfileAppsFragment;
@@ -92,6 +92,7 @@
9292
import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment;
9393
import com.afwsamples.testdpc.common.CertificateUtil;
9494
import com.afwsamples.testdpc.common.MediaDisplayFragment;
95+
import com.afwsamples.testdpc.common.PackageInstallationUtils;
9596
import com.afwsamples.testdpc.common.UserArrayAdapter;
9697
import com.afwsamples.testdpc.common.Util;
9798
import com.afwsamples.testdpc.common.preference.CustomConstraint;
@@ -100,7 +101,6 @@
100101
import com.afwsamples.testdpc.common.preference.DpcPreferenceHelper;
101102
import com.afwsamples.testdpc.common.preference.DpcSwitchPreference;
102103
import com.afwsamples.testdpc.comp.BindDeviceAdminFragment;
103-
import com.afwsamples.testdpc.common.PackageInstallationUtils;
104104
import com.afwsamples.testdpc.policy.blockuninstallation.BlockUninstallationInfoArrayAdapter;
105105
import com.afwsamples.testdpc.policy.certificate.DelegatedCertInstallerFragment;
106106
import com.afwsamples.testdpc.policy.keyguard.LockScreenPolicyFragment;
@@ -126,7 +126,6 @@
126126
import com.afwsamples.testdpc.profilepolicy.permission.ManageAppPermissionsFragment;
127127
import com.afwsamples.testdpc.transferownership.PickTransferComponentFragment;
128128
import com.afwsamples.testdpc.util.MainThreadExecutor;
129-
130129
import java.io.ByteArrayInputStream;
131130
import java.io.File;
132131
import java.io.FileNotFoundException;
@@ -371,6 +370,8 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
371370

372371
private static final String MANAGE_OVERRIDE_APN_KEY = "manage_override_apn";
373372

373+
private static final String MANAGED_SYSTEM_UPDATES_KEY = "managed_system_updates";
374+
374375
private static final String SET_PRIVATE_DNS_MODE_KEY = "set_private_dns_mode";
375376

376377
private static final String BATTERY_PLUGGED_ANY = Integer.toString(
@@ -621,6 +622,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
621622
findPreference(SET_TIME_ZONE_KEY).setOnPreferenceClickListener(this);
622623

623624
findPreference(MANAGE_OVERRIDE_APN_KEY).setOnPreferenceClickListener(this);
625+
findPreference(MANAGED_SYSTEM_UPDATES_KEY).setOnPreferenceClickListener(this);
624626

625627
findPreference(CROSS_PROFILE_CALENDAR_KEY).setOnPreferenceClickListener(this);
626628

@@ -1061,13 +1063,46 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
10611063
case MANAGE_OVERRIDE_APN_KEY:
10621064
showFragment(new OverrideApnFragment());
10631065
return true;
1066+
case MANAGED_SYSTEM_UPDATES_KEY:
1067+
promptInstallUpdate();
1068+
return true;
10641069
case CROSS_PROFILE_CALENDAR_KEY:
10651070
showFragment(new CrossProfileCalendarFragment());
10661071
return true;
10671072
}
10681073
return false;
10691074
}
10701075

1076+
@TargetApi(Build.VERSION_CODES.Q)
1077+
private void promptInstallUpdate() {
1078+
new AlertDialog.Builder(getActivity())
1079+
.setMessage(R.string.install_update_prompt)
1080+
.setTitle(R.string.install_update)
1081+
.setPositiveButton(
1082+
R.string.install_update_prompt_yes, (dialogInterface, i) -> installUpdate())
1083+
.setNegativeButton(
1084+
R.string.install_update_prompt_no, (dialogInterface, i) -> {})
1085+
.create()
1086+
.show();
1087+
}
1088+
1089+
@TargetApi(Build.VERSION_CODES.Q)
1090+
private void installUpdate() {
1091+
File file = new File(getContext().getFilesDir(), "ota.zip");
1092+
Uri uri = FileProvider.getUriForFile(
1093+
getActivity(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
1094+
mDevicePolicyManager.installSystemUpdate(
1095+
mAdminComponentName,
1096+
uri,
1097+
new MainThreadExecutor(),
1098+
new InstallUpdateCallback() {
1099+
@Override
1100+
public void onInstallUpdateError(int errorCode, String errorMessage) {
1101+
showToast("Error code: " + errorCode);
1102+
}
1103+
});
1104+
}
1105+
10711106
@RequiresApi(api = Build.VERSION_CODES.M)
10721107
private void testKeyCanBeUsedForSigning() {
10731108
KeyChain.choosePrivateKeyAlias(getActivity(), new KeyChainAliasCallback() {

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,4 +1062,9 @@
10621062
<string name="cross_profile_apps_available">TestDPC is installed in another profile, tap the icon to launch it in that profile.</string>
10631063
<string name="cross_profile_apps_not_available">TestDPC is not installed in another profile, try installing it and come back to here.</string>
10641064

1065+
<string name="install_update">Install update from file</string>
1066+
<string name="install_update_prompt">This will attempt to install the update file ota.zip in the TestDPC files directory, if the file exists (i.e. if you pushed it there with adb). Do you want to continue?</string>
1067+
<string name="install_update_prompt_yes">Yes</string>
1068+
<string name="install_update_prompt_no">No</string>
1069+
10651070
</resources>

app/src/main/res/xml/device_policy_header.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,10 @@
695695
testdpc:admin="any"
696696
testdpc:minSdkVersion="P" />
697697
</PreferenceCategory>
698+
699+
<com.afwsamples.testdpc.common.preference.DpcPreference
700+
android:key="managed_system_updates"
701+
android:title="@string/install_update"
702+
testdpc:admin="deviceOwner"
703+
testdpc:minSdkVersion="Q" />
698704
</PreferenceScreen>

app/src/main/res/xml/filepaths.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717

1818
<paths>
1919
<files-path path="media/" name="media" />
20+
<files-path path="/" name="allfiles" />
2021
</paths>

0 commit comments

Comments
 (0)