Allow the user to change the BackupTransport

Set the following config overlays to activate this feature:

* config_backup_settings_intent to settings://com.android.settings.backup.transport
* config_backup_settings_label to some user-facing label
  e.g. Change backup provider

Squashed with:

Author: Michael Bestas <mkbestas@lineageos.org>
Date:   Wed Nov 18 16:08:44 2020 +0200

    Backup: Don't hardcode ignored backup transports

    * Allow extending the ignored backup transport list
      by moving it to an overlay
    * Set config_ignored_backup_transports to override

    Change-Id: If4ccad0acac708cac272f791392f9ac827a7491f

Change-Id: I080d96e2c34045a0e61f3fa1b839f463550f2028
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index a8e0390..1a24714 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -4487,6 +4487,18 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".backup.transport.TransportActivity"
+                  android:label="@string/backup_transport_title"
+                  android:icon="@drawable/ic_settings_backup"
+                  android:exported="false">
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:scheme="settings"
+                      android:host="com.android.settings.backup.transport" />
+            </intent-filter>
+        </activity>
+
         <!-- This is the longest AndroidManifest.xml ever. -->
     </application>
 </manifest>
diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml
new file mode 100644
index 0000000..a7c465a
--- /dev/null
+++ b/res/values/cm_strings.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2017-2021 The LineageOS Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Backup Transport selection settings menu and activity title -->
+    <string name="backup_transport_setting_label">Change backup provider</string>
+    <string name="backup_transport_title">Select backup provider</string>
+</resources>
diff --git a/res/values/lineage_config.xml b/res/values/lineage_config.xml
new file mode 100644
index 0000000..083a3d6
--- /dev/null
+++ b/res/values/lineage_config.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2020-2021 The LineageOS Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <string-array name="config_ignored_backup_transports" translatable="false">
+        <item>com.android.localtransport/.LocalTransport</item>
+    </string-array>
+</resources>
diff --git a/res/xml/backup_transport_settings.xml b/res/xml/backup_transport_settings.xml
new file mode 100644
index 0000000..cc8cc9a
--- /dev/null
+++ b/res/xml/backup_transport_settings.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 The Calyx Institute
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res-auto"
+    android:title="@string/backup_transport_title">
+
+    <!-- content gets filled programmatically -->
+
+</PreferenceScreen>
diff --git a/src/com/android/settings/backup/BackupSettingsFragment.java b/src/com/android/settings/backup/BackupSettingsFragment.java
index 7df19f5..7fcbd63 100644
--- a/src/com/android/settings/backup/BackupSettingsFragment.java
+++ b/src/com/android/settings/backup/BackupSettingsFragment.java
@@ -42,6 +42,13 @@
         super.onCreate(savedInstanceState);
     }
 
+    @Override
+    public void onStart() {
+        super.onStart();
+        // update information when we navigate back from TransportActivity
+        displayResourceTilesToScreen(getPreferenceScreen());
+    }
+
     /**
      * Get the tag string for logging.
      */
diff --git a/src/com/android/settings/backup/BackupSettingsPreferenceController.java b/src/com/android/settings/backup/BackupSettingsPreferenceController.java
index 4e0e3b4..3208ae4 100644
--- a/src/com/android/settings/backup/BackupSettingsPreferenceController.java
+++ b/src/com/android/settings/backup/BackupSettingsPreferenceController.java
@@ -30,24 +30,24 @@
         implements PreferenceControllerMixin {
     private static final String BACKUP_SETTINGS = "backup_settings";
     private static final  String MANUFACTURER_SETTINGS = "manufacturer_backup";
-    private Intent mBackupSettingsIntent;
-    private CharSequence mBackupSettingsTitle;
-    private String mBackupSettingsSummary;
+    private final BackupSettingsHelper settingsHelper;
     private Intent mManufacturerIntent;
     private String mManufacturerLabel;
 
     public BackupSettingsPreferenceController(Context context) {
         super(context);
-        BackupSettingsHelper settingsHelper = new BackupSettingsHelper(context);
-        mBackupSettingsIntent = settingsHelper.getIntentForBackupSettings();
-        mBackupSettingsTitle = settingsHelper.getLabelForBackupSettings();
-        mBackupSettingsSummary = settingsHelper.getSummaryForBackupSettings();
+        settingsHelper = new BackupSettingsHelper(context);
         mManufacturerIntent = settingsHelper.getIntentProvidedByManufacturer();
         mManufacturerLabel = settingsHelper.getLabelProvidedByManufacturer();
     }
 
     @Override
     public void displayPreference(PreferenceScreen screen) {
+        // we don't get these in the constructor, so we can get updates for them later
+        Intent mBackupSettingsIntent = settingsHelper.getIntentForBackupSettings();
+        CharSequence mBackupSettingsTitle = settingsHelper.getLabelForBackupSettings();
+        String mBackupSettingsSummary = settingsHelper.getSummaryForBackupSettings();
+
         Preference backupSettings = screen.findPreference(BACKUP_SETTINGS);
         Preference manufacturerSettings = screen.findPreference(MANUFACTURER_SETTINGS);
         backupSettings.setIntent(mBackupSettingsIntent);
diff --git a/src/com/android/settings/backup/transport/Transport.java b/src/com/android/settings/backup/transport/Transport.java
new file mode 100644
index 0000000..d2fd6e0
--- /dev/null
+++ b/src/com/android/settings/backup/transport/Transport.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2020 The Calyx Institute
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.settings.backup.transport;
+
+class Transport {
+    final String name;
+    final CharSequence dataManagementLabel;
+    final CharSequence destinationString;
+
+    Transport(String name, CharSequence dataManagementLabel, CharSequence destinationString) {
+        this.name = name;
+        this.dataManagementLabel = dataManagementLabel;
+        this.destinationString = destinationString;
+    }
+}
diff --git a/src/com/android/settings/backup/transport/TransportActivity.java b/src/com/android/settings/backup/transport/TransportActivity.java
new file mode 100644
index 0000000..1adcb90
--- /dev/null
+++ b/src/com/android/settings/backup/transport/TransportActivity.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2020 The Calyx Institute
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.settings.backup.transport;
+
+import android.os.Bundle;
+import androidx.fragment.app.FragmentActivity;
+
+/**
+ * Activity to allow the user to choose the {@link android.app.backup.BackupTransport}.
+ *
+ * Set {@code config_backup_settings_intent} to {@code settings://com.android.settings.backup.transport} to activate.
+ * Don't forget to also set {@code config_backup_settings_label} or else it won't be shown.
+ */
+public class TransportActivity extends FragmentActivity {
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        getSupportFragmentManager().beginTransaction()
+            .replace(android.R.id.content, new TransportFragment())
+            .commit();
+    }
+
+}
diff --git a/src/com/android/settings/backup/transport/TransportFragment.java b/src/com/android/settings/backup/transport/TransportFragment.java
new file mode 100644
index 0000000..6c6a8ce
--- /dev/null
+++ b/src/com/android/settings/backup/transport/TransportFragment.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2020 The Calyx Institute
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.settings.backup.transport;
+
+import android.app.settings.SettingsEnums;
+import android.content.Context;
+import com.android.settings.R;
+import com.android.settings.backup.transport.TransportPreferenceController.OnTransportChangedListener;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TransportFragment extends DashboardFragment implements OnTransportChangedListener {
+
+    private static final String TAG = "TransportFragment";
+
+    /**
+     * Get the tag string for logging.
+     */
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    /**
+     * Get the res id for static preference xml for this fragment.
+     */
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.backup_transport_settings;
+    }
+
+    /**
+     * Get a list of {@link AbstractPreferenceController} for this fragment.
+     */
+    @Override
+    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
+        final List<AbstractPreferenceController> controllers = new ArrayList<>();
+        controllers.add(new TransportPreferenceController(context, this));
+        return controllers;
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return SettingsEnums.BACKUP_SETTINGS;
+    }
+
+    @Override
+    public void onTransportChanged(String transportName) {
+        requireActivity().finish();
+    }
+
+}
diff --git a/src/com/android/settings/backup/transport/TransportHelper.java b/src/com/android/settings/backup/transport/TransportHelper.java
new file mode 100644
index 0000000..1ab5a59
--- /dev/null
+++ b/src/com/android/settings/backup/transport/TransportHelper.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2020 The Calyx Institute
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.settings.backup.transport;
+
+import android.app.backup.IBackupManager;
+import android.content.Context;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.UserHandle;
+import android.util.Log;
+import androidx.annotation.Nullable;
+
+import com.android.settings.R;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Helper class for {@link TransportActivity} that interacts with {@link IBackupManager}.
+ */
+class TransportHelper {
+    private static final String TAG = "TransportHelper";
+
+    private final IBackupManager mBackupManager = IBackupManager.Stub.asInterface(
+        ServiceManager.getService(Context.BACKUP_SERVICE));
+
+    private Context mContext;
+
+    TransportHelper(Context context) {
+        mContext = context;
+    }
+
+    List<Transport> getTransports() {
+        String[] backupTransports = getBackupTransports();
+        if (backupTransports == null) return Collections.emptyList();
+        ArrayList<Transport> transports = new ArrayList<>(backupTransports.length);
+        String[] ignoredTransports = mContext.getResources().getStringArray(
+                R.array.config_ignored_backup_transports);
+        for (String name : getBackupTransports()) {
+            boolean ignored = false;
+            for (String ignoredTransport : ignoredTransports) {
+                if (name.equals(ignoredTransport)) ignored = true;
+            }
+            if (ignored) continue;
+            CharSequence label = getLabelFromBackupTransport(name);
+            if (label == null || label.length() == 0) label = name;
+            Transport transport = new Transport(name, label, getSummaryFromBackupTransport(name));
+            transports.add(transport);
+        }
+        return transports;
+    }
+
+    void selectTransport(String name) {
+        try {
+            mBackupManager.selectBackupTransport(name);
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error selecting transport: " + name, e);
+        }
+    }
+
+    @Nullable
+    private String[] getBackupTransports() {
+        try {
+            String[] transports = mBackupManager.listAllTransports();
+            if (Log.isLoggable(TAG, Log.DEBUG)) {
+                Log.d(TAG, "Received all backup transports: " + Arrays.toString(transports));
+            }
+            return transports;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error getting all backup transports", e);
+        }
+        return null;
+    }
+
+    private CharSequence getLabelFromBackupTransport(String transport) {
+        try {
+            CharSequence label = mBackupManager.getDataManagementLabelForUser(UserHandle.myUserId(), transport);
+            if (Log.isLoggable(TAG, Log.DEBUG)) {
+                Log.d(TAG, "Received the backup settings label from " + transport + ": " + label);
+            }
+            return label;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error getting data management label for " + transport, e);
+        }
+        return null;
+    }
+
+    private String getSummaryFromBackupTransport(String transport) {
+        try {
+            String summary = mBackupManager.getDestinationString(transport);
+            if (Log.isLoggable(TAG, Log.DEBUG)) {
+                Log.d(TAG, "Received the backup settings summary from " + transport + ": " + summary);
+            }
+            return summary;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error getting data management summary", e);
+        }
+        return null;
+    }
+}
diff --git a/src/com/android/settings/backup/transport/TransportPreferenceController.java b/src/com/android/settings/backup/transport/TransportPreferenceController.java
new file mode 100644
index 0000000..1dc5a51
--- /dev/null
+++ b/src/com/android/settings/backup/transport/TransportPreferenceController.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2020 The Calyx Institute
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.settings.backup.transport;
+
+import android.content.Context;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+public class TransportPreferenceController extends AbstractPreferenceController {
+
+    interface OnTransportChangedListener {
+        void onTransportChanged(String transportName);
+    }
+
+    private final OnTransportChangedListener listener;
+    private final TransportHelper transportHelper;
+
+    public TransportPreferenceController(Context context, OnTransportChangedListener listener) {
+        super(context);
+        this.listener = listener;
+        transportHelper = new TransportHelper(context);
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        for (Transport transport : transportHelper.getTransports()) {
+            screen.addPreference(getPreferenceForTransport(transport));
+        }
+    }
+
+    private Preference getPreferenceForTransport(Transport transport) {
+        Preference p = new Preference(mContext);
+        p.setTitle(transport.dataManagementLabel);
+        p.setSummary(transport.destinationString);
+        p.setIconSpaceReserved(false);
+        p.setOnPreferenceClickListener(preference -> {
+            transportHelper.selectTransport(transport.name);
+            listener.onTransportChanged(transport.name);
+            return true;
+        });
+        return p;
+    }
+
+    /**
+     * Returns true if preference is available (should be displayed)
+     */
+    @Override
+    public boolean isAvailable() {
+        return true;
+    }
+
+    /**
+     * Returns the key for this preference.
+     */
+    @Override
+    public String getPreferenceKey() {
+        return null;
+    }
+}