Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.database.ContentObserver; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Handler; |
| 25 | import android.provider.Settings; |
| 26 | import android.util.KeyValueListParser; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import com.android.systemui.R; |
| 30 | |
| 31 | public 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 | } |