1 /*
2  * Copyright (C) 2017 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 package com.android.server.devicepolicy;
17 
18 import android.util.IndentingPrintWriter;
19 import android.util.KeyValueListParser;
20 
21 import com.android.server.utils.Slogf;
22 
23 import java.util.concurrent.TimeUnit;
24 
25 /**
26  * Constants that are configurable via the global settings for {@link DevicePolicyManagerService}.
27  *
28  * Example of setting the values for testing.
29  * adb shell settings put global device_policy_constants das_died_service_reconnect_backoff_sec=10,das_died_service_reconnect_backoff_increase=1.5,das_died_service_reconnect_max_backoff_sec=30
30  */
31 public class DevicePolicyConstants {
32     private static final String TAG = DevicePolicyManagerService.LOG_TAG;
33 
34     private static final String DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC_KEY =
35             "das_died_service_reconnect_backoff_sec";
36 
37     private static final String DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE_KEY =
38             "das_died_service_reconnect_backoff_increase";
39 
40     private static final String DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC_KEY =
41             "das_died_service_reconnect_max_backoff_sec";
42 
43     private static final String DAS_DIED_SERVICE_STABLE_CONNECTION_THRESHOLD_SEC_KEY =
44             "das_died_service_stable_connection_threshold_sec";
45 
46     private static final String BATTERY_THRESHOLD_NOT_CHARGING_KEY =
47             "battery_threshold_not_charging";
48 
49     private static final String BATTERY_THRESHOLD_CHARGING_KEY =
50             "battery_threshold_charging";
51 
52     // TODO(b/182994391): Replace with more generic solution to override the supervision
53     // component.
54     private static final String USE_TEST_ADMIN_AS_SUPERVISION_COMPONENT_KEY =
55             "use_test_admin_as_supervision_component";
56 
57     /**
58      * The back-off before re-connecting, when a service binding died, due to the owner
59      * crashing repeatedly.
60      */
61     public final long DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC;
62 
63     /**
64      * The exponential back-off increase factor when a binding dies multiple times.
65      */
66     public final double DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE;
67 
68     /**
69      * The max back-off
70      */
71     public final long DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC;
72 
73     /**
74      * If a connection lasts more than this duration, we reset the re-connect back-off time.
75      */
76     public final long DAS_DIED_SERVICE_STABLE_CONNECTION_THRESHOLD_SEC;
77 
78     /**
79      * Battery threshold for installing system update while the device is not charging.
80      */
81     public final int BATTERY_THRESHOLD_NOT_CHARGING;
82 
83     /**
84      * Battery threshold for installing system update while the device is charging.
85      */
86     public final int BATTERY_THRESHOLD_CHARGING;
87 
88     /**
89      * Whether to default to considering the current DO/PO as the supervision component
90      * if they are a testOnly admin.
91      */
92     public final boolean USE_TEST_ADMIN_AS_SUPERVISION_COMPONENT;
93 
94 
DevicePolicyConstants(String settings)95     private DevicePolicyConstants(String settings) {
96 
97         final KeyValueListParser parser = new KeyValueListParser(',');
98         try {
99             parser.setString(settings);
100         } catch (IllegalArgumentException e) {
101             // Failed to parse the settings string, log this and move on
102             // with defaults.
103             Slogf.e(TAG, "Bad device policy settings: %s", settings);
104         }
105 
106         long dasDiedServiceReconnectBackoffSec = parser.getLong(
107                 DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC_KEY, TimeUnit.HOURS.toSeconds(1));
108 
109         double dasDiedServiceReconnectBackoffIncrease = parser.getFloat(
110                 DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE_KEY, 2f);
111 
112         long dasDiedServiceReconnectMaxBackoffSec = parser.getLong(
113                 DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC_KEY, TimeUnit.DAYS.toSeconds(1));
114 
115         long dasDiedServiceStableConnectionThresholdSec = parser.getLong(
116                 DAS_DIED_SERVICE_STABLE_CONNECTION_THRESHOLD_SEC_KEY,
117                 TimeUnit.MINUTES.toSeconds(2));
118 
119         int batteryThresholdNotCharging = parser.getInt(
120                 BATTERY_THRESHOLD_NOT_CHARGING_KEY, 40);
121 
122         int batteryThresholdCharging = parser.getInt(
123                 BATTERY_THRESHOLD_CHARGING_KEY, 20);
124 
125         boolean useTestAdminAsSupervisionComponent = parser.getBoolean(
126                 USE_TEST_ADMIN_AS_SUPERVISION_COMPONENT_KEY, false);
127 
128         // Set minimum: 5 seconds.
129         dasDiedServiceReconnectBackoffSec = Math.max(5, dasDiedServiceReconnectBackoffSec);
130 
131         // Set minimum: 1.0.
132         dasDiedServiceReconnectBackoffIncrease =
133                 Math.max(1, dasDiedServiceReconnectBackoffIncrease);
134 
135         // Make sure max >= default back off.
136         dasDiedServiceReconnectMaxBackoffSec = Math.max(dasDiedServiceReconnectBackoffSec,
137                 dasDiedServiceReconnectMaxBackoffSec);
138 
139         DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC = dasDiedServiceReconnectBackoffSec;
140         DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE = dasDiedServiceReconnectBackoffIncrease;
141         DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC = dasDiedServiceReconnectMaxBackoffSec;
142         DAS_DIED_SERVICE_STABLE_CONNECTION_THRESHOLD_SEC =
143                 dasDiedServiceStableConnectionThresholdSec;
144         BATTERY_THRESHOLD_NOT_CHARGING = batteryThresholdNotCharging;
145         BATTERY_THRESHOLD_CHARGING = batteryThresholdCharging;
146         USE_TEST_ADMIN_AS_SUPERVISION_COMPONENT = useTestAdminAsSupervisionComponent;
147     }
148 
loadFromString(String settings)149     public static DevicePolicyConstants loadFromString(String settings) {
150         return new DevicePolicyConstants(settings);
151     }
152 
153     /** Dump constants */
dump(IndentingPrintWriter pw)154     public void dump(IndentingPrintWriter pw) {
155         pw.println("Constants:");
156 
157         pw.increaseIndent();
158         pw.print("DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC: ");
159         pw.println(DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC);
160 
161         pw.print("DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE: ");
162         pw.println(DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE);
163 
164         pw.print("DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC: ");
165         pw.println(DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC);
166 
167         pw.print("DAS_DIED_SERVICE_STABLE_CONNECTION_THRESHOLD_SEC: ");
168         pw.println(DAS_DIED_SERVICE_STABLE_CONNECTION_THRESHOLD_SEC);
169         pw.decreaseIndent();
170     }
171 }
172