Merge tag 'android-13.0.0_r16' into staging/lineage-20.0_merge-android-13.0.0_r16

Android 13.0.0 Release 16 (TQ1A.221205.011)

# -----BEGIN PGP SIGNATURE-----
#
# iF0EABECAB0WIQRDQNE1cO+UXoOBCWTorT+BmrEOeAUCY45r+QAKCRDorT+BmrEO
# eJ2eAKCPwwS819aZBSX7weztedjTRHiUHACfT26XIHeFojc9EQbZCL2C51uP6Zw=
# =Lsvh
# -----END PGP SIGNATURE-----
# gpg: Signature made Tue Dec  6 00:08:57 2022 EET
# gpg:                using DSA key 4340D13570EF945E83810964E8AD3F819AB10E78
# gpg: Good signature from "The Android Open Source Project <initial-contribution@android.com>" [marginal]
# gpg: initial-contribution@android.com: Verified 1444 signatures in the past
#      13 months.  Encrypted 4 messages in the past 10 months.
# gpg: WARNING: This key is not certified with sufficiently trusted signatures!
# gpg:          It is not certain that the signature belongs to the owner.
# Primary key fingerprint: 4340 D135 70EF 945E 8381  0964 E8AD 3F81 9AB1 0E78

# By Kelvin Zhang
# Via Automerger Merge Worker (14) and others
* tag 'android-13.0.0_r16':
  Cleanup DAP state before installing new OTA
  Fix partition mapping bug

Change-Id: I4a66db975e0505b939c7d75e7797287c5e0bd23b
diff --git a/aosp/dynamic_partition_control_android.cc b/aosp/dynamic_partition_control_android.cc
index 27d1d54..334ccdc 100644
--- a/aosp/dynamic_partition_control_android.cc
+++ b/aosp/dynamic_partition_control_android.cc
@@ -98,7 +98,8 @@
 constexpr std::chrono::milliseconds kMapSnapshotTimeout{10000};
 
 DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
-  Cleanup();
+  UnmapAllPartitions();
+  metadata_device_.reset();
 }
 
 static FeatureFlag GetFeatureFlag(const char* enable_prop,
@@ -312,6 +313,12 @@
 void DynamicPartitionControlAndroid::Cleanup() {
   UnmapAllPartitions();
   metadata_device_.reset();
+  if (GetVirtualAbFeatureFlag().IsEnabled()) {
+    snapshot_ = SnapshotManager::New();
+  } else {
+    snapshot_ = SnapshotManagerStub::New();
+  }
+  CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
 }
 
 bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
diff --git a/aosp/update_attempter_android.cc b/aosp/update_attempter_android.cc
index 7bef9db..b02e600 100644
--- a/aosp/update_attempter_android.cc
+++ b/aosp/update_attempter_android.cc
@@ -265,6 +265,7 @@
   install_plan_.is_resume = !payload_id.empty() &&
                             DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
   if (!install_plan_.is_resume) {
+    boot_control_->GetDynamicPartitionControl()->Cleanup();
     // No need to reset dynamic_partititon_metadata_updated. If previous calls
     // to AllocateSpaceForPayload uses the same payload_id, reuse preallocated
     // space. Otherwise, DeltaPerformer re-allocates space when the payload is