blob: 6193159ec0a5a700c43b016a3b12ffdc312fd37c [file] [log] [blame]
Petr Cermak10011fa2018-02-05 19:00:54 +00001/*
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
17package com.android.systemui.statusbar.policy;
18
Jason Monk27d01a622018-12-10 15:57:09 -050019import static com.android.systemui.Dependency.MAIN_HANDLER_NAME;
20
Petr Cermak10011fa2018-02-05 19:00:54 +000021import android.content.Context;
22import android.content.res.Resources;
23import android.database.ContentObserver;
24import android.net.Uri;
25import android.os.Handler;
26import android.provider.Settings;
27import android.util.KeyValueListParser;
28import android.util.Log;
29
30import com.android.systemui.R;
31
Jason Monk27d01a622018-12-10 15:57:09 -050032import javax.inject.Inject;
33import javax.inject.Named;
34import javax.inject.Singleton;
35
36@Singleton
Petr Cermak10011fa2018-02-05 19:00:54 +000037public final class SmartReplyConstants extends ContentObserver {
38
39 private static final String TAG = "SmartReplyConstants";
40
41 private static final String KEY_ENABLED = "enabled";
Richard Ledley28944cb2018-02-26 10:36:00 +000042 private static final String KEY_REQUIRES_TARGETING_P = "requires_targeting_p";
Petr Cermak10011fa2018-02-05 19:00:54 +000043 private static final String KEY_MAX_SQUEEZE_REMEASURE_ATTEMPTS =
44 "max_squeeze_remeasure_attempts";
45
46 private final boolean mDefaultEnabled;
Richard Ledley28944cb2018-02-26 10:36:00 +000047 private final boolean mDefaultRequiresP;
Petr Cermak10011fa2018-02-05 19:00:54 +000048 private final int mDefaultMaxSqueezeRemeasureAttempts;
49
50 private boolean mEnabled;
Richard Ledley28944cb2018-02-26 10:36:00 +000051 private boolean mRequiresTargetingP;
Petr Cermak10011fa2018-02-05 19:00:54 +000052 private int mMaxSqueezeRemeasureAttempts;
53
54 private final Context mContext;
55 private final KeyValueListParser mParser = new KeyValueListParser(',');
56
Jason Monk27d01a622018-12-10 15:57:09 -050057 @Inject
58 public SmartReplyConstants(@Named(MAIN_HANDLER_NAME) Handler handler, Context context) {
Petr Cermak10011fa2018-02-05 19:00:54 +000059 super(handler);
60
61 mContext = context;
62 final Resources resources = mContext.getResources();
63 mDefaultEnabled = resources.getBoolean(
64 R.bool.config_smart_replies_in_notifications_enabled);
Richard Ledley28944cb2018-02-26 10:36:00 +000065 mDefaultRequiresP = resources.getBoolean(
66 R.bool.config_smart_replies_in_notifications_requires_targeting_p);
Petr Cermak10011fa2018-02-05 19:00:54 +000067 mDefaultMaxSqueezeRemeasureAttempts = resources.getInteger(
68 R.integer.config_smart_replies_in_notifications_max_squeeze_remeasure_attempts);
69
70 mContext.getContentResolver().registerContentObserver(
71 Settings.Global.getUriFor(Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS),
72 false, this);
73 updateConstants();
74 }
75
76 @Override
77 public void onChange(boolean selfChange, Uri uri) {
78 updateConstants();
79 }
80
81 private void updateConstants() {
82 synchronized (SmartReplyConstants.this) {
83 try {
84 mParser.setString(Settings.Global.getString(mContext.getContentResolver(),
85 Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS));
86 } catch (IllegalArgumentException e) {
87 Log.e(TAG, "Bad smart reply constants", e);
88 }
89 mEnabled = mParser.getBoolean(KEY_ENABLED, mDefaultEnabled);
Richard Ledley28944cb2018-02-26 10:36:00 +000090 mRequiresTargetingP = mParser.getBoolean(KEY_REQUIRES_TARGETING_P, mDefaultRequiresP);
Petr Cermak10011fa2018-02-05 19:00:54 +000091 mMaxSqueezeRemeasureAttempts = mParser.getInt(
92 KEY_MAX_SQUEEZE_REMEASURE_ATTEMPTS, mDefaultMaxSqueezeRemeasureAttempts);
93 }
94 }
95
96 /** Returns whether smart replies in notifications are enabled. */
97 public boolean isEnabled() {
98 return mEnabled;
99 }
100
101 /**
Richard Ledley28944cb2018-02-26 10:36:00 +0000102 * Returns whether smart replies in notifications should be disabled when the app targets a
103 * version of Android older than P.
104 */
105 public boolean requiresTargetingP() {
106 return mRequiresTargetingP;
107 }
108
109 /**
Petr Cermak10011fa2018-02-05 19:00:54 +0000110 * Returns the maximum number of times {@link SmartReplyView#onMeasure(int, int)} will try to
111 * find a better (narrower) line-break for a double-line smart reply button.
112 */
113 public int getMaxSqueezeRemeasureAttempts() {
114 return mMaxSqueezeRemeasureAttempts;
115 }
116}