1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.statusbar.policy;
import android.app.RemoteInput;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.provider.DeviceConfig;
import android.text.TextUtils;
import android.util.KeyValueListParser;
import android.util.Log;
import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
import com.android.systemui.R;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.util.DeviceConfigProxy;
import javax.inject.Inject;
@SysUISingleton
public final class SmartReplyConstants {
private static final String TAG = "SmartReplyConstants";
private final boolean mDefaultEnabled;
private final boolean mDefaultRequiresP;
private final int mDefaultMaxSqueezeRemeasureAttempts;
private final boolean mDefaultEditChoicesBeforeSending;
private final boolean mDefaultShowInHeadsUp;
private final int mDefaultMinNumSystemGeneratedReplies;
private final int mDefaultMaxNumActions;
private final int mDefaultOnClickInitDelay;
// These fields are updated on the UI thread but can be accessed on both the UI thread and
// background threads. We use the volatile keyword here instead of synchronization blocks since
// we only care about variable updates here being visible to other threads (and not for example
// whether the variables we are reading were updated in the same go).
private volatile boolean mEnabled;
private volatile boolean mRequiresTargetingP;
private volatile int mMaxSqueezeRemeasureAttempts;
private volatile boolean mEditChoicesBeforeSending;
private volatile boolean mShowInHeadsUp;
private volatile int mMinNumSystemGeneratedReplies;
private volatile int mMaxNumActions;
private volatile long mOnClickInitDelay;
private final Handler mHandler;
private final Context mContext;
private final DeviceConfigProxy mDeviceConfig;
private final KeyValueListParser mParser = new KeyValueListParser(',');
@Inject
public SmartReplyConstants(
@Main Handler handler,
Context context,
DeviceConfigProxy deviceConfig
) {
mHandler = handler;
mContext = context;
final Resources resources = mContext.getResources();
mDefaultEnabled = resources.getBoolean(
R.bool.config_smart_replies_in_notifications_enabled);
mDefaultRequiresP = resources.getBoolean(
R.bool.config_smart_replies_in_notifications_requires_targeting_p);
mDefaultMaxSqueezeRemeasureAttempts = resources.getInteger(
R.integer.config_smart_replies_in_notifications_max_squeeze_remeasure_attempts);
mDefaultEditChoicesBeforeSending = resources.getBoolean(
R.bool.config_smart_replies_in_notifications_edit_choices_before_sending);
mDefaultShowInHeadsUp = resources.getBoolean(
R.bool.config_smart_replies_in_notifications_show_in_heads_up);
mDefaultMinNumSystemGeneratedReplies = resources.getInteger(
R.integer.config_smart_replies_in_notifications_min_num_system_generated_replies);
mDefaultMaxNumActions = resources.getInteger(
R.integer.config_smart_replies_in_notifications_max_num_actions);
mDefaultOnClickInitDelay = resources.getInteger(
R.integer.config_smart_replies_in_notifications_onclick_init_delay);
mDeviceConfig = deviceConfig;
registerDeviceConfigListener();
updateConstants();
}
private void registerDeviceConfigListener() {
mDeviceConfig.addOnPropertiesChangedListener(
DeviceConfig.NAMESPACE_SYSTEMUI,
this::postToHandler,
mOnPropertiesChangedListener);
}
private void postToHandler(Runnable r) {
this.mHandler.post(r);
}
private final DeviceConfig.OnPropertiesChangedListener mOnPropertiesChangedListener =
new DeviceConfig.OnPropertiesChangedListener() {
@Override
public void onPropertiesChanged(DeviceConfig.Properties properties) {
if (!DeviceConfig.NAMESPACE_SYSTEMUI.equals(properties.getNamespace())) {
Log.e(TAG,
"Received update from DeviceConfig for unrelated namespace: "
+ properties.getNamespace());
return;
}
updateConstants();
}
};
private void updateConstants() {
synchronized (SmartReplyConstants.this) {
mEnabled = readDeviceConfigBooleanOrDefaultIfEmpty(
SystemUiDeviceConfigFlags.SSIN_ENABLED,
mDefaultEnabled);
mRequiresTargetingP = readDeviceConfigBooleanOrDefaultIfEmpty(
SystemUiDeviceConfigFlags.SSIN_REQUIRES_TARGETING_P,
mDefaultRequiresP);
mMaxSqueezeRemeasureAttempts = mDeviceConfig.getInt(
DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.SSIN_MAX_SQUEEZE_REMEASURE_ATTEMPTS,
mDefaultMaxSqueezeRemeasureAttempts);
mEditChoicesBeforeSending = readDeviceConfigBooleanOrDefaultIfEmpty(
SystemUiDeviceConfigFlags.SSIN_EDIT_CHOICES_BEFORE_SENDING,
mDefaultEditChoicesBeforeSending);
mShowInHeadsUp = readDeviceConfigBooleanOrDefaultIfEmpty(
SystemUiDeviceConfigFlags.SSIN_SHOW_IN_HEADS_UP,
mDefaultShowInHeadsUp);
mMinNumSystemGeneratedReplies = mDeviceConfig.getInt(
DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.SSIN_MIN_NUM_SYSTEM_GENERATED_REPLIES,
mDefaultMinNumSystemGeneratedReplies);
mMaxNumActions = mDeviceConfig.getInt(
DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.SSIN_MAX_NUM_ACTIONS,
mDefaultMaxNumActions);
mOnClickInitDelay = mDeviceConfig.getInt(
DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.SSIN_ONCLICK_INIT_DELAY,
mDefaultOnClickInitDelay);
}
}
private boolean readDeviceConfigBooleanOrDefaultIfEmpty(String propertyName,
boolean defaultValue) {
String value = mDeviceConfig.getProperty(DeviceConfig.NAMESPACE_SYSTEMUI, propertyName);
if (TextUtils.isEmpty(value)) {
return defaultValue;
}
if ("true".equals(value)) {
return true;
}
if ("false".equals(value)) {
return false;
}
// For invalid configs we return the default value.
return defaultValue;
}
/** Returns whether smart replies in notifications are enabled. */
public boolean isEnabled() {
return mEnabled;
}
/**
* Returns whether smart replies in notifications should be disabled when the app targets a
* version of Android older than P.
*/
public boolean requiresTargetingP() {
return mRequiresTargetingP;
}
/**
* Returns the maximum number of times {@link SmartReplyView#onMeasure(int, int)} will try to
* find a better (narrower) line-break for a double-line smart reply button.
*/
public int getMaxSqueezeRemeasureAttempts() {
return mMaxSqueezeRemeasureAttempts;
}
/**
* Returns whether by tapping on a choice should let the user edit the input before it
* is sent to the app.
*
* @param remoteInputEditChoicesBeforeSending The value from
* {@link RemoteInput#getEditChoicesBeforeSending()}
*/
public boolean getEffectiveEditChoicesBeforeSending(
@RemoteInput.EditChoicesBeforeSending int remoteInputEditChoicesBeforeSending) {
switch (remoteInputEditChoicesBeforeSending) {
case RemoteInput.EDIT_CHOICES_BEFORE_SENDING_DISABLED:
return false;
case RemoteInput.EDIT_CHOICES_BEFORE_SENDING_ENABLED:
return true;
case RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO:
default:
return mEditChoicesBeforeSending;
}
}
/**
* Returns whether smart suggestions should be enabled in heads-up notifications.
*/
public boolean getShowInHeadsUp() {
return mShowInHeadsUp;
}
/**
* Returns the minimum number of system generated replies to show in a notification.
* If we cannot show at least this many system generated replies we should show none.
*/
public int getMinNumSystemGeneratedReplies() {
return mMinNumSystemGeneratedReplies;
}
/**
* Returns the maximum number smart actions to show in a notification, or -1 if there shouldn't
* be a limit.
*/
public int getMaxNumActions() {
return mMaxNumActions;
}
/**
* Returns the amount of time (ms) before smart suggestions are clickable, since the suggestions
* were added.
*/
public long getOnClickInitDelay() {
return mOnClickInitDelay;
}
}
|