Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2f81559d authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Return a boolean from the mandatory backup transport setter." into pi-dev

parents c7034f7a 56681f76
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -6559,7 +6559,7 @@ package android.app.admin {
    method public void setLockTaskPackages(android.content.ComponentName, java.lang.String[]) throws java.lang.SecurityException;
    method public void setLogoutEnabled(android.content.ComponentName, boolean);
    method public void setLongSupportMessage(android.content.ComponentName, java.lang.CharSequence);
    method public void setMandatoryBackupTransport(android.content.ComponentName, android.content.ComponentName);
    method public boolean setMandatoryBackupTransport(android.content.ComponentName, android.content.ComponentName);
    method public void setMasterVolumeMuted(android.content.ComponentName, boolean);
    method public void setMaximumFailedPasswordsForWipe(android.content.ComponentName, int);
    method public void setMaximumTimeToLock(android.content.ComponentName, long);
+8 −3
Original line number Diff line number Diff line
@@ -8877,15 +8877,20 @@ public class DevicePolicyManager {
     * <p>If backups were disabled and a non-null backup transport {@link ComponentName} is
     * specified, backups will be enabled.
     *
     * <p>NOTE: The method shouldn't be called on the main thread.
     *
     * @param admin admin Which {@link DeviceAdminReceiver} this request is associated with.
     * @param backupTransportComponent The backup transport layer to be used for mandatory backups.
     * @return {@code true} if the backup transport was successfully set; {@code false} otherwise.
     * @throws SecurityException if {@code admin} is not a device owner.
     */
    public void setMandatoryBackupTransport(
            @NonNull ComponentName admin, @Nullable ComponentName backupTransportComponent) {
    @WorkerThread
    public boolean setMandatoryBackupTransport(
            @NonNull ComponentName admin,
            @Nullable ComponentName backupTransportComponent) {
        throwIfParentInstance("setMandatoryBackupTransport");
        try {
            mService.setMandatoryBackupTransport(admin, backupTransportComponent);
            return mService.setMandatoryBackupTransport(admin, backupTransportComponent);
        } catch (RemoteException re) {
            throw re.rethrowFromSystemServer();
        }
+1 −1
Original line number Diff line number Diff line
@@ -364,7 +364,7 @@ interface IDevicePolicyManager {

    void setBackupServiceEnabled(in ComponentName admin, boolean enabled);
    boolean isBackupServiceEnabled(in ComponentName admin);
    void setMandatoryBackupTransport(in ComponentName admin, in ComponentName backupTransportComponent);
    boolean setMandatoryBackupTransport(in ComponentName admin, in ComponentName backupTransportComponent);
    ComponentName getMandatoryBackupTransport();

    void setNetworkLoggingEnabled(in ComponentName admin, boolean enabled);
+51 −11
Original line number Diff line number Diff line
@@ -107,7 +107,9 @@ import android.app.admin.SecurityLog;
import android.app.admin.SecurityLog.SecurityEvent;
import android.app.admin.SystemUpdateInfo;
import android.app.admin.SystemUpdatePolicy;
import android.app.backup.BackupManager;
import android.app.backup.IBackupManager;
import android.app.backup.ISelectBackupTransportCallback;
import android.app.trust.TrustManager;
import android.app.usage.UsageStatsManagerInternal;
import android.content.BroadcastReceiver;
@@ -252,6 +254,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
@@ -11997,20 +12000,32 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    @Override
    public void setMandatoryBackupTransport(
            ComponentName admin, ComponentName backupTransportComponent) {
    public boolean setMandatoryBackupTransport(
            ComponentName admin,
            ComponentName backupTransportComponent) {
        if (!mHasFeature) {
            return;
            return false;
        }
        Preconditions.checkNotNull(admin);
        synchronized (this) {
            ActiveAdmin activeAdmin =
            getActiveAdminForCallerLocked(admin, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
            if (!Objects.equals(backupTransportComponent, activeAdmin.mandatoryBackupTransport)) {
                activeAdmin.mandatoryBackupTransport = backupTransportComponent;
                saveSettingsLocked(UserHandle.USER_SYSTEM);
        }
        final int callingUid = mInjector.binderGetCallingUid();
        final AtomicBoolean success = new AtomicBoolean(false);
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final ISelectBackupTransportCallback selectBackupTransportCallbackInternal =
                new ISelectBackupTransportCallback.Stub() {
                    public void onSuccess(String transportName) {
                        saveMandatoryBackupTransport(admin, callingUid, backupTransportComponent);
                        success.set(true);
                        countDownLatch.countDown();
                    }
                    public void onFailure(int reason) {
                        countDownLatch.countDown();
                    }
                };
        final long identity = mInjector.binderClearCallingIdentity();
        try {
            IBackupManager ibm = mInjector.getIBackupManager();
@@ -12018,14 +12033,39 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
                if (!ibm.isBackupServiceActive(UserHandle.USER_SYSTEM)) {
                    ibm.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
                }
                ibm.selectBackupTransportAsync(backupTransportComponent, null);
                ibm.selectBackupTransportAsync(
                        backupTransportComponent, selectBackupTransportCallbackInternal);
                countDownLatch.await();
                if (success.get()) {
                    ibm.setBackupEnabled(true);
                }
            } else if (backupTransportComponent == null) {
                saveMandatoryBackupTransport(admin, callingUid, backupTransportComponent);
                success.set(true);
            }
        } catch (RemoteException e) {
            throw new IllegalStateException("Failed to set mandatory backup transport.", e);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Failed to set mandatory backup transport.", e);
        } finally {
            mInjector.binderRestoreCallingIdentity(identity);
        }
        return success.get();
    }
    synchronized private void saveMandatoryBackupTransport(
            ComponentName admin, int callingUid, ComponentName backupTransportComponent) {
        ActiveAdmin activeAdmin =
                getActiveAdminWithPolicyForUidLocked(
                        admin,
                        DeviceAdminInfo.USES_POLICY_DEVICE_OWNER,
                        callingUid);
        if (!Objects.equals(backupTransportComponent,
                activeAdmin.mandatoryBackupTransport)) {
            activeAdmin.mandatoryBackupTransport =
                    backupTransportComponent;
            saveSettingsLocked(UserHandle.USER_SYSTEM);
        }
    }
    @Override
+16 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.app.admin.DevicePolicyManagerInternal;
import android.app.admin.PasswordMetrics;
import android.app.backup.ISelectBackupTransportCallback;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
@@ -2264,6 +2265,21 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        assertEquals(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE,
                intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION));

        // Make the backup transport selection succeed
        doAnswer(new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                ISelectBackupTransportCallback callback =
                    (ISelectBackupTransportCallback) invocation.getArguments()[1];
                if (callback != null) {
                    callback.onSuccess("");
                }
                return null;
            }
        }).when(getServices().ibackupManager).selectBackupTransportAsync(
                any(ComponentName.class), any(ISelectBackupTransportCallback.class));


        // Backups are not mandatory
        intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_MANDATORY_BACKUPS);
        assertNull(intent);