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 android.ext.services.notification; 18 19 import android.os.Handler; 20 import android.os.Looper; 21 import android.provider.DeviceConfig; 22 import android.util.Log; 23 24 import androidx.annotation.VisibleForTesting; 25 26 import com.android.textclassifier.notification.SmartSuggestionsConfig; 27 28 /** 29 * Observes the settings for {@link Assistant}. 30 */ 31 final class AssistantSettings implements SmartSuggestionsConfig { 32 private static final String LOG_TAG = "AssistantSettings"; 33 public static Factory FACTORY = AssistantSettings::createAndRegister; 34 private static final boolean DEFAULT_GENERATE_REPLIES = true; 35 private static final boolean DEFAULT_GENERATE_ACTIONS = true; 36 private static final int DEFAULT_MAX_MESSAGES_TO_EXTRACT = 5; 37 @VisibleForTesting 38 static final int DEFAULT_MAX_SUGGESTIONS = 3; 39 40 41 // Copied from SystemUiDeviceConfigFlags.java 42 /** 43 * Whether the Notification Assistant should generate replies for notifications. 44 */ 45 static final String NAS_GENERATE_REPLIES = "nas_generate_replies"; 46 47 /** 48 * Whether the Notification Assistant should generate contextual actions for notifications. 49 */ 50 static final String NAS_GENERATE_ACTIONS = "nas_generate_actions"; 51 52 /** 53 * The maximum number of messages the Notification Assistant should extract from a 54 * conversation when constructing responses for that conversation. 55 */ 56 static final String NAS_MAX_MESSAGES_TO_EXTRACT = "nas_max_messages_to_extract"; 57 58 /** 59 * The maximum number of suggestions the Notification Assistant should provide for a 60 * messaging conversation. 61 */ 62 static final String NAS_MAX_SUGGESTIONS = "nas_max_suggestions"; 63 64 private final Handler mHandler; 65 66 // Actual configuration settings. 67 boolean mGenerateReplies = DEFAULT_GENERATE_REPLIES; 68 boolean mGenerateActions = DEFAULT_GENERATE_ACTIONS; 69 int mMaxMessagesToExtract = DEFAULT_MAX_MESSAGES_TO_EXTRACT; 70 int mMaxSuggestions = DEFAULT_MAX_SUGGESTIONS; 71 72 @VisibleForTesting 73 DeviceConfig.OnPropertiesChangedListener mDeviceConfigChangedListener; 74 AssistantSettings()75 public AssistantSettings() { 76 mHandler = new Handler(Looper.getMainLooper()); 77 } 78 createAndRegister()79 private static AssistantSettings createAndRegister() { 80 AssistantSettings assistantSettings = new AssistantSettings(); 81 assistantSettings.registerDeviceConfigs(); 82 return assistantSettings; 83 } 84 registerDeviceConfigs()85 private void registerDeviceConfigs() { 86 mDeviceConfigChangedListener = 87 properties -> onDeviceConfigPropertiesChanged(properties.getNamespace()); 88 DeviceConfig.addOnPropertiesChangedListener( 89 DeviceConfig.NAMESPACE_SYSTEMUI, 90 this::postToHandler, 91 mDeviceConfigChangedListener); 92 93 // Update the fields in this class from the current state of the device config. 94 updateFromDeviceConfigFlags(); 95 } 96 postToHandler(Runnable r)97 private void postToHandler(Runnable r) { 98 this.mHandler.post(r); 99 } 100 101 @VisibleForTesting unregisterDeviceConfigs()102 void unregisterDeviceConfigs() { 103 if (mDeviceConfigChangedListener != null) { 104 DeviceConfig.removeOnPropertiesChangedListener(mDeviceConfigChangedListener); 105 mDeviceConfigChangedListener = null; 106 } 107 } 108 109 @VisibleForTesting onDeviceConfigPropertiesChanged(String namespace)110 void onDeviceConfigPropertiesChanged(String namespace) { 111 if (!DeviceConfig.NAMESPACE_SYSTEMUI.equals(namespace)) { 112 Log.e(LOG_TAG, "Received update from DeviceConfig for unrelated namespace: " 113 + namespace); 114 return; 115 } 116 117 updateFromDeviceConfigFlags(); 118 } 119 updateFromDeviceConfigFlags()120 private void updateFromDeviceConfigFlags() { 121 mGenerateReplies = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SYSTEMUI, 122 NAS_GENERATE_REPLIES, DEFAULT_GENERATE_REPLIES); 123 124 mGenerateActions = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SYSTEMUI, 125 NAS_GENERATE_ACTIONS, DEFAULT_GENERATE_ACTIONS); 126 127 mMaxMessagesToExtract = DeviceConfig.getInt(DeviceConfig.NAMESPACE_SYSTEMUI, 128 NAS_MAX_MESSAGES_TO_EXTRACT, 129 DEFAULT_MAX_MESSAGES_TO_EXTRACT); 130 131 mMaxSuggestions = DeviceConfig.getInt(DeviceConfig.NAMESPACE_SYSTEMUI, 132 NAS_MAX_SUGGESTIONS, DEFAULT_MAX_SUGGESTIONS); 133 134 } 135 136 @Override shouldGenerateReplies()137 public boolean shouldGenerateReplies() { 138 return mGenerateReplies; 139 } 140 141 @Override shouldGenerateActions()142 public boolean shouldGenerateActions() { 143 return mGenerateActions; 144 } 145 146 @Override getMaxSuggestions()147 public int getMaxSuggestions() { 148 return mMaxSuggestions; 149 } 150 151 @Override getMaxMessagesToExtract()152 public int getMaxMessagesToExtract() { 153 return mMaxMessagesToExtract; 154 } 155 156 public interface Factory { createAndRegister()157 AssistantSettings createAndRegister(); 158 } 159 } 160