blob: 71847694b1d8b9756aba2bed681a24568e346218 [file] [log] [blame]
Makoto Onuki87d260a2018-09-26 16:58:32 -07001/*
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 */
16package com.android.server.appbinding;
17
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -070018import android.content.Context;
Makoto Onuki87d260a2018-09-26 16:58:32 -070019import android.util.KeyValueListParser;
20import android.util.Slog;
21
22import java.io.PrintWriter;
23import java.util.concurrent.TimeUnit;
24
25/**
26 * Constants that are configurable via the global settings for {@link AppBindingService}.
27 */
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -070028public class AppBindingConstants {
Makoto Onuki87d260a2018-09-26 16:58:32 -070029 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 Onuki8c7c5cc2018-10-02 09:29:58 -070040 private static final String SERVICE_STABLE_CONNECTION_THRESHOLD_SEC_KEY =
41 "service_stable_connection_threshold_sec";
42
Makoto Onuki7a2d35e2018-10-03 11:15:52 -070043 private static final String SMS_SERVICE_ENABLED_KEY =
44 "sms_service_enabled";
45
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -070046 private static final String SMS_APP_BIND_FLAGS_KEY =
47 "sms_app_bind_flags";
48
Makoto Onuki87d260a2018-09-26 16:58:32 -070049 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 Onuki8c7c5cc2018-10-02 09:29:58 -070067 /**
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 Onuki7a2d35e2018-10-03 11:15:52 -070073 * Whether to actually bind to the default SMS app service. (Feature flag)
74 */
75 public final boolean SMS_SERVICE_ENABLED;
76
77 /**
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -070078 * Extra binding flags for SMS service.
79 */
80 public final int SMS_APP_BIND_FLAGS;
81
Makoto Onuki87d260a2018-09-26 16:58:32 -070082 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 Onuki8c7c5cc2018-10-02 09:29:58 -070095 SERVICE_RECONNECT_BACKOFF_SEC_KEY, 10);
Makoto Onuki87d260a2018-09-26 16:58:32 -070096
97 double serviceReconnectBackoffIncrease = parser.getFloat(
98 SERVICE_RECONNECT_BACKOFF_INCREASE_KEY, 2f);
99
100 long serviceReconnectMaxBackoffSec = parser.getLong(
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -0700101 SERVICE_RECONNECT_MAX_BACKOFF_SEC_KEY, TimeUnit.HOURS.toSeconds(1));
102
Makoto Onuki7a2d35e2018-10-03 11:15:52 -0700103 boolean smsServiceEnabled = parser.getBoolean(SMS_SERVICE_ENABLED_KEY, true);
104
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -0700105 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 Onuki87d260a2018-09-26 16:58:32 -0700111
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 Onuki8c7c5cc2018-10-02 09:29:58 -0700126 SERVICE_STABLE_CONNECTION_THRESHOLD_SEC = serviceStableConnectionThresholdSec;
Makoto Onuki7a2d35e2018-10-03 11:15:52 -0700127 SMS_SERVICE_ENABLED = smsServiceEnabled;
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -0700128 SMS_APP_BIND_FLAGS = smsAppBindFlags;
Makoto Onuki87d260a2018-09-26 16:58:32 -0700129 }
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 Onuki7a2d35e2018-10-03 11:15:52 -0700143 pw.print("Constants: ");
144 pw.println(sourceSettings);
Makoto Onuki87d260a2018-09-26 16:58:32 -0700145
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 Onuki8c7c5cc2018-10-02 09:29:58 -0700157
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 Onuki7a2d35e2018-10-03 11:15:52 -0700163 pw.print(" SMS_SERVICE_ENABLED: ");
164 pw.println(SMS_SERVICE_ENABLED);
165
166 pw.print(prefix);
Makoto Onuki8c7c5cc2018-10-02 09:29:58 -0700167 pw.print(" SMS_APP_BIND_FLAGS: 0x");
168 pw.println(Integer.toHexString(SMS_APP_BIND_FLAGS));
Makoto Onuki87d260a2018-09-26 16:58:32 -0700169 }
170}