Merge "Send APP_NOTIFICATION_PROMOTION_SETTINGS intent to the preference" into main

This commit is contained in:
Yuri Lin
2025-01-06 10:59:50 -08:00
committed by Android (Google) Code Review
4 changed files with 135 additions and 1 deletions

View File

@@ -4114,6 +4114,11 @@
<action android:name="android.settings.APP_NOTIFICATION_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter android:priority="2"
android:featureFlag="android.app.ui_rich_ongoing">
<action android:name="android.settings.APP_NOTIFICATION_PROMOTION_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.notification.app.AppNotificationSettings" />
<meta-data android:name="com.android.settings.HIGHLIGHT_MENU_KEY"

View File

@@ -16,13 +16,20 @@
package com.android.settings.notification.app;
import android.app.Flags;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import androidx.preference.PreferenceScreen;
import androidx.recyclerview.widget.RecyclerView;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settingslib.core.AbstractPreferenceController;
import java.util.ArrayList;
@@ -40,6 +47,26 @@ public class AppNotificationSettings extends NotificationSettings {
return SettingsEnums.NOTIFICATION_APP_NOTIFICATION;
}
@Override
protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
if (Flags.uiRichOngoing()) {
// If we got here via APP_NOTIFICATION_PROMOTION_SETTINGS intent, add the relevant
// preference key to navigate to & highlight. This argument is passed into
// HighlightablePreferenceGroupAdapter in the SettingsPreferenceFragment onCreateAdapter
// call.
if (mIntent != null && TextUtils.equals(mIntent.getAction(),
Settings.ACTION_APP_NOTIFICATION_PROMOTION_SETTINGS)) {
Bundle args = getArguments();
if (args == null) {
args = new Bundle();
}
args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY,
PromotedNotificationsPreferenceController.KEY_PROMOTED_SWITCH);
setArguments(args);
}
}
return super.onCreateAdapter(preferenceScreen);
}
@Override
public void onResume() {

View File

@@ -28,7 +28,7 @@ import com.android.settingslib.RestrictedSwitchPreference;
public class PromotedNotificationsPreferenceController extends
NotificationPreferenceController implements Preference.OnPreferenceChangeListener {
private static final String KEY_PROMOTED_CATEGORY = "promoted_category";
private static final String KEY_PROMOTED_SWITCH = "promoted_switch";
protected static final String KEY_PROMOTED_SWITCH = "promoted_switch";
public PromotedNotificationsPreferenceController(@NonNull Context context,
@NonNull NotificationBackend backend) {

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2024 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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.app.Flags;
import android.content.Context;
import android.content.Intent;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.SettingsActivity;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class AppNotificationSettingsTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private static Intent sAppNotifSettingsIntent = new Intent()
.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
private static Intent sAppNotifPromotionSettingsIntent = new Intent()
.setAction(Settings.ACTION_APP_NOTIFICATION_PROMOTION_SETTINGS);
@Mock
private PreferenceScreen mPreferenceScreen;
private AppNotificationSettings mSettings;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mSettings = new AppNotificationSettings();
// Non-null context is needed to call onCreateAdapter
Context context = ApplicationProvider.getApplicationContext();
when(mPreferenceScreen.getContext()).thenReturn(context);
}
@Test
@EnableFlags(Flags.FLAG_UI_RICH_ONGOING)
public void testOnCreateAdapter_flagOn_wrongIntent() {
mSettings.mIntent = sAppNotifSettingsIntent;
// notification settings intent goes to this page, but should not trigger a highlight.
// we check specifically that getArguments() is null because we haven't set up any other
// arguments, so the only ones would be ones created during onCreateAdapter.
mSettings.onCreateAdapter(mPreferenceScreen);
assertThat(mSettings.getArguments()).isNull();
}
@Test
@EnableFlags(Flags.FLAG_UI_RICH_ONGOING)
public void testOnCreateAdapter_flagOn_correctIntent() {
mSettings.mIntent = sAppNotifPromotionSettingsIntent;
// for promotion settings intent, we expect a highlighted setting
mSettings.onCreateAdapter(mPreferenceScreen);
assertThat(mSettings.getArguments().getString(
SettingsActivity.EXTRA_FRAGMENT_ARG_KEY)).isEqualTo(
PromotedNotificationsPreferenceController.KEY_PROMOTED_SWITCH);
}
@Test
@DisableFlags(Flags.FLAG_UI_RICH_ONGOING)
public void testOnCreateAdapter_flagOff() {
mSettings.mIntent = sAppNotifPromotionSettingsIntent;
// regardless of intent, if the flag is off we should not have a highlight
mSettings.onCreateAdapter(mPreferenceScreen);
assertThat(mSettings.getArguments()).isNull();
}
}