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

Commit dc0556d7 authored by Sarp Misoglu's avatar Sarp Misoglu Committed by Android (Google) Code Review
Browse files

Merge "Add a class for retrieving server flag values"

parents f36182d1 a7de6bf8
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source 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.
 */

package com.android.server.backup;

import android.Manifest;
import android.annotation.RequiresPermission;
import android.provider.DeviceConfig;

/**
 * Retrieves values of feature flags.
 *
 * <p>These flags are intended to be configured server-side and their values should be set in {@link
 * DeviceConfig} by a service that periodically syncs with the server.
 *
 * <p>This class must ensure that the namespace, flag name, and default value passed into {@link
 * DeviceConfig} matches what's declared on the server. The namespace is shared for all backup and
 * restore flags.
 */
public class BackupAndRestoreFeatureFlags {
    private static final String NAMESPACE = "backup_and_restore";

    private BackupAndRestoreFeatureFlags() {}

    /** Retrieves the value of the flag "backup_transport_future_timeout_millis". */
    @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
    public static long getBackupTransportFutureTimeoutMillis() {
        return DeviceConfig.getLong(
                NAMESPACE,
                /* name= */ "backup_transport_future_timeout_millis",
                /* defaultValue= */ 600000); // 10 minutes
    }

    /** Retrieves the value of the flag "backup_transport_callback_timeout_millis". */
    @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
    public static long getBackupTransportCallbackTimeoutMillis() {
        return DeviceConfig.getLong(
                NAMESPACE,
                /* name= */ "backup_transport_callback_timeout_millis",
                /* defaultValue= */ 300000); // 5 minutes
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.util.Slog;

import com.android.internal.backup.IBackupTransport;
import com.android.internal.infra.AndroidFuture;
import com.android.server.backup.BackupAndRestoreFeatureFlags;

import java.util.ArrayDeque;
import java.util.HashSet;
@@ -385,7 +386,8 @@ public class BackupTransportClient {

    private <T> T getFutureResult(AndroidFuture<T> future) {
        try {
            return future.get(600, TimeUnit.SECONDS);
            return future.get(BackupAndRestoreFeatureFlags.getBackupTransportFutureTimeoutMillis(),
                    TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException
                 | CancellationException e) {
            Slog.w(TAG, "Failed to get result from transport:", e);
+3 −3
Original line number Diff line number Diff line
@@ -23,13 +23,13 @@ import android.util.Slog;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.ITransportStatusCallback;
import com.android.server.backup.BackupAndRestoreFeatureFlags;

public class TransportStatusCallback extends ITransportStatusCallback.Stub {
    private static final String TAG = "TransportStatusCallback";
    private static final int TIMEOUT_MILLIS = 300 * 1000; // 5 minutes.
    private static final int OPERATION_STATUS_DEFAULT = 0;

    private final int mOperationTimeout;
    private final long mOperationTimeout;

    @GuardedBy("this")
    private int mOperationStatus = OPERATION_STATUS_DEFAULT;
@@ -37,7 +37,7 @@ public class TransportStatusCallback extends ITransportStatusCallback.Stub {
    private boolean mHasCompletedOperation = false;

    public TransportStatusCallback() {
        mOperationTimeout = TIMEOUT_MILLIS;
        mOperationTimeout = BackupAndRestoreFeatureFlags.getBackupTransportCallbackTimeoutMillis();
    }

    @VisibleForTesting
+1 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ android_test {
        "service-jobscheduler",
        "service-permission.impl",
        "service-sdksandbox.impl",
        "services.backup",
        "services.companion",
        "services.core",
        "services.devicepolicy",
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source 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.
 */

package com.android.server.backup;

import android.platform.test.annotations.Presubmit;
import android.provider.DeviceConfig;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.android.modules.utils.testing.TestableDeviceConfig;

import static com.google.common.truth.Truth.assertThat;


import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@Presubmit
@RunWith(AndroidJUnit4.class)
public class BackupAndRestoreFeatureFlagsTest {
    @Rule
    public TestableDeviceConfig.TestableDeviceConfigRule
            mDeviceConfigRule = new TestableDeviceConfig.TestableDeviceConfigRule();

    @Test
    public void getBackupTransportFutureTimeoutMillis_notSet_returnsDefault() {
        assertThat(
                BackupAndRestoreFeatureFlags.getBackupTransportFutureTimeoutMillis()).isEqualTo(
                600000);
    }

    @Test
    public void getBackupTransportFutureTimeoutMillis_set_returnsSetValue() {
        DeviceConfig.setProperty("backup_and_restore", "backup_transport_future_timeout_millis",
                "1234", false);

        assertThat(
                BackupAndRestoreFeatureFlags.getBackupTransportFutureTimeoutMillis()).isEqualTo(
                1234);
    }

    @Test
    public void getBackupTransportCallbackTimeoutMillis_notSet_returnsDefault() {
        assertThat(
                BackupAndRestoreFeatureFlags.getBackupTransportCallbackTimeoutMillis()).isEqualTo(
                300000);
    }

    @Test
    public void getBackupTransportCallbackTimeoutMillis_set_returnsSetValue() {
        DeviceConfig.setProperty("backup_and_restore", "backup_transport_callback_timeout_millis",
                "5678", false);

        assertThat(
                BackupAndRestoreFeatureFlags.getBackupTransportCallbackTimeoutMillis()).isEqualTo(
                5678);
    }
}