Merge "Add a toggle that enables/disables an app's notifications in the Settings." into pi-car-dev
am: 0b677a476a
Change-Id: I2cca69446bcc4cc477484d3959b4ce089a4caf1f
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 37fa470..60cef10 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -56,6 +56,7 @@
<uses-permission android:name="android.permission.SET_TIME"/>
<uses-permission android:name="android.permission.SET_TIME_ZONE"/>
<uses-permission android:name="android.permission.START_FOREGROUND"/>
+ <uses-permission android:name="android.permission.STATUS_BAR_SERVICE"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
diff --git a/res/values/preference_keys.xml b/res/values/preference_keys.xml
index de3b2b2..996092d 100644
--- a/res/values/preference_keys.xml
+++ b/res/values/preference_keys.xml
@@ -45,7 +45,7 @@
<!-- Network -->
<string name="pk_mobile_network_settings_entry" translatable="false">mobile_network_settings_entry</string>
<string name="pk_network_and_internet_extra_settings" translatable="false">
- pk_network_and_internet_extra_settings
+ network_and_internet_extra_settings
</string>
<string name="pk_mobile_data_toggle" translatable="false">mobile_data_toggle</string>
<string name="pk_data_usage_settings_entry" translatable="false">data_usage_settings_entry</string>
@@ -110,11 +110,14 @@
applications_settings_screen
</string>
<string name="pk_application_extra_settings" translatable="false">
- pk_application_extra_settings
+ application_extra_settings
</string>
<!-- Application Details -->
<string name="pk_application_details_app" translatable="false">application_details_app</string>
+ <string name="pk_application_details_notifications" translatable="false">
+ application_details_notifications
+ </string>
<string name="pk_application_details_permissions" translatable="false">
application_details_permissions
</string>
@@ -148,8 +151,8 @@
<!-- Accounts -->
<string name="pk_account_list" translatable="false">account_list</string>
- <string name="pk_account_settings" translatable="false">pk_account_settings</string>
- <string name="pk_accounts_extra_settings" translatable="false">pk_accounts_extra_settings</string>
+ <string name="pk_account_settings" translatable="false">account_settings</string>
+ <string name="pk_accounts_extra_settings" translatable="false">accounts_extra_settings</string>
<string name="pk_account_auto_sync" translatable="false">account_auto_sync</string>
<string name="pk_add_account" translatable="false">add_account</string>
<string name="pk_account_details" translatable="false">account_details</string>
@@ -161,14 +164,14 @@
<string name="pk_adaptive_brightness_switch" translatable="false">adaptive_brightness_switch
</string>
<string name="pk_brightness_level" translatable="false">brightness_level</string>
- <string name="pk_display_extra_settings" translatable="false">pk_display_extra_settings</string>
+ <string name="pk_display_extra_settings" translatable="false">display_extra_settings</string>
<!-- Sound Settings -->
<string name="pk_volume_settings" translatable="false">volume_settings</string>
<string name="pk_default_ringtone" translatable="false">default_ringtone</string>
<string name="pk_default_notification" translatable="false">default_notification</string>
<string name="pk_default_alarm" translatable="false">default_alarm</string>
- <string name="pk_sounds_extra_settings" translatable="false">pk_sounds_extra_settings</string>
+ <string name="pk_sounds_extra_settings" translatable="false">sounds_extra_settings</string>
<!-- Location Settings -->
<string name="pk_location_recent_requests_entry" translatable="false">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 2e121ff..2952163 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -327,6 +327,8 @@
<string name="app_disable_dialog_positive">Disable app</string>
<!-- Manage applications, individual application info screen, heading for settings related to the app's permissions. for example, it may list all the permissions the app has. -->
<string name="permissions_label">Permissions</string>
+ <!-- Label for the toggle that enables/disables an app's notifications. [CHAR LIMIT=20] -->
+ <string name="notifications_label">Notifications</string>
<!-- Label for displaying application version. -->
<string name="application_version_label">Version: %1$s</string>
<!-- Runtime permissions preference summary, shown when the app has no permissions granted. [CHAR LIMIT=40] -->
diff --git a/res/xml/application_details_fragment.xml b/res/xml/application_details_fragment.xml
index 4026f43..2f4ac1a 100644
--- a/res/xml/application_details_fragment.xml
+++ b/res/xml/application_details_fragment.xml
@@ -22,6 +22,10 @@
<Preference
android:key="@string/pk_application_details_app"
settings:controller="com.android.car.settings.applications.ApplicationPreferenceController"/>
+ <SwitchPreference
+ android:key="@string/pk_application_details_notifications"
+ android:title="@string/notifications_label"
+ settings:controller="com.android.car.settings.applications.NotificationsPreferenceController"/>
<Preference
android:key="@string/pk_application_details_permissions"
android:title="@string/permissions_label"
diff --git a/src/com/android/car/settings/applications/ApplicationDetailsFragment.java b/src/com/android/car/settings/applications/ApplicationDetailsFragment.java
index 3b781d4..275968c 100644
--- a/src/com/android/car/settings/applications/ApplicationDetailsFragment.java
+++ b/src/com/android/car/settings/applications/ApplicationDetailsFragment.java
@@ -145,6 +145,8 @@
use(ApplicationPreferenceController.class,
R.string.pk_application_details_app)
.setAppEntry(mAppEntry).setAppState(mAppState);
+ use(NotificationsPreferenceController.class,
+ R.string.pk_application_details_notifications).setPackageInfo(mPackageInfo);
use(PermissionsPreferenceController.class,
R.string.pk_application_details_permissions).setPackageName(mPackageName);
use(VersionPreferenceController.class,
diff --git a/src/com/android/car/settings/applications/NotificationsPreferenceController.java b/src/com/android/car/settings/applications/NotificationsPreferenceController.java
new file mode 100644
index 0000000..db7da71
--- /dev/null
+++ b/src/com/android/car/settings/applications/NotificationsPreferenceController.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2019 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.car.settings.applications;
+
+import static android.app.NotificationManager.IMPORTANCE_NONE;
+import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
+
+import android.app.INotificationManager;
+import android.app.NotificationChannel;
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.os.ServiceManager;
+
+import androidx.preference.TwoStatePreference;
+
+import com.android.car.settings.common.FragmentController;
+import com.android.car.settings.common.Logger;
+import com.android.car.settings.common.PreferenceController;
+import com.android.internal.annotations.VisibleForTesting;
+
+/**
+ * Controller for preference which enables / disables showing notifications for an application.
+ */
+public class NotificationsPreferenceController extends PreferenceController<TwoStatePreference> {
+
+ private static final Logger LOG = new Logger(NotificationsPreferenceController.class);
+
+ private String mPackageName;
+ private int mUid;
+
+ @VisibleForTesting
+ INotificationManager mNotificationManager =
+ INotificationManager.Stub.asInterface(
+ ServiceManager.getService(Context.NOTIFICATION_SERVICE));
+
+ public NotificationsPreferenceController(Context context, String preferenceKey,
+ FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+ super(context, preferenceKey, fragmentController, uxRestrictions);
+ }
+
+ /**
+ * Set the package info of the application.
+ */
+ public void setPackageInfo(PackageInfo packageInfo) {
+ mPackageName = packageInfo.packageName;
+ mUid = packageInfo.applicationInfo.uid;
+ }
+
+ @Override
+ protected Class<TwoStatePreference> getPreferenceType() {
+ return TwoStatePreference.class;
+ }
+
+ @Override
+ protected void updateState(TwoStatePreference preference) {
+ preference.setChecked(isNotificationsEnabled());
+ }
+
+ @Override
+ protected boolean handlePreferenceChanged(TwoStatePreference preference, Object newValue) {
+ boolean enabled = (boolean) newValue;
+
+ try {
+ if (mNotificationManager.onlyHasDefaultChannel(mPackageName, mUid)) {
+ NotificationChannel defaultChannel =
+ mNotificationManager.getNotificationChannelForPackage(
+ mPackageName,
+ mUid,
+ NotificationChannel.DEFAULT_CHANNEL_ID,
+ /* includeDeleted= */ true);
+ defaultChannel.setImportance(enabled ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_NONE);
+ mNotificationManager
+ .updateNotificationChannelForPackage(mPackageName, mUid, defaultChannel);
+ }
+ mNotificationManager.setNotificationsEnabledForPackage(mPackageName, mUid, enabled);
+ } catch (Exception e) {
+ LOG.w("Error querying notification setting for package");
+ return false;
+ }
+ return true;
+ }
+
+ private boolean isNotificationsEnabled() {
+ try {
+ return mNotificationManager.areNotificationsEnabledForPackage(mPackageName, mUid);
+ } catch (Exception e) {
+ LOG.w("Error querying notification setting for package");
+ return false;
+ }
+ }
+}
diff --git a/tests/robotests/src/com/android/car/settings/applications/NotificationsPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/applications/NotificationsPreferenceControllerTest.java
new file mode 100644
index 0000000..90630ea
--- /dev/null
+++ b/tests/robotests/src/com/android/car/settings/applications/NotificationsPreferenceControllerTest.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2019 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.car.settings.applications;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.INotificationManager;
+import android.app.NotificationChannel;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.preference.SwitchPreference;
+import androidx.preference.TwoStatePreference;
+
+import com.android.car.settings.CarSettingsRobolectricTestRunner;
+import com.android.car.settings.common.PreferenceControllerTestHelper;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(CarSettingsRobolectricTestRunner.class)
+public class NotificationsPreferenceControllerTest {
+ private static final String PKG_NAME = "package.name";
+ private static final int UID = 1001010;
+ private Context mContext;
+ private NotificationsPreferenceController mController;
+ private PreferenceControllerTestHelper<NotificationsPreferenceController>
+ mPreferenceControllerHelper;
+ private TwoStatePreference mTwoStatePreference;
+ @Mock
+ private INotificationManager mMockManager;
+ @Mock
+ private NotificationChannel mMockChannel;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mContext = RuntimeEnvironment.application;
+ mTwoStatePreference = new SwitchPreference(mContext);
+
+ mPreferenceControllerHelper = new PreferenceControllerTestHelper<>(mContext,
+ NotificationsPreferenceController.class, mTwoStatePreference);
+ mController = mPreferenceControllerHelper.getController();
+ mController.mNotificationManager = mMockManager;
+
+ PackageInfo packageInfo = new PackageInfo();
+ packageInfo.packageName = PKG_NAME;
+
+ ApplicationInfo applicationInfo = new ApplicationInfo();
+ applicationInfo.packageName = PKG_NAME;
+ packageInfo.applicationInfo = applicationInfo;
+ packageInfo.applicationInfo.uid = UID;
+ mController.setPackageInfo(packageInfo);
+ }
+
+ @Test
+ public void onCreate_notificationEnabled_isChecked() throws Exception {
+ when(mMockManager.areNotificationsEnabledForPackage(PKG_NAME, UID)).thenReturn(true);
+
+ mPreferenceControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_CREATE);
+
+ assertThat(mTwoStatePreference.isChecked()).isTrue();
+ }
+
+ @Test
+ public void onCreate_notificationDisabled_isNotChecked() throws Exception {
+ when(mMockManager.areNotificationsEnabledForPackage(PKG_NAME, UID)).thenReturn(false);
+
+ mPreferenceControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_CREATE);
+
+ assertThat(mTwoStatePreference.isChecked()).isFalse();
+ }
+
+ @Test
+ public void callChangeListener_setEnable_enablingNotification() throws Exception {
+ when(mMockManager.onlyHasDefaultChannel(PKG_NAME, UID)).thenReturn(false);
+
+ mTwoStatePreference.callChangeListener(true);
+
+ verify(mMockManager).setNotificationsEnabledForPackage(PKG_NAME, UID, true);
+ }
+
+ @Test
+ public void callChangeListener_setDisable_disablingNotification() throws Exception {
+ when(mMockManager.onlyHasDefaultChannel(PKG_NAME, UID)).thenReturn(false);
+
+ mTwoStatePreference.callChangeListener(false);
+
+ verify(mMockManager).setNotificationsEnabledForPackage(PKG_NAME, UID, false);
+ }
+
+ @Test
+ public void callChangeListener_onlyHasDefaultChannel_updateChannel() throws Exception {
+ when(mMockManager.onlyHasDefaultChannel(PKG_NAME, UID)).thenReturn(true);
+ when(mMockManager
+ .getNotificationChannelForPackage(
+ PKG_NAME, UID, NotificationChannel.DEFAULT_CHANNEL_ID, true))
+ .thenReturn(mMockChannel);
+
+ mTwoStatePreference.callChangeListener(true);
+
+ verify(mMockManager).updateNotificationChannelForPackage(PKG_NAME, UID, mMockChannel);
+ }
+}