From e44848cdcea67decd1045987e9a774a17be2f29a Mon Sep 17 00:00:00 2001 From: Hiroki Sato Date: Tue, 2 May 2023 12:13:50 +0900 Subject: [PATCH] Don't show bubble settings when framework doesn't support it Some devices don't support bubbles by configuring config_supportsBubble value. When the value is set false, it doesn't make sense to show bubble related preferences. This CL disables showing bubble preferences when device doesn't support it. Bug: 274711609 Test: Set config_supportsBubble false, and check UI. Test: m -j80 RunSettingsRoboTests ROBOTEST_FILTER="Bubble" Change-Id: I670ad2a9e243819ea014e5e1ddb9d62ad76d2168 --- .../settings/notification/BubbleHelper.java | 65 +++++++++++++++++++ ...ubbleNotificationPreferenceController.java | 18 ++--- ...mmaryNotificationPreferenceController.java | 11 +--- .../BubbleCategoryPreferenceController.java | 15 +---- .../app/BubbleLinkPreferenceController.java | 15 +---- .../app/BubblePreferenceController.java | 15 ++--- .../BubbleSummaryPreferenceController.java | 11 +--- .../app/BubblePreferenceControllerTest.java | 4 +- ...BubbleSummaryPreferenceControllerTest.java | 4 +- 9 files changed, 88 insertions(+), 70 deletions(-) create mode 100644 src/com/android/settings/notification/BubbleHelper.java diff --git a/src/com/android/settings/notification/BubbleHelper.java b/src/com/android/settings/notification/BubbleHelper.java new file mode 100644 index 00000000000..3bde307da28 --- /dev/null +++ b/src/com/android/settings/notification/BubbleHelper.java @@ -0,0 +1,65 @@ +/* + * Copyright 2023 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_BUBBLES; + +import android.app.ActivityManager; +import android.content.Context; +import android.content.res.Resources; +import android.provider.Settings; + +/** + * Helper class for configuring notification bubbles. + */ +public class BubbleHelper { + + /** + * {@link Settings.Secure.NOTIFICATION_BUBBLES} is enabled. + */ + public static final int SYSTEM_WIDE_ON = 1; + + /** + * {@link Settings.Secure.NOTIFICATION_BUBBLES} is disabled. + */ + public static final int SYSTEM_WIDE_OFF = 0; + + /** + * Returns true if the device supports bubbles. + */ + public static boolean isSupportedByDevice(Context context) { + ActivityManager am = context.getSystemService(ActivityManager.class); + if (am.isLowRamDevice()) { + return false; + } + if (!Resources.getSystem().getBoolean(com.android.internal.R.bool.config_supportsBubble)) { + return false; + } + return true; + } + + /** + * Returns true if the device supports bubbles and the global settings is enabled. + */ + public static boolean isEnabledSystemWide(Context context) { + if (!isSupportedByDevice(context)) { + return false; + } + return Settings.Secure.getInt(context.getContentResolver(), NOTIFICATION_BUBBLES, + SYSTEM_WIDE_ON) == SYSTEM_WIDE_ON; + } +} diff --git a/src/com/android/settings/notification/BubbleNotificationPreferenceController.java b/src/com/android/settings/notification/BubbleNotificationPreferenceController.java index eeb9924f50e..d11e2e965d4 100644 --- a/src/com/android/settings/notification/BubbleNotificationPreferenceController.java +++ b/src/com/android/settings/notification/BubbleNotificationPreferenceController.java @@ -18,7 +18,6 @@ package com.android.settings.notification; import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; -import android.app.ActivityManager; import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; @@ -26,7 +25,6 @@ import android.net.Uri; import android.os.Handler; import android.provider.Settings; -import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; @@ -44,11 +42,6 @@ public class BubbleNotificationPreferenceController extends private static final String TAG = "BubbleNotifPrefContr"; - @VisibleForTesting - static final int ON = 1; - @VisibleForTesting - static final int OFF = 0; - private SettingObserver mSettingObserver; public BubbleNotificationPreferenceController(Context context, String preferenceKey) { @@ -79,8 +72,7 @@ public class BubbleNotificationPreferenceController extends @Override public int getAvailabilityStatus() { - ActivityManager am = mContext.getSystemService(ActivityManager.class); - return am.isLowRamDevice() ? UNSUPPORTED_ON_DEVICE : AVAILABLE; + return BubbleHelper.isSupportedByDevice(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; } @Override @@ -96,14 +88,14 @@ public class BubbleNotificationPreferenceController extends @Override public boolean isChecked() { - return Settings.Global.getInt(mContext.getContentResolver(), - NOTIFICATION_BUBBLES, ON) == ON; + return Settings.Global.getInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, + BubbleHelper.SYSTEM_WIDE_ON) == BubbleHelper.SYSTEM_WIDE_ON; } @Override public boolean setChecked(boolean isChecked) { - Settings.Global.putInt(mContext.getContentResolver(), - NOTIFICATION_BUBBLES, isChecked ? ON : OFF); + Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, + isChecked ? BubbleHelper.SYSTEM_WIDE_ON : BubbleHelper.SYSTEM_WIDE_OFF); return true; } diff --git a/src/com/android/settings/notification/BubbleSummaryNotificationPreferenceController.java b/src/com/android/settings/notification/BubbleSummaryNotificationPreferenceController.java index 3dac732c54b..541e9200b5f 100644 --- a/src/com/android/settings/notification/BubbleSummaryNotificationPreferenceController.java +++ b/src/com/android/settings/notification/BubbleSummaryNotificationPreferenceController.java @@ -18,12 +18,9 @@ package com.android.settings.notification; import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; -import android.app.ActivityManager; import android.content.Context; import android.provider.Settings; -import androidx.annotation.VisibleForTesting; - import com.android.settings.R; import com.android.settings.core.BasePreferenceController; @@ -32,9 +29,6 @@ import com.android.settings.core.BasePreferenceController; */ public class BubbleSummaryNotificationPreferenceController extends BasePreferenceController { - @VisibleForTesting - static final int ON = 1; - public BubbleSummaryNotificationPreferenceController(Context context, String preferenceKey) { super(context, preferenceKey); } @@ -49,12 +43,11 @@ public class BubbleSummaryNotificationPreferenceController extends BasePreferenc @Override public int getAvailabilityStatus() { - ActivityManager am = mContext.getSystemService(ActivityManager.class); - return am.isLowRamDevice() ? UNSUPPORTED_ON_DEVICE : AVAILABLE; + return BubbleHelper.isSupportedByDevice(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; } private boolean areBubblesEnabled() { return Settings.Secure.getInt(mContext.getContentResolver(), - NOTIFICATION_BUBBLES, ON) == ON; + NOTIFICATION_BUBBLES, BubbleHelper.SYSTEM_WIDE_ON) == BubbleHelper.SYSTEM_WIDE_ON; } } diff --git a/src/com/android/settings/notification/app/BubbleCategoryPreferenceController.java b/src/com/android/settings/notification/app/BubbleCategoryPreferenceController.java index ad3a10cf381..37baf8b7c38 100644 --- a/src/com/android/settings/notification/app/BubbleCategoryPreferenceController.java +++ b/src/com/android/settings/notification/app/BubbleCategoryPreferenceController.java @@ -16,20 +16,17 @@ package com.android.settings.notification.app; -import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; - import android.content.Context; import android.content.Intent; import android.provider.Settings; -import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; +import com.android.settings.notification.BubbleHelper; + public class BubbleCategoryPreferenceController extends NotificationPreferenceController { private static final String KEY = "bubbles"; - @VisibleForTesting - static final int ON = 1; public BubbleCategoryPreferenceController(Context context) { super(context, null); @@ -40,7 +37,7 @@ public class BubbleCategoryPreferenceController extends NotificationPreferenceCo if (!super.isAvailable()) { return false; } - return areBubblesEnabled(); + return BubbleHelper.isEnabledSystemWide(mContext); } @Override @@ -64,10 +61,4 @@ public class BubbleCategoryPreferenceController extends NotificationPreferenceCo preference.setIntent(intent); } } - - - private boolean areBubblesEnabled() { - return Settings.Secure.getInt(mContext.getContentResolver(), - NOTIFICATION_BUBBLES, ON) == ON; - } } diff --git a/src/com/android/settings/notification/app/BubbleLinkPreferenceController.java b/src/com/android/settings/notification/app/BubbleLinkPreferenceController.java index 0b9529b10b5..fcabe9b87af 100644 --- a/src/com/android/settings/notification/app/BubbleLinkPreferenceController.java +++ b/src/com/android/settings/notification/app/BubbleLinkPreferenceController.java @@ -16,20 +16,17 @@ package com.android.settings.notification.app; -import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; - import android.content.Context; import android.content.Intent; import android.provider.Settings; -import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; +import com.android.settings.notification.BubbleHelper; + public class BubbleLinkPreferenceController extends NotificationPreferenceController { private static final String KEY = "notification_bubbles"; - @VisibleForTesting - static final int ON = 1; public BubbleLinkPreferenceController(Context context) { super(context, null); @@ -40,7 +37,7 @@ public class BubbleLinkPreferenceController extends NotificationPreferenceContro if (!super.isAvailable()) { return false; } - return areBubblesEnabled(); + return BubbleHelper.isEnabledSystemWide(mContext); } @Override @@ -64,10 +61,4 @@ public class BubbleLinkPreferenceController extends NotificationPreferenceContro preference.setIntent(intent); } } - - - private boolean areBubblesEnabled() { - return Settings.Secure.getInt(mContext.getContentResolver(), - NOTIFICATION_BUBBLES, ON) == ON; - } } diff --git a/src/com/android/settings/notification/app/BubblePreferenceController.java b/src/com/android/settings/notification/app/BubblePreferenceController.java index 351b4635bb6..f2c5cdde2ee 100644 --- a/src/com/android/settings/notification/app/BubblePreferenceController.java +++ b/src/com/android/settings/notification/app/BubblePreferenceController.java @@ -20,16 +20,15 @@ import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE; import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; import android.annotation.Nullable; -import android.app.ActivityManager; import android.app.NotificationChannel; import android.content.Context; import android.provider.Settings; -import androidx.annotation.VisibleForTesting; import androidx.fragment.app.FragmentManager; import androidx.preference.Preference; import com.android.settings.core.PreferenceControllerMixin; +import com.android.settings.notification.BubbleHelper; import com.android.settings.notification.NotificationBackend; import com.android.settingslib.RestrictedSwitchPreference; @@ -42,10 +41,6 @@ public class BubblePreferenceController extends NotificationPreferenceController private static final String TAG = "BubblePrefContr"; private static final String KEY = "bubble_pref"; - @VisibleForTesting - static final int SYSTEM_WIDE_ON = 1; - @VisibleForTesting - static final int SYSTEM_WIDE_OFF = 0; private FragmentManager mFragmentManager; private boolean mIsAppPage; @@ -146,9 +141,7 @@ public class BubblePreferenceController extends NotificationPreferenceController } private boolean isEnabled() { - ActivityManager am = mContext.getSystemService(ActivityManager.class); - return !am.isLowRamDevice() && Settings.Secure.getInt(mContext.getContentResolver(), - NOTIFICATION_BUBBLES, SYSTEM_WIDE_OFF) == SYSTEM_WIDE_ON; + return BubbleHelper.isEnabledSystemWide(mContext); } /** @@ -163,7 +156,7 @@ public class BubblePreferenceController extends NotificationPreferenceController // correct preference state Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BUBBLES, - SYSTEM_WIDE_OFF); + BubbleHelper.SYSTEM_WIDE_OFF); } /** @@ -176,6 +169,6 @@ public class BubblePreferenceController extends NotificationPreferenceController // correct preference state Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BUBBLES, - SYSTEM_WIDE_ON); + BubbleHelper.SYSTEM_WIDE_ON); } } diff --git a/src/com/android/settings/notification/app/BubbleSummaryPreferenceController.java b/src/com/android/settings/notification/app/BubbleSummaryPreferenceController.java index 51370b16bef..888bd25c770 100644 --- a/src/com/android/settings/notification/app/BubbleSummaryPreferenceController.java +++ b/src/com/android/settings/notification/app/BubbleSummaryPreferenceController.java @@ -18,18 +18,16 @@ package com.android.settings.notification.app; import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL; import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE; -import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; -import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.provider.Settings; -import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; import com.android.settings.R; +import com.android.settings.notification.BubbleHelper; import com.android.settings.notification.NotificationBackend; /** @@ -38,9 +36,6 @@ import com.android.settings.notification.NotificationBackend; public class BubbleSummaryPreferenceController extends NotificationPreferenceController { private static final String KEY = "bubble_pref_link"; - @VisibleForTesting - static final int ON = 1; - public BubbleSummaryPreferenceController(Context context, NotificationBackend backend) { super(context, backend); } @@ -105,8 +100,6 @@ public class BubbleSummaryPreferenceController extends NotificationPreferenceCon } private boolean isGloballyEnabled() { - ActivityManager am = mContext.getSystemService(ActivityManager.class); - return !am.isLowRamDevice() && Settings.Secure.getInt(mContext.getContentResolver(), - NOTIFICATION_BUBBLES, ON) == ON; + return BubbleHelper.isEnabledSystemWide(mContext); } } diff --git a/tests/robotests/src/com/android/settings/notification/app/BubblePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/BubblePreferenceControllerTest.java index 09f6b7d634a..e567590d2ac 100644 --- a/tests/robotests/src/com/android/settings/notification/app/BubblePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/app/BubblePreferenceControllerTest.java @@ -25,8 +25,8 @@ import static android.app.NotificationManager.IMPORTANCE_LOW; import static android.app.NotificationManager.IMPORTANCE_NONE; import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; -import static com.android.settings.notification.app.BubblePreferenceController.SYSTEM_WIDE_OFF; -import static com.android.settings.notification.app.BubblePreferenceController.SYSTEM_WIDE_ON; +import static com.android.settings.notification.BubbleHelper.SYSTEM_WIDE_OFF; +import static com.android.settings.notification.BubbleHelper.SYSTEM_WIDE_ON; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; diff --git a/tests/robotests/src/com/android/settings/notification/app/BubbleSummaryPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/BubbleSummaryPreferenceControllerTest.java index 75c53c12f45..a9a1b97d1b6 100644 --- a/tests/robotests/src/com/android/settings/notification/app/BubbleSummaryPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/app/BubbleSummaryPreferenceControllerTest.java @@ -23,8 +23,8 @@ import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED; import static android.app.NotificationManager.IMPORTANCE_HIGH; import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; -import static com.android.settings.notification.app.BubblePreferenceController.SYSTEM_WIDE_OFF; -import static com.android.settings.notification.app.BubblePreferenceController.SYSTEM_WIDE_ON; +import static com.android.settings.notification.BubbleHelper.SYSTEM_WIDE_OFF; +import static com.android.settings.notification.BubbleHelper.SYSTEM_WIDE_ON; import static junit.framework.TestCase.assertEquals;