Home | History | Annotate | Download | only in devicepolicy
      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     /**
     43      * The back-off before re-connecting, when a service binding died, due to the owner
     44      * crashing repeatedly.
     45      */
     46     public final long DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC;
     47 
     48     /**
     49      * The exponential back-off increase factor when a binding dies multiple times.
     50      */
     51     public final double DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE;
     52 
     53     /**
     54      * The max back-off
     55      */
     56     public final long DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC;
     57 
     58     private DevicePolicyConstants(String settings) {
     59 
     60         final KeyValueListParser parser = new KeyValueListParser(',');
     61         try {
     62             parser.setString(settings);
     63         } catch (IllegalArgumentException e) {
     64             // Failed to parse the settings string, log this and move on
     65             // with defaults.
     66             Slog.e(TAG, "Bad device policy settings: " + settings);
     67         }
     68 
     69         long dasDiedServiceReconnectBackoffSec = parser.getLong(
     70                 DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC_KEY, TimeUnit.HOURS.toSeconds(1));
     71 
     72         double dasDiedServiceReconnectBackoffIncrease = parser.getFloat(
     73                 DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE_KEY, 2f);
     74 
     75         long dasDiedServiceReconnectMaxBackoffSec = parser.getLong(
     76                 DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC_KEY, TimeUnit.DAYS.toSeconds(1));
     77 
     78         // Set minimum: 5 seconds.
     79         dasDiedServiceReconnectBackoffSec = Math.max(5, dasDiedServiceReconnectBackoffSec);
     80 
     81         // Set minimum: 1.0.
     82         dasDiedServiceReconnectBackoffIncrease =
     83                 Math.max(1, dasDiedServiceReconnectBackoffIncrease);
     84 
     85         // Make sure max >= default back off.
     86         dasDiedServiceReconnectMaxBackoffSec = Math.max(dasDiedServiceReconnectBackoffSec,
     87                 dasDiedServiceReconnectMaxBackoffSec);
     88 
     89         DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC = dasDiedServiceReconnectBackoffSec;
     90         DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE = dasDiedServiceReconnectBackoffIncrease;
     91         DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC = dasDiedServiceReconnectMaxBackoffSec;
     92 
     93     }
     94 
     95     public static DevicePolicyConstants loadFromString(String settings) {
     96         return new DevicePolicyConstants(settings);
     97     }
     98 
     99     public void dump(String prefix, PrintWriter pw) {
    100         pw.print(prefix);
    101         pw.println("Constants:");
    102 
    103         pw.print(prefix);
    104         pw.print("  DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC: ");
    105         pw.println( DAS_DIED_SERVICE_RECONNECT_BACKOFF_SEC);
    106 
    107         pw.print(prefix);
    108         pw.print("  DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE: ");
    109         pw.println( DAS_DIED_SERVICE_RECONNECT_BACKOFF_INCREASE);
    110 
    111         pw.print(prefix);
    112         pw.print("  DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC: ");
    113         pw.println( DAS_DIED_SERVICE_RECONNECT_MAX_BACKOFF_SEC);
    114     }
    115 }
    116