blob: c5067a6578eb7506c68b1cabf410b1d5607d91eb [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
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.res.Resources;
22import android.database.ContentObserver;
23import android.net.Uri;
24import android.os.Handler;
25import android.provider.Settings;
26import android.util.KeyValueListParser;
27import android.util.Log;
28
29import com.android.systemui.R;
30
31public final class SmartReplyConstants extends ContentObserver {
32
33 private static final String TAG = "SmartReplyConstants";
34
35 private static final String KEY_ENABLED = "enabled";
36 private static final String KEY_MAX_SQUEEZE_REMEASURE_ATTEMPTS =
37 "max_squeeze_remeasure_attempts";
38
39 private final boolean mDefaultEnabled;
40 private final int mDefaultMaxSqueezeRemeasureAttempts;
41
42 private boolean mEnabled;
43 private int mMaxSqueezeRemeasureAttempts;
44
45 private final Context mContext;
46 private final KeyValueListParser mParser = new KeyValueListParser(',');
47
48 public SmartReplyConstants(Handler handler, Context context) {
49 super(handler);
50
51 mContext = context;
52 final Resources resources = mContext.getResources();
53 mDefaultEnabled = resources.getBoolean(
54 R.bool.config_smart_replies_in_notifications_enabled);
55 mDefaultMaxSqueezeRemeasureAttempts = resources.getInteger(
56 R.integer.config_smart_replies_in_notifications_max_squeeze_remeasure_attempts);
57
58 mContext.getContentResolver().registerContentObserver(
59 Settings.Global.getUriFor(Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS),
60 false, this);
61 updateConstants();
62 }
63
64 @Override
65 public void onChange(boolean selfChange, Uri uri) {
66 updateConstants();
67 }
68
69 private void updateConstants() {
70 synchronized (SmartReplyConstants.this) {
71 try {
72 mParser.setString(Settings.Global.getString(mContext.getContentResolver(),
73 Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS));
74 } catch (IllegalArgumentException e) {
75 Log.e(TAG, "Bad smart reply constants", e);
76 }
77 mEnabled = mParser.getBoolean(KEY_ENABLED, mDefaultEnabled);
78 mMaxSqueezeRemeasureAttempts = mParser.getInt(
79 KEY_MAX_SQUEEZE_REMEASURE_ATTEMPTS, mDefaultMaxSqueezeRemeasureAttempts);
80 }
81 }
82
83 /** Returns whether smart replies in notifications are enabled. */
84 public boolean isEnabled() {
85 return mEnabled;
86 }
87
88 /**
89 * Returns the maximum number of times {@link SmartReplyView#onMeasure(int, int)} will try to
90 * find a better (narrower) line-break for a double-line smart reply button.
91 */
92 public int getMaxSqueezeRemeasureAttempts() {
93 return mMaxSqueezeRemeasureAttempts;
94 }
95}