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_REQUIRES_TARGETING_P = "requires_targeting_p"; 37 private static final String KEY_MAX_SQUEEZE_REMEASURE_ATTEMPTS = 38 "max_squeeze_remeasure_attempts"; 39 40 private final boolean mDefaultEnabled; 41 private final boolean mDefaultRequiresP; 42 private final int mDefaultMaxSqueezeRemeasureAttempts; 43 44 private boolean mEnabled; 45 private boolean mRequiresTargetingP; 46 private int mMaxSqueezeRemeasureAttempts; 47 48 private final Context mContext; 49 private final KeyValueListParser mParser = new KeyValueListParser(','); 50 SmartReplyConstants(Handler handler, Context context)51 public SmartReplyConstants(Handler handler, Context context) { 52 super(handler); 53 54 mContext = context; 55 final Resources resources = mContext.getResources(); 56 mDefaultEnabled = resources.getBoolean( 57 R.bool.config_smart_replies_in_notifications_enabled); 58 mDefaultRequiresP = resources.getBoolean( 59 R.bool.config_smart_replies_in_notifications_requires_targeting_p); 60 mDefaultMaxSqueezeRemeasureAttempts = resources.getInteger( 61 R.integer.config_smart_replies_in_notifications_max_squeeze_remeasure_attempts); 62 63 mContext.getContentResolver().registerContentObserver( 64 Settings.Global.getUriFor(Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS), 65 false, this); 66 updateConstants(); 67 } 68 69 @Override onChange(boolean selfChange, Uri uri)70 public void onChange(boolean selfChange, Uri uri) { 71 updateConstants(); 72 } 73 updateConstants()74 private void updateConstants() { 75 synchronized (SmartReplyConstants.this) { 76 try { 77 mParser.setString(Settings.Global.getString(mContext.getContentResolver(), 78 Settings.Global.SMART_REPLIES_IN_NOTIFICATIONS_FLAGS)); 79 } catch (IllegalArgumentException e) { 80 Log.e(TAG, "Bad smart reply constants", e); 81 } 82 mEnabled = mParser.getBoolean(KEY_ENABLED, mDefaultEnabled); 83 mRequiresTargetingP = mParser.getBoolean(KEY_REQUIRES_TARGETING_P, mDefaultRequiresP); 84 mMaxSqueezeRemeasureAttempts = mParser.getInt( 85 KEY_MAX_SQUEEZE_REMEASURE_ATTEMPTS, mDefaultMaxSqueezeRemeasureAttempts); 86 } 87 } 88 89 /** Returns whether smart replies in notifications are enabled. */ isEnabled()90 public boolean isEnabled() { 91 return mEnabled; 92 } 93 94 /** 95 * Returns whether smart replies in notifications should be disabled when the app targets a 96 * version of Android older than P. 97 */ requiresTargetingP()98 public boolean requiresTargetingP() { 99 return mRequiresTargetingP; 100 } 101 102 /** 103 * Returns the maximum number of times {@link SmartReplyView#onMeasure(int, int)} will try to 104 * find a better (narrower) line-break for a double-line smart reply button. 105 */ getMaxSqueezeRemeasureAttempts()106 public int getMaxSqueezeRemeasureAttempts() { 107 return mMaxSqueezeRemeasureAttempts; 108 } 109 } 110