Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.appbinding; |
| 17 | |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 18 | import android.content.Context; |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 19 | import android.util.KeyValueListParser; |
| 20 | import android.util.Slog; |
| 21 | |
| 22 | import java.io.PrintWriter; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | /** |
| 26 | * Constants that are configurable via the global settings for {@link AppBindingService}. |
| 27 | */ |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 28 | public class AppBindingConstants { |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 29 | private static final String TAG = AppBindingService.TAG; |
| 30 | |
| 31 | private static final String SERVICE_RECONNECT_BACKOFF_SEC_KEY = |
| 32 | "service_reconnect_backoff_sec"; |
| 33 | |
| 34 | private static final String SERVICE_RECONNECT_BACKOFF_INCREASE_KEY = |
| 35 | "service_reconnect_backoff_increase"; |
| 36 | |
| 37 | private static final String SERVICE_RECONNECT_MAX_BACKOFF_SEC_KEY = |
| 38 | "service_reconnect_max_backoff_sec"; |
| 39 | |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 40 | private static final String SERVICE_STABLE_CONNECTION_THRESHOLD_SEC_KEY = |
| 41 | "service_stable_connection_threshold_sec"; |
| 42 | |
Makoto Onuki | 7a2d35e | 2018-10-03 11:15:52 -0700 | [diff] [blame] | 43 | private static final String SMS_SERVICE_ENABLED_KEY = |
| 44 | "sms_service_enabled"; |
| 45 | |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 46 | private static final String SMS_APP_BIND_FLAGS_KEY = |
| 47 | "sms_app_bind_flags"; |
| 48 | |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 49 | public final String sourceSettings; |
| 50 | |
| 51 | /** |
| 52 | * The back-off before re-connecting, when a service binding died, due to the app |
| 53 | * crashing repeatedly. |
| 54 | */ |
| 55 | public final long SERVICE_RECONNECT_BACKOFF_SEC; |
| 56 | |
| 57 | /** |
| 58 | * The exponential back-off increase factor when a binding dies multiple times. |
| 59 | */ |
| 60 | public final double SERVICE_RECONNECT_BACKOFF_INCREASE; |
| 61 | |
| 62 | /** |
| 63 | * The max back-off |
| 64 | */ |
| 65 | public final long SERVICE_RECONNECT_MAX_BACKOFF_SEC; |
| 66 | |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 67 | /** |
| 68 | * If a connection lasts more than this duration, we reset the re-connect back-off time. |
| 69 | */ |
| 70 | public final long SERVICE_STABLE_CONNECTION_THRESHOLD_SEC; |
| 71 | |
| 72 | /** |
Makoto Onuki | 7a2d35e | 2018-10-03 11:15:52 -0700 | [diff] [blame] | 73 | * Whether to actually bind to the default SMS app service. (Feature flag) |
| 74 | */ |
| 75 | public final boolean SMS_SERVICE_ENABLED; |
| 76 | |
| 77 | /** |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 78 | * Extra binding flags for SMS service. |
| 79 | */ |
| 80 | public final int SMS_APP_BIND_FLAGS; |
| 81 | |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 82 | private AppBindingConstants(String settings) { |
| 83 | sourceSettings = 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 setting: " + settings); |
| 92 | } |
| 93 | |
| 94 | long serviceReconnectBackoffSec = parser.getLong( |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 95 | SERVICE_RECONNECT_BACKOFF_SEC_KEY, 10); |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 96 | |
| 97 | double serviceReconnectBackoffIncrease = parser.getFloat( |
| 98 | SERVICE_RECONNECT_BACKOFF_INCREASE_KEY, 2f); |
| 99 | |
| 100 | long serviceReconnectMaxBackoffSec = parser.getLong( |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 101 | SERVICE_RECONNECT_MAX_BACKOFF_SEC_KEY, TimeUnit.HOURS.toSeconds(1)); |
| 102 | |
Makoto Onuki | 7a2d35e | 2018-10-03 11:15:52 -0700 | [diff] [blame] | 103 | boolean smsServiceEnabled = parser.getBoolean(SMS_SERVICE_ENABLED_KEY, true); |
| 104 | |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 105 | int smsAppBindFlags = parser.getInt( |
| 106 | SMS_APP_BIND_FLAGS_KEY, |
| 107 | Context.BIND_NOT_VISIBLE | Context.BIND_FOREGROUND_SERVICE); |
| 108 | |
| 109 | long serviceStableConnectionThresholdSec = parser.getLong( |
| 110 | SERVICE_STABLE_CONNECTION_THRESHOLD_SEC_KEY, TimeUnit.MINUTES.toSeconds(2)); |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 111 | |
| 112 | // Set minimum: 5 seconds. |
| 113 | serviceReconnectBackoffSec = Math.max(5, serviceReconnectBackoffSec); |
| 114 | |
| 115 | // Set minimum: 1.0. |
| 116 | serviceReconnectBackoffIncrease = |
| 117 | Math.max(1, serviceReconnectBackoffIncrease); |
| 118 | |
| 119 | // Make sure max >= default back off. |
| 120 | serviceReconnectMaxBackoffSec = Math.max(serviceReconnectBackoffSec, |
| 121 | serviceReconnectMaxBackoffSec); |
| 122 | |
| 123 | SERVICE_RECONNECT_BACKOFF_SEC = serviceReconnectBackoffSec; |
| 124 | SERVICE_RECONNECT_BACKOFF_INCREASE = serviceReconnectBackoffIncrease; |
| 125 | SERVICE_RECONNECT_MAX_BACKOFF_SEC = serviceReconnectMaxBackoffSec; |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 126 | SERVICE_STABLE_CONNECTION_THRESHOLD_SEC = serviceStableConnectionThresholdSec; |
Makoto Onuki | 7a2d35e | 2018-10-03 11:15:52 -0700 | [diff] [blame] | 127 | SMS_SERVICE_ENABLED = smsServiceEnabled; |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 128 | SMS_APP_BIND_FLAGS = smsAppBindFlags; |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Create a new instance from a settings string. |
| 133 | */ |
| 134 | public static AppBindingConstants initializeFromString(String settings) { |
| 135 | return new AppBindingConstants(settings); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * dumpsys support. |
| 140 | */ |
| 141 | public void dump(String prefix, PrintWriter pw) { |
| 142 | pw.print(prefix); |
Makoto Onuki | 7a2d35e | 2018-10-03 11:15:52 -0700 | [diff] [blame] | 143 | pw.print("Constants: "); |
| 144 | pw.println(sourceSettings); |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 145 | |
| 146 | pw.print(prefix); |
| 147 | pw.print(" SERVICE_RECONNECT_BACKOFF_SEC: "); |
| 148 | pw.println(SERVICE_RECONNECT_BACKOFF_SEC); |
| 149 | |
| 150 | pw.print(prefix); |
| 151 | pw.print(" SERVICE_RECONNECT_BACKOFF_INCREASE: "); |
| 152 | pw.println(SERVICE_RECONNECT_BACKOFF_INCREASE); |
| 153 | |
| 154 | pw.print(prefix); |
| 155 | pw.print(" SERVICE_RECONNECT_MAX_BACKOFF_SEC: "); |
| 156 | pw.println(SERVICE_RECONNECT_MAX_BACKOFF_SEC); |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 157 | |
| 158 | pw.print(prefix); |
| 159 | pw.print(" SERVICE_STABLE_CONNECTION_THRESHOLD_SEC: "); |
| 160 | pw.println(SERVICE_STABLE_CONNECTION_THRESHOLD_SEC); |
| 161 | |
| 162 | pw.print(prefix); |
Makoto Onuki | 7a2d35e | 2018-10-03 11:15:52 -0700 | [diff] [blame] | 163 | pw.print(" SMS_SERVICE_ENABLED: "); |
| 164 | pw.println(SMS_SERVICE_ENABLED); |
| 165 | |
| 166 | pw.print(prefix); |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 167 | pw.print(" SMS_APP_BIND_FLAGS: 0x"); |
| 168 | pw.println(Integer.toHexString(SMS_APP_BIND_FLAGS)); |
Makoto Onuki | 87d260a | 2018-09-26 16:58:32 -0700 | [diff] [blame] | 169 | } |
| 170 | } |