Add flag to allow NAS to indicate adjustments on notifications and

solicit feedback.

Bug: 145559028
Test: make -j RunSettingsRoboTests
Change-Id: I45e880168352e4dc5409e5d91ff22d7e4ce69ca7
This commit is contained in:
Alex Mang
2020-02-18 23:33:16 -08:00
parent eb28171891
commit ea5526ca7d
4 changed files with 268 additions and 0 deletions

View File

@@ -8079,6 +8079,12 @@
<!-- Configure Notifications: setting summary [CHAR LIMIT=200] --> <!-- Configure Notifications: setting summary [CHAR LIMIT=200] -->
<string name="asst_capability_ranking_summary">Automatically rank notifications by relevance</string> <string name="asst_capability_ranking_summary">Automatically rank notifications by relevance</string>
<!-- Configure Notifications: setting title [CHAR LIMIT=80 BACKUP_MESSAGE_ID=6691908606916292167] -->
<string name="asst_feedback_indicator_title">Adaptive notification feedback</string>
<!-- Configure Notifications: setting summary [CHAR LIMIT=200] -->
<string name="asst_feedback_indicator_summary">Indicate adjustments made to notifications and show the option to provide feedback to the system</string>
<!-- Configure Notifications: setting title [CHAR LIMIT=80] --> <!-- Configure Notifications: setting title [CHAR LIMIT=80] -->
<string name="asst_capabilities_actions_replies_title">Suggested actions and replies</string> <string name="asst_capabilities_actions_replies_title">Suggested actions and replies</string>

View File

@@ -587,6 +587,11 @@
android:title="@string/asst_capability_ranking_title" android:title="@string/asst_capability_ranking_title"
settings:controller="com.android.settings.notification.AssistantCapabilityPreferenceController" /> settings:controller="com.android.settings.notification.AssistantCapabilityPreferenceController" />
<SwitchPreference
android:key="asst_feedback_indicator"
android:title="@string/asst_feedback_indicator_title"
settings:controller="com.android.settings.notification.AssistantFeedbackPreferenceController" />
<Preference <Preference
android:key="inactive_apps" android:key="inactive_apps"
android:title="@string/inactive_apps_title" android:title="@string/inactive_apps_title"

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2020 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;
import static android.provider.Settings.Secure.NOTIFICATION_FEEDBACK_ENABLED;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.google.common.annotations.VisibleForTesting;
public class AssistantFeedbackPreferenceController extends TogglePreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
LifecycleObserver, OnResume, OnPause {
@VisibleForTesting
static final int ON = 1;
@VisibleForTesting
static final int OFF = 0;
private SettingObserver mSettingObserver;
public AssistantFeedbackPreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
Preference preference = screen.findPreference(NOTIFICATION_FEEDBACK_ENABLED);
if (preference != null) {
mSettingObserver = new SettingObserver(preference);
}
}
@Override
public void onResume() {
if (mSettingObserver != null) {
mSettingObserver.register(mContext.getContentResolver(), true /* register */);
}
}
@Override
public void onPause() {
if (mSettingObserver != null) {
mSettingObserver.register(mContext.getContentResolver(), false /* register */);
}
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean isChecked() {
return Settings.Secure.getInt(mContext.getContentResolver(),
NOTIFICATION_FEEDBACK_ENABLED, OFF) == ON;
}
@Override
public boolean setChecked(boolean isChecked) {
return Settings.Secure.putInt(mContext.getContentResolver(),
NOTIFICATION_FEEDBACK_ENABLED, isChecked ? ON : OFF);
}
class SettingObserver extends ContentObserver {
private final Uri NOTIFICATION_URI =
Settings.Secure.getUriFor(NOTIFICATION_FEEDBACK_ENABLED);
private final Preference mPreference;
SettingObserver(Preference preference) {
super(new Handler());
mPreference = preference;
}
public void register(ContentResolver cr, boolean register) {
if (register) {
cr.registerContentObserver(NOTIFICATION_URI, false, this);
} else {
cr.unregisterContentObserver(this);
}
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
if (NOTIFICATION_URI.equals(uri)) {
updateState(mPreference);
}
}
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright (C) 2020 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;
import static android.provider.Settings.Secure.NOTIFICATION_FEEDBACK_ENABLED;
import static com.android.settings.notification.AssistantFeedbackPreferenceController.OFF;
import static com.android.settings.notification.AssistantFeedbackPreferenceController.ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.TwoStatePreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class AssistantFeedbackPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
private AssistantFeedbackPreferenceController mController;
private Preference mPreference;
private static final String KEY = "asst_feedback_indicator";
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new AssistantFeedbackPreferenceController(mContext, KEY);
mPreference = new Preference(RuntimeEnvironment.application);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
}
@Test
public void testIsVisible() {
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isTrue();
}
@Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
final Context context = RuntimeEnvironment.application;
Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_FEEDBACK_ENABLED, ON);
mController = new AssistantFeedbackPreferenceController(context, KEY);
mController.updateState(preference);
verify(preference).setChecked(true);
}
@Test
public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
final Context context = RuntimeEnvironment.application;
Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_FEEDBACK_ENABLED, OFF);
mController = new AssistantFeedbackPreferenceController(context, KEY);
mController.updateState(preference);
verify(preference).setChecked(false);
}
@Test
public void isChecked_settingIsOff_shouldReturnFalse() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_FEEDBACK_ENABLED, OFF);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void isChecked_settingIsOn_shouldReturnTrue() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_FEEDBACK_ENABLED, ON);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void setChecked_setFalse_disablesSetting() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_FEEDBACK_ENABLED, ON);
mController.setChecked(false);
int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
NOTIFICATION_FEEDBACK_ENABLED, -1);
assertThat(updatedValue).isEqualTo(OFF);
}
@Test
public void setChecked_setTrue_enablesSetting() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_FEEDBACK_ENABLED, OFF);
mController.setChecked(true);
int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
NOTIFICATION_FEEDBACK_ENABLED, -1);
assertThat(updatedValue).isEqualTo(ON);
}
}