Merge branch 'android13' of https://github.com/seedvault-app/seedvault into lineage-20.0

* 'android13' of https://github.com/seedvault-app/seedvault:
  Document POST_NOTIFICATIONS permission usage
  Document that MANAGE_DOCUMENTS is optional
  Properly pre-grant notification permission
  Android.bp: whitelist -> allowlist
  README: Add more details about branching (#445)
  Expect BackupObserver to be called with null packages
  Expect ApplicationInfo#splitSourceDirs to be null
  {seedvault|storage}: Add explicit dependencies for androidx.{fragment|activity}

Change-Id: Ia50235b2421d65efaa1a54b9013bf2e82e6c240c
diff --git a/Android.bp b/Android.bp
index 951547c..04c3cc1 100644
--- a/Android.bp
+++ b/Android.bp
@@ -26,6 +26,8 @@
     static_libs: [
         "kotlin-stdlib-jdk8",
         "androidx.core_core-ktx",
+        "androidx.fragment_fragment-ktx",
+        "androidx.activity_activity-ktx",
         "androidx.preference_preference",
         "androidx.documentfile_documentfile",
         "androidx.lifecycle_lifecycle-viewmodel-ktx",
@@ -50,8 +52,9 @@
     privileged: true,
     required: [
         "LocalContactsBackup",
-        "privapp_whitelist_com.stevesoltys.backup",
-        "com.stevesoltys.backup_whitelist"
+        "com.stevesoltys.backup_allowlist",
+        "com.stevesoltys.backup_default-permissions",
+        "com.stevesoltys.backup_privapp_allowlist"
     ],
     optimize: {
         enabled: false,
@@ -59,17 +62,25 @@
 }
 
 prebuilt_etc {
-    name: "privapp_whitelist_com.stevesoltys.backup",
+    name: "com.stevesoltys.backup_allowlist",
     system_ext_specific: true,
-    sub_dir: "permissions",
-    src: "permissions_com.stevesoltys.seedvault.xml",
+    sub_dir: "sysconfig",
+    src: "allowlist_com.stevesoltys.seedvault.xml",
     filename_from_src: true,
 }
 
 prebuilt_etc {
-    name: "com.stevesoltys.backup_whitelist",
+    name: "com.stevesoltys.backup_default-permissions",
     system_ext_specific: true,
-    sub_dir: "sysconfig",
-    src: "whitelist_com.stevesoltys.seedvault.xml",
+    sub_dir: "default-permissions",
+    src: "default-permissions_com.stevesoltys.seedvault.xml",
+    filename_from_src: true,
+}
+
+prebuilt_etc {
+    name: "com.stevesoltys.backup_privapp_allowlist",
+    system_ext_specific: true,
+    sub_dir: "permissions",
+    src: "permissions_com.stevesoltys.seedvault.xml",
     filename_from_src: true,
 }
diff --git a/README.md b/README.md
index 216caa8..1f53e0d 100644
--- a/README.md
+++ b/README.md
@@ -19,10 +19,19 @@
 
 ## Requirements
 
-- Android 13
+SeedVault is developed alongwith AOSP releases
+
+We update it every time Google releases a new Android version, make any changes required for basic functionality, and any improvements possible through API changes in the OS.
+
+This means that for ROMs using SeedVault it's recommended to use the same branch as your android version
+
+- This current branch `android13` is meant for usage with Android 13
+- This is indicated by the version name starting with `13`, and the version code starting with `33` - the Android 13 API version
 
 For older versions of Android, check out [the branches](https://github.com/seedvault-app/seedvault/branches).
 
+Trying to use an older branch on a newer version may lead to issues and is not something we can support.
+
 ## Getting Started
 - Check out [the wiki](https://github.com/seedvault-app/seedvault/wiki) for information on building the application with 
 AOSP.
diff --git a/whitelist_com.stevesoltys.seedvault.xml b/allowlist_com.stevesoltys.seedvault.xml
similarity index 100%
rename from whitelist_com.stevesoltys.seedvault.xml
rename to allowlist_com.stevesoltys.seedvault.xml
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index eadc02d..8e18f3e 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -16,9 +16,11 @@
     <!-- This is needed to check for internet access when backup is stored on network storage -->
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
+    <!-- This is needed to inform users about backup status and errors -->
     <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
 
-    <!-- This is needed to retrieve the available storage roots -->
+    <!-- (Optional) This is needed to retrieve the available storage roots.
+         The user needs to manually select a storage root, if not granted. -->
     <uses-permission
         android:name="android.permission.MANAGE_DOCUMENTS"
         tools:ignore="ProtectedPermissions" />
diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt
index 60c9c9a..02208eb 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt
@@ -154,7 +154,8 @@
         streamGetter: suspend (name: String) -> OutputStream,
     ): List<ApkSplit> {
         check(packageInfo.splitNames != null)
-        val splitSourceDirs = packageInfo.applicationInfo.splitSourceDirs
+        // attention: though not documented, splitSourceDirs can be null
+        val splitSourceDirs = packageInfo.applicationInfo.splitSourceDirs ?: emptyArray()
         check(packageInfo.splitNames.size == splitSourceDirs.size) {
             "Size Mismatch! ${packageInfo.splitNames.size} != ${splitSourceDirs.size} " +
                 "splitNames is ${packageInfo.splitNames.toList()}, " +
diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt
index ac77983..5d791fc 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt
@@ -381,7 +381,13 @@
                 }
                 // hook in here to back up APKs of apps that are otherwise not allowed for backup
                 if (isPmBackup && settingsManager.canDoBackupNow()) {
-                    backUpApksOfNotBackedUpPackages()
+                    try {
+                        backUpApksOfNotBackedUpPackages()
+                    } catch (e: Exception) {
+                        Log.e(TAG, "Error backing up APKs of opt-out apps: ", e)
+                        // We are re-throwing this, because we want to know about problems here
+                        throw e
+                    }
                 }
             }
             result
diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVBackup.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVBackup.kt
index dd89cad..060f543 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVBackup.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVBackup.kt
@@ -209,6 +209,7 @@
             else state.db.close()
             TRANSPORT_OK
         } catch (e: IOException) {
+            Log.e(TAG, "Error uploading DB", e)
             TRANSPORT_ERROR
         } finally {
             this.state = null
diff --git a/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt b/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt
index 6eb6007..d35971f 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt
@@ -43,7 +43,7 @@
      * @param currentBackupPackage The name of the package that now being backed up.
      * @param backupProgress Current progress of backup for the package.
      */
-    override fun onUpdate(currentBackupPackage: String, backupProgress: BackupProgress) {
+    override fun onUpdate(currentBackupPackage: String?, backupProgress: BackupProgress) {
         showProgressNotification(currentBackupPackage)
     }
 
@@ -57,7 +57,7 @@
      *                  that was initialized
      * @param status Zero on success; a nonzero error code if the backup operation failed.
      */
-    override fun onResult(target: String, status: Int) {
+    override fun onResult(target: String?, status: Int) {
         if (isLoggable(TAG, INFO)) {
             Log.i(TAG, "Completed. Target: $target, status: $status")
         }
@@ -81,8 +81,8 @@
         nm.onBackupFinished(success, numBackedUp)
     }
 
-    private fun showProgressNotification(packageName: String) {
-        if (currentPackage == packageName) return
+    private fun showProgressNotification(packageName: String?) {
+        if (packageName == null || currentPackage == packageName) return
 
         if (isLoggable(TAG, INFO)) {
             "Showing progress notification for $currentPackage $numPackages/$expectedPackages".let {
diff --git a/default-permissions_com.stevesoltys.seedvault.xml b/default-permissions_com.stevesoltys.seedvault.xml
new file mode 100644
index 0000000..656e711
--- /dev/null
+++ b/default-permissions_com.stevesoltys.seedvault.xml
@@ -0,0 +1,7 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<exceptions>
+    <exception package="com.stevesoltys.seedvault">
+        <!-- Notifications -->
+        <permission name="android.permission.POST_NOTIFICATIONS" fixed="false"/>
+    </exception>
+</exceptions>
diff --git a/permissions_com.stevesoltys.seedvault.xml b/permissions_com.stevesoltys.seedvault.xml
index 43cc5c5..d7bf61e 100644
--- a/permissions_com.stevesoltys.seedvault.xml
+++ b/permissions_com.stevesoltys.seedvault.xml
@@ -7,6 +7,5 @@
         <permission name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
         <permission name="android.permission.WRITE_SECURE_SETTINGS"/>
         <permission name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
-        <permission name="android.permission.POST_NOTIFICATIONS"/>
     </privapp-permissions>
 </permissions>
diff --git a/storage/lib/Android.bp b/storage/lib/Android.bp
index 3712d5f..4d25e3d 100644
--- a/storage/lib/Android.bp
+++ b/storage/lib/Android.bp
@@ -17,6 +17,8 @@
         "seedvault-lib-tink-android",
         "libprotobuf-java-lite",
         "androidx.core_core-ktx",
+        "androidx.fragment_fragment-ktx",
+        "androidx.activity_activity-ktx",
         "androidx.documentfile_documentfile",
         "androidx.lifecycle_lifecycle-viewmodel-ktx",
         "androidx.room_room-runtime",