1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.backup;
18 
19 import android.Manifest;
20 import android.annotation.RequiresPermission;
21 import android.provider.DeviceConfig;
22 
23 /**
24  * Retrieves values of feature flags.
25  *
26  * <p>These flags are intended to be configured server-side and their values should be set in {@link
27  * DeviceConfig} by a service that periodically syncs with the server.
28  *
29  * <p>This class must ensure that the namespace, flag name, and default value passed into {@link
30  * DeviceConfig} matches what's declared on the server. The namespace is shared for all backup and
31  * restore flags.
32  */
33 public class BackupAndRestoreFeatureFlags {
34     private static final String NAMESPACE = "backup_and_restore";
35 
BackupAndRestoreFeatureFlags()36     private BackupAndRestoreFeatureFlags() {}
37 
38     /** Retrieves the value of the flag "backup_transport_future_timeout_millis". */
39     @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
getBackupTransportFutureTimeoutMillis()40     public static long getBackupTransportFutureTimeoutMillis() {
41         return DeviceConfig.getLong(
42                 NAMESPACE,
43                 /* name= */ "backup_transport_future_timeout_millis",
44                 /* defaultValue= */ 600000); // 10 minutes
45     }
46 
47     /** Retrieves the value of the flag "backup_transport_callback_timeout_millis". */
48     @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
getBackupTransportCallbackTimeoutMillis()49     public static long getBackupTransportCallbackTimeoutMillis() {
50         return DeviceConfig.getLong(
51                 NAMESPACE,
52                 /* name= */ "backup_transport_callback_timeout_millis",
53                 /* defaultValue= */ 300000); // 5 minutes
54     }
55 
56     /**
57      * Retrieves the value of the flag "full_backup_write_to_transport_buffer_size_bytes".
58      * The returned value is max size of a chunk of backup data that is sent to the transport.
59      */
60     @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
getFullBackupWriteToTransportBufferSizeBytes()61     public static int getFullBackupWriteToTransportBufferSizeBytes() {
62         return DeviceConfig.getInt(
63                 NAMESPACE,
64                 /* name= */ "full_backup_write_to_transport_buffer_size_bytes",
65                 /* defaultValue= */ 8 * 1024); // 8 KB
66     }
67 
68     /**
69      * Retrieves the value of the flag "full_backup_utils_route_buffer_size_bytes".
70      * The returned value is max size of a chunk of backup data that routed from write end of
71      * pipe from BackupAgent, to read end of pipe to Full Backup Task (PFTBT).
72      */
73     @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
getFullBackupUtilsRouteBufferSizeBytes()74     public static int getFullBackupUtilsRouteBufferSizeBytes() {
75         return DeviceConfig.getInt(
76                 NAMESPACE,
77                 /* name= */ "full_backup_utils_route_buffer_size_bytes",
78                 /* defaultValue= */ 32 * 1024); // 32 KB
79     }
80 
81     /**
82      * Retrieves the value of the flag
83      * "unified_restore_continue_after_transport_failure_in_kv_restore".
84      * If true, Unified restore task will continue to next package if key-value restore of a
85      * package fails due to Transport-level failure. See b/128499560 for more context.
86      */
87     @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
getUnifiedRestoreContinueAfterTransportFailureInKvRestore()88     public static boolean getUnifiedRestoreContinueAfterTransportFailureInKvRestore() {
89         return DeviceConfig.getBoolean(
90                 NAMESPACE,
91                 /* name= */
92                 "unified_restore_continue_after_transport_failure_in_kv_restore",
93                 /* defaultValue= */ true);
94     }
95 }
96