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