Merge "Add app level settings for classification and summarization" into main

This commit is contained in:
Julia Reynolds
2025-02-04 12:56:49 -08:00
committed by Android (Google) Code Review
6 changed files with 293 additions and 2 deletions

View File

@@ -54,6 +54,7 @@ import android.text.format.DateUtils;
import android.util.IconDrawableFactory;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.android.internal.util.CollectionUtils;
@@ -436,7 +437,7 @@ public class NotificationBackend {
}
}
public List<String> getAssistantAdjustments(String pkg) {
public List<String> getAllowedAssistantAdjustments(String pkg) {
try {
return sINM.getAllowedAssistantAdjustments(pkg);
} catch (Exception e) {
@@ -769,6 +770,23 @@ public class NotificationBackend {
}
}
public @NonNull String[] getAdjustmentDeniedPackages(String key) {
try {
return sINM.getAdjustmentDeniedPackages(key);
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
return new String[]{};
}
}
public @NonNull void setAdjustmentSupportedForPackage(String key, String pkg, boolean enabled) {
try {
sINM.setAdjustmentSupportedForPackage(key, pkg, enabled);
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
}
}
@VisibleForTesting
void setNm(INotificationManager inm) {
sINM = inm;

View File

@@ -0,0 +1,98 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.app;
import android.app.Flags;
import android.content.Context;
import android.service.notification.Adjustment;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.RestrictedSwitchPreference;
/**
* Used for the app-level preference screen to opt the app in or out of a provided Adjustment key.
* E.g. to say an app can or cannot be classified by the NotificationAssistantService.
*/
public class AdjustmentKeyPreferenceController extends
NotificationPreferenceController implements Preference.OnPreferenceChangeListener {
private String mKey;
public AdjustmentKeyPreferenceController(@NonNull Context context,
@NonNull NotificationBackend backend, String key) {
super(context, backend);
mKey = key;
}
@Override
@NonNull
public String getPreferenceKey() {
return mKey;
}
@Override
public boolean isAvailable() {
if (!(Flags.notificationClassificationUi() || Flags.nmSummarizationUi()
|| Flags.nmSummarization())) {
return false;
}
boolean isBundlePref = Adjustment.KEY_TYPE.equals(mKey);
boolean isSummarizePref = Adjustment.KEY_SUMMARIZATION.equals(mKey);
if (!Flags.notificationClassificationUi() && isBundlePref) {
return false;
}
if (!(Flags.nmSummarizationUi() || Flags.nmSummarization()) && isSummarizePref) {
return false;
}
if (!isSummarizePref && !isBundlePref) {
return false;
}
if (isSummarizePref && !(mBackend.hasSentValidMsg(mAppRow.pkg, mAppRow.uid)
|| mBackend.isInInvalidMsgState(mAppRow.pkg, mAppRow.uid))) {
return false;
}
return super.isAvailable();
}
@Override
boolean isIncludedInFilter() {
// not a channel-specific preference; only at the app level
return false;
}
public void updateState(@NonNull Preference preference) {
RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
if (pref.getParent() != null) {
pref.getParent().setVisible(true);
}
if (pref != null && mAppRow != null) {
pref.setDisabledByAdmin(mAdmin);
pref.setEnabled(!pref.isDisabledByAdmin());
pref.setChecked(mBackend.getAllowedAssistantAdjustments(mAppRow.pkg).contains(mKey));
pref.setOnPreferenceChangeListener(this);
}
}
@Override
public boolean onPreferenceChange(@NonNull Preference preference, @NonNull Object newValue) {
final boolean allowedForPkg = (Boolean) newValue;
mBackend.setAdjustmentSupportedForPackage(mKey, mAppRow.pkg, allowedForPkg);
return true;
}
}

View File

@@ -21,6 +21,7 @@ import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.service.notification.Adjustment;
import android.text.TextUtils;
import android.util.Log;
@@ -132,6 +133,10 @@ public class AppNotificationSettings extends NotificationSettings {
context, mDependentFieldListener, mBackend));
mControllers.add(new BundleListPreferenceController(context, mBackend));
mControllers.add(new PromotedNotificationsPreferenceController(context, mBackend));
mControllers.add(new AdjustmentKeyPreferenceController(context, mBackend,
Adjustment.KEY_SUMMARIZATION));
mControllers.add(new AdjustmentKeyPreferenceController(context, mBackend,
Adjustment.KEY_TYPE));
return new ArrayList<>(mControllers);
}
}