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 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 19 | import static com.android.systemui.Dependency.MAIN_HANDLER_NAME; |
| 20 | |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
| 23 | import android.database.ContentObserver; |
| 24 | import android.net.Uri; |
| 25 | import android.os.Handler; |
| 26 | import android.provider.Settings; |
| 27 | import android.util.KeyValueListParser; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | import com.android.systemui.R; |
| 31 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 32 | import javax.inject.Inject; |
| 33 | import javax.inject.Named; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | @Singleton |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 37 | public final class SmartReplyConstants extends ContentObserver { |
| 38 | |
| 39 | private static final String TAG = "SmartReplyConstants"; |
| 40 | |
| 41 | private static final String KEY_ENABLED = "enabled"; |
Richard Ledley | 28944cb | 2018-02-26 10:36:00 +0000 | [diff] [blame] | 42 | private static final String KEY_REQUIRES_TARGETING_P = "requires_targeting_p"; |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 43 | private static final String KEY_MAX_SQUEEZE_REMEASURE_ATTEMPTS = |
| 44 | "max_squeeze_remeasure_attempts"; |
| 45 | |
| 46 | private final boolean mDefaultEnabled; |
Richard Ledley | 28944cb | 2018-02-26 10:36:00 +0000 | [diff] [blame] | 47 | private final boolean mDefaultRequiresP; |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 48 | private final int mDefaultMaxSqueezeRemeasureAttempts; |
| 49 | |
| 50 | private boolean mEnabled; |
Richard Ledley | 28944cb | 2018-02-26 10:36:00 +0000 | [diff] [blame] | 51 | private boolean mRequiresTargetingP; |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 52 | private int mMaxSqueezeRemeasureAttempts; |
| 53 | |
| 54 | private final Context mContext; |
| 55 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 56 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 57 | @Inject |
| 58 | public SmartReplyConstants(@Named(MAIN_HANDLER_NAME) Handler handler, Context context) { |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 59 | 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 Ledley | 28944cb | 2018-02-26 10:36:00 +0000 | [diff] [blame] | 65 | mDefaultRequiresP = resources.getBoolean( |
| 66 | R.bool.config_smart_replies_in_notifications_requires_targeting_p); |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 67 | 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 Ledley | 28944cb | 2018-02-26 10:36:00 +0000 | [diff] [blame] | 90 | mRequiresTargetingP = mParser.getBoolean(KEY_REQUIRES_TARGETING_P, mDefaultRequiresP); |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 91 | 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 Ledley | 28944cb | 2018-02-26 10:36:00 +0000 | [diff] [blame] | 102 | * 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 Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 110 | * 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 | } |